@@ -536,7 +536,11 PythonQtObjectPtr PythonQt::lookupObject(PyObject* module, const QString& name) | |||||
536 | for (QStringList::ConstIterator i = l.begin(); i!=l.end() && p; ++i) { |
|
536 | for (QStringList::ConstIterator i = l.begin(); i!=l.end() && p; ++i) { | |
537 | prev = p; |
|
537 | prev = p; | |
538 | b = (*i).toLatin1(); |
|
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 | PyErr_Clear(); |
|
545 | PyErr_Clear(); | |
542 | return p; |
|
546 | return p; | |
@@ -548,10 +552,19 PythonQtObjectPtr PythonQt::getMainModule() { | |||||
548 | return PyDict_GetItemString(dict, "__main__"); |
|
552 | return PyDict_GetItemString(dict, "__main__"); | |
549 | } |
|
553 | } | |
550 |
|
554 | |||
551 |
QVariant PythonQt::evalCode(PyObject* |
|
555 | QVariant PythonQt::evalCode(PyObject* object, PyObject* pycode) { | |
552 | QVariant result; |
|
556 | QVariant result; | |
553 | if (pycode) { |
|
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 | if (r) { |
|
568 | if (r) { | |
556 | result = PythonQtConv::PyObjToQVariant(r); |
|
569 | result = PythonQtConv::PyObjToQVariant(r); | |
557 | Py_DECREF(r); |
|
570 | Py_DECREF(r); | |
@@ -564,11 +577,19 QVariant PythonQt::evalCode(PyObject* module, PyObject* pycode) { | |||||
564 | return result; |
|
577 | return result; | |
565 | } |
|
578 | } | |
566 |
|
579 | |||
567 |
QVariant PythonQt::evalScript(PyObject* |
|
580 | QVariant PythonQt::evalScript(PyObject* object, const QString& script, int start) | |
568 | { |
|
581 | { | |
569 | QVariant result; |
|
582 | QVariant result; | |
570 | PythonQtObjectPtr p; |
|
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 | if (p) { |
|
593 | if (p) { | |
573 | result = PythonQtConv::PyObjToQVariant(p); |
|
594 | result = PythonQtConv::PyObjToQVariant(p); | |
574 | } else { |
|
595 | } else { | |
@@ -625,25 +646,41 PythonQtObjectPtr PythonQt::createUniqueModule() | |||||
625 | return createModuleFromScript(moduleName); |
|
646 | return createModuleFromScript(moduleName); | |
626 | } |
|
647 | } | |
627 |
|
648 | |||
628 |
void PythonQt::addObject(PyObject* |
|
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* |
|
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* |
|
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* |
|
680 | QVariant PythonQt::getVariable(PyObject* object, const QString& objectname) | |
644 | { |
|
681 | { | |
645 | QVariant result; |
|
682 | QVariant result; | |
646 |
PythonQtObjectPtr obj = lookupObject( |
|
683 | PythonQtObjectPtr obj = lookupObject(object, objectname); | |
647 | if (obj) { |
|
684 | if (obj) { | |
648 | result = PythonQtConv::PyObjToQVariant(obj); |
|
685 | result = PythonQtConv::PyObjToQVariant(obj); | |
649 | } |
|
686 | } | |
@@ -694,7 +731,14 QStringList PythonQt::introspection(PyObject* module, const QString& objectname, | |||||
694 | } |
|
731 | } | |
695 | } |
|
732 | } | |
696 | } else { |
|
733 | } else { | |
697 |
PyObject* keys = |
|
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 | if (keys) { |
|
742 | if (keys) { | |
699 | int count = PyList_Size(keys); |
|
743 | int count = PyList_Size(keys); | |
700 | PyObject* key; |
|
744 | PyObject* key; | |
@@ -702,7 +746,12 QStringList PythonQt::introspection(PyObject* module, const QString& objectname, | |||||
702 | QString keystr; |
|
746 | QString keystr; | |
703 | for (int i = 0;i<count;i++) { |
|
747 | for (int i = 0;i<count;i++) { | |
704 | key = PyList_GetItem(keys,i); |
|
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 | if (!value) continue; |
|
755 | if (!value) continue; | |
707 | keystr = PyString_AsString(key); |
|
756 | keystr = PyString_AsString(key); | |
708 | static const QString underscoreStr("__tmp"); |
|
757 | static const QString underscoreStr("__tmp"); |
@@ -159,13 +159,13 public: | |||||
159 |
|
159 | |||
160 | //! evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string) |
|
160 | //! evaluates the given code and returns the result value (use Py_Compile etc. to create pycode from string) | |
161 | //! If pycode is NULL, a python error is printed. |
|
161 | //! If pycode is NULL, a python error is printed. | |
162 |
QVariant evalCode(PyObject* |
|
162 | QVariant evalCode(PyObject* object, PyObject* pycode); | |
163 |
|
163 | |||
164 | //! evaluates the given script code and returns the result value |
|
164 | //! evaluates the given script code and returns the result value | |
165 |
QVariant evalScript(PyObject* |
|
165 | QVariant evalScript(PyObject* object, const QString& script, int start = Py_file_input); | |
166 |
|
166 | |||
167 | //! evaluates the given script code from file |
|
167 | //! evaluates the given script code from file | |
168 |
void evalFile(PyObject* |
|
168 | void evalFile(PyObject* object, const QString& filename); | |
169 |
|
169 | |||
170 | //! creates the new module \c name and evaluates the given file in the context of that module |
|
170 | //! creates the new module \c name and evaluates the given file in the context of that module | |
171 | //! If the \c script is empty, the module contains no initial code. You can use evalScript/evalCode to add code |
|
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 | //@{ Variable access |
|
202 | //@{ Variable access | |
203 |
|
203 | |||
204 |
//! add the given \c |
|
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* |
|
205 | void addObject(PyObject* object, const QString& name, QObject* qObject); | |
206 |
|
206 | |||
207 |
//! add the given variable to the |
|
207 | //! add the given variable to the object | |
208 |
void addVariable(PyObject* |
|
208 | void addVariable(PyObject* object, const QString& name, const QVariant& v); | |
209 |
|
209 | |||
210 | //! remove the given variable |
|
210 | //! remove the given variable | |
211 | void removeVariable(PyObject* module, const QString& name); |
|
211 | void removeVariable(PyObject* module, const QString& name); | |
212 |
|
212 | |||
213 |
//! get the variable with the \c name of the \c |
|
213 | //! get the variable with the \c name of the \c object, returns an invalid QVariant on error | |
214 |
QVariant getVariable(PyObject* |
|
214 | QVariant getVariable(PyObject* object, const QString& name); | |
215 |
|
215 | |||
216 |
//! read vars etc. in scope of a |
|
216 | //! read vars etc. in scope of an \c object, optional looking inside of an object \c objectname | |
217 |
QStringList introspection(PyObject* |
|
217 | QStringList introspection(PyObject* object, const QString& objectname, ObjectType type); | |
218 |
|
218 | |||
219 | //! returns the found callable object or NULL |
|
219 | //! returns the found callable object or NULL | |
220 | //! @return new reference |
|
220 | //! @return new reference | |
221 |
PythonQtObjectPtr lookupCallable(PyObject* |
|
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 | _stdOut = ""; |
|
173 | _stdOut = ""; | |
174 | _stdErr = ""; |
|
174 | _stdErr = ""; | |
175 | PythonQtObjectPtr p; |
|
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 | if (!p) { |
|
186 | if (!p) { | |
178 | PythonQt::self()->handleError(); |
|
187 | PythonQt::self()->handleError(); | |
179 | } |
|
188 | } |
General Comments 0
You need to be logged in to leave comments.
Login now