lib

pythonobject.cpp

00001 /***************************************************************************
00002  * pythonobject.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
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         //if(! m_pyobject.hasAttr( (*i).str() )) continue;
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         //if(! m_calls.contains(n)) throw ...
00073 
00074         PyObject* r = PyObject_CallMethod(m_pyobject.ptr(), (char*) name.latin1(), 0);
00075         if(! r) { //FIXME happens too if e.g. number of arguments doesn't match !!!
00076             Py::Object errobj = Py::value(Py::Exception()); // get last error
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         //kdDebug() << QString("PythonObject::call(%1) call return value = '%2'").arg(name).arg(result.as_string().c_str()) << endl;
00082         return PythonExtension::toObject(result);
00083     }
00084     /*TODO??? ELSE create class instance for class-definitions???
00085     Kross::Api::ClassBase* clazz = new Kross::Api::ClassBase("", this);
00086     return new PythonExtension(clazz);
00087     */
00088 
00089     return Kross::Api::Object::call(name, arguments);
00090 }
00091 
00092 QStringList PythonObject::getCalls()
00093 {
00094     return m_calls;
00095 }
00096 
KDE Home | KDE Accessibility Home | Description of Access Keys