##// END OF EJS Templates
changed to support dicts for variable lookup and eval...
florianlink -
r25:60e7b3114ac6
parent child
Show More
@@ -536,7 +536,11 PythonQtObjectPtr PythonQt::lookupObject(PyObject* module, const QString& name)
536 536 for (QStringList::ConstIterator i = l.begin(); i!=l.end() && p; ++i) {
537 537 prev = p;
538 538 b = (*i).toLatin1();
539 p.setNewRef(PyObject_GetAttrString(p, b.data()));
539 if (PyDict_Check(p)) {
540 p = PyDict_GetItemString(p, b.data());
541 } else {
542 p.setNewRef(PyObject_GetAttrString(p, b.data()));
543 }
540 544 }
541 545 PyErr_Clear();
542 546 return p;
@@ -548,10 +552,19 PythonQtObjectPtr PythonQt::getMainModule() {
548 552 return PyDict_GetItemString(dict, "__main__");
549 553 }
550 554
551 QVariant PythonQt::evalCode(PyObject* module, PyObject* pycode) {
555 QVariant PythonQt::evalCode(PyObject* object, PyObject* pycode) {
552 556 QVariant result;
553 557 if (pycode) {
554 PyObject* r = PyEval_EvalCode((PyCodeObject*)pycode, PyModule_GetDict((PyObject*)module) , PyModule_GetDict((PyObject*)module));
558 PyObject* dict = NULL;
559 if (PyModule_Check(object)) {
560 dict = PyModule_GetDict(object);
561 } else if (PyDict_Check(object)) {
562 dict = object;
563 }
564 PyObject* r = NULL;
565 if (dict) {
566 r = PyEval_EvalCode((PyCodeObject*)pycode, dict , dict);
567 }
555 568 if (r) {
556 569 result = PythonQtConv::PyObjToQVariant(r);
557 570 Py_DECREF(r);
@@ -564,11 +577,19 QVariant PythonQt::evalCode(PyObject* module, PyObject* pycode) {
564 577 return result;
565 578 }
566 579
567 QVariant PythonQt::evalScript(PyObject* module, const QString& script, int start)
580 QVariant PythonQt::evalScript(PyObject* object, const QString& script, int start)
568 581 {
569 582 QVariant result;
570 583 PythonQtObjectPtr p;
571 p.setNewRef(PyRun_String(script.toLatin1().data(), start, PyModule_GetDict(module), PyModule_GetDict(module)));
584 PyObject* dict = NULL;
585 if (PyModule_Check(object)) {
586 dict = PyModule_GetDict(object);
587 } else if (PyDict_Check(object)) {
588 dict = object;
589 }
590 if (dict) {
591 p.setNewRef(PyRun_String(script.toLatin1().data(), start, dict, dict));
592 }
572 593 if (p) {
573 594 result = PythonQtConv::PyObjToQVariant(p);
574 595 } else {
@@ -625,25 +646,41 PythonQtObjectPtr PythonQt::createUniqueModule()
625 646 return createModuleFromScript(moduleName);
626 647 }
627 648
628 void PythonQt::addObject(PyObject* module, const QString& name, QObject* object)
649 void PythonQt::addObject(PyObject* object, const QString& name, QObject* qObject)
629 650 {
630 PyModule_AddObject(module, name.toLatin1().data(), _p->wrapQObject(object));
651 if (PyModule_Check(object)) {
652 PyModule_AddObject(object, name.toLatin1().data(), _p->wrapQObject(qObject));
653 } else if (PyDict_Check(object)) {
654 PyDict_SetItemString(object, name.toLatin1().data(), _p->wrapQObject(qObject));
655 } else {
656 PyObject_SetAttrString(object, name.toLatin1().data(), _p->wrapQObject(qObject));
657 }
631 658 }
632 659
633 void PythonQt::addVariable(PyObject* module, const QString& name, const QVariant& v)
660 void PythonQt::addVariable(PyObject* object, const QString& name, const QVariant& v)
634 661 {
635 PyModule_AddObject(module, name.toLatin1().data(), PythonQtConv::QVariantToPyObject(v));
662 if (PyModule_Check(object)) {
663 PyModule_AddObject(object, name.toLatin1().data(), PythonQtConv::QVariantToPyObject(v));
664 } else if (PyDict_Check(object)) {
665 PyDict_SetItemString(object, name.toLatin1().data(), PythonQtConv::QVariantToPyObject(v));
666 } else {
667 PyObject_SetAttrString(object, name.toLatin1().data(), PythonQtConv::QVariantToPyObject(v));
668 }
636 669 }
637 670
638 void PythonQt::removeVariable(PyObject* module, const QString& name)
671 void PythonQt::removeVariable(PyObject* object, const QString& name)
639 672 {
640 PyObject_DelAttrString(module, name.toLatin1().data());
673 if (PyDict_Check(object)) {
674 PyDict_DelItemString(object, name.toLatin1().data());
675 } else {
676 PyObject_DelAttrString(object, name.toLatin1().data());
677 }
641 678 }
642 679
643 QVariant PythonQt::getVariable(PyObject* module, const QString& objectname)
680 QVariant PythonQt::getVariable(PyObject* object, const QString& objectname)
644 681 {
645 682 QVariant result;
646 PythonQtObjectPtr obj = lookupObject(module, objectname);
683 PythonQtObjectPtr obj = lookupObject(object, objectname);
647 684 if (obj) {
648 685 result = PythonQtConv::PyObjToQVariant(obj);
649 686 }
@@ -694,7 +731,14 QStringList PythonQt::introspection(PyObject* module, const QString& objectname,
694 731 }
695 732 }
696 733 } else {
697 PyObject* keys = PyObject_Dir(object);
734 PyObject* keys = NULL;
735 bool isDict = false;
736 if (PyDict_Check(object)) {
737 keys = PyDict_Keys(object);
738 isDict = true;
739 } else {
740 keys = PyObject_Dir(object);
741 }
698 742 if (keys) {
699 743 int count = PyList_Size(keys);
700 744 PyObject* key;
@@ -702,7 +746,12 QStringList PythonQt::introspection(PyObject* module, const QString& objectname,
702 746 QString keystr;
703 747 for (int i = 0;i<count;i++) {
704 748 key = PyList_GetItem(keys,i);
705 value = PyObject_GetAttr(object, key);
749 if (isDict) {
750 value = PyDict_GetItem(object, key);
751 Py_INCREF(value);
752 } else {
753 value = PyObject_GetAttr(object, key);
754 }
706 755 if (!value) continue;
707 756 keystr = PyString_AsString(key);
708 757 static const QString underscoreStr("__tmp");
@@ -159,13 +159,13 public:
159 159
160 160 //! evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string)
161 161 //! If pycode is NULL, a python error is printed.
162 QVariant evalCode(PyObject* module, PyObject* pycode);
162 QVariant evalCode(PyObject* object, PyObject* pycode);
163 163
164 164 //! evaluates the given script code and returns the result value
165 QVariant evalScript(PyObject* module, const QString& script, int start = Py_file_input);
165 QVariant evalScript(PyObject* object, const QString& script, int start = Py_file_input);
166 166
167 167 //! evaluates the given script code from file
168 void evalFile(PyObject* module, const QString& filename);
168 void evalFile(PyObject* object, const QString& filename);
169 169
170 170 //! creates the new module \c name and evaluates the given file in the context of that module
171 171 //! If the \c script is empty, the module contains no initial code. You can use evalScript/evalCode to add code
@@ -201,24 +201,24 public:
201 201
202 202 //@{ Variable access
203 203
204 //! add the given \c object to the \c module as a variable with \c name (it can be removed via clearVariable)
205 void addObject(PyObject* module, const QString& name, QObject* object);
204 //! add the given \c qObject to the python \c object as a variable with \c name (it can be removed via clearVariable)
205 void addObject(PyObject* object, const QString& name, QObject* qObject);
206 206
207 //! add the given variable to the module
208 void addVariable(PyObject* module, const QString& name, const QVariant& v);
207 //! add the given variable to the object
208 void addVariable(PyObject* object, const QString& name, const QVariant& v);
209 209
210 210 //! remove the given variable
211 211 void removeVariable(PyObject* module, const QString& name);
212 212
213 //! get the variable with the \c name of the \c module, returns an invalid QVariant on error
214 QVariant getVariable(PyObject* module, const QString& name);
213 //! get the variable with the \c name of the \c object, returns an invalid QVariant on error
214 QVariant getVariable(PyObject* object, const QString& name);
215 215
216 //! read vars etc. in scope of a module, optional looking inside of an object \c objectname
217 QStringList introspection(PyObject* module, const QString& objectname, ObjectType type);
216 //! read vars etc. in scope of an \c object, optional looking inside of an object \c objectname
217 QStringList introspection(PyObject* object, const QString& objectname, ObjectType type);
218 218
219 219 //! returns the found callable object or NULL
220 220 //! @return new reference
221 PythonQtObjectPtr lookupCallable(PyObject* module, const QString& name);
221 PythonQtObjectPtr lookupCallable(PyObject* object, const QString& name);
222 222
223 223 //@}
224 224
@@ -173,7 +173,16 void PythonQtScriptingConsole::executeCode(const QString& code)
173 173 _stdOut = "";
174 174 _stdErr = "";
175 175 PythonQtObjectPtr p;
176 p.setNewRef(PyRun_String(code.toLatin1().data(), Py_single_input, PyModule_GetDict(_context), PyModule_GetDict(_context)));
176 PyObject* dict = NULL;
177 if (PyModule_Check(_context)) {
178 dict = PyModule_GetDict(_context);
179 } else if (PyDict_Check(_context)) {
180 dict = _context;
181 }
182 if (dict) {
183 p.setNewRef(PyRun_String(code.toLatin1().data(), Py_single_input, dict, dict));
184 }
185
177 186 if (!p) {
178 187 PythonQt::self()->handleError();
179 188 }
General Comments 0
You need to be logged in to leave comments. Login now