lib

pythonobject.cpp
1/***************************************************************************
2 * pythonobject.cpp
3 * This file is part of the KDE project
4 * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 ***************************************************************************/
19
20#include "pythonobject.h"
21#include "pythonextension.h"
22
23using namespace Kross::Python;
24
25PythonObject::PythonObject(const Py::Object& object)
26 : Kross::Api::Object()
27 , m_pyobject(object)
28{
29 krossdebug( TQString("PythonObject::PythonObject() constructor") );
30
31 Py::List x( object.dir() );
32 for(Py::Sequence::iterator i= x.begin(); i != x.end(); ++i) {
33 std::string s = (*i).str();
34 if(s == "__init__")
35 continue;
36
37 //if(! m_pyobject.hasAttr( (*i).str() )) continue;
38 Py::Object o = m_pyobject.getAttr(s);
39
40 TQString t;
41 if(o.isCallable()) t += "isCallable ";
42 if(o.isDict()) t += "isDict ";
43 if(o.isList()) t += "isList ";
44 if(o.isMapping()) t += "isMapping ";
45 if(o.isNumeric()) t += "isNumeric ";
46 if(o.isSequence()) t += "isSequence ";
47 if(o.isTrue()) t += "isTrue ";
48 if(o.isInstance()) t += "isInstance ";
49 krossdebug( TQString("PythonObject::PythonObject() method '%1' (%2)").arg( (*i).str().as_string().c_str() ).arg(t) );
50
51 if(o.isCallable())
52 m_calls.append( (*i).str().as_string().c_str() );
53 }
54}
55
59
60const TQString PythonObject::getClassName() const
61{
62 return "Kross::Python::PythonObject";
63}
64
66{
67 krossdebug( TQString("PythonObject::call(%1)").arg(name) );
68
69 if(m_pyobject.isInstance()) {
70 //if(! m_calls.contains(n)) throw ...
71
72 PyObject* r = PyObject_CallMethod(m_pyobject.ptr(), (char*) name.latin1(), 0);
73 if(! r) { //FIXME happens too if e.g. number of arguments doesn't match !!!
74 Py::Object errobj = Py::value(Py::Exception()); // get last error
75 throw Kross::Api::Exception::Ptr( new Kross::Api::Exception(TQString("Failed to call method '%1': %2").arg(name).arg(errobj.as_string().c_str())) );
76 }
77 Py::Object result(r, true);
78
79 //krossdebug( TQString("PythonObject::call(%1) call return value = '%2'").arg(name).arg(result.as_string().c_str()) );
80 return PythonExtension::toObject(result);
81 }
82 /*TODO??? ELSE create class instance for class-definitions???
83 Kross::Api::ClassBase* clazz = new Kross::Api::ClassBase("", this);
84 return new PythonExtension(clazz);
85 */
86
87 return Kross::Api::Object::call(name, arguments);
88}
89
91{
92 return m_calls;
93}
94
Common exception class used for representing exceptions in Kross.
Definition exception.h:39
TDESharedPtr< Exception > Ptr
Shared pointer to implement reference-counting.
Definition exception.h:45
TDESharedPtr< List > Ptr
Shared pointer to implement reference-counting.
Definition list.h:46
virtual Object::Ptr call(const TQString &name, TDESharedPtr< List > arguments)
Pass a call to the object and evaluated it recursive down the object-hierachy.
Definition object.cpp:50
Object()
Constructor.
Definition object.cpp:29
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
Definition object.h:56
virtual const TQString getClassName() const
Return the class name.
virtual TQStringList getCalls()
Return a list of supported callable objects.
virtual ~PythonObject()
Destructor.
virtual Kross::Api::Object::Ptr call(const TQString &name, Kross::Api::List::Ptr arguments)
Pass a call to the object.
PythonObject(const Py::Object &object)
Constructor.
The common Kross API used as common codebase.
Definition callable.h:33
The Python plugin for the Kross scripting framework.
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:33
KOFFICE_EXPORT void krossdebug(const TQString &s)
Debugging function.