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