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 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         //if(! m_pyobject.hasAttr( (*i).str() )) continue;
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         //if(! m_calls.contains(n)) throw ...
00071 
00072         PyObject* r = PyObject_CallMethod(m_pyobject.ptr(), (char*) name.latin1(), 0);
00073         if(! r) { //FIXME happens too if e.g. number of arguments doesn't match !!!
00074             Py::Object errobj = Py::value(Py::Exception()); // get last error
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         //krossdebug( QString("PythonObject::call(%1) call return value = '%2'").arg(name).arg(result.as_string().c_str()) );
00080         return PythonExtension::toObject(result);
00081     }
00082     /*TODO??? ELSE create class instance for class-definitions???
00083     Kross::Api::ClassBase* clazz = new Kross::Api::ClassBase("", this);
00084     return new PythonExtension(clazz);
00085     */
00086 
00087     return Kross::Api::Object::call(name, arguments);
00088 }
00089 
00090 QStringList PythonObject::getCalls()
00091 {
00092     return m_calls;
00093 }
00094 
KDE Home | KDE Accessibility Home | Description of Access Keys