[C++-sig] [pybindgen] Problems on PyObject* param
Olaf Peter
ope-devel at gmx.de
Wed Aug 20 21:27:48 CEST 2008
Hi,
I try out some binding generator tools and start with pybindgen :)
Anyway, here my script:
import pybindgen
import sys
# convinience
param=pybindgen.param
retval=pybindgen.retval
mod = pybindgen.Module('redirector')
mod.add_include('"redirector.hpp"')
stdout_py = mod.add_class('StdoutPy')
stdout_py.add_constructor([])
stdout_py.add_method('write', None, [param('PyObject*', 'value')])
stdout_py.add_method('flush', None, [])
stderr_py = mod.add_class('StderrPy')
stderr_py.add_constructor([])
stderr_py.add_method('write', None, [param('PyObject*', 'value')])
stderr_py.add_method('flush', None, [])
stdin_py = mod.add_class('StdinPy')
stdin_py.add_constructor([])
stdin_py.add_method('readline', retval('PyObject*'), [param('PyObject*',
'value')])
mod.generate(sys.stdout)
for this classes:
class PyConsole;
class StdoutPy
: public boost::noncopyable
{
public:
StdoutPy(PyConsole* console);
void write(PyObject* args);
void flush();
private:
PyConsole* m_console;
};
class StderrPy
: public boost::noncopyable
{
public:
StderrPy(PyObject* console);
void write(PyObject* args);
void flush();
private:
PyConsole* m_console;
};
class StdinPy
: public boost::noncopyable
{
public:
StdinPy(PyConsole* console);
PyObject* readline(PyObject* args);
private:
PyConsole* m_console;
};
as you assume, topic is redirection of the sys write function to a gui.
Anyway, I've got errors. Any help?
$ python redirector.py
/usr/lib/python2.5/site-packages/pybindgen/typehandlers/base.py:1173:
UserWarning: Exception '__init__() takes at least 4 arguments (3 given)'
in type handler <class
'pybindgen.typehandlers.pyobjecttype.PyObjectParam'> constructor
warnings.warn("Exception %r in type handler %s constructor" %
(str(ex), type_handler_class))
Traceback (most recent call last):
File "redirector.py", line 13, in <module>
stdout_py.add_method('write', None, [param('PyObject*', 'value')])
File "/usr/lib/python2.5/site-packages/pybindgen/cppclass.py", line
1127, in add_method
meth = CppMethod(*args, **kwargs)
File "/usr/lib/python2.5/site-packages/pybindgen/cppmethod.py", line
104, in __init__
parameters = [utils.eval_param(param, self) for param in parameters]
File "/usr/lib/python2.5/site-packages/pybindgen/utils.py", line 205,
in eval_param
TypeLookupError))
File "/usr/lib/python2.5/site-packages/pybindgen/utils.py", line 115,
in call_with_error_handling
return callable(*args, **kwargs)
File
"/usr/lib/python2.5/site-packages/pybindgen/typehandlers/base.py", line
1171, in new
return type_handler_class(*args, **kwargs)
TypeError: __init__() takes at least 4 arguments (3 given)
The idea is to create the object in C++ and then set it to python,
inside PyConsole class like:
stdoutPy = new StdoutPy(this);
...
PySys_SetObject("stdout", stdoutPy);
and the redirect the member calls from python into C++.
Thanks,
Olaf
More information about the Cplusplus-sig
mailing list