[C++-sig] [pybindgen] Problems on PyObject* param

Gustavo Carneiro gjcarneiro at gmail.com
Wed Aug 20 22:41:21 CEST 2008


2008/8/20 Olaf Peter <ope-devel at gmx.de>

> 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)


I know the exception message isn't terribly informative, but you are missing
an argument.

should be:

   retval('PyObject*', caller_owns_return=True|False)

and:

   param('PyObject*', 'name', transfer_ownership=True|False)

caller_owns_return indicates whether the caller owns the returned PyObject*
reference or not.  tranfer_ownership tells whether the callee expects to own
the reference passed in.

I am unable to say, in your example, what are the correct values.  Only the
implementation file can tell.

Hope it helps.

-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080820/9f98b0c4/attachment.htm>


More information about the Cplusplus-sig mailing list