20#include "pythonextension.h"
21#include "pythonobject.h"
23#include "../api/variant.h"
24#include "../api/dict.h"
25#include "../api/exception.h"
33#ifdef KROSS_PYTHON_EXTENSION_CTOR_DEBUG
34 krossdebug( TQString(
"Kross::Python::PythonExtension::Constructor objectname='%1' objectclass='%2'").arg(m_object->getName()).arg(m_object->getClassName()) );
37 behaviors().name(
"KrossPythonExtension");
45 behaviors().supportGetattr();
47 m_proxymethod =
new Py::MethodDefExt<PythonExtension>(
50 Py::method_varargs_call_handler_t( proxyhandler ),
57#ifdef KROSS_PYTHON_EXTENSION_DTOR_DEBUG
58 krossdebug( TQString(
"Kross::Python::PythonExtension::Destructor objectname='%1' objectclass='%2'").arg(m_object->getName()).arg(m_object->getClassName()) );
64Py::Object PythonExtension::str()
67 TQString s = callable ? callable->
getName() : m_object->getClassName();
68 return toPyObject(s.isEmpty() ? : s);
71Py::Object PythonExtension::repr()
73 return toPyObject( m_object->toString() );
79#ifdef KROSS_PYTHON_EXTENSION_GETATTR_DEBUG
80 krossdebug( TQString(
"Kross::Python::PythonExtension::getattr name='%1'").arg(n) );
84 if(!strcmp(n,
"__methods__")) {
86 TQStringList calls = m_object->getCalls();
87 for(TQStringList::Iterator it = calls.begin(); it != calls.end(); ++it) {
88#ifdef KROSS_PYTHON_EXTENSION_GETATTR_DEBUG
89 krossdebug( TQString(
"Kross::Python::PythonExtension::getattr name='%1' callable='%2'").arg(n).arg(*it) );
91 methods.append(Py::String( (*it).latin1() ));
96 if(!strcmp(n,
"__members__")) {
100 TQMap<TQString, Kross::Api::Object::Ptr> children = callable->
getChildren();
101 TQMap<TQString, Kross::Api::Object::Ptr>::Iterator it( children.begin() );
102 for(; it != children.end(); ++it) {
103#ifdef KROSS_PYTHON_EXTENSION_GETATTR_DEBUG
104 krossdebug( TQString(
"Kross::Python::PythonExtension::getattr n='%1' child='%2'").arg(n).arg(it.key()) );
106 members.append(Py::String( it.key().latin1() ));
115#ifdef KROSS_PYTHON_EXTENSION_GETATTR_DEBUG
116 krossdebug( TQString(
"Kross::Python::PythonExtension::getattr name='%1' is a internal name.").arg(n) );
118 return Py::PythonExtension<PythonExtension>::getattr_methods(n);
124 self[0] = Py::Object(
this);
125 self[1] = Py::String(n);
126 return Py::Object(PyCFunction_New( &m_proxymethod->ext_meth_def, self.ptr() ),
true);
149#ifdef KROSS_PYTHON_EXTENSION_TOOBJECT_DEBUG
150 krossdebug( TQString(
"Kross::Python::PythonExtension::toObject(Py::Tuple)") );
153 TQValueList<Kross::Api::Object::Ptr> l;
154 uint size = tuple.size();
155 for(uint i = 0; i < size; i++)
156 l.append( toObject( tuple[i] ) );
162#ifdef KROSS_PYTHON_EXTENSION_TOOBJECT_DEBUG
163 krossdebug( TQString(
"Kross::Python::PythonExtension::toObject(Py::List)") );
166 TQValueList<Kross::Api::Object::Ptr> l;
167 uint length = list.length();
168 for(uint i = 0; i < length; i++)
169 l.append( toObject( list[i] ) );
175 TQMap<TQString, Kross::Api::Object::Ptr> map;
176 Py::List l = dict.keys();
177 uint length = l.length();
178 for(Py::List::size_type i = 0; i < length; ++i) {
179 const char* n = l[i].str().as_string().c_str();
180 map.replace(n, toObject( dict[n] ));
182 return new Kross::Api::Dict(map);
187#ifdef KROSS_PYTHON_EXTENSION_TOOBJECT_DEBUG
188 krossdebug( TQString(
"Kross::Python::PythonExtension::toObject(Py::Object) object='%1'").arg(
object.as_string().c_str()) );
190 if(
object == Py::None())
192 PyTypeObject *type = (PyTypeObject*)
object.type().ptr();
193#ifdef KROSS_PYTHON_EXTENSION_TOOBJECT_DEBUG
194 krossdebug( TQString(
"Kross::Python::PythonExtension::toObject(Py::Object) type='%1'").arg(type->tp_name) );
196 if(type == &PyInt_Type)
197 return new Kross::Api::Variant(
int(Py::Int(
object)));
198 if(type == &PyBool_Type)
199 return new Kross::Api::Variant(TQVariant(
object.isTrue()));
200 if(type == &PyLong_Type)
201 return new Kross::Api::Variant(TQ_LLONG(
long(Py::Long(
object))));
202 if(type == &PyFloat_Type)
203 return new Kross::Api::Variant(
double(Py::Float(
object)));
205 if( PyType_IsSubtype(type,&PyString_Type) ) {
206#ifdef Py_USING_UNICODE
216 return new Kross::Api::Variant(
object.as_string().c_str());
219 if(type == &PyTuple_Type)
220 return toObject(Py::Tuple(
object)).data();
221 if(type == &PyList_Type)
222 return toObject(Py::List(
object)).data();
223 if(type == &PyDict_Type)
224 return toObject(Py::Dict(
object.ptr())).data();
226 if(
object.isInstance())
227 return new PythonObject(
object);
229 Py::ExtensionObject<PythonExtension> extobj(
object);
232 krosswarning(
"EXCEPTION in PythonExtension::toObject(): Failed to determinate PythonExtension object.");
233 throw Py::Exception(
"Failed to determinate PythonExtension object.");
235 if(! extension->m_object) {
236 krosswarning(
"EXCEPTION in PythonExtension::toObject(): Failed to convert the PythonExtension object into a Kross::Api::Object.");
237 throw Py::Exception(
"Failed to convert the PythonExtension object into a Kross::Api::Object.");
240#ifdef KROSS_PYTHON_EXTENSION_TOOBJECT_DEBUG
241 krossdebug(
"Kross::Python::PythonExtension::toObject(Py::Object) successfully converted into Kross::Api::Object." );
243 return extension->m_object;
246const Py::Object PythonExtension::toPyObject(
const TQString& s)
248#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
249 krossdebug( TQString(
"Kross::Python::PythonExtension::toPyObject(TQString)") );
251 return s.isNull() ? Py::String() : Py::
String(s.latin1());
254const Py::List PythonExtension::toPyObject(
const TQStringList& list)
256#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
257 krossdebug( TQString(
"Kross::Python::PythonExtension::toPyObject(TQStringList)") );
260 for(TQStringList::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it)
261 l.append(toPyObject(*it));
265const Py::Dict PythonExtension::toPyObject(
const TQStringVariantMap& map)
267#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
268 krossdebug( TQString(
"Kross::Python::PythonExtension::toPyObject(TQStringVariantMap)") );
271 for(TQStringVariantMap::ConstIterator it = map.constBegin(); it != map.constEnd(); ++it)
272 d.setItem(it.key().latin1(), toPyObject(it.data()));
276const Py::List PythonExtension::toPyObject(
const TQValueList<TQVariant>& list)
278#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
279 krossdebug( TQString(
"Kross::Python::PythonExtension::toPyObject(TQValueList<TQVariant>)") );
282 for(TQValueList<TQVariant>::ConstIterator it = list.constBegin(); it != list.constEnd(); ++it)
283 l.append(toPyObject(*it));
287const Py::Object PythonExtension::toPyObject(
const TQVariant& variant)
289#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
290 krossdebug( TQString(
"Kross::Python::PythonExtension::toPyObject(TQVariant) typename='%1'").arg(variant.typeName()) );
293 switch(variant.type()) {
294 case TQVariant::Invalid:
296 case TQVariant::Bool:
297 return Py::Int(variant.toBool());
299 return Py::Int(variant.toInt());
300 case TQVariant::UInt:
301 return Py::Long((
unsigned long)variant.toUInt());
302 case TQVariant::Double:
303 return Py::Float(variant.toDouble());
304 case TQVariant::Date:
305 case TQVariant::Time:
306 case TQVariant::DateTime:
307 case TQVariant::ByteArray:
308 case TQVariant::BitArray:
309 case TQVariant::CString:
310 case TQVariant::String:
311 return toPyObject(variant.toString());
312 case TQVariant::StringList:
313 return toPyObject(variant.toStringList());
315 return toPyObject(variant.toMap());
316 case TQVariant::List:
317 return toPyObject(variant.toList());
323 case TQVariant::LongLong: {
324 TQ_LLONG l = variant.toLongLong();
326 return Py::Long((
long)l);
329 case TQVariant::ULongLong: {
330 return Py::Long((
unsigned long)variant.toULongLong());
334 krosswarning( TQString(
"Kross::Python::PythonExtension::toPyObject(TQVariant) Not possible to convert the TQVariant type '%1' to a Py::Object.").arg(variant.typeName()) );
343#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
344 krossdebug(
"Kross::Python::PythonExtension::toPyObject(Kross::Api::Object) is NULL => Py::None");
349 const TQString classname =
object->getClassName();
350 if(classname ==
"Kross::Api::Variant") {
351 TQVariant v =
static_cast<Kross::Api::Variant*
>(
object.data() )->getValue();
352#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
353 krossdebug( TQString(
"Kross::Python::PythonExtension::toPyObject(Kross::Api::Object) is Kross::Api::Variant %1").arg(v.toString()) );
355 return toPyObject(v);
358 if(classname ==
"Kross::Api::List") {
359#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
360 krossdebug(
"Kross::Python::PythonExtension::toPyObject(Kross::Api::Object) is Kross::Api::List");
363 Kross::Api::List* list =
static_cast<Kross::Api::List*
>(
object.data() );
364 TQValueList<Kross::Api::Object::Ptr> valuelist = list->
getValue();
365 for(TQValueList<Kross::Api::Object::Ptr>::Iterator it = valuelist.begin(); it != valuelist.end(); ++it)
366 pylist.append( toPyObject(*it) );
370 if(classname ==
"Kross::Api::Dict") {
371#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
372 krossdebug(
"Kross::Python::PythonExtension::toPyObject(Kross::Api::Object) is Kross::Api::Dict");
375 Kross::Api::Dict* dict =
static_cast<Kross::Api::Dict*
>(
object.data() );
376 TQMap<TQString, Kross::Api::Object::Ptr> valuedict = dict->
getValue();
377 for(TQMap<TQString, Kross::Api::Object::Ptr>::Iterator it = valuedict.begin(); it != valuedict.end(); ++it) {
378 const char* n = it.key().latin1();
379 pydict[ n ] = toPyObject( it.data() );
384#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
385 krossdebug( TQString(
"Trying to handle PythonExtension::toPyObject(%1) as PythonExtension").arg(object->getClassName()) );
392#ifdef KROSS_PYTHON_EXTENSION_TOPYOBJECT_DEBUG
393 krossdebug( TQString(
"Kross::Python::PythonExtension::toPyTuple(Kross::Api::List) name='%1'").arg(list ? list->getName() :
"NULL") );
395 uint count = list ? list->count() : 0;
396 Py::Tuple tuple(count);
397 for(uint i = 0; i < count; i++)
398 tuple.setItem(i, toPyObject(list->item(i)));
402PyObject* PythonExtension::proxyhandler(PyObject *_self_and_name_tuple, PyObject *args)
404 Py::Tuple tuple(_self_and_name_tuple);
406 TQString methodname = Py::String(tuple[1]).as_string().c_str();
411#ifdef KROSS_PYTHON_EXTENSION_CALL_DEBUG
412 krossdebug( TQString(
"Kross::Python::PythonExtension::proxyhandler methodname='%1' arguments='%2'").arg(methodname).arg(arguments->toString()) );
415 Kross::Api::Callable* callable =
dynamic_cast<Kross::Api::Callable*
>(self->m_object.data());
416 if(callable && callable->
hasChild(methodname)) {
417#ifdef KROSS_PYTHON_EXTENSION_CALL_DEBUG
418 krossdebug( TQString(
"Kross::Python::PythonExtension::proxyhandler methodname='%1' is a child object of '%2'.").arg(methodname).arg(self->m_object->getName()) );
420 Py::Object result = toPyObject( callable->
getChild(methodname)->call(TQString(), arguments) );
421 result.increment_reference_count();
424#ifdef KROSS_PYTHON_EXTENSION_CALL_DEBUG
425 krossdebug( TQString(
"Kross::Python::PythonExtension::proxyhandler try to call function with methodname '%1' in object '%2'.").arg(methodname).arg(self->m_object->getName()) );
427 Py::Object result = toPyObject( self->m_object->call(methodname, arguments) );
428 result.increment_reference_count();
431 catch(Py::Exception& e) {
432 const TQString err = Py::value(e).as_string().c_str();
433 krosswarning( TQString(
"Py::Exception in Kross::Python::PythonExtension::proxyhandler %1").arg(err) );
437 const TQString err = e->toString();
438 krosswarning( TQString(
"Kross::Api::Exception in Kross::Python::PythonExtension::proxyhandler %1").arg(err) );
Base class for callable objects.
TQMap< TQString, Object::Ptr > getChildren() const
Return all children.
bool hasChild(const TQString &name) const
Returns if the defined child is avaible.
const TQString getName() const
Object::Ptr getChild(const TQString &name) const
Return the defined child or NULL if there is no such object with that name avaible.
TDESharedPtr< Exception > Ptr
Shared pointer to implement reference-counting.
The List class implementates Value to handle lists and collections.
TDESharedPtr< List > Ptr
Shared pointer to implement reference-counting.
TDESharedPtr< Object > Ptr
Shared pointer to implement reference-counting.
V & getValue()
Return the value.
virtual ~PythonExtension()
Destructor.
virtual Py::Object getattr(const char *name)
Overloaded method to handle attribute calls from within python.
PythonExtension(Kross::Api::Object::Ptr object)
Constructor.
The Python plugin for the Kross scripting framework.
void krossdebug(const TQString &s)
Debugging function.
void krosswarning(const TQString &s)
Warning function.