[C++-sig] Problem using the ptr() function

Emmanuel Taurel etaurel at cells.es
Thu Oct 27 18:31:32 CEST 2005


Hi Dave,

I have attached to this e-mail five files which demonstrates my problem.
These files are :
1 - ds.h : The C++ include file
2 - ds.cpp : The C++ implementation
3 - py_ds.cpp : The boost interface stuff
4 - py_ds.h : The boost interface include file
5 - main.py : The python code

I am a beginner in both Python and boost. Therefore, they are certainly some
piece of code in these files that you will find strange...
I have isolated the problem as far as I am able to.

If in file de.cpp, I use line 16, the python code outputs are

------------------------------------------------------------
In init_module function
Conversion from MyStrArray to python installed in Boost
In Device constructor with string a/b/c
in the MyStrArray convert function to_python
Input args =  The first string
Input args =  The second string
------------------------------------------------------------

And it is correct. If I use line 15 (using the ptr() function), the python
code outputs are

-----------------------------------------------------------

In init_module function
Conversion from MyStrArray to python installed in Boost
In Device constructor with string a/b/c
Traceback (most recent call last):
  File "main.py", line 23, in ?
    c.execute_device_method()
TypeError: No Python class registered for C++ class std::vector<std::string,
std::allocator<std::string> >
------------------------------------------------------------

The principle of this software is :
I create one python instance of the "MyContainer" class. Then I create one
instance of the "MyDevice" class which is stored in  a data member of the
C++ class from which MyContainer inherits from. This is done by calling the
create_device() method of the MyContainer object in the python code.
Then, I call the execute_device_method() of the MyContainer class which is
coded in C++ and which callback one of the MyDevice method defined in
Python. This is this callback (C++ calls a Python method) which fails if I
use the ptr() function.
You will also certainly notice that I have installed my own converters
>From my type which in this small example is a vector<string>. In the real
application, it is not a vector<string> but something more complicated.

Thank's for your help

Emmanuel Taurel (etaurel at cells.es)





-----Original Message-----
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On
Behalf Of David Abrahams
Sent: Thursday, October 27, 2005 1:40 PM
To: c++-sig at python.org
Subject: Re: [C++-sig] Problem using the ptr() function

Emmanuel Taurel <etaurel at cells.es> writes:

> Hello everybody,
>
>  
>
> I am using Boost.python to interface a C++ set of classes. In one of my
C+++
> class, I am calling a method implemented in Python with the 
> boost "call_method" call. This method has one parameter of our own data
type
> and return a pointer to this type. I have installed converters for this
type
> as it is adviced in the FAQ "How can I automatically convert my custom
> string type..." If I call my method like this
>
>  
>
> MyType *ret = call_method<MyType *>(my_py_obj,"My_method", my_ptr)
>
>  
>
> With my_ptr being a MyType *, it works. But if I call it like this
>
>  
>
> MyType *ret = call_method<MyType *>(my_py_obj,"My_method", ptr(my_ptr)) to
> avoid a copy, I received a run time error complaining that 
>
>  
>
> "No Python class registered for C++..."
>
>  
>
> Could someone explain me why it works in the first case and not in the
> second one.

Please post a minimal, complete, reproducible testcase: one C++ file,
one Python file, the result you expect and the result you got.

Thanks,
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig
-------------- next part --------------
A non-text attachment was scrubbed...
Name: py_ds.h
Type: application/octet-stream
Size: 731 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20051027/767ef605/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ds.cpp
Type: application/octet-stream
Size: 431 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20051027/767ef605/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ds.h
Type: application/octet-stream
Size: 615 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20051027/767ef605/attachment-0002.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.py
Type: application/octet-stream
Size: 495 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20051027/767ef605/attachment-0003.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: py_ds.cpp
Type: application/octet-stream
Size: 3571 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20051027/767ef605/attachment-0004.obj>


More information about the Cplusplus-sig mailing list