lib
pythonobject.cpp00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "pythonobject.h"
00021 #include "pythonextension.h"
00022
00023 using namespace Kross::Python;
00024
00025 PythonObject::PythonObject(const Py::Object& object)
00026 : Kross::Api::Object()
00027 , m_pyobject(object)
00028 {
00029 krossdebug( QString("PythonObject::PythonObject() constructor") );
00030
00031 Py::List x( object.dir() );
00032 for(Py::Sequence::iterator i= x.begin(); i != x.end(); ++i) {
00033 std::string s = (*i).str();
00034 if(s == "__init__")
00035 continue;
00036
00037
00038 Py::Object o = m_pyobject.getAttr(s);
00039
00040 QString t;
00041 if(o.isCallable()) t += "isCallable ";
00042 if(o.isDict()) t += "isDict ";
00043 if(o.isList()) t += "isList ";
00044 if(o.isMapping()) t += "isMapping ";
00045 if(o.isNumeric()) t += "isNumeric ";
00046 if(o.isSequence()) t += "isSequence ";
00047 if(o.isTrue()) t += "isTrue ";
00048 if(o.isInstance()) t += "isInstance ";
00049 krossdebug( QString("PythonObject::PythonObject() method '%1' (%2)").arg( (*i).str().as_string().c_str() ).arg(t) );
00050
00051 if(o.isCallable())
00052 m_calls.append( (*i).str().as_string().c_str() );
00053 }
00054 }
00055
00056 PythonObject::~PythonObject()
00057 {
00058 }
00059
00060 const QString PythonObject::getClassName() const
00061 {
00062 return "Kross::Python::PythonObject";
00063 }
00064
00065 Kross::Api::Object::Ptr PythonObject::call(const QString& name, Kross::Api::List::Ptr arguments)
00066 {
00067 krossdebug( QString("PythonObject::call(%1)").arg(name) );
00068
00069 if(m_pyobject.isInstance()) {
00070
00071
00072 PyObject* r = PyObject_CallMethod(m_pyobject.ptr(), (char*) name.latin1(), 0);
00073 if(! r) {
00074 Py::Object errobj = Py::value(Py::Exception());
00075 throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(QString("Failed to call method '%1': %2").arg(name).arg(errobj.as_string().c_str())) );
00076 }
00077 Py::Object result(r, true);
00078
00079
00080 return PythonExtension::toObject(result);
00081 }
00082
00083
00084
00085
00086
00087 return Kross::Api::Object::call(name, arguments);
00088 }
00089
00090 QStringList PythonObject::getCalls()
00091 {
00092 return m_calls;
00093 }
00094
|