[C++-sig] Re: creating an instance of a class_<> object in c+ + and exporting it to python

Max Khesin MKhesin at liquidnet.com
Tue Aug 17 00:05:13 CEST 2004


I believe that you will not see the "dummy" method by doing dir(). IIRC
boost.python hijacks __getattr__ to delegate the lookup to the c++
implemented code. Just try a.dummy(), does it work?
max

> -----Original Message-----
> From: Francois Ostiguy [mailto:ostiguy at fnal.gov]
> Sent: Monday, August 16, 2004 5:51 PM
> To: c++-sig at python.org
> Subject: Re: [C++-sig] Re: creating an instance of a class_<> 
> object in
> c++ and exporting it to python
> 
> 
> 
> >
> > As Mike and I wrote, just do this:
> >
> >   object PythonInstanceOfA  = py_get_existing();
> >
> 
> Unfortunately, that does not seem to do quite what I want.
> I constructed a simple complete toy example to demonstrate.
> 
> Note the following:
> 
> (1) creating python_object_a forces an undesired instantiation of
>     a type A (i.e. in addition to the the existing instance a)
> 
> (2) if the interface defines a python object without
>      an __init__ function, the result is a runtime error:
>      "Object cannot by created from python."
> 
> (3) if you compile the example, here is what you get when the
>     module is imported from python
> 
> 
>   Python 2.3.3 (#1, Jul  7 2004, 13:43:38)
>   [GCC 3.3.3] on linux2
>   Type "help", "copyright", "credits" or "license" for more 
> information.
>   >>> from inject import *
>   >>> a
>   <inject.A object at 0xb733b10c>
>   >>> dir()
>   ['A', '__builtins__', '__doc__', '__name__', 'a']
>   >>> dir (a)
>   ['A', '__class__', '__delattr__', '__dict__', '__doc__',
>   '__getattribute__', '__hash__', '__init__', '__instance_size__',
>   '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
>   '__setattr__', '__str__', '__weakref__']
>   >>>
> 
>   Note that the object instance "a" has been injected. However, for a
>   reason that escapes me, the method "dummy" which is
>   part of the specified interface is not correctly exported.
> 
>   Again, my goal is to wrap an existing instance of A, without
>   having to create a spurious object (basically without having to
>   call the A constructor more than once).
> 
>   Comments, suggestions ???
> 
> -Francois
> 
> --------------------------------------------------------------
> --------------
> Dr. Jean-Francois OSTIGUY                              voice: 
> (630) 840-2231
> Beam Physics Dept MS220                                  FAX: 
> (630) 840-6039
> Fermi National Accelerator Laboratory                email: 
> ostiguy at fnal.gov
> Batavia IL 60510-0500                           
> WWW:www-ap.fnal.gov/~ostiguy
> 
> 
> /////// *************** example: inject.cpp **************** /////////
> 
> #include <boost/python.hpp>
> 
> class A {
> public:
>   A(int d) { _dummy = d; }
>   int dummy() { return _dummy;}
> private:
> 
>   int _dummy;
> };
> 
> A* a = 0;
> 
> A* get_existing() { return a; }
> 
> void inject_object();
> 
> using namespace boost::python;
> 
> class_ <A, A*,  boost::noncopyable>* PythonTypeAPtr;
> 
> BOOST_PYTHON_MODULE( inject )
> {
> 
> 
>     static class_ <A, A*,  boost::noncopyable> 
> PythonTypeA("A", init<int>());
> 
>    //*** note: using no_init here will cause a runtime error !!
> 
>    PythonTypeA.def("A", &A::dummy);
> 
>    /***** this does not seem to export the dummy function to 
> python. Why ?
> 
>    PythonTypeAPtr = &PythonTypeA;
> 
>     inject_object();
> 
> }
> 
> void  inject_object() {
> 
>  a = new A(99);
> 
>  object py_get_existing =
>      make_function( &get_existing, return_value_policy<
> reference_existing_object>() );
> 
>  object python_object_a = (*PythonTypeAPtr)(2); // <<<<<<==========
>  //****** instantiation of a spurious object
> 
>  python_object_a = py_get_existing();
> 
> 
>  handle<> main_module ( boost::python::borrowed(
> PyImport_AddModule("__main__") )  );
>  handle<> main_dict   ( boost::python::borrowed(
> PyModule_GetDict(main_module.get()) ));
>  PyDict_SetItemString  ( main_dict.get(), "a",  
> python_object_a.ptr() );
> 
> }
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
> 
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager. This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20040816/b021c953/attachment.htm>


More information about the Cplusplus-sig mailing list