From lloyd at fusion.net.nz Tue Jan 1 02:40:57 2008 From: lloyd at fusion.net.nz (Lloyd Weehuizen) Date: Tue, 01 Jan 2008 14:40:57 +1300 Subject: [C++-sig] Help with custom converters In-Reply-To: <4776B111.9020607@ncsu.edu> References: <4771F0AE.40406@ncsu.edu> <7465b6170712252340o159aa52qe1dae3707e3ab625@mail.gmail.com> <47732B84.70400@ncsu.edu> <4773325D.3040907@gmail.com> <4773BD97.1000703@ncsu.edu> <4773C17B.4050802@ncsu.edu> <4775DF5D.3030400@ncsu.edu> <7465b6170712291047k2933ceb5o20a0faad77bd85c0@mail.gmail.com> <4776B111.9020607@ncsu.edu> Message-ID: <47799A29.60601@fusion.net.nz> Hi I'm having a strange problem when using custom converters, they seem to work fine when I have a function/method that returns an object type but fails if I try use the object as a boost::python::object. The error I get is: TypeError: No to_python (by-value) converter found for C++ type: class Interface For example: Interface* TestConverter(Interface* interface) { return interface; } Works correctly, the python class instance that inherits from Interface passes correctly through the TestConverter method when called from python. However, if I try this: Interface* TestConverter(Interface* interface) { boost::python::object obj(interface); return interface; } Then the TypeError is thrown. I would have thought the converter would be called for this case as well? Here's the converter code: template < typename T > struct ConverterScriptObject { ConverterScriptObject() { // Register custom the to python converter boost::python::to_python_converter< T*, ConverterScriptObject< T > >(); } static PyObject* convert(T* object) { PyObject* pyobject = Py_None; if (object) { PyObject* script_object = object->GetScriptObject(); if (script_object) { pyobject = script_object; } } Py_INCREF(pyobject); return pyobject; } }; ConverterScriptObject< Interface >(); Thanks, Lloyd From kloeckner at dam.brown.edu Tue Jan 1 20:24:15 2008 From: kloeckner at dam.brown.edu (Andreas =?iso-8859-1?q?Kl=F6ckner?=) Date: Tue, 1 Jan 2008 20:24:15 +0100 Subject: [C++-sig] [patch] Boost.Python Vector indexing suite::extend accepts general iterables Message-ID: <200801012024.18162.kloeckner@dam.brown.edu> Hi Joel, The attached patch allows the extend() method of containers wrapped using the vector indexing suite to accept general iterables (such as generator expressions, sets, etc.), not just ones that have __len__ and __getitem__. It does so by using Eric Niebler's stl_input_iterator, already present in recent versions of Boost.Python. I'd appreciate it if you could take a brief look and commit this if appropriate. If there are any issues, please let me know and I'll do my best to fix them. Thanks, Andreas -------------- next part -------------- A non-text attachment was scrubbed... Name: 0001-Allow-vector-indexing-suite-extend-to-accept-arbi.patch Type: text/x-diff Size: 1688 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part. URL: From kloeckner at dam.brown.edu Tue Jan 1 20:24:20 2008 From: kloeckner at dam.brown.edu (Andreas =?iso-8859-1?q?Kl=F6ckner?=) Date: Tue, 1 Jan 2008 20:24:20 +0100 Subject: [C++-sig] [patch] Boost.Python STL input iterator does not propagate exceptions Message-ID: <200801012024.22293.kloeckner@dam.brown.edu> Hi Eric, The attached patch fixes an exception propagation issue in the STL input iterator in Boost.Python. The problem is as follows: PyIter_Next may return 0 for two reasons: - No more iterants left. - An exception was caught. The current implementation interprets the latter as the former. A simple fix (throw error_already_set if appropriate) is attached. I'd appreciate it if you could commit this. If there are any issues, please let me know and I'll do my best to fix them. Thanks, Andreas -------------- next part -------------- A non-text attachment was scrubbed... Name: bpl-stl-iterator-prop-ex-2007-12-20.patch Type: text/x-diff Size: 635 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part. URL: From scott.h.cushman at citi.com Wed Jan 2 19:47:39 2008 From: scott.h.cushman at citi.com (Cushman, Scott H ) Date: Wed, 2 Jan 2008 13:47:39 -0500 Subject: [C++-sig] boost.python on Solaris Message-ID: With help from various posts I have compiled boost.python on Solaris. However, I get the following error running the quickstart example (in python 2.4.2 - Active State): >>> from extending import * Traceback (most recent call last): File "", line 1, in ? TypeError: unsupported operand type(s) for +=: 'NoneType' and 'str' >>> Any hints would be appreciated, Thanks again :shc From eric at boost-consulting.com Thu Jan 3 21:39:02 2008 From: eric at boost-consulting.com (Eric Niebler) Date: Thu, 03 Jan 2008 15:39:02 -0500 Subject: [C++-sig] [patch] Boost.Python STL input iterator does not propagate exceptions In-Reply-To: <200801012024.22293.kloeckner@dam.brown.edu> References: <200801012024.22293.kloeckner@dam.brown.edu> Message-ID: <477D47E6.5040705@boost-consulting.com> Andreas Kl?ckner wrote: > > The attached patch fixes an exception propagation issue in the STL input > iterator in Boost.Python. The problem is as follows: PyIter_Next may return 0 > for two reasons: > > - No more iterants left. > - An exception was caught. > > The current implementation interprets the latter as the former. A simple fix > (throw error_already_set if appropriate) is attached. > > I'd appreciate it if you could commit this. If there are any issues, please > let me know and I'll do my best to fix them. Thanks, please provide a test case. -- Eric Niebler Boost Consulting www.boost-consulting.com From ndbecker2 at gmail.com Fri Jan 4 18:51:15 2008 From: ndbecker2 at gmail.com (Neal Becker) Date: Fri, 04 Jan 2008 12:51:15 -0500 Subject: [C++-sig] How to return handle? Message-ID: I have a function that returns handle. What is the best return policy? From roman.yakovenko at gmail.com Fri Jan 4 21:13:39 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 4 Jan 2008 22:13:39 +0200 Subject: [C++-sig] How to return handle? In-Reply-To: References: Message-ID: <7465b6170801041213j25b5af2dk7927f6e3186521a8@mail.gmail.com> On Jan 4, 2008 7:51 PM, Neal Becker wrote: > I have a function that returns handle. What is the best > return policy? I think the default one. I think it's better to create boost::python::object and return it. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From ndbecker2 at gmail.com Fri Jan 4 22:48:40 2008 From: ndbecker2 at gmail.com (Neal Becker) Date: Fri, 04 Jan 2008 16:48:40 -0500 Subject: [C++-sig] How to return handle? References: <7465b6170801041213j25b5af2dk7927f6e3186521a8@mail.gmail.com> Message-ID: Roman Yakovenko wrote: > On Jan 4, 2008 7:51 PM, Neal Becker wrote: >> I have a function that returns handle. What is the best >> return policy? > > I think the default one. I think it's better to create > boost::python::object and return it. > > Both seem to work OK. FYI, neither compiles until after I added this: namespace boost { namespace python { namespace converter { template<> struct pyobject_traits : public pyobject_traits {}; }}} FYI, here is the code in question: inline handle test_new () { numpy_1d_array a (10, 2); << this contains a handle member (a.arr) return a.arr; } inline object test_new2 () { numpy_1d_array a (10, 2); return object(a.arr); } From lloyd at fusion.net.nz Sat Jan 5 01:44:24 2008 From: lloyd at fusion.net.nz (Lloyd Weehuizen) Date: Sat, 05 Jan 2008 13:44:24 +1300 Subject: [C++-sig] Help with custom converters Message-ID: <477ED2E8.6090305@fusion.net.nz> Hi I've done more research into the problem and it appears that when passing an Interface* object into a bp::object's constructor, boost looks up a converter for type Interface, not Interface* (which the converter is registered against). Registering a converter for an abstract interface seems impossible at the moment due to the compile time checks in as_to_python_function which cause the compiler to attempt to instance the class. template static void convert_function_must_take_value_or_const_reference(U(*)(T), int, T* = 0) {} template static void convert_function_must_take_value_or_const_reference(U(*)(T const&), long ...) {} Commenting out the above checks and registering the converter directly makes everything work correctly. Is there a cleaner solution to my abstract interface/converter problem? Thanks, Lloyd Lloyd Weehuizen wrote: > Hi > > I'm having a strange problem when using custom converters, they seem > to work fine when I have a function/method that returns an object type > but fails if I try use the object as a boost::python::object. > > The error I get is: > > TypeError: No to_python (by-value) converter found for C++ type: class > Interface > > For example: > > Interface* TestConverter(Interface* interface) > { > return interface; > } > > Works correctly, the python class instance that inherits from > Interface passes correctly through the TestConverter method when > called from python. > > However, if I try this: > Interface* TestConverter(Interface* interface) > { > boost::python::object obj(interface); > return interface; > } > > Then the TypeError is thrown. I would have thought the converter would > be called for this case as well? > > Here's the converter code: > From dwolfe at gforcetech.com Sun Jan 6 02:23:55 2008 From: dwolfe at gforcetech.com (David Wolfe) Date: Sat, 05 Jan 2008 20:23:55 -0500 Subject: [C++-sig] Getting operator[]() to throw RangeError using py++ Message-ID: <47802DAB.4080700@gforcetech.com> Using py++, can anyone help me figure out how to get operator[]() to throw a RangeError for some vector classes I'm trying to wrap? The default behavior is a bit undesirable: >>> import osg >>> v = osg.Vec3f(1,2,3) >>> v.x 1.0 >>> v.y 2.0 >>> v.z = -1 >>> v[0] 1.0 >>> v[1] 2.0 >>> v[2] -1.0 >>> v[3] 1.4326351413631272e-038 >>> v[100] 0.0 >>> v[-10] 1.0636871398322276e+037 >>> How can I firewall off 'garbage' index values from operator[]()? Any advice much appreciated... :-) TIA, - Dave W. From roman.yakovenko at gmail.com Sun Jan 6 08:22:00 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 6 Jan 2008 09:22:00 +0200 Subject: [C++-sig] Getting operator[]() to throw RangeError using py++ In-Reply-To: <47802DAB.4080700@gforcetech.com> References: <47802DAB.4080700@gforcetech.com> Message-ID: <7465b6170801052322o495e1e0exc48be8555869814f@mail.gmail.com> On Jan 6, 2008 3:23 AM, David Wolfe wrote: > Using py++, can anyone help me figure out how to get operator[]() to > throw a RangeError for some vector classes I'm trying to wrap? The > default behavior is a bit undesirable: > ... > How can I firewall off 'garbage' index values from operator[]()? > > Any advice much appreciated... :-) Do you insist on using C++ to solve this problem? If not than: http://boost.org/libs/python/doc/tutorial/doc/html/python/techniques.html#python.extending_wrapped_objects_in_python else: http://language-binding.net/pyplusplus/documentation/how_to.html#how-to-expose-function-which-has-hand-written-wrapper HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From roman.yakovenko at gmail.com Sun Jan 6 08:38:44 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 6 Jan 2008 09:38:44 +0200 Subject: [C++-sig] Help with custom converters In-Reply-To: <477ED2E8.6090305@fusion.net.nz> References: <477ED2E8.6090305@fusion.net.nz> Message-ID: <7465b6170801052338k5cb83dfaoce9306b296a231d3@mail.gmail.com> I think that you need to use boost::python::ptr if you want to construct object from pointer. I also found it useful to use "call policies" to construct the object: namespace bpl = boost::python; template< typename CallPolicies, class T > bpl::object make_object( T x ){ //constructs object using CallPolicies result_converter typedef BOOST_DEDUCED_TYPENAME CallPolicies::result_converter:: template apply< T >::type result_converter_t; result_converter_t rc; return bpl::object( bpl::handle<>( rc( x ) ) ); } I am not sure whether it will help you or not. On Jan 5, 2008 2:44 AM, Lloyd Weehuizen wrote: > Hi > > I've done more research into the problem and it appears that when > passing an Interface* object into a bp::object's constructor, boost > looks up a converter for type Interface, not Interface* (which the > converter is registered against). > > Registering a converter for an abstract interface seems impossible at > the moment due to the compile time checks in as_to_python_function which > cause the compiler to attempt to instance the class. > > template > static void convert_function_must_take_value_or_const_reference(U(*)(T), > int, T* = 0) {} > template > > static void convert_function_must_take_value_or_const_reference(U(*)(T > const&), long ...) {} > > Commenting out the above checks and registering the converter directly > makes everything work correctly. > > Is there a cleaner solution to my abstract interface/converter problem? > > Thanks, > Lloyd > > Lloyd Weehuizen wrote: > > Hi > > > > I'm having a strange problem when using custom converters, they seem > > to work fine when I have a function/method that returns an object > type > but fails if I try use the object as a boost::python::object. > > > > The error I get is: > > > > TypeError: No to_python (by-value) converter found for C++ type: class > > Interface > > > > For example: > > > > Interface* TestConverter(Interface* interface) > > { > > return interface; > > } > > > > Works correctly, the python class instance that inherits from > > Interface passes correctly through the TestConverter method when > > called from python. > > > > However, if I try this: > > Interface* TestConverter(Interface* interface) > > { > > boost::python::object obj(interface); > > return interface; > > } > > > > Then the TypeError is thrown. I would have thought the converter would > > be called for this case as well? > > > > Here's the converter code: > > > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From dwolfe at gforcetech.com Sun Jan 6 09:45:42 2008 From: dwolfe at gforcetech.com (David Wolfe) Date: Sun, 06 Jan 2008 03:45:42 -0500 Subject: [C++-sig] Getting operator[]() to throw RangeError using py++ In-Reply-To: <7465b6170801052322o495e1e0exc48be8555869814f@mail.gmail.com> References: <47802DAB.4080700@gforcetech.com> <7465b6170801052322o495e1e0exc48be8555869814f@mail.gmail.com> Message-ID: <47809536.3060206@gforcetech.com> Thanks, Roman, for those references. I'll think about doing this in python instead, since it'll be a lot easier. I did figure out that what I really needed to do if I want to use C++ is: - Exclude operator[](), since it wasn't really doing the 'pythonic' thing - Create helper functions to get the vector length, get an item by index, and set an item by index - Expose these functions to python as __len__, __setitem__ and __getitem__. Correction to my previous post: the C++ helper functions mapped to __setitem__ and __getitem__ need to throw std::out_of_range(), which gets automatically translated by Boost.Python to IndexError (not RangeError). So this works now, FWIW: >>> for p in osg.Vec3f(1,2,3): ... print p ... 1.0 2.0 3.0 >>> v[2] 3.0 >>> v[3] Traceback (most recent call last): File "", line 1, in IndexError: vector index out of range (Many thanks to Tim Shead: http://tinyurl.com/2kgzjo ...) From lloyd at fusion.net.nz Sun Jan 6 12:04:03 2008 From: lloyd at fusion.net.nz (Lloyd Weehuizen) Date: Mon, 07 Jan 2008 00:04:03 +1300 Subject: [C++-sig] Help with custom converters In-Reply-To: <7465b6170801052338k5cb83dfaoce9306b296a231d3@mail.gmail.com> References: <477ED2E8.6090305@fusion.net.nz> <7465b6170801052338k5cb83dfaoce9306b296a231d3@mail.gmail.com> Message-ID: <4780B5A3.2080702@fusion.net.nz> Hey Roman Thanks for the tip, the result converter did the trick. Bit of a shame, it can't be done directly, but I'll happily use your method rather than hacking up my boost headers :) Cheers, Lloyd Roman Yakovenko wrote: > I think that you need to use boost::python::ptr if you want to > construct object from pointer. > > I also found it useful to use "call policies" to construct the object: > > namespace bpl = boost::python; > > template< typename CallPolicies, class T > > bpl::object make_object( T x ){ > //constructs object using CallPolicies result_converter > typedef BOOST_DEDUCED_TYPENAME CallPolicies::result_converter:: > template apply< T >::type result_converter_t; > result_converter_t rc; > return bpl::object( bpl::handle<>( rc( x ) ) ); > } > > I am not sure whether it will help you or not. > > On Jan 5, 2008 2:44 AM, Lloyd Weehuizen wrote: >> Hi >> >> I've done more research into the problem and it appears that when >> passing an Interface* object into a bp::object's constructor, boost >> looks up a converter for type Interface, not Interface* (which the >> converter is registered against). >> >> Registering a converter for an abstract interface seems impossible at >> the moment due to the compile time checks in as_to_python_function which >> cause the compiler to attempt to instance the class. >> >> template >> static void convert_function_must_take_value_or_const_reference(U(*)(T), >> int, T* = 0) {} >> template >> >> static void convert_function_must_take_value_or_const_reference(U(*)(T >> const&), long ...) {} >> >> Commenting out the above checks and registering the converter directly >> makes everything work correctly. >> >> Is there a cleaner solution to my abstract interface/converter problem? >> >> Thanks, >> Lloyd >> >> Lloyd Weehuizen wrote: >> > Hi >> > >> > I'm having a strange problem when using custom converters, they seem >> > to work fine when I have a function/method that returns an object >> type > but fails if I try use the object as a boost::python::object. >> > >> > The error I get is: >> > >> > TypeError: No to_python (by-value) converter found for C++ type: class >> > Interface >> > >> > For example: >> > >> > Interface* TestConverter(Interface* interface) >> > { >> > return interface; >> > } >> > >> > Works correctly, the python class instance that inherits from >> > Interface passes correctly through the TestConverter method when >> > called from python. >> > >> > However, if I try this: >> > Interface* TestConverter(Interface* interface) >> > { >> > boost::python::object obj(interface); >> > return interface; >> > } >> > >> > Then the TypeError is thrown. I would have thought the converter would >> > be called for this case as well? >> > >> > Here's the converter code: >> > From roman.yakovenko at gmail.com Sun Jan 6 12:21:02 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 6 Jan 2008 13:21:02 +0200 Subject: [C++-sig] Help with custom converters In-Reply-To: <4780B5A3.2080702@fusion.net.nz> References: <477ED2E8.6090305@fusion.net.nz> <7465b6170801052338k5cb83dfaoce9306b296a231d3@mail.gmail.com> <4780B5A3.2080702@fusion.net.nz> Message-ID: <7465b6170801060321t75124e43w691655180a4daa0f@mail.gmail.com> On Jan 6, 2008 1:04 PM, Lloyd Weehuizen wrote: > Hey Roman > > Thanks for the tip, the result converter did the trick. Bit of a shame, > it can't be done directly, but I'll happily use your method rather than > hacking up my boost headers :) Cool. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From sipickles at hotmail.com Mon Jan 7 09:07:45 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Mon, 07 Jan 2008 08:07:45 +0000 Subject: [C++-sig] Is boost::python good for scripting? Message-ID: Hi, I have a c++ game engine. I also use a lot of python, mainly for my own tools and small apps. I'm hoping I can use Python as a scripting language in my c++ engine (which is already well developed). Looking at boost::python, its main emphasis seems to be on exposing c++ to python. However, I am looking to do the reverse. Ideally, I'd like to be able to write small modules in Python, effectively functions loaded and run at call-time by the c++ engine, to which I could pass c++ data structures for processing. Could anyone give me a pointer to getting started using python in c++? So kind! Simon -- Linux Counter: User# 424693 From roman.yakovenko at gmail.com Mon Jan 7 09:14:47 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 7 Jan 2008 10:14:47 +0200 Subject: [C++-sig] Is boost::python good for scripting? In-Reply-To: References: Message-ID: <7465b6170801070014he5e1283o2613ec20c31636cd@mail.gmail.com> On Jan 7, 2008 10:07 AM, Simon Pickles wrote: > Hi, > > I have a c++ game engine. I also use a lot of python, mainly for my own > tools and small apps. > > I'm hoping I can use Python as a scripting language in my c++ engine > (which is already well developed). > > Looking at boost::python, its main emphasis seems to be on exposing c++ > to python. However, I am looking to do the reverse. > > Ideally, I'd like to be able to write small modules in Python, > effectively functions loaded and run at call-time by the c++ engine, to > which I could pass c++ data structures for processing. > > Could anyone give me a pointer to getting started using python in c++? > > So kind! Example of a product that uses such strategy: http://www.eve-online.com/faq/faq_07.asp Start here http://boost.org/libs/python/doc/tutorial/doc/html/python/embedding.html and continue with Google :-) -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From matthieu.brucher at gmail.com Mon Jan 7 09:18:05 2008 From: matthieu.brucher at gmail.com (Matthieu Brucher) Date: Mon, 7 Jan 2008 09:18:05 +0100 Subject: [C++-sig] Is boost::python good for scripting? In-Reply-To: References: Message-ID: Hi, It works quite good as Civilization 4 uses Boost.Python ;) Matthieu 2008/1/7, Simon Pickles : > > Hi, > > I have a c++ game engine. I also use a lot of python, mainly for my own > tools and small apps. > > I'm hoping I can use Python as a scripting language in my c++ engine > (which is already well developed). > > Looking at boost::python, its main emphasis seems to be on exposing c++ > to python. However, I am looking to do the reverse. > > Ideally, I'd like to be able to write small modules in Python, > effectively functions loaded and run at call-time by the c++ engine, to > which I could pass c++ data structures for processing. > > Could anyone give me a pointer to getting started using python in c++? > > So kind! > > > Simon > > -- > Linux Counter: User# 424693 > > > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -- French PhD student Website : http://matthieu-brucher.developpez.com/ Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn : http://www.linkedin.com/in/matthieubrucher -------------- next part -------------- An HTML attachment was scrubbed... URL: From sipickles at hotmail.com Mon Jan 7 14:29:40 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Mon, 07 Jan 2008 13:29:40 +0000 Subject: [C++-sig] Building boost::python on Ubuntu Message-ID: Hi, I obviously have not got my system, Ubuntu gutsy, set up yet, since I can't seem to build the boost::python libs. I am quite new to Linux after escaping from Windows When trying to build the libs, I am using: bjam --with-python stage in the boost root directory. It correctly finds my gcc compiler, but then spits out 1000's of errors. Those which stand out are: In file included from ./boost/python/detail/prefix.hpp:13, from ./boost/python/ssize_t.hpp:9, from ./boost/python/object.hpp:8, from ./boost/python/exec.hpp:8, from libs/python/src/exec.cpp:6: ./boost/python/detail/wrap_python.hpp:75:24: error: patchlevel.h: No such file or directory ./boost/python/detail/wrap_python.hpp:78:2: error: #error Python 2.2 or higher is required for this version of Boost.Python. ./boost/python/detail/wrap_python.hpp:142:21: error: Python.h: No such file or directory ./boost/python/detail/wrap_python.hpp:190:4: error: #error "HAVE_LONG_LONG defined but not PY_LONG_LONG or LONG_LONG" This is odd since I have Python 2.5 installed: simon at simon-linux:~$ python Python 2.5.1 (r251:54863, Oct 5 2007, 13:36:32) [GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> A problem with my path perhaps? As an alternative, I tried using synaptic to install the library from the repositories, but am nor sure hwo to link to that... where is it installed to ? Your help much appreciated Thanks Simon -- Linux Counter: User# 424693 From seefeld at sympatico.ca Mon Jan 7 14:26:47 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 07 Jan 2008 08:26:47 -0500 Subject: [C++-sig] Is boost::python good for scripting? In-Reply-To: References: Message-ID: <47822897.9090808@sympatico.ca> Simon Pickles wrote: > Hi, > > I have a c++ game engine. I also use a lot of python, mainly for my own > tools and small apps. > > I'm hoping I can use Python as a scripting language in my c++ engine > (which is already well developed). > > Looking at boost::python, its main emphasis seems to be on exposing c++ > to python. However, I am looking to do the reverse. > > Ideally, I'd like to be able to write small modules in Python, > effectively functions loaded and run at call-time by the c++ engine, to > which I could pass c++ data structures for processing. > > Could anyone give me a pointer to getting started using python in c++? http://boost.org/libs/python/doc/tutorial/doc/html/python/embedding.html You might also want to look into the test/exec.cpp and test/import_.cpp files for inspiration that are part of the boost source package. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From fullung at gmail.com Mon Jan 7 14:34:57 2008 From: fullung at gmail.com (Albert Strasheim) Date: Mon, 7 Jan 2008 15:34:57 +0200 Subject: [C++-sig] Building boost::python on Ubuntu In-Reply-To: References: Message-ID: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> Hello You need to install the Python headers. The package is calling something like python-devel or python-dev (or maybe libpython-dev), depending on your Linux distribution. You might also consider installing the Boost packages, if the version you want is available in your distribution. Cheers, Albert On Jan 7, 2008 3:29 PM, Simon Pickles wrote: > Hi, > > I obviously have not got my system, Ubuntu gutsy, set up yet, since I > can't seem to build the boost::python libs. I am quite new to Linux > after escaping from Windows From sipickles at hotmail.com Mon Jan 7 15:00:27 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Mon, 07 Jan 2008 14:00:27 +0000 Subject: [C++-sig] Building boost::python on Ubuntu In-Reply-To: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> References: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> Message-ID: Thank you for your patience, thats got it working. Sorry for the noob question - linux is a bit of a mental leap after 15 years of being in bed with Mr Gates ;) Albert Strasheim wrote: > Hello > > You need to install the Python headers. The package is calling > something like python-devel or python-dev (or maybe libpython-dev), > depending on your Linux distribution. > > You might also consider installing the Boost packages, if the version > you want is available in your distribution. > > Cheers, > > Albert > > On Jan 7, 2008 3:29 PM, Simon Pickles wrote: > >> Hi, >> >> I obviously have not got my system, Ubuntu gutsy, set up yet, since I >> can't seem to build the boost::python libs. I am quite new to Linux >> after escaping from Windows >> > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > -- Linux Counter: User# 424693 From nomicon at pacbell.net Mon Jan 7 14:57:01 2008 From: nomicon at pacbell.net (nomicon) Date: Mon, 07 Jan 2008 05:57:01 -0800 Subject: [C++-sig] Is boost::python good for scripting? In-Reply-To: References: Message-ID: <47822FAD.3000506@pacbell.net> Hi Simon, I did something similar and it works well. Below is an example that will hopefully get you started. I'm copying pieces from a larger piece of working code. Forgive me if there are minor mistakes and/or omissions. I'm just trying to give you the general idea of what I did. Its not exactly what you asked for but its an approach that you might consider. I created an abstract base class in a header file to serve as an interface like this: #include #include #include using namespace std; using namespace boost::python; class ModelIF : public boost::noncopyable { public: virtual bool AdvanceTime( const std::string& last_time, const std::string& now_time, const int& adv_seconds ) = 0; // pure virtual }; Then created a struct in the same file like this: struct ModelIFWrap : ModelIF, boost::python::wrapper { virtual bool AdvanceTime( const std::string& last_time, const std::string& now_time, const int& adv_seconds ) { #if BOOST_WORKAROUND(BOOST_MSVC, <= 1300) // workaround for VC++ 6.x or 7.0, see return boost::python::call(this->get_override("AdvanceTime").ptr(seconds)); #else return this->get_override("AdvanceTime")(last_time, now_time, adv_seconds); #endif } }; In a source file, I exposed the interface to python like this: BOOST_PYTHON_MODULE(model_if) { class_("ModelIF") .def("AdvanceTime", boost::python::pure_virtual(&ModelIF::AdvanceTime)) ; } The python code will looked something like this (btw - do not use __init__ it crashes the code): from model_if import * class LoadDemo(ModelIF): def AdvanceTime(self, last_time, now_time, adv_seconds): self.lastTimeStr = last_time self.nowTimeStr = now_time self.thisStepSec = adv_seconds return 1 To execute I wrote a C++ class and in the source file initialized the python interpreter like this: // Add the model interface definition to the Python interpreter if (PyImport_AppendInittab("model_if", initmodel_if) == -1) throw std::runtime_error("Failed to add model_if to the interpreter's " "builtin modules"); // Initialize the python interpreter Py_Initialize(); // Retrieve the main module pyMainModule = bpy::import("__main__"); // Retrieve the main module's namespace pyMainSpace = pyMainModule.attr("__dict__"); Then in a source file you can create a python object is C++ and use it like any other object: bpy::object pyClass = pyMainSpace["LoadDemo"]; // Create an python instance of this class if (bpy::object pyObj = pyClass()) { // Get the pointer to this instance ModelIF *pyPtr = boost::python::extract(pyObj ) BOOST_EXTRACT_WORKAROUND; pyPtr->AdvanceTime( oldSimTimeStr, newSimTimeStr, stepDuration.total_seconds() ); } Hope this helps. Please forgive any omissions and syntax errors. good luck, Mark Kordon Simon Pickles wrote: >Hi, > >I have a c++ game engine. I also use a lot of python, mainly for my own >tools and small apps. > >I'm hoping I can use Python as a scripting language in my c++ engine >(which is already well developed). > >Looking at boost::python, its main emphasis seems to be on exposing c++ >to python. However, I am looking to do the reverse. > >Ideally, I'd like to be able to write small modules in Python, >effectively functions loaded and run at call-time by the c++ engine, to >which I could pass c++ data structures for processing. > >Could anyone give me a pointer to getting started using python in c++? > >So kind! > > >Simon > > > From sipickles at hotmail.com Mon Jan 7 17:22:38 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Mon, 07 Jan 2008 16:22:38 +0000 Subject: [C++-sig] Building boost::python on Ubuntu In-Reply-To: References: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> Message-ID: So after adding the built libraries, I am stuck with this: > -------------- Build: Debug in embeddedTest --------------- > > Linking console executable: bin/Debug/embeddedTest > obj/Debug/main.o: In function `_object* > boost::python::incref<_object>(_object*)': > /usr/local/boost_1_34_1/boost/python/refcount.hpp:16: undefined > reference to `_Py_RefTotal' > /usr/local/boost_1_34_1/boost/python/refcount.hpp:16: undefined > reference to `_Py_RefTotal' > obj/Debug/main.o: In function `~object_base': > /usr/local/boost_1_34_1/boost/python/object_core.hpp:436: undefined > reference to `_Py_RefTotal' > /usr/local/boost_1_34_1/boost/python/object_core.hpp:436: undefined > reference to `_Py_RefTotal' > /usr/local/boost_1_34_1/boost/python/object_core.hpp:436: undefined > reference to `_Py_NegativeRefcount' > /usr/local/boost_1_34_1/boost/python/object_core.hpp:436: undefined > reference to `_Py_Dealloc' > collect2: ld returned 1 exit status > Process terminated with status 1 (0 minutes, 0 seconds) > 6 errors, 0 warnings Nearly there! Simon Pickles wrote: > Thank you for your patience, thats got it working. > > Sorry for the noob question - linux is a bit of a mental leap after 15 > years of being in bed with Mr Gates ;) > > Albert Strasheim wrote: > >> Hello >> >> You need to install the Python headers. The package is calling >> something like python-devel or python-dev (or maybe libpython-dev), >> depending on your Linux distribution. >> >> You might also consider installing the Boost packages, if the version >> you want is available in your distribution. >> >> Cheers, >> >> Albert >> >> On Jan 7, 2008 3:29 PM, Simon Pickles wrote: >> >> >>> Hi, >>> >>> I obviously have not got my system, Ubuntu gutsy, set up yet, since I >>> can't seem to build the boost::python libs. I am quite new to Linux >>> after escaping from Windows >>> >>> >> _______________________________________________ >> C++-sig mailing list >> C++-sig at python.org >> http://mail.python.org/mailman/listinfo/c++-sig >> >> >> >> > > -- Linux Counter: User# 424693 From seefeld at sympatico.ca Mon Jan 7 17:30:30 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 07 Jan 2008 11:30:30 -0500 Subject: [C++-sig] Building boost::python on Ubuntu In-Reply-To: References: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> Message-ID: <478253A6.3020503@sympatico.ca> Simon Pickles wrote: > So after adding the built libraries, I am stuck with this: >> -------------- Build: Debug in embeddedTest --------------- >> >> Linking console executable: bin/Debug/embeddedTest >> obj/Debug/main.o: In function `_object* >> boost::python::incref<_object>(_object*)': >> /usr/local/boost_1_34_1/boost/python/refcount.hpp:16: undefined >> reference to `_Py_RefTotal' I'm not sure what 'embeddedTest' is and how you attempt to link it, but from the error it is clear that you are not linking in the python interpreter itself, i.e. you are missing -lpython in your linker command line. (Note that you don't need this when compiling extension modules, as the symbols are then provided by the python executable. In this case, however, you are embedding the python interpreter itself into your app...) HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From sipickles at hotmail.com Mon Jan 7 17:39:22 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Mon, 07 Jan 2008 16:39:22 +0000 Subject: [C++-sig] Building boost::python on Ubuntu In-Reply-To: <478253A6.3020503@sympatico.ca> References: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> <478253A6.3020503@sympatico.ca> Message-ID: I thought so, and already have -lpython2.5 in my linker command line, along with -lpthread. But still this problem :( Stefan Seefeld wrote: > Simon Pickles wrote: > >> So after adding the built libraries, I am stuck with this: >> >>> -------------- Build: Debug in embeddedTest --------------- >>> >>> Linking console executable: bin/Debug/embeddedTest >>> obj/Debug/main.o: In function `_object* >>> boost::python::incref<_object>(_object*)': >>> /usr/local/boost_1_34_1/boost/python/refcount.hpp:16: undefined >>> reference to `_Py_RefTotal' >>> > > I'm not sure what 'embeddedTest' is and how you attempt to link it, but > from the error it is clear that you are not linking in the python > interpreter itself, i.e. you are missing -lpython in your > linker command line. > > (Note that you don't need this when compiling extension modules, as the > symbols are then provided by the python executable. In this case, > however, you are embedding the python interpreter itself into your app...) > > > HTH, > Stefan > > -- Linux Counter: User# 424693 From seefeld at sympatico.ca Mon Jan 7 18:11:43 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 07 Jan 2008 12:11:43 -0500 Subject: [C++-sig] Building boost::python on Ubuntu In-Reply-To: References: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> <478253A6.3020503@sympatico.ca> Message-ID: <47825D4F.4070400@sympatico.ca> Simon Pickles wrote: > I thought so, and already have -lpython2.5 in my linker command line, > along with -lpthread. > > But still this problem :( Sorry, nobody will be able to help you without more information. Please include at least the full command you used for the link. Ideally a complete and minimal test case so others can (attempt to) reproduce the failure. Thanks, Stefan -- ...ich hab' noch einen Koffer in Berlin... From dan at thetrowbridgefamily.com Tue Jan 8 03:10:13 2008 From: dan at thetrowbridgefamily.com (Dan Trowbridge) Date: Mon, 7 Jan 2008 21:10:13 -0500 Subject: [C++-sig] Memory Leak in MFC/C++ based Python Extended/Embedded code. Message-ID: Hi all, I have a MS Windows/MFC based C++ code that I have implemented Python embedding/extending in. (I am using Visual Studio 2003.) The code seems to do what I have intended except it always reports a memory leak when it exits. I tried all kinds of things but there did not seem to be anything I could do to get rid of it. So... I built a new MFC project and the only things I added was... #include at the top of the application class .cpp file and the following two lines Py_Initialize(); Py_Finalize(); in the application class constructor. The minimal code STILL reports a memory error. It looks like the memory leak had nothing to do with my code. Anybody have any ideas how to fix this? Background: I am using Python 2.5.1. I am not using BOOST::PYTHON (I am using BOOST::NUMERIC and BOOST::SHARED_PTR for other things in the code - maybe I should use BOOST::PYTHON - maybe their shared_ptr's would take care of this for me? ). I also am not using SWIG or any other "helpers". Any help would be greatly appreciated. If this is not the right forum please advise where to post this. It got to figure that someone has seen this before - considering as long as Python has been around and as big as the developer community is. Thanks in advance for your collective help. Dannyt From seefeld at sympatico.ca Tue Jan 8 03:13:19 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 07 Jan 2008 21:13:19 -0500 Subject: [C++-sig] Memory Leak in MFC/C++ based Python Extended/Embedded code. In-Reply-To: References: Message-ID: <4782DC3F.7070201@sympatico.ca> Dan Trowbridge wrote: > The minimal code STILL reports a memory error. It looks like the memory > leak had nothing to do with my code. > > Anybody have any ideas how to fix this? It isn't clear that there is actually anything to fix. Reporting memory errors isn't easy, and most tools I have used in the past are erroneous. For example, I once used a memory leak checker with MSVC that generated its table even before global objects of my application had been destroyed. It obviously generated wrong results, and thus was of little use. I'm not sure whether you can assist your leak detector by forcing a gc cleanup in python. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From sipickles at hotmail.com Tue Jan 8 08:58:25 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Tue, 08 Jan 2008 07:58:25 +0000 Subject: [C++-sig] Building boost::python on Ubuntu - now linking problem In-Reply-To: <47825D4F.4070400@sympatico.ca> References: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> <478253A6.3020503@sympatico.ca> <47825D4F.4070400@sympatico.ca> Message-ID: Yes, sorry, good point! Strangely, boost::python was very simple to set up on my home ubuntu box last night. At work (identical os), I still have the problem as soon as I include . Its like I have not linked to the python API, but from my command line below, you can see I have. Heres is my extensive program: #include int main() { return 0; } Here is my output, embeddedTest is simply the name of my project : *-------------- Build: Debug in embeddedTest ---------------* g++ -L/usr/local/boost_1_34_1/stage/lib -o bin/Debug/embeddedTest obj/Debug/main.o -lpthread -lpython2.5 obj/Debug/main.o: In function `_object* boost::python::incref<_object>(_object*)': /usr/local/boost_1_34_1/boost/python/refcount.hpp:16: undefined reference to `_Py_RefTotal' /usr/local/boost_1_34_1/boost/python/refcount.hpp:16: undefined reference to `_Py_RefTotal' obj/Debug/main.o: In function `~object_base': /usr/local/boost_1_34_1/boost/python/object_core.hpp:436: undefined reference to `_Py_RefTotal' /usr/local/boost_1_34_1/boost/python/object_core.hpp:436: undefined reference to `_Py_RefTotal' /usr/local/boost_1_34_1/boost/python/object_core.hpp:436: undefined reference to `_Py_NegativeRefcount' /usr/local/boost_1_34_1/boost/python/object_core.hpp:436: undefined reference to `_Py_Dealloc' collect2: ld returned 1 exit status Process terminated with status 1 (0 minutes, 1 seconds) 6 errors, 0 warnings Thanks for your advice. Simon Stefan Seefeld wrote: > Simon Pickles wrote: > >> I thought so, and already have -lpython2.5 in my linker command line, >> along with -lpthread. >> >> But still this problem :( >> > > Sorry, nobody will be able to help you without more information. Please > include at least the full command you used for the link. Ideally a > complete and minimal test case so others can (attempt to) reproduce the > failure. > > Thanks, > Stefan > > > -- Linux Counter: User# 424693 From meine at informatik.uni-hamburg.de Tue Jan 8 09:52:12 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Tue, 8 Jan 2008 09:52:12 +0100 Subject: [C++-sig] Building boost::python on Ubuntu - now linking problem In-Reply-To: References: <47825D4F.4070400@sympatico.ca> Message-ID: <200801080952.12237.meine@informatik.uni-hamburg.de> Am Dienstag, 08. Januar 2008 08:58:25 schrieb Simon Pickles: > g++ -L/usr/local/boost_1_34_1/stage/lib -o bin/Debug/embeddedTest > obj/Debug/main.o -lpthread -lpython2.5 > obj/Debug/main.o: In function `_object* > boost::python::incref<_object>(_object*)': > /usr/local/boost_1_34_1/boost/python/refcount.hpp:16: undefined > reference to `_Py_RefTotal' Maybe the wrong python lib is picked up - are there multiple python versions installed? What does "locate libpython" tell you? Ciao, / / /--/ / / ANS From sipickles at hotmail.com Tue Jan 8 10:15:59 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Tue, 08 Jan 2008 09:15:59 +0000 Subject: [C++-sig] Building boost::python on Ubuntu - now linking problem In-Reply-To: <200801080952.12237.meine@informatik.uni-hamburg.de> References: <47825D4F.4070400@sympatico.ca> <200801080952.12237.meine@informatik.uni-hamburg.de> Message-ID: Hans Meine wrote: > Am Dienstag, 08. Januar 2008 08:58:25 schrieb Simon Pickles: > >> g++ -L/usr/local/boost_1_34_1/stage/lib -o bin/Debug/embeddedTest >> obj/Debug/main.o -lpthread -lpython2.5 >> obj/Debug/main.o: In function `_object* >> boost::python::incref<_object>(_object*)': >> /usr/local/boost_1_34_1/boost/python/refcount.hpp:16: undefined >> reference to `_Py_RefTotal' >> > > Maybe the wrong python lib is picked up - are there multiple python versions > installed? What does "locate libpython" tell you? > > Ciao, / / > /--/ > / / ANS > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > Hello Hans Here is the output from locate libpython. /usr/lib/debug/usr/lib/libpython2.5.so.1.0 /usr/lib/libpython2.5.so /usr/lib/gnome-vfs-2.0/modules/libpythonmethod.so /usr/lib/gnome-vfs-2.0/modules/libpythonmethod-2.4.so /usr/lib/gnome-vfs-2.0/modules/libpythonmethod-2.5.so /usr/lib/libpython2.5.so.1.0 /usr/lib/python2.5/config/libpython2.5.so /usr/lib/python2.5/config/libpython2.5.a /usr/lib/python2.5/config/libpython2.5-pic.a /usr/lib/python2.5/config_d/libpython2.5.a /usr/lib/libpython2.5.so.1 /home/simon/.wine/drive_c/Python25/libs/libpython25.a Thanks -- Linux Counter: User# 424693 From seefeld at sympatico.ca Tue Jan 8 13:38:39 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Tue, 08 Jan 2008 07:38:39 -0500 Subject: [C++-sig] Building boost::python on Ubuntu - now linking problem In-Reply-To: References: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> <478253A6.3020503@sympatico.ca> <47825D4F.4070400@sympatico.ca> Message-ID: <47836ECF.6010803@sympatico.ca> Simon Pickles wrote: > Yes, sorry, good point! > > Strangely, boost::python was very simple to set up on my home ubuntu box > last night. > > At work (identical os), I still have the problem as soon as I include > . Its like I have not linked to the python API, but > from my command line below, you can see I have. > > Heres is my extensive program: > > #include > > int main() > { > return 0; > } > > > Here is my output, embeddedTest is simply the name of my project : > > *-------------- Build: Debug in embeddedTest ---------------* > g++ -L/usr/local/boost_1_34_1/stage/lib -o bin/Debug/embeddedTest > obj/Debug/main.o -lpthread -lpython2.5 What happens if you "#include " instead of above ? If you are using boost.python, you should also link with -lboost_python (careful to put prerequisite libs after depending libs in the command line !). May be that would resolve the symbols ? One possible reason for you seeing two different behaviors might be how boost (and boost.python, in particular) was compiled. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From sipickles at hotmail.com Tue Jan 8 14:22:19 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Tue, 08 Jan 2008 13:22:19 +0000 Subject: [C++-sig] Building boost::python on Ubuntu - now linking problem In-Reply-To: <47836ECF.6010803@sympatico.ca> References: <5eec5f300801070534m676d1855l5ef25af9d35dbcfc@mail.gmail.com> <478253A6.3020503@sympatico.ca> <47825D4F.4070400@sympatico.ca> <47836ECF.6010803@sympatico.ca> Message-ID: Stefan Seefeld wrote: > Simon Pickles wrote: > >> Yes, sorry, good point! >> >> Strangely, boost::python was very simple to set up on my home ubuntu box >> last night. >> >> At work (identical os), I still have the problem as soon as I include >> . Its like I have not linked to the python API, but >> from my command line below, you can see I have. >> >> Heres is my extensive program: >> >> #include >> >> int main() >> { >> return 0; >> } >> >> >> Here is my output, embeddedTest is simply the name of my project : >> >> *-------------- Build: Debug in embeddedTest ---------------* >> g++ -L/usr/local/boost_1_34_1/stage/lib -o bin/Debug/embeddedTest >> obj/Debug/main.o -lpthread -lpython2.5 >> > > What happens if you "#include " instead of > above ? If you are using boost.python, you should also link with > -lboost_python (careful to put prerequisite libs after depending libs in > the command line !). May be that would resolve the symbols ? > > One possible reason for you seeing two different behaviors might be how > boost (and boost.python, in particular) was compiled. > > Regards, > Stefan > > Well, trying #include fails, it is not found on my system. Strangely, Python.h is, however I think adding -lboost_python has fixed it! Thank you so much stefan! I hope I am not set to bug you again too soon! Simon -- Linux Counter: User# 424693 From s_sourceforge at nedprod.com Tue Jan 8 22:32:41 2008 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Tue, 08 Jan 2008 21:32:41 -0000 Subject: [C++-sig] Memory Leak in MFC/C++ based Python Extended/Embedded code. In-Reply-To: <4782DC3F.7070201@sympatico.ca> References: , <4782DC3F.7070201@sympatico.ca> Message-ID: <4783EBF9.10115.2FE2D9@s_sourceforge.nedprod.com> On 7 Jan 2008 at 21:13, Stefan Seefeld wrote: > > The minimal code STILL reports a memory error. It looks like the memory > > leak had nothing to do with my code. > > > > Anybody have any ideas how to fix this? > > It isn't clear that there is actually anything to fix. Reporting memory > errors isn't easy, and most tools I have used in the past are erroneous. > For example, I once used a memory leak checker with MSVC that generated > its table even before global objects of my application had been > destroyed. It obviously generated wrong results, and thus was of little use. > > I'm not sure whether you can assist your leak detector by forcing a gc > cleanup in python. Glowcode is THE leak detection program for Windows. Worth every penny. And no I have no commercial association with them, the tool is simply that good. Cheers, Niall From jlisee at gmail.com Fri Jan 11 19:28:28 2008 From: jlisee at gmail.com (Joseph Lisee) Date: Fri, 11 Jan 2008 18:28:28 +0000 (UTC) Subject: [C++-sig] Cross Module Inheritance and Downcast Message-ID: Hello All, I am having trouble getting cross module inheritance/downcasting to work. All of the following types are wrapped up in shared_ptrs. Here is the code layout: Shared Library A: Has the Base class. It implements virtual functions of the Base class. It also has a factory which produces objects referred to by a Base class shared_ptr. This includes the ability to produce Derived class objects referred to by the Base class shared_ptr. It *does not* link to "Shared Library B", or know anything of the derived types. Shared Library B: Has the Derived class and links to "Shared Library A". It implements virtual functions of the Derived class. Extension Module A: This is linked to "Shared Library A". The base class is wrapped like so: class BaseWrapper: public Base, public bp::wrapper< Base > { ... more code ... } void registerBaseClass() { bp::class_, boost::noncopyable >( ... more code ...). ... more code ... } Extension Module B: This is linked to both "Shared Library A", and "Shared Library B". It wraps the derived class like so: struct Derived_wrapper : Derived, bp::wrapper< Derived > { ... more code ... } void register_Derived_class(){ bp::class_< Derived_wrapper, bp::bases< Base >, boost::noncopyable >( ... more code ...). ... more code ... } This issue is that when I call the factory in "Extension Module A" from python it only gives me the Base class objects. It does not automatically downcast. I do have both the Base and Derived class extension modules imported, and I imported the Base class one first. I get this same behavior on Windows, Linux and Mac. So to fix this I tried to create a manual cast function: boost::shared_ptr castTo(boost::shared_ptr from) { return boost::dynamic_pointer_cast(from); } Now the cast function works on Linux just fine, but on Windows and Mac it just returns the same python object I give it. Can anyone point me to how I should be doing this? Has anyone got a system like this to work? -Joseph Lisee From roman.yakovenko at gmail.com Fri Jan 11 20:19:01 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 11 Jan 2008 21:19:01 +0200 Subject: [C++-sig] Cross Module Inheritance and Downcast In-Reply-To: References: Message-ID: <7465b6170801111119t714ca6d9s6818a2861cdd3003@mail.gmail.com> On Jan 11, 2008 8:28 PM, Joseph Lisee wrote: > Hello All, > > I am having trouble getting cross module inheritance/downcasting to work. All > of the following types are wrapped up in shared_ptrs. Here is the code layout: > > Shared Library A: Has the Base class. It implements virtual functions of the > Base class. It also has a factory which produces objects referred to by a Base > class shared_ptr. This includes the ability to produce Derived class objects > referred to by the Base class shared_ptr. It *does not* link to "Shared Library > B", or know anything of the derived types. > > Shared Library B: Has the Derived class and links to "Shared Library A". It > implements virtual functions of the Derived class. > > Extension Module A: This is linked to "Shared Library A". The base class is > wrapped like so: > > class BaseWrapper: public Base, public bp::wrapper< Base > { > ... more code ... > } > > void registerBaseClass() { > bp::class_ bp::bases< ... more code ...>, > boost::noncopyable >( ... more code ...). > ... more code ... > } > > > > Extension Module B: This is linked to both "Shared Library A", and "Shared > Library B". It wraps the derived class like so: > > struct Derived_wrapper : Derived, bp::wrapper< Derived > { > ... more code ... > } > > void register_Derived_class(){ > bp::class_< Derived_wrapper, > bp::bases< Base >, > boost::noncopyable >( ... more code ...). > ... more code ... > } > > > This issue is that when I call the factory in "Extension Module A" from python > it only gives me the Base class objects. It does not automatically downcast. I > do have both the Base and Derived class extension modules imported, and I > imported the Base class one first. I get this same behavior on Windows, Linux > and Mac. > > So to fix this I tried to create a manual cast function: > boost::shared_ptr castTo(boost::shared_ptr from) { > return boost::dynamic_pointer_cast(from); > } > > Now the cast function works on Linux just fine, but on Windows and Mac it just > returns the same python object I give it. > > Can anyone point me to how I should be doing this? Has anyone got a system like > this to work? I could be wrong, but there is a known problem with the library. You can search this mailing list and find the patch. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From jlisee at gmail.com Fri Jan 11 22:40:53 2008 From: jlisee at gmail.com (Joseph Lisee) Date: Fri, 11 Jan 2008 21:40:53 +0000 (UTC) Subject: [C++-sig] Cross Module Inheritance and Downcast References: <7465b6170801111119t714ca6d9s6818a2861cdd3003@mail.gmail.com> Message-ID: Roman Yakovenko gmail.com> writes: > I could be wrong, but there is a known problem with the library. You > can search this mailing list and find the patch. This is where I need the help. I found people who had similar cross module issues and David A. said that they need to make sure they were linking with the right libraries, and had RTTI enabled. There was also talk of cross module support being added to boost.python, but no talk of what exactly this support did and didn't do. So if anyone can point me in the direction of some documentation which says where exactly cross module support in Boost.Python sits, that would be helpful. I am also curious as to why that castTo function doesn't work. I think its because Boost.Python tries to be too smart and realizes that it already has a corresponding python object for C++ object inside the shared_ptr and just returns it instead. It is odd that this only happens on Windows, and OS X though. -Joseph Lisee From sipickles at hotmail.com Sat Jan 12 12:41:08 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Sat, 12 Jan 2008 11:41:08 +0000 Subject: [C++-sig] Trouble linking on Ubuntu Linux Message-ID: Hi, I can't quite get linkage to work with c++ to stackless python, it worked with Cpython. I can use stackless in the python interpreter okay too. I thought I had rebuilt the boost.python library with stackless included, but it seems not. I used: simon at simon-desktop:/usr/local/boost_1_34_1$ sudo bjam --with-python stage This is building using the python code in /usr/local/include/python2.5 (if I move this directory, the above bjam command fails). bjam runs sucessfully and copies the libs to /usr/local/boost_1_34_1/stage/libs. However, when I try and integrate a stackless python type like 'PyChannel_new' in c++, it says undefined reference. Note, these commands do not cause an error so I guess I am linking to the old library? Py_SetProgramName(argv[0]); Py_InitializeEx(0); /usr/local/boost_1_34_1/stage/libs is in my linker search path, and I have linked with option -lboost_python. Have I missed something? Thanks Simon -- Linux user #458601 - http://counter.li.org. From sipickles at hotmail.com Sat Jan 12 14:49:55 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Sat, 12 Jan 2008 13:49:55 +0000 Subject: [C++-sig] [Stackless] Trouble linking on Ubuntu Linux In-Reply-To: References: Message-ID: Well, I made a little progress. I used grep to find PyChannel_new in: /home/simon/Source/Stackless/release25-maint/libpython2.5.a (the new version including stackless?) and manually copied it over: /usr/local/lib/python2.5/config/libpython2.5.a (the old version without stackless?) Then the program compiles and link! Woot I thought! However I get a seg fault when running it. The debugger in code::blocks IDE with g++ points to a problem quite deep in Py_InitializeEx(). I think my manual copy of the library is bad. How come make install didn't do that? I guess I've got two copies of python (at least in parts) on my machine: /usr/local/include/python2.5 /usr/include/python2.5 Is this causing some problem? My linux experience is still limited, so not so hot on controlling installation locations etc :) Thanks for any advice Simon Linux user #458601 - http://counter.li.org. Simon Pickles wrote: > Hi, > > I can't quite get linkage to work with c++ to stackless python, it > worked with Cpython. I can use stackless in the python interpreter okay too. > > I thought I had rebuilt the boost.python library with stackless > included, but it seems not. > > I used: > > simon at simon-desktop:/usr/local/boost_1_34_1$ sudo bjam --with-python stage > > This is building using the python code in /usr/local/include/python2.5 > (if I move this directory, the above bjam command fails). > > bjam runs sucessfully and copies the libs to > /usr/local/boost_1_34_1/stage/libs. > > However, when I try and integrate a stackless python type like > 'PyChannel_new' in c++, it says undefined reference. Note, these > commands do not cause an error so I guess I am linking to the old library? > > Py_SetProgramName(argv[0]); > Py_InitializeEx(0); > > /usr/local/boost_1_34_1/stage/libs is in my linker search path, and I > have linked with option -lboost_python. Have I missed something? > > Thanks > > Simon > > > From farcaller at gmail.com Sat Jan 12 20:29:59 2008 From: farcaller at gmail.com (Vladimir Pouzanov) Date: Sat, 12 Jan 2008 21:29:59 +0200 Subject: [C++-sig] static linking of python&boost::python under windows Message-ID: <200801122130.03425.farcaller@gmail.com> Can someone give me a hint what boost library type do I have to use to link both boost::python and python to my app statically? In fact it seems that there's no python.dll in windows python distribution anyways. I'm getting either some linkage problems about basic_string's or just a sigsegv (uhm.. access violation) on a code: // bootstrapping PyImport_ExtendInittab(Module_inittab); // starting python... Py_Initialize(); // <-- here I can't make a debug build without python_d.lib and I don't feel like compiling it myself. I've just commented out PyImport_ExtendInittab and figured that it "works" (it doesn't work as expected, it just doesn't crash). -- Sincerely, Vladimir "Farcaller" Pouzanov Hack&Dev Team PGP/GPG: 0x3A40FF29 Fingerprint: FA36 877A 2DC3 B56F CAB5 7DB3 4C97 A596 3A40 FF29 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 481 bytes Desc: This is a digitally signed message part. URL: From jlisee at gmail.com Sun Jan 13 03:00:48 2008 From: jlisee at gmail.com (Joseph Lisee) Date: Sun, 13 Jan 2008 02:00:48 +0000 (UTC) Subject: [C++-sig] [Stackless] Trouble linking on Ubuntu Linux References: Message-ID: I forget the exact settings, but boost.python most likely picks up your default python installation. This will be at the one in /usr/lib. If you wish to build Boost.Python with Stackless, then you will need to point it at your stackless python installation. I think you want to use the following options: -sPYTHON_ROOT= -sPYTHON_VERSION=2.5 I have no idea where you have stackless python installed though. Also you should almost never manually copy over libraries, especially if you don't really know what you are doing. -Joseph Lisee From they4kman at gmail.com Mon Jan 14 02:03:57 2008 From: they4kman at gmail.com (theY4Kman) Date: Sun, 13 Jan 2008 20:03:57 -0500 Subject: [C++-sig] Python Packages from embedded interpreter Message-ID: <5148624c0801131703t5c32a2a7u417b468e2f9a107@mail.gmail.com> I'm embedding Python into a C/C++ application and it has an enormous amount of functions I wish to be accessible in Python. Right now, I have a module named "sourcemod" initialized and running. It has its own functions that can be called by importing the module. However, I want these functions spread out in different modules in "sourcemod", turning "sourcemod" into a package. For example, the user should be able to "import sourcemod" and use " sourcemod.console.PrintToConsole('Hello, World!')". I've been searching everywhere, I've gone through the Python/C API reference, the Embedding and Extending Python document, the #python channel on EFnet, and other people's source code. None of the pages I've found shine any light on how to write a Python package directly from C/C++. Thanks, Zach Kanzler -------------- next part -------------- An HTML attachment was scrubbed... URL: From sipickles at hotmail.com Mon Jan 14 09:02:26 2008 From: sipickles at hotmail.com (Simon Pickles) Date: Mon, 14 Jan 2008 08:02:26 +0000 Subject: [C++-sig] [Stackless] Trouble linking on Ubuntu Linux In-Reply-To: References: Message-ID: Thanks for the tip Joseph, My default python dir is /usr/lib, with the stackless version in /usr/local/lib so I tried adding this to the bjam line like so, and got errors: simon at simon-linux:/usr/local/boost_1_34_1$ sudo bjam --with-python -sPYTHON_ROOT=/usr/local/lib/python25 -sPYTHON_VERSION=2.5 stage Jamfile.v2:341: in load-aux rule path.glob-tree unknown in module Jamfile. /usr/share/boost-build/build/project.jam:318: in load-jamfile /usr/share/boost-build/build/project.jam:68: in load /usr/share/boost-build/build/project.jam:170: in project.find /usr/share/boost-build/build-system.jam:148: in load /usr/share/boost-build/kernel/modules.jam:261: in import /usr/share/boost-build/kernel/bootstrap.jam:132: in boost-build /usr/local/boost_1_34_1/boost-build.jam:9: in module scope Oh dear! Si Joseph Lisee wrote: > I forget the exact settings, but boost.python most likely picks up your default > python installation. This will be at the one in /usr/lib. If you wish to build > Boost.Python with Stackless, then you will need to point it at your stackless > python installation. I think you want to use the following options: > > -sPYTHON_ROOT= -sPYTHON_VERSION=2.5 > > I have no idea where you have stackless python installed though. > > Also you should almost never manually copy over libraries, especially if you > don't really know what you are doing. > > -Joseph Lisee > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > -- Linux Counter: User# 424693 From max.bret at yahoo.com Mon Jan 14 19:28:54 2008 From: max.bret at yahoo.com (Max Bret) Date: Mon, 14 Jan 2008 10:28:54 -0800 (PST) Subject: [C++-sig] python wrapper to log in executables' usage Message-ID: <218125.96550.qm@web45715.mail.sp1.yahoo.com> Hi, Does anyone have a script which will log in the dates, times and usernames for each run of a particular executable on a linux system? Can this be done by callbacks? Thanks, Max. ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs -------------- next part -------------- An HTML attachment was scrubbed... URL: From andre.dos.anjos at gmail.com Tue Jan 15 12:21:42 2008 From: andre.dos.anjos at gmail.com (=?utf-8?b?QW5kcsOp?= Anjos) Date: Tue, 15 Jan 2008 11:21:42 +0000 (UTC) Subject: [C++-sig] =?utf-8?q?make=5Fconstructor_with_custodian=5Fand=5Fwar?= =?utf-8?q?d_not_possible=3F?= Message-ID: Hi, I'm trying to inject constructors to an existing class using Boost.Python, to which I have not access to (for modifying the source). The basic Idea is reproduced in the example bellow. In the way it is, it compiles. If the comments on the lines with the "custodian_and_ward" are removed (any of them), gcc (3.4)/boost (1.33.1) complains: /afs/cern.ch/sw/lcg/external/Boost/1.33.1/slc4_ia32_gcc34/include/ boost-1_33_1/boost/python/with_custodian_and_ward.hpp:25: error: no matching function for call to `get(mpl_::int_<0>, const boost::python::detail::offset_args >&)' Here is what I'm trying: #include #include using namespace boost::python; class Holder { public: Holder(int x): my_x() { my_x[0] = x; /*...*/ my_x[4] = x; } const int* get_c_array() const { return my_x; } private: int my_x[5]; }; class Pointer { public: Pointer (const int* ref): cannot_copy(ref) {} //... methods to access the contents of "cannot_copy" private: const int* cannot_copy; //for a special reason I cannot copy this integer }; //Make Foo to use data in Bar, want it to be the custodian in this case boost::shared_ptr make_pointer(const Holder& h) { return boost::shared_ptr(new Pointer(h.get_c_array())); } BOOST_PYTHON_MODULE(test) { class_("Holder", init()) ; class_("Pointer", no_init) //.def("__init__", make_constructor(&make_pointer, with_custodian_and_ward_postcall<0, 1>())) //if comment out, gives error above //.def("__init__", make_constructor(&make_pointer, with_custodian_and_ward<1, 2>())) //if comment out, gives error above .def("__init__", make_constructor(&make_pointer, default_call_policies())) ; } Always in the same point. Is this what I'm trying not possible? Many thanks for any hint. Andre From andre.dos.anjos at gmail.com Tue Jan 15 18:53:45 2008 From: andre.dos.anjos at gmail.com (=?utf-8?b?QW5kcsOp?= Anjos) Date: Tue, 15 Jan 2008 17:53:45 +0000 (UTC) Subject: [C++-sig] =?utf-8?q?make=5Fconstructor_with_custodian=5Fand=5Fwar?= =?utf-8?q?d_not_possible=3F?= References: Message-ID: Andr? Anjos gmail.com> writes: I have found several other posts on the same subject, suggesting there is no known solution. Has anybody found a way to circumvent this? http://mail.python.org/pipermail/c++-sig/2007-September/012846.html and http://mail.python.org/pipermail/c++-sig/2006-June/010746.html and http://www.nabble.com/-C%2B%2B-sig--make_constructor-%2B- with_custodian_and_ward-td4663469.html Andre From roman.yakovenko at gmail.com Tue Jan 15 20:10:42 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 15 Jan 2008 21:10:42 +0200 Subject: [C++-sig] make_constructor with custodian_and_ward not possible? In-Reply-To: References: Message-ID: <7465b6170801151110m38b6f1ffkd919beef3179283b@mail.gmail.com> On Jan 15, 2008 7:53 PM, Andr? Anjos wrote: > Andr? Anjos gmail.com> writes: > > I have found several other posts on the same subject, suggesting there is no > known solution. Has anybody found a way to circumvent this? > > http://mail.python.org/pipermail/c++-sig/2007-September/012846.html > > and > > http://mail.python.org/pipermail/c++-sig/2006-June/010746.html > > and > > http://www.nabble.com/-C%2B%2B-sig--make_constructor-%2B- > with_custodian_and_ward-td4663469.html I guess you can work around this problem: You can expose "make_pointer" as regular function, with any policy you want, and than add small code in Python: from your module import Pointer as PointerImpl from your module import make_pointer def Pointer( x ): return make_pointer( x) The only inconvenience is that "isinstance" will not work. Another idea is to add new property to Pointer class "object holder", that will keep reference to the Holder instance, than from Python initialize that property: from your module import Pointer as PointerImpl class Pointer( PointerImpl ): def __init__( self, arg ): PointerImpl.__init__( self, arg ) self.__arg = arg HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From steve at chinavfx.net Wed Jan 16 05:14:48 2008 From: steve at chinavfx.net (Steve Yin) Date: Wed, 16 Jan 2008 12:14:48 +0800 Subject: [C++-sig] How can I wrap assign operator(=) in Boost::Python? Message-ID: Hi all, I'm a new guy to this, I have a class, eg: Vector, by default, If I create a variable v1, then use v2 = v1, the result is v2 and v1 are exact same object, but in C++ I have a assign operator, How can I export this assign operator to Python? From roman.yakovenko at gmail.com Wed Jan 16 08:07:57 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 16 Jan 2008 09:07:57 +0200 Subject: [C++-sig] How can I wrap assign operator(=) in Boost::Python? In-Reply-To: References: Message-ID: <7465b6170801152307j1e5cd0b5l8d84c7e68b865ed2@mail.gmail.com> On Jan 16, 2008 6:14 AM, Steve Yin wrote: > Hi all, > I'm a new guy to this, I have a class, eg: Vector, by default, If I > create a variable v1, then use v2 = v1, the result is v2 and v1 are exact > same object, but in C++ I have a assign operator, How can I export this > assign operator to Python? You can only expose it as regular method. Python doesn't have "assign" operator. ... .def( "assign", ::your_class::operator= ) -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From huangyougong at gmail.com Wed Jan 16 09:21:54 2008 From: huangyougong at gmail.com (Huang You Gong) Date: Wed, 16 Jan 2008 16:21:54 +0800 Subject: [C++-sig] How to use a C++ function with pointer/length arguments in Python Message-ID: Hi, in c++ there are codes like this: *struct x* *{* * int a;* * short b;* * short c;* *};* *struct y* *{* * byte a;* * byte b;* * byte c;* *};* *void foo( byte * data, int len ){ //... }* in boost_python_module, I defined it as: *class_< x >( "x" ) * * .def_readwrite( "a", &x::a ) * * .def_readwrite( "b", &x::b ) * * .def_readwrite( "c", &x::c )* *.def( "foo", foo )* then in python, how to write code to send x or y object to foo? in c++ way, it should be like this: *x X;* *foo( &X, sizeof( x ) );* *y Y;* *foo( &Y, sizeof( y ) );* -- Thanks Huang You Gong -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Wed Jan 16 10:24:38 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 16 Jan 2008 11:24:38 +0200 Subject: [C++-sig] How to use a C++ function with pointer/length arguments in Python In-Reply-To: References: Message-ID: <7465b6170801160124y39db70far86434d287fb24204@mail.gmail.com> 2008/1/16 Huang You Gong : > > Hi, > > in c++ there are codes like this: > > struct x > { > int a; > short b; > short c; > }; > struct y > { > byte a; > byte b; > byte c; > }; > void foo( byte * data, int len ){ //... } > > in boost_python_module, I defined it as: > > class_< x >( "x" ) > .def_readwrite( "a", &x::a ) > .def_readwrite( "b", &x::b ) > .def_readwrite( "c", &x::c ) > .def( "foo", foo ) > > then in python, how to write code to send x or y object to foo? in c++ way, > it should be like this: > > x X; > foo( &X, sizeof( x ) ); > y Y; > foo( &Y, sizeof( y ) ); I suggest you to create another function, that wraps "foo". Take a look on http://language-binding.net/pyplusplus/documentation/functions/transformation/built_in/input_c_buffer.html and http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/pyplusplus/code_repository/convenience.py?revision=1032&view=markup HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From andre.dos.anjos at gmail.com Wed Jan 16 10:57:09 2008 From: andre.dos.anjos at gmail.com (=?utf-8?b?QW5kcsOp?= Anjos) Date: Wed, 16 Jan 2008 09:57:09 +0000 (UTC) Subject: [C++-sig] =?utf-8?q?make=5Fconstructor_with_custodian=5Fand=5Fwar?= =?utf-8?q?d_not_possible=3F?= References: <7465b6170801151110m38b6f1ffkd919beef3179283b@mail.gmail.com> Message-ID: Roman Yakovenko gmail.com> writes: > > You can expose "make_pointer" as regular function, with any policy you > want, and than add small code in Python: > > ... > > The only inconvenience is that "isinstance" will not work. > > Another idea is to add new property to Pointer class "object holder", > that will keep reference to the Holder instance, than from Python > initialize that property: > > from your module import Pointer as PointerImpl > > class Pointer( PointerImpl ): > def __init__( self, arg ): > PointerImpl.__init__( self, arg ) > self.__arg = arg Thanks for this, as you noted, this requires python wrapping in one or other form. I would prefer, of course, solution #2 as it makes "isinstance()" work correctly as you pointed out yourself - in fact that is the solution I had already. I was a bit hoping there was either a definitive Boost.Python answer on this. In failing that, plans to actually get that kind of operation supported. From what I could explore at Boost.Python and the lines of the compiler, it seems that anything else than "default_call_policies" is not working. It looks as if the other policies are not implemented on the same way (missing types or specializations). So, if this is actually the matter of extending, say, with_custodian_and_ward policy, it would be (for a Boost.Python developer - which I'm not) easy to propose some code (a policy, say), that would fit OK with make_constructor as default_call_policies does. Any experts on this? Andre From hans_meine at gmx.net Wed Jan 16 16:40:37 2008 From: hans_meine at gmx.net (Hans Meine) Date: Wed, 16 Jan 2008 16:40:37 +0100 Subject: [C++-sig] make_constructor with custodian_and_ward not possible? In-Reply-To: References: Message-ID: <200801161640.37157.hans_meine@gmx.net> Am Dienstag, 15. Januar 2008 18:53:45 schrieb Andr? Anjos: > Andr? Anjos gmail.com> writes: > > I have found several other posts on the same subject, suggesting there is > no known solution. Has anybody found a way to circumvent this? > > http://mail.python.org/pipermail/c++-sig/2007-September/012846.html As you may have seen from the above link, I am one of the guys who suffered from this bug/limitation. IIRC I was told that it's among the things that are clearly missing and wait for an expert to implement. Unfortunately, boost::python's implementation is quite a beast, and there are not too many such experts. I did not manage to get more than the simplest one-line changes to its codebase working yet. Ciao, / / /--/ / / ANS From kloeckner at dam.brown.edu Wed Jan 16 19:33:44 2008 From: kloeckner at dam.brown.edu (Andreas =?iso-8859-1?q?Kl=F6ckner?=) Date: Wed, 16 Jan 2008 13:33:44 -0500 Subject: [C++-sig] Fwd: Re: [patch] Boost.Python Vector indexing suite::extend accepts general iterables Message-ID: <200801161333.46696.kloeckner@dam.brown.edu> -------------- next part -------------- An embedded message was scrubbed... From: Andreas =?iso-8859-1?q?Kl=F6ckner?= Subject: Re: [patch] Boost.Python Vector indexing suite::extend accepts general iterables Date: Wed, 16 Jan 2008 13:02:28 -0500 Size: 5366 URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part. URL: From kloeckner at dam.brown.edu Wed Jan 16 19:35:43 2008 From: kloeckner at dam.brown.edu (Andreas =?iso-8859-1?q?Kl=F6ckner?=) Date: Wed, 16 Jan 2008 13:35:43 -0500 Subject: [C++-sig] [patch] Boost.Python STL input iterator does not propagate exceptions In-Reply-To: <477D47E6.5040705@boost-consulting.com> References: <200801012024.22293.kloeckner@dam.brown.edu> <477D47E6.5040705@boost-consulting.com> Message-ID: <200801161335.45041.kloeckner@dam.brown.edu> Hi Eric, On Donnerstag 03 Januar 2008, you wrote: > Andreas Kl?ckner wrote: > > The attached patch fixes an exception propagation issue in the STL input > > iterator in Boost.Python. The problem is as follows: PyIter_Next may > > return 0 for two reasons: > > > > - No more iterants left. > > - An exception was caught. > > > > The current implementation interprets the latter as the former. A simple > > fix (throw error_already_set if appropriate) is attached. > > > > I'd appreciate it if you could commit this. If there are any issues, > > please let me know and I'll do my best to fix them. > > Thanks, please provide a test case. Test case attached, along with original patch. On unpatched code, this gives: ********************************************************************** File "stl_iterator.py", line 19, in __main__ Failed example: try: x.assign(iter(generator())) print "NOT OK" except RuntimeError: print "OK" Expected: OK Got: NOT OK ********************************************************************** or it crashes outright. Andreas -------------- next part -------------- A non-text attachment was scrubbed... Name: bpl-stl-iterator-prop-ex-2007-12-20.patch Type: text/x-diff Size: 635 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: stl-iterator-ex-prop-test.diff Type: text/x-diff Size: 462 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part. URL: From matthew.scouten at gmail.com Wed Jan 16 21:56:49 2008 From: matthew.scouten at gmail.com (Matthew Scouten) Date: Wed, 16 Jan 2008 14:56:49 -0600 Subject: [C++-sig] Compiler Woes Message-ID: <3dc9bcf00801161256v7159716ay3c4f15402b043220@mail.gmail.com> I have been working on wrapping a big, complicated, pre-existing C++ lib for a while now. Recently I've been getting some strange runtime errors. After a lot of debugging I realized that python was linking to msvcr71.dll and my lib was linking to msvcr80.dll. Google tells me that mixing compilers/c runtimes like that is a bad idea, and may well be the source of my trouble. So I need to match compilers. The lib is to be compiled with msvc8, and that is written is stone. I seem to be in the position of compiling a python myself and distributing it to my users. Be for I take this drastic step, I would like to know: Are there any python distros/implementations/versions out there that are already compiled with msvc8? Or am I up the creek? -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariofutire at googlemail.com Wed Jan 16 22:23:07 2008 From: mariofutire at googlemail.com (Andrea) Date: Wed, 16 Jan 2008 21:23:07 +0000 Subject: [C++-sig] std::tr1::shared_ptr as boost::shared_ptr In-Reply-To: <200704201410.14020.p.jaroszynski@gmail.com> References: <200704201410.14020.p.jaroszynski@gmail.com> Message-ID: <478E75BB.9040007@googlemail.com> Piotr Jaroszynski wrote: > Hello, > > I am exposing a C++ app, which makes heavy use of tr1::shared_ptr, which seems > to be exactly(?) the same as boost::shared_ptr. I have managed to make most > of the stuff work just as it would be boost::shared_ptr ( additionally adding > support for shared_ptr - thanks to [1] ): > > Btw. I have seen [2] already, but it doesn't seem to cover that. > > [1] - > http://language-binding.net/pyplusplus/troubleshooting_guide/shared_ptr/shared_ptr.html > [2] - > http://language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html > Hi, I've got the same problem. Basically, given class A { ... }; class B : public A { ... }; It seems that Boost.Python is always able to call void foo(const boost::shared_ptr & x); with and argument of type OTHER_SMART_PTR How is it possile? I would like to achieve the same with my own smart pointer. I've tried with tr1::shared_ptr, my own smart pointer, the *exact* same code as boost::shared_ptr copied with a different name... It looks like boost::shared_ptr is *much* more powerful than all other smart pointers that can be registered. Here is the code. How you can see, I never tell Boost.Python anything about boost::shared_ptr. Then you can run the following python code >>> import bo >>> b=bo.newB(10) >>> print bo.getI_OK(b) 30 >>> print bo.getI_FAIL(b) Traceback (most recent call last): File "", line 1, in Boost.Python.ArgumentError: Python argument types in bo.getI_FAIL(B) did not match C++ signature: getI_FAIL(std::tr1::shared_ptr) It seems that Boost.Python can extract the object from my smart pointer, dynamic_cast it to the right type and create a boost::shared_pointer. Just how...? ============================================================================================================= #include #include #include using namespace boost::python; #define OTHER_PTR std::tr1::shared_ptr // needed by Boost.Python template T * get_pointer(OTHER_PTR const & p) { return p.get(); } class A { public: A(int i) : myInt(i) {} virtual ~A() {} virtual int getI() const { return myInt * 2; } protected: int myInt; }; class B : public A { public: B(int i) : A(i) {} virtual int getI() const { return myInt * 3; } }; // how is it possible that Boost.Python knows how to create a boost::shared_ptr? // basically it can convert a OTHER_PTR (as returned by newB) to a boost::shared_ptr needed here int getI_OK(const boost::shared_ptr & a) { return a->getI(); } // but it cannot do it here int getI_FAIL(const OTHER_PTR & a) { return a->getI(); } OTHER_PTR newB(int i) { return OTHER_PTR(new B(i)); } BOOST_PYTHON_MODULE(bo) { def("newB", newB); def("getI_OK", getI_OK); def("getI_FAIL", getI_FAIL); class_ ("A", no_init); class_ >("B", no_init); register_ptr_to_python >(); } ============================================================================================================ Andrea From mariofutire at googlemail.com Wed Jan 16 22:23:07 2008 From: mariofutire at googlemail.com (Andrea) Date: Wed, 16 Jan 2008 21:23:07 +0000 Subject: [C++-sig] std::tr1::shared_ptr as boost::shared_ptr In-Reply-To: <200704201410.14020.p.jaroszynski@gmail.com> References: <200704201410.14020.p.jaroszynski@gmail.com> Message-ID: <478E75BB.9040007@googlemail.com> Piotr Jaroszynski wrote: > Hello, > > I am exposing a C++ app, which makes heavy use of tr1::shared_ptr, which seems > to be exactly(?) the same as boost::shared_ptr. I have managed to make most > of the stuff work just as it would be boost::shared_ptr ( additionally adding > support for shared_ptr - thanks to [1] ): > > Btw. I have seen [2] already, but it doesn't seem to cover that. > > [1] - > http://language-binding.net/pyplusplus/troubleshooting_guide/shared_ptr/shared_ptr.html > [2] - > http://language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html > Hi, I've got the same problem. Basically, given class A { ... }; class B : public A { ... }; It seems that Boost.Python is always able to call void foo(const boost::shared_ptr & x); with and argument of type OTHER_SMART_PTR How is it possile? I would like to achieve the same with my own smart pointer. I've tried with tr1::shared_ptr, my own smart pointer, the *exact* same code as boost::shared_ptr copied with a different name... It looks like boost::shared_ptr is *much* more powerful than all other smart pointers that can be registered. Here is the code. How you can see, I never tell Boost.Python anything about boost::shared_ptr. Then you can run the following python code >>> import bo >>> b=bo.newB(10) >>> print bo.getI_OK(b) 30 >>> print bo.getI_FAIL(b) Traceback (most recent call last): File "", line 1, in Boost.Python.ArgumentError: Python argument types in bo.getI_FAIL(B) did not match C++ signature: getI_FAIL(std::tr1::shared_ptr) It seems that Boost.Python can extract the object from my smart pointer, dynamic_cast it to the right type and create a boost::shared_pointer. Just how...? ============================================================================================================= #include #include #include using namespace boost::python; #define OTHER_PTR std::tr1::shared_ptr // needed by Boost.Python template T * get_pointer(OTHER_PTR const & p) { return p.get(); } class A { public: A(int i) : myInt(i) {} virtual ~A() {} virtual int getI() const { return myInt * 2; } protected: int myInt; }; class B : public A { public: B(int i) : A(i) {} virtual int getI() const { return myInt * 3; } }; // how is it possible that Boost.Python knows how to create a boost::shared_ptr? // basically it can convert a OTHER_PTR (as returned by newB) to a boost::shared_ptr needed here int getI_OK(const boost::shared_ptr & a) { return a->getI(); } // but it cannot do it here int getI_FAIL(const OTHER_PTR & a) { return a->getI(); } OTHER_PTR newB(int i) { return OTHER_PTR(new B(i)); } BOOST_PYTHON_MODULE(bo) { def("newB", newB); def("getI_OK", getI_OK); def("getI_FAIL", getI_FAIL); class_ ("A", no_init); class_ >("B", no_init); register_ptr_to_python >(); } ============================================================================================================ Andrea From t_spens at yahoo.com Thu Jan 17 00:18:00 2008 From: t_spens at yahoo.com (Tim Spens) Date: Wed, 16 Jan 2008 15:18:00 -0800 (PST) Subject: [C++-sig] Problems using wrapper.hpp to override virtual functions Message-ID: <761084.61904.qm@web45110.mail.sp1.yahoo.com> Here is the simplest example of what I'm trying to do, I hope this is enough the complete project to quite large. I'm using wrapper.hpp so that I can use a python handler to handle callbacks from the c++ code. //c++ pure virtual function definition virtual void handle(const CLIENT::client_response & rsp) = 0; //c++ wrapper for virtual function "handle" virtual void handle(client_response const & rsp) { if(override func_handle = this->get_override("handle")){ //I cannot get this section of code to run, from what I understand this would be the callback into python? func_handle(boost::ref(rsp)); cout << "here handle in python" << endl;} else{ //I currently have a c++ handler which is called here, I would like to use the python handler though. this->python_handler::handle(boost::ref(rsp)); cout << "here handle in c++" << endl;} } BOOST_PYTHON_MODULE(libclientpy) { class_, boost::noncopyable >("python_client_handler", init<>()) .def("handle", &client_handler::handle); } #PYTHON CODE import libclientpy handler = libclientpy.python_handler() client = libclientpy.client(handler, '127.0.0.1', 7900) class callback(handler): def handle(self, x): print 'handle python' azc = addZapCallback() Traceback (most recent call last): File "./test_client.py", line 304, in class callback(handler): Boost.Python.ArgumentError: Error when calling the metaclass bases Python argument types in python_client_handler.__init__(python_client_handler, str, tuple, dict) did not match C++ signature: __init__(_object*) I am unsure what is happening here. I cannot even find a __init__ function in my python_client_handler that has the said arguments? __init__(python_client_handler, str, tuple, dict) Any ideas? Tim ____________________________________________________________________________________ Looking for last minute shopping deals? Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping From eric at boost-consulting.com Thu Jan 17 06:02:10 2008 From: eric at boost-consulting.com (Eric Niebler) Date: Wed, 16 Jan 2008 21:02:10 -0800 Subject: [C++-sig] [patch] Boost.Python STL input iterator does not propagate exceptions In-Reply-To: <200801161335.45041.kloeckner@dam.brown.edu> References: <200801012024.22293.kloeckner@dam.brown.edu> <477D47E6.5040705@boost-consulting.com> <200801161335.45041.kloeckner@dam.brown.edu> Message-ID: <478EE152.7010309@boost-consulting.com> Andreas Kl?ckner wrote: > On Donnerstag 03 Januar 2008, you wrote: >> Andreas Kl?ckner wrote: >>> The attached patch fixes an exception propagation issue in the STL input >>> iterator in Boost.Python. > > Test case attached, along with original patch. On unpatched code, this gives: Great! I'm currently serving jury duty, but I'll get to this as soon as I can. Not later than next week, I promise. Thanks, -- Eric Niebler Boost Consulting www.boost-consulting.com From roman.yakovenko at gmail.com Thu Jan 17 10:44:26 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 17 Jan 2008 11:44:26 +0200 Subject: [C++-sig] Problems using wrapper.hpp to override virtual functions In-Reply-To: <761084.61904.qm@web45110.mail.sp1.yahoo.com> References: <761084.61904.qm@web45110.mail.sp1.yahoo.com> Message-ID: <7465b6170801170144h21af19d4rdb38b8ac00922d7f@mail.gmail.com> On Jan 17, 2008 1:18 AM, Tim Spens wrote: > Here is the simplest example of what I'm trying to do, I hope this is enough the complete > project to quite large. I'm using wrapper.hpp so that I can use a python handler to handle > callbacks from the c++ code. > > > //c++ pure virtual function definition > virtual void handle(const CLIENT::client_response & rsp) = 0; > > //c++ wrapper for virtual function "handle" > virtual void handle(client_response const & rsp) > { > if(override func_handle = this->get_override("handle")){ > //I cannot get this section of code to run, from what I understand this would be the callback into python? > func_handle(boost::ref(rsp)); > cout << "here handle in python" << endl;} > else{ > //I currently have a c++ handler which is called here, I would like to use the python handler though. > this->python_handler::handle(boost::ref(rsp)); > cout << "here handle in c++" << endl;} > } > > BOOST_PYTHON_MODULE(libclientpy) > { > class_, boost::noncopyable >("python_client_handler", init<>()) > .def("handle", &client_handler::handle); > } > > #PYTHON CODE > import libclientpy > handler = libclientpy.python_handler() > client = libclientpy.client(handler, '127.0.0.1', 7900) > > class callback(handler): > def handle(self, x): > print 'handle python' > > azc = addZapCallback() > > Traceback (most recent call last): > File "./test_client.py", line 304, in > class callback(handler): > Boost.Python.ArgumentError: Error when calling the metaclass bases > Python argument types in > python_client_handler.__init__(python_client_handler, str, tuple, dict) > did not match C++ signature: > __init__(_object*) > > > I am unsure what is happening here. I cannot even find a __init__ function in my python_client_handler that has the said arguments? __init__(python_client_handler, str, tuple, dict) > > Any ideas? Small complete example helps. you have to call __init__ method of exposed C++ class class callback(handler): def __init__( self ): handler.__init__( self ) def handle(self, x): print 'handle python' HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From fullung at gmail.com Thu Jan 17 12:59:30 2008 From: fullung at gmail.com (Albert Strasheim) Date: Thu, 17 Jan 2008 13:59:30 +0200 Subject: [C++-sig] Compiler Woes In-Reply-To: <3dc9bcf00801161256v7159716ay3c4f15402b043220@mail.gmail.com> References: <3dc9bcf00801161256v7159716ay3c4f15402b043220@mail.gmail.com> Message-ID: <5eec5f300801170359s2227a46cob80b042e8c2ce9b2@mail.gmail.com> Hello 2008/1/16 Matthew Scouten : > Are there any python distros/implementations/versions out there that are > already compiled with msvc8? Or am I up the creek? You're probably up the creek. The Python folks decided to skip VS2005. However, the SVN repository (and maybe the official sources), includes the necessary project files in the PCbuild8 directory. Getting it to build isn't too hard. Cheers, Albert From t_spens at yahoo.com Thu Jan 17 18:14:37 2008 From: t_spens at yahoo.com (Tim Spens) Date: Thu, 17 Jan 2008 09:14:37 -0800 (PST) Subject: [C++-sig] Problems using wrapper.hpp to override virtual functions Message-ID: <516485.18268.qm@web45102.mail.sp1.yahoo.com> I've added in the __init__ constructor like you suggested but I'm still getting the same error? I also tried adding in a default forwarding function like in http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html I'm using py++ to generate most of my wrapper code, with slight modifications. void default_handle(client_response const & rsp) { this->python_client_handler::handle(boost::ref(rsp)); } ..def("handle", &client_handler::handle, &python_client_handler_wrapper::default_handle) Traceback (most recent call last): File "./test_client.py", line 304, in class callback(handler): Boost.Python.ArgumentError: Error when calling the metaclass bases Python argument types in python_client_handler.__init__(python_client_handler, str, tuple, dict) did not match C++ signature: __init__(_object*) Tim ----- Original Message ---- From: Roman Yakovenko To: Development of Python/C++ integration Sent: Thursday, January 17, 2008 2:44:26 AM Subject: Re: [C++-sig] Problems using wrapper.hpp to override virtual functions On Jan 17, 2008 1:18 AM, Tim Spens wrote: > Here is the simplest example of what I'm trying to do, I hope this is enough the complete > project to quite large. I'm using wrapper.hpp so that I can use a python handler to handle > callbacks from the c++ code. > > > //c++ pure virtual function definition > virtual void handle(const CLIENT::client_response & rsp) = 0; > > //c++ wrapper for virtual function "handle" > virtual void handle(client_response const & rsp) > { > if(override func_handle = this->get_override("handle")){ > //I cannot get this section of code to run, from what I understand this would be the callback into python? > func_handle(boost::ref(rsp)); > cout << "here handle in python" << endl;} > else{ > //I currently have a c++ handler which is called here, I would like to use the python handler though. > this->python_handler::handle(boost::ref(rsp)); > cout << "here handle in c++" << endl;} > } > > BOOST_PYTHON_MODULE(libclientpy) > { > class_, boost::noncopyable >("python_client_handler", init<>()) > .def("handle", &client_handler::handle); > } > > #PYTHON CODE > import libclientpy > handler = libclientpy.python_handler() > client = libclientpy.client(handler, '127.0.0.1', 7900) > > class callback(handler): > def handle(self): > print 'handle python' > > azc = addZapCallback() > > Traceback (most recent call last): > File "./test_client.py", line 304, in > class callback(handler): > Boost.Python.ArgumentError: Error when calling the metaclass bases > Python argument types in > python_client_handler.__init__(python_client_handler, str, tuple, dict) > did not match C++ signature: > __init__(_object*) > > > I am unsure what is happening here. I cannot even find a __init__ function in my python_client_handler that has the said arguments? __init__(python_client_handler, str, tuple, dict) > > Any ideas? Small complete example helps. you have to call __init__ method of exposed C++ class class callback(handler): def __init__( self ): handler.__init__( self ) def handle(self): print 'handle python' HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ From roman.yakovenko at gmail.com Thu Jan 17 19:53:06 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 17 Jan 2008 20:53:06 +0200 Subject: [C++-sig] Problems using wrapper.hpp to override virtual functions In-Reply-To: <516485.18268.qm@web45102.mail.sp1.yahoo.com> References: <516485.18268.qm@web45102.mail.sp1.yahoo.com> Message-ID: <7465b6170801171053h79c57b29v47d3f8ff39de8404@mail.gmail.com> can you create small and complete example? I willtake a look on it. On 1/17/08, Tim Spens wrote: > I've added in the __init__ constructor like you suggested but I'm still > getting the same error? > I also tried adding in a default forwarding function like in > http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html > I'm using py++ to generate most of my wrapper code, with slight > modifications. > > void default_handle(client_response const & rsp) > { > this->python_client_handler::handle(boost::ref(rsp)); > } > > ..def("handle", &client_handler::handle, > &python_client_handler_wrapper::default_handle) > > Traceback (most recent call last): > File "./test_client.py", line 304, in > class callback(handler): > Boost.Python.ArgumentError: Error when calling the metaclass bases > Python argument types in > python_client_handler.__init__(python_client_handler, str, tuple, > dict) > did not match C++ signature: > __init__(_object*) > > Tim > > ----- Original Message ---- > From: Roman Yakovenko > To: Development of Python/C++ integration > Sent: Thursday, January 17, 2008 2:44:26 AM > Subject: Re: [C++-sig] Problems using wrapper.hpp to override virtual > functions > > > On Jan 17, 2008 1:18 AM, Tim Spens wrote: > > Here is the simplest example of what I'm trying to do, I hope this is > enough the complete > > project to quite large. I'm using wrapper.hpp so that I can use a > python handler to handle > > callbacks from the c++ code. > > > > > > //c++ pure virtual function definition > > virtual void handle(const CLIENT::client_response & rsp) = 0; > > > > //c++ wrapper for virtual function "handle" > > virtual void handle(client_response const & rsp) > > { > > if(override func_handle = this->get_override("handle")){ > > //I cannot get this section of code to run, from > what I understand this would be the callback into python? > > func_handle(boost::ref(rsp)); > > cout << "here handle in python" << endl;} > > else{ > > //I currently have a c++ handler which is called > here, I would like to use the python handler though. > > this->python_handler::handle(boost::ref(rsp)); > > cout << "here handle in c++" << endl;} > > } > > > > BOOST_PYTHON_MODULE(libclientpy) > > { > > class_ bases, boost::noncopyable >("python_client_handler", > init<>()) > > .def("handle", &client_handler::handle); > > } > > > > #PYTHON CODE > > import libclientpy > > handler = libclientpy.python_handler() > > client = libclientpy.client(handler, '127.0.0.1', 7900) > > > > class callback(handler): > > def handle(self): > > print 'handle python' > > > > azc = addZapCallback() > > > > Traceback (most recent call last): > > File "./test_client.py", line 304, in > > class callback(handler): > > Boost.Python.ArgumentError: Error when calling the metaclass bases > > Python argument types in > > python_client_handler.__init__(python_client_handler, str, tuple, > dict) > > did not match C++ signature: > > __init__(_object*) > > > > > > I am unsure what is happening here. I cannot even find a __init__ > function in my python_client_handler that has the said arguments? > __init__(python_client_handler, str, tuple, dict) > > > > Any ideas? > > Small complete example helps. > > you have to call __init__ method of exposed C++ class > > > class callback(handler): > def __init__( self ): > handler.__init__( self ) > def handle(self): > print 'handle python' > > HTH > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > know-it-all with Yahoo! Mobile. Try it now. > http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ > > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From mariofutire at googlemail.com Thu Jan 17 21:31:51 2008 From: mariofutire at googlemail.com (Andrea) Date: Thu, 17 Jan 2008 20:31:51 +0000 Subject: [C++-sig] Boost.Python, shared_ptr and *other* smart pointers Message-ID: Hi, I've already posted yesterday this message answering a very old message, but It does not appear in the archive, so I post it again. I'm trying to use my own smart pointer, but I cannot reproduce the behavior of boost::shared_ptr. This is an other post about the same problem http://mail.python.org/pipermail/c++-sig/2007-April/012241.html I've been able to track down the issue to that: givem class A { ... }; class B : public A { ... }; It seems that Boost.Python is always able to call void foo(const boost::shared_ptr & x); passing and argument of type OTHER_SMART_PTR How is it possile? I would like to achieve the same with my own smart pointer. I've tried with tr1::shared_ptr, my own smart pointer, the *exact* same code as boost::shared_ptr copied with a different name... It looks like boost::shared_ptr is *much* more powerful than all other smart pointers that can be registered. Here is the code. How you can see, I never tell Boost.Python anything about boost::shared_ptr, but it still understands it. Then you can run the following python code >>> import bo >>> b=bo.newB(10) >>> print bo.getI_OK(b) 30 >>> print bo.getI_FAIL(b) Traceback (most recent call last): File "", line 1, in Boost.Python.ArgumentError: Python argument types in bo.getI_FAIL(B) did not match C++ signature: getI_FAIL(std::tr1::shared_ptr) It seems that Boost.Python can extract the object from my smart pointer, dynamic_cast it to the right type and create a boost::shared_pointer. Just how...? Could anybody help me? ============================================================================================================= #include #include #include using namespace boost::python; #define OTHER_PTR std::tr1::shared_ptr // needed by Boost.Python template T * get_pointer(OTHER_PTR const & p) { return p.get(); } class A { public: A(int i) : myInt(i) {} virtual ~A() {} virtual int getI() const { return myInt * 2; } protected: int myInt; }; class B : public A { public: B(int i) : A(i) {} virtual int getI() const { return myInt * 3; } }; // how is it possible that Boost.Python knows how to create a boost::shared_ptr? // basically it can convert a OTHER_PTR (as returned by newB) to a boost::shared_ptr needed here int getI_OK(const boost::shared_ptr & a) { return a->getI(); } // but it cannot do it here int getI_FAIL(const OTHER_PTR & a) { return a->getI(); } OTHER_PTR newB(int i) { return OTHER_PTR(new B(i)); } BOOST_PYTHON_MODULE(bo) { def("newB", newB); def("getI_OK", getI_OK); def("getI_FAIL", getI_FAIL); class_ ("A", no_init); class_ >("B", no_init); register_ptr_to_python >(); } ============================================================================================================ Andrea From mariofutire at googlemail.com Thu Jan 17 21:52:22 2008 From: mariofutire at googlemail.com (Andrea) Date: Thu, 17 Jan 2008 20:52:22 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: <87tzvrrfri.fsf@valverde.peloton> References: <87tzvrrfri.fsf@valverde.peloton> Message-ID: <478FC006.6000602@googlemail.com> David Abrahams wrote: > on Mon Apr 02 2007, "Francis Moreau" wrote: > > > Please read http://www.boost.org/libs/python/doc/v2/pointee.html > > However, be aware that you won't get exactly the "magic" shared_ptr > behavior described in > http://www.boost.org/libs/python/doc/v2/faq.html#xref unless your > pointer has a deleter just as shared_ptr does AND you do some extra > work to define special converters for it (and perhaps not even then; I > haven't looked into the details). > > Could you please explain a bit more what you mean by 1) deleter 2) which converters 3) what more is needed I've tried to copy boost::shared_ptr and rename it, to see whether the "magic" was coming from boost::share_ptr, but it looks like the "magic" is inside Boost.Python. I've tried also tr1::shared_ptr and they all fail where boost::shared_ptr succeeds. My library is just too huge to change all smart pointers to boost::shared_ptr... Cheers From mariofutire at googlemail.com Thu Jan 17 21:52:22 2008 From: mariofutire at googlemail.com (Andrea) Date: Thu, 17 Jan 2008 20:52:22 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: <87tzvrrfri.fsf@valverde.peloton> References: <87tzvrrfri.fsf@valverde.peloton> Message-ID: <478FC006.6000602@googlemail.com> David Abrahams wrote: > on Mon Apr 02 2007, "Francis Moreau" wrote: > > > Please read http://www.boost.org/libs/python/doc/v2/pointee.html > > However, be aware that you won't get exactly the "magic" shared_ptr > behavior described in > http://www.boost.org/libs/python/doc/v2/faq.html#xref unless your > pointer has a deleter just as shared_ptr does AND you do some extra > work to define special converters for it (and perhaps not even then; I > haven't looked into the details). > > Could you please explain a bit more what you mean by 1) deleter 2) which converters 3) what more is needed I've tried to copy boost::shared_ptr and rename it, to see whether the "magic" was coming from boost::share_ptr, but it looks like the "magic" is inside Boost.Python. I've tried also tr1::shared_ptr and they all fail where boost::shared_ptr succeeds. My library is just too huge to change all smart pointers to boost::shared_ptr... Cheers From theboyhaddon at hotmail.com Thu Jan 17 23:41:36 2008 From: theboyhaddon at hotmail.com (john haddon) Date: Thu, 17 Jan 2008 22:41:36 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: <478FC006.6000602@googlemail.com> References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> Message-ID: > I've tried to copy boost::shared_ptr and rename it, to see whether the "magic" was coming from > boost::share_ptr, but it looks like the "magic" is inside Boost.Python. > I've tried also tr1::shared_ptr and they all fail where boost::shared_ptr succeeds. > > My library is just too huge to change all smart pointers to boost::shared_ptr... we had similar problems when wrapping a library where we used boost::intrusive_ptr as our smart pointer of choice. we managed to work around most of the problems using the magic incantations in this file : http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/IntrusivePtrPatch.h you can see an example of how we use that in this file : http://cortex-vfx.googlecode.com/svn/trunk/src/IECore/bindings/ObjectBinding.cpp hopefully those might give you some clue as to how to fix your problems too... cheers... john _________________________________________________________________ -------------- next part -------------- An HTML attachment was scrubbed... URL: From eric at boost-consulting.com Thu Jan 17 23:49:31 2008 From: eric at boost-consulting.com (Eric Niebler) Date: Thu, 17 Jan 2008 14:49:31 -0800 Subject: [C++-sig] [patch] Boost.Python STL input iterator does not propagate exceptions In-Reply-To: <478EE152.7010309@boost-consulting.com> References: <200801012024.22293.kloeckner@dam.brown.edu> <477D47E6.5040705@boost-consulting.com> <200801161335.45041.kloeckner@dam.brown.edu> <478EE152.7010309@boost-consulting.com> Message-ID: <478FDB7B.9050000@boost-consulting.com> Eric Niebler wrote: > Andreas Kl?ckner wrote: >> On Donnerstag 03 Januar 2008, you wrote: >>> Andreas Kl?ckner wrote: >>>> The attached patch fixes an exception propagation issue in the STL input >>>> iterator in Boost.Python. >> Test case attached, along with original patch. On unpatched code, this gives: Committed to trunk, and you've been credited in the source. Thanks. -- Eric Niebler Boost Consulting www.boost-consulting.com From mattjomeara at gmail.com Fri Jan 18 19:58:19 2008 From: mattjomeara at gmail.com (Matthew O'Meara) Date: Fri, 18 Jan 2008 13:58:19 -0500 Subject: [C++-sig] trouble with boost.python build (blame boost-1_34_1/boost/function/function_template.hpp?) Message-ID: Hi all, I've got a complicated boost.python build and I'm running into an error that I can't figure out. I've tried to simplify it as much as I can while still reproducing the same error. For reasons outside of my control I am trying to build using g++ as follows. in /path/to/source.cc I have: #include "boost/python.hpp" running: $g++ -v -I/users/momeara/svn/mini_19621/src -I/usr/local/include/boost-1_34_1 -I/usr/local/include/python2.5 -o /path/to/target.o /path/to/source.cc gives: Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=relea\ se --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --di\ sable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java- 1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/\ usr/share/java/eclipse-ecj.jar --with-cpu=generic --host=i386-redhat-linux Thread model: posix gcc version 4.1.2 20070925 (Red Hat 4.1.2-27) /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus -quiet -v -I/users/momeara/svn/mini_19621/src -I/usr/local/include/boost-1_34_1 -I/usr/local/include/pytho\ n2.5 -D_GNU_SOURCE /users/momeara/svn/_utility__by_hand.cc -quiet -dumpbase _utility__by_hand.cc -mtune=generic -auxbase _utility__by_hand -version -o /tmp/\ ccWhDU3J.s ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /users/momeara/svn/mini_19621/src /usr/local/include/boost-1_34_1 /usr/local/include/python2.5 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/i386-redhat-linux /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward /usr/local/include /usr/lib/gcc/i386-redhat-linux/4.1.2/include /usr/include End of search list. GNU C++ version 4.1.2 20070925 (Red Hat 4.1.2-27) (i386-redhat-linux) compiled by GNU C version 4.1.2 20070925 (Red Hat 4.1.2-27). GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 47ecd5b49be3842ab15019d13f95ada6 /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:242: error: 'is_void' was not declared in this scope /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:242: error: '::value' has not been declared /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:253: error: template argument 1 is invalid /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:253: error: typedef name may not be a nested-name-specifier /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:253: error: expected ';' before 'type' /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:263: error: 'is_void' was not declared in this scope /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:263: error: '::value' has not been declared /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:274: error: template argument 1 is invalid /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:274: error: typedef name may not be a nested-name-specifier /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:274: error: expected ';' before 'type' /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:284: error: 'is_void' was not declared in this scope /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:284: error: '::value' has not been declared /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:295: error: template argument 1 is invalid /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:295: error: typedef name may not be a nested-name-specifier /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:295: error: expected ';' before 'type' /usr/local/include/boost-1_34_1/boost/function/function_template.hpp: In instantiation of 'boost::detail::function::get_invoker0::apply, void, std::allocator >': /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:783: instantiated from 'void boost::function0::assign_to(const Functor&\ ) [with Functor = boost::reference_wrapper, R = void, Allocator = std::allocator]' /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:624: instantiated from 'boost::function0::function0(Functor, typename b\ oost::enable_if_c::value>::value, int>::type) [with Functor = boost::reference_wrapper, \ R = void, Allocator = std::allocator]' /usr/local/include/boost-1_34_1/boost/python/errors.hpp:29: instantiated from 'bool boost::python::handle_exception(T) [with T = void (*)()]' /usr/local/include/boost-1_34_1/boost/python/errors.hpp:36: instantiated from here /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:410: error: no type named 'type' in 'struct boost::detail::function::get_function_ref_i\ nvoker0' I have seen seen posts ( http://archives.free.net.ph/message/20071221.134700.e0c7e2b6.pl.html) claiming that there is a bug in function_template that causes it to be non-thread-safe. I tried the suggested patch--replacing function_template.hpp and function_base.hpp with the most current version from boost svn and it returns the same error. For reference I downloaded the July 2007 boost 1.34.1 source from sourceforge and built using ./configure --with-libraries=python --with-python=python2.5. I've been trying to get the quickstart builds working--but I am having different issues. If anyone has any suggestions for things to look at or try, I would be very thankful. Best, Matt -------------- next part -------------- An HTML attachment was scrubbed... URL: From t_spens at yahoo.com Fri Jan 18 21:12:52 2008 From: t_spens at yahoo.com (Tim Spens) Date: Fri, 18 Jan 2008 12:12:52 -0800 (PST) Subject: [C++-sig] Problems using wrapper.hpp to override virtual functions Message-ID: <25741.25980.qm@web45112.mail.sp1.yahoo.com> Here is a complete simple example that shows the same error. #include #include #include #include #include using namespace boost::python; // Class with one pure virtual function struct P { virtual ~P(){} virtual char const* f() = 0; char const* g() { return "P::g()"; } }; struct PCallback : P, wrapper

{ char const* f() { return this->get_override("f")(); } }; // Class with one non-pure virtual function struct A { virtual ~A(){} virtual char const* f() { return "A::f()"; } }; struct ACallback : A, wrapper { char const* f() { if (override f = this->get_override("f")) return f(); return A::f(); } char const* default_f() { return this->A::f(); } }; int main() { return 0; } BOOST_PYTHON_MODULE_INIT(callback) { class_("P") .def("f", boost::python::pure_virtual(&P::f)); class_("A") .def("f", &A::f, &ACallback::default_f); } >>> import callback >>> b = callback.P() >>> class derived(b): .... def __init__(self): .... b.__init__(self) .... def f(self): .... return 42 .... Traceback (most recent call last): File "", line 1, in Boost.Python.ArgumentError: Error when calling the metaclass bases Python argument types in P.__init__(P, str, tuple, dict) did not match C++ signature: __init__(_object*) That's it, Tim >can you create small and complete example? >I willtake a look on it. On 1/17/08, Tim Spens wrote: > I've added in the __init__ constructor like you suggested but I'm still > getting the same error? > I also tried adding in a default forwarding function like in > http://www.boost.org/libs/python/doc/tutorial/doc/html/python/exposing.html > I'm using py++ to generate most of my wrapper code, with slight > modifications. > > void default_handle(client_response const & rsp) > { > this->python_client_handler::handle(boost::ref(rsp)); > } > > ..def("handle", &client_handler::handle, > &python_client_handler_wrapper::default_handle) > > Traceback (most recent call last): > File "./test_client.py", line 304, in > class callback(handler): > Boost.Python.ArgumentError: Error when calling the metaclass bases > Python argument types in > python_client_handler.__init__(python_client_handler, str, tuple, > dict) > did not match C++ signature: > __init__(_object*) > > Tim > > ----- Original Message ---- > From: Roman Yakovenko > To: Development of Python/C++ integration > Sent: Thursday, January 17, 2008 2:44:26 AM > Subject: Re: [C++-sig] Problems using wrapper.hpp to override virtual > functions > > > On Jan 17, 2008 1:18 AM, Tim Spens wrote: > > Here is the simplest example of what I'm trying to do, I hope this is > enough the complete > > project to quite large. I'm using wrapper.hpp so that I can use a > python handler to handle > > callbacks from the c++ code. > > > > > > //c++ pure virtual function definition > > virtual void handle(const CLIENT::client_response & rsp) = 0; > > > > //c++ wrapper for virtual function "handle" > > virtual void handle(client_response const & rsp) > > { > > if(override func_handle = this->get_override("handle")){ > > //I cannot get this section of code to run, from > what I understand this would be the callback into python? > > func_handle(boost::ref(rsp)); > > cout << "here handle in python" << endl;} > > else{ > > //I currently have a c++ handler which is called > here, I would like to use the python handler though. > > this->python_handler::handle(boost::ref(rsp)); > > cout << "here handle in c++" << endl;} > > } > > > > BOOST_PYTHON_MODULE(libclientpy) > > { > > class_ bases, boost::noncopyable >("python_client_handler", > init<>()) > > .def("handle", &client_handler::handle); > > } > > > > #PYTHON CODE > > import libclientpy > > handler = libclientpy.python_handler() > > client = libclientpy.client(handler, '127.0.0.1', 7900) > > > > class callback(handler): > > def handle(self): > > print 'handle python' > > > > azc = addZapCallback() > > > > Traceback (most recent call last): > > File "./test_client.py", line 304, in > > class callback(handler): > > Boost.Python.ArgumentError: Error when calling the metaclass bases > > Python argument types in > > python_client_handler.__init__(python_client_handler, str, tuple, > dict) > > did not match C++ signature: > > __init__(_object*) > > > > > > I am unsure what is happening here. I cannot even find a __init__ > function in my python_client_handler that has the said arguments? > __init__(python_client_handler, str, tuple, dict) > > > > Any ideas? > > Small complete example helps. > > you have to call __init__ method of exposed C++ class > > > class callback(handler): > def __init__( self ): > handler.__init__( self ) > def handle(self): > print 'handle python' > > HTH > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > > > > > > ____________________________________________________________________________________ > Be a better friend, newshound, and > know-it-all with Yahoo! Mobile. Try it now. > http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ > > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs From roman.yakovenko at gmail.com Fri Jan 18 21:21:10 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 18 Jan 2008 22:21:10 +0200 Subject: [C++-sig] Problems using wrapper.hpp to override virtual functions In-Reply-To: <25741.25980.qm@web45112.mail.sp1.yahoo.com> References: <25741.25980.qm@web45112.mail.sp1.yahoo.com> Message-ID: <7465b6170801181221w4b02005cv43e27869dbb35234@mail.gmail.com> On Jan 18, 2008 10:12 PM, Tim Spens wrote: > Here is a complete simple example that shows the same error. > > #include > #include > #include > #include > #include > using namespace boost::python; > > // Class with one pure virtual function > struct P { > virtual ~P(){} > virtual char const* f() = 0; > char const* g() { return "P::g()"; } > }; > > struct PCallback : P, wrapper

{ > char const* f() { > return this->get_override("f")(); > } > }; > > // Class with one non-pure virtual function > struct A { > virtual ~A(){} > virtual char const* f() { return "A::f()"; } > }; > > struct ACallback : A, wrapper { > char const* f() { > if (override f = this->get_override("f")) > return f(); > return A::f(); > } > char const* default_f() { return this->A::f(); } > }; > > int main() { return 0; } > > BOOST_PYTHON_MODULE_INIT(callback) { > class_("P") > .def("f", boost::python::pure_virtual(&P::f)); > class_("A") > .def("f", &A::f, &ACallback::default_f); > } > > >>> import callback > >>> b = callback.P() > >>> class derived(b): Is this intentional to create Python class that derived from variable? I guess you wanted to write class derived( callback.P ): Am I right? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From senwin30 at gmail.com Fri Jan 18 22:20:13 2008 From: senwin30 at gmail.com (senthil arasu) Date: Fri, 18 Jan 2008 15:20:13 -0600 Subject: [C++-sig] Boost Python-C++ library Integration Message-ID: <1ef9860e0801181320w182ed929j117240fd74c1d5f2@mail.gmail.com> Hi All, Iam new to Boost Python. Currently iam working on Python script support feature for my static C++ library. I have found BoostPython is a suitable framework . Any body can suggest me the integration procedure of c++ static library(.lib) in boost python. I have succesfuly installed boost python & tested helloworld example. In tutorials i could not find details specific to my requirement. Thanks in Advance senthil -------------- next part -------------- An HTML attachment was scrubbed... URL: From t_spens at yahoo.com Fri Jan 18 23:08:26 2008 From: t_spens at yahoo.com (Tim Spens) Date: Fri, 18 Jan 2008 14:08:26 -0800 (PST) Subject: [C++-sig] Problems using wrapper.hpp to override virtual functions Message-ID: <589546.56636.qm@web45114.mail.sp1.yahoo.com> Your right I didn't want to derive the class from the variable (strange that all the examples I've seen have it this way?). I've changed this and I'm no longer getting the error. But when the virtual function is invoked in c++ my overridden python class is still not being called? Thanks for the help, Tim ----- Original Message ---- From: Roman Yakovenko To: Development of Python/C++ integration Sent: Friday, January 18, 2008 1:21:10 PM Subject: Re: [C++-sig] Problems using wrapper.hpp to override virtual functions On Jan 18, 2008 10:12 PM, Tim Spens wrote: > Here is a complete simple example that shows the same error. > > #include > #include > #include > #include > #include > using namespace boost::python; > > // Class with one pure virtual function > struct P { > virtual ~P(){} > virtual char const* f() = 0; > char const* g() { return "P::g()"; } > }; > > struct PCallback : P, wrapper

{ > char const* f() { > return this->get_override("f")(); > } > }; > > // Class with one non-pure virtual function > struct A { > virtual ~A(){} > virtual char const* f() { return "A::f()"; } > }; > > struct ACallback : A, wrapper { > char const* f() { > if (override f = this->get_override("f")) > return f(); > return A::f(); > } > char const* default_f() { return this->A::f(); } > }; > > int main() { return 0; } > > BOOST_PYTHON_MODULE_INIT(callback) { > class_("P") > .def("f", boost::python::pure_virtual(&P::f)); > class_("A") > .def("f", &A::f, &ACallback::default_f); > } > > >>> import callback > >>> b = callback.P() > >>> class derived(b): Is this intentional to create Python class that derived from variable? I guess you wanted to write class derived( callback.P ): Am I right? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs From nat at lindenlab.com Fri Jan 18 22:48:25 2008 From: nat at lindenlab.com (Nat Goodspeed) Date: Fri, 18 Jan 2008 16:48:25 -0500 Subject: [C++-sig] Boost Python-C++ library Integration In-Reply-To: <1ef9860e0801181320w182ed929j117240fd74c1d5f2@mail.gmail.com> References: <1ef9860e0801181320w182ed929j117240fd74c1d5f2@mail.gmail.com> Message-ID: <47911EA9.6090806@lindenlab.com> senthil arasu wrote: > Any body can suggest me the integration procedure of c++ static > library(.lib) in boost python. > I have succesfuly installed boost python & tested helloworld example. > > In tutorials i could not find details specific to my requirement. I'm not sure what you mean by integrating a static library into Python. The simplest meaning would be that you'd like to rebuild the Python interpreter, statically linking your library with it. But I don't think that's what you really want. If the primary use for this library is as a Python extension, then you're better off changing it to a dynamic library so the Python import statement can dynamically load it. If the primary use for this library is to be statically linked with other (e.g.) C++ programs, but you also want to be able to use it as a Python module, then maybe you should introduce a new dynamic library that statically links with your original library and contains the Boost.Python definitions to publish its symbols to Python. Does that make sense? From mariofutire at googlemail.com Fri Jan 18 23:13:10 2008 From: mariofutire at googlemail.com (Andrea) Date: Fri, 18 Jan 2008 22:13:10 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> Message-ID: <47912476.9030309@googlemail.com> john haddon wrote: > > we had similar problems when wrapping a library where we used > boost::intrusive_ptr as our > smart pointer of choice. we managed to work around most of the problems > using the magic > incantations in this file : > > http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/IntrusivePtrPatch.h > > you can see an example of how we use that in this file : > > http://cortex-vfx.googlecode.com/svn/trunk/src/IECore/bindings/ObjectBinding.cpp > > hopefully those might give you some clue as to how to fix your problems > too... > > cheers... > john Thanks for the suggestion. I end up debugging until I found a place where the *magic* boost::shared_ptr was created (out of the blue) and I ended up in the file It is the same file you sent me, and I've seen what you did to change it. So I am going to try it out. Could you please tell me what you meant by > "we managed to work around most of the problems" since it looks like I will be following the same steps as you. Maybe this whole approach should be documented (if it is not too dodgy) Andrea From mariofutire at googlemail.com Fri Jan 18 23:13:10 2008 From: mariofutire at googlemail.com (Andrea) Date: Fri, 18 Jan 2008 22:13:10 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> Message-ID: <47912476.9030309@googlemail.com> john haddon wrote: > > we had similar problems when wrapping a library where we used > boost::intrusive_ptr as our > smart pointer of choice. we managed to work around most of the problems > using the magic > incantations in this file : > > http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/IntrusivePtrPatch.h > > you can see an example of how we use that in this file : > > http://cortex-vfx.googlecode.com/svn/trunk/src/IECore/bindings/ObjectBinding.cpp > > hopefully those might give you some clue as to how to fix your problems > too... > > cheers... > john Thanks for the suggestion. I end up debugging until I found a place where the *magic* boost::shared_ptr was created (out of the blue) and I ended up in the file It is the same file you sent me, and I've seen what you did to change it. So I am going to try it out. Could you please tell me what you meant by > "we managed to work around most of the problems" since it looks like I will be following the same steps as you. Maybe this whole approach should be documented (if it is not too dodgy) Andrea From senwin30 at gmail.com Sat Jan 19 00:00:48 2008 From: senwin30 at gmail.com (senthil arasu) Date: Fri, 18 Jan 2008 17:00:48 -0600 Subject: [C++-sig] Boost Python-C++ library Integration In-Reply-To: <47911EA9.6090806@lindenlab.com> References: <1ef9860e0801181320w182ed929j117240fd74c1d5f2@mail.gmail.com> <47911EA9.6090806@lindenlab.com> Message-ID: <1ef9860e0801181500w2282b990te06be3e6f5c25344@mail.gmail.com> My objective is to implement Python CLI script for my C++ library. Its a Python extension. Other than making DLL , is it possible to make use of static c++ library for python extension. On Jan 18, 2008 3:48 PM, Nat Goodspeed wrote: > senthil arasu wrote: > > > Any body can suggest me the integration procedure of c++ static > > library(.lib) in boost python. > > I have succesfuly installed boost python & tested helloworld example. > > > > In tutorials i could not find details specific to my requirement. > > I'm not sure what you mean by integrating a static library into > Python. The simplest meaning would be that you'd like to rebuild the > Python interpreter, statically linking your library with it. But I don't > think that's what you really want. > If the primary use for this library is as a Python extension, then > you're better off changing it to a dynamic library so the Python import > statement can dynamically load it. > If the primary use for this library is to be statically linked with > other (e.g.) C++ programs, but you also want to be able to use it as a > Python module, then maybe you should introduce a new dynamic library > that statically links with your original library and contains the > Boost.Python definitions to publish its symbols to Python. > Does that make sense? > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nat at lindenlab.com Sat Jan 19 00:10:26 2008 From: nat at lindenlab.com (Nat Goodspeed) Date: Fri, 18 Jan 2008 18:10:26 -0500 Subject: [C++-sig] Boost Python-C++ library Integration In-Reply-To: <1ef9860e0801181500w2282b990te06be3e6f5c25344@mail.gmail.com> References: <1ef9860e0801181320w182ed929j117240fd74c1d5f2@mail.gmail.com> <47911EA9.6090806@lindenlab.com> <1ef9860e0801181500w2282b990te06be3e6f5c25344@mail.gmail.com> Message-ID: <479131E2.8090803@lindenlab.com> senthil arasu wrote: > My objective is to implement Python CLI script for my C++ library. > Its a Python extension. > Other than making DLL , is it possible to make use of static c++ library > for python extension. When a Python script issues 'import somelibrary', the Python interpreter expects to dynamically load something -- whether that's a Python module or a .pyd dynamic extension library. So I think the short answer to your question is no: you can't ask Python to import a static library. But I believe you could reasonably create a dynamic library whose sole purpose is to link with your static library, and to contain the Boost.Python definitions necessary to publish your static library's classes, functions etc. to Python. From t_spens at yahoo.com Sat Jan 19 00:51:09 2008 From: t_spens at yahoo.com (Tim Spens) Date: Fri, 18 Jan 2008 15:51:09 -0800 (PST) Subject: [C++-sig] Problems using wrapper.hpp to override virtual functions Message-ID: <456592.23572.qm@web45113.mail.sp1.yahoo.com> I must be missing something still. I'm trying to get the python overridden functions to be called when the c++ virtual function is invoked. Here is what I have so far... #include #include #include #include #include #include #include using namespace std; using namespace boost::python; // Class with one pure virtual function struct A { virtual ~A(){} virtual char const* f() = 0; char const* g() { return "A::g()"; } }; struct ACallback : A, wrapper { char const* f() { return this->get_override("f")(); } }; // Class with one non-pure virtual function struct B { virtual ~B(){} virtual char const* f() { return "B::f()"; } }; struct BCallback : B, wrapper { char const* f() { override f = this->get_override("f"); if (f) return f(); return B::f(); } char const* default_f() { return this->B::f(); } }; int main() { while(1){ ACallback a; a.f(); // I was expecting that everytime I called a.f() the string "A::g()" would be returned to the python method in the class A_.f() ??? BCallback b; b.f(); // I was expecting that everytime I called b.f() the string "B::f()" would be returned to the python method in the class B_.f() ??? sleep(1); } } BOOST_PYTHON_MODULE_INIT(callback) { class_("A") .def("f", boost::python::pure_virtual(&A::f)); class_("B") .def("f", &B::f, &BCallback::default_f); } #python code import callback class A_(callback.A): def __init__(self): callback.A.__init__(self) def f(self): print 'hello A' acallback = A_() class B_(callback.B): def __init__(self): callback.B.__init__(self) def f(self): print 'hello B' bcallback = B_() #running ../callback Program received signal SIGSEGV, Segmentation fault. [Switching to Thread -1212557632 (LWP 12339)] 0xb7e4f645 in PyErr_Restore () from /usr/lib/libpython2.5.so.1.0 (gdb) bt #0 0xb7e4f645 in PyErr_Restore () from /usr/lib/libpython2.5.so.1.0 #1 0xb7e4f72a in PyErr_SetObject () from /usr/lib/libpython2.5.so.1.0 #2 0xb7e5011d in PyErr_Format () from /usr/lib/libpython2.5.so.1.0 #3 0xb7d74c18 in boost::python::converter::registration::get_class_object () from /usr/local/lib/libboost_python-gcc-mt-1_33_1.so.1.33.1 #4 0x0804f5e3 in boost::python::wrapper::get_override (this=0xbfcebb6c, name=0x8050df2 "f") at /boost/python/wrapper.hpp:27 #5 0x0804f6fe in ACallback::f (this=0xbfcebb68) at callback.cpp:52 #return this->get_override("f")(); #6 0x0804a0b2 in main () at callback.cpp:75 #a.f(); Thanks for the help, Tim ----- Original Message ---- From: Tim Spens To: Development of Python/C++ integration Sent: Friday, January 18, 2008 3:08:26 PM Subject: Re: [C++-sig] Problems using wrapper.hpp to override virtual functions Your right I didn't want to derive the class from the variable (strange that all the examples I've seen have it this way?). I've changed this and I'm no longer getting the error. But when the virtual function is invoked in c++ my overridden python class is still not being called? Thanks for the help, Tim ----- Original Message ---- From: Roman Yakovenko To: Development of Python/C++ integration Sent: Friday, January 18, 2008 1:21:10 PM Subject: Re: [C++-sig] Problems using wrapper.hpp to override virtual functions On Jan 18, 2008 10:12 PM, Tim Spens wrote: > Here is a complete simple example that shows the same error. > > #include > #include > #include > #include > #include > using namespace boost::python; > > // Class with one pure virtual function > struct P { > virtual ~P(){} > virtual char const* f() = 0; > char const* g() { return "P::g()"; } > }; > > struct PCallback : P, wrapper

{ > char const* f() { > return this->get_override("f")(); > } > }; > > // Class with one non-pure virtual function > struct A { > virtual ~A(){} > virtual char const* f() { return "A::f()"; } > }; > > struct ACallback : A, wrapper { > char const* f() { > if (override f = this->get_override("f")) > return f(); > return A::f(); > } > char const* default_f() { return this->A::f(); } > }; > > int main() { return 0; } > > BOOST_PYTHON_MODULE_INIT(callback) { > class_("P") > .def("f", boost::python::pure_virtual(&P::f)); > class_("A") > .def("f", &A::f, &ACallback::default_f); > } > > >>> import callback > >>> b = callback.P() > >>> class derived(b): Is this intentional to create Python class that derived from variable? I guess you wanted to write class derived( callback.P ): Am I right? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig ____________________________________________________________________________________ Never miss a thing. Make Yahoo your home page. http://www.yahoo.com/r/hs From theboyhaddon at hotmail.com Sat Jan 19 02:59:04 2008 From: theboyhaddon at hotmail.com (john haddon) Date: Sat, 19 Jan 2008 01:59:04 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: <47912476.9030309@googlemail.com> References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> Message-ID: > > we had similar problems when wrapping a library where we used > > boost::intrusive_ptr as our > > smart pointer of choice. we managed to work around most of the problems > > using the magic > > incantations in this file : > > > > http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/IntrusivePtrPatch.h > > > > Could you please tell me what you meant by > > > "we managed to work around most of the problems" > > since it looks like I will be following the same steps as you. Our main problem was that when returning an intrusive_ptr from C++ to python, it would create a Python object of type Base, when what we really wanted to do was create a Python object of type Derived, by downcasting to the most derived type. The code I posted deals with that successfully (and I think one other issue I don't recall). We still have the problem that passing the same object to python twice results in two different objects : # x always returns an intrusive pointer to the same c++ object a = x() b = x() # assertion fails - two python objects refer to one c++ object and don't know it assert( a is b ) We are working around this problem in two ways. Firstly we have an ugly isSame() method bound for object to be used instead of "is". Secondly we have another to_python converter for use when we wrap classes. This solves the object identity problem by keeping a mapping from C++ objects to python objects. You can see that here : http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/WrapperToPython.h I suspect the two solutions could be combined into one general solution, but currently it's working just well enough so we've left it there... Hope that helps... Cheers... John _________________________________________________________________ -------------- next part -------------- An HTML attachment was scrubbed... URL: From theboyhaddon at hotmail.com Sat Jan 19 02:59:04 2008 From: theboyhaddon at hotmail.com (john haddon) Date: Sat, 19 Jan 2008 01:59:04 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: <47912476.9030309@googlemail.com> References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> Message-ID: > > we had similar problems when wrapping a library where we used > > boost::intrusive_ptr as our > > smart pointer of choice. we managed to work around most of the problems > > using the magic > > incantations in this file : > > > > http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/IntrusivePtrPatch.h > > > > Could you please tell me what you meant by > > > "we managed to work around most of the problems" > > since it looks like I will be following the same steps as you. Our main problem was that when returning an intrusive_ptr from C++ to python, it would create a Python object of type Base, when what we really wanted to do was create a Python object of type Derived, by downcasting to the most derived type. The code I posted deals with that successfully (and I think one other issue I don't recall). We still have the problem that passing the same object to python twice results in two different objects : # x always returns an intrusive pointer to the same c++ object a = x() b = x() # assertion fails - two python objects refer to one c++ object and don't know it assert( a is b ) We are working around this problem in two ways. Firstly we have an ugly isSame() method bound for object to be used instead of "is". Secondly we have another to_python converter for use when we wrap classes. This solves the object identity problem by keeping a mapping from C++ objects to python objects. You can see that here : http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/WrapperToPython.h I suspect the two solutions could be combined into one general solution, but currently it's working just well enough so we've left it there... Hope that helps... Cheers... John _________________________________________________________________ -------------- next part -------------- An HTML attachment was scrubbed... URL: From theboyhaddon at hotmail.com Sat Jan 19 03:00:31 2008 From: theboyhaddon at hotmail.com (john haddon) Date: Sat, 19 Jan 2008 02:00:31 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: <47912476.9030309@googlemail.com> References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> Message-ID: > > we had similar problems when wrapping a library where we used > > boost::intrusive_ptr as our > > smart pointer of choice. we managed to work around most of the problems > > using the magic > > incantations in this file : > > > > http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/IntrusivePtrPatch.h > > Could you please tell me what you meant by > > > "we managed to work around most of the problems" > > since it looks like I will be following the same steps as you. Our main problem was that when returning an intrusive_ptr from C++ to python, it would create a Python object of type Base, when what we really wanted to do was create a Python object of type Derived, by downcasting to the most derived type. The code I posted deals with that successfully (and I think one other issue I don't recall). We still have the problem that passing the same object to python twice results in two different objects : # x always returns an intrusive pointer to the same c++ object a = x() b = x() # assertion fails - two python objects refer to one c++ object and don't know it assert( a is b ) We are working around this problem in two ways. Firstly we have an ugly isSame() method bound for object to be used instead of "is". Secondly we have another to_python converter for use when we wrap classes. This solves the object identity problem by keeping a mapping from C++ objects to python objects. You can see that here : http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/WrapperToPython.h I suspect the two solutions could be combined into one general solution, but currently it's working just well enough so we've left it there... Hope that helps... Cheers... John _________________________________________________________________ -------------- next part -------------- An HTML attachment was scrubbed... URL: From mariofutire at googlemail.com Sat Jan 19 13:03:17 2008 From: mariofutire at googlemail.com (Andrea) Date: Sat, 19 Jan 2008 12:03:17 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> Message-ID: <4791E705.1080505@googlemail.com> john haddon wrote: > It works... at least in my small proof of concept... > Our main problem was that when returning an intrusive_ptr from C++ > to python, > it would create a Python object of type Base, when what we really wanted > to do was create > a Python object of type Derived, by downcasting to the most derived > type. The code I > posted deals with that successfully (and I think one other issue I don't > recall). We still have > the problem that passing the same object to python twice results in two > different objects : I am not sure I follow here. My hierarchy is base class A B : A C : B then I call a function returning an intrusive_ptr(new C()) into c >>> type(c) So Python can tell that the object is of type C even if it was returned as a pointer to the base class A. Other point: please correct me if I am wrong. I need to register every class I want to be able to cast to (i.e. every class I want to accept as argument). So in that case I need to add both INTRUSIVE_PTR_PATCH(B, B_class); and INTRUSIVE_PTR_PATCH(C, C_class); so that my functions can take intrusive_ptr of type B and C. No need to do it with A. > > We are working around this problem in two ways. Firstly we have an ugly > isSame() method bound for object > to be used instead of "is". Secondly we have another to_python converter > for use when we wrap classes. This > solves the object identity problem by keeping a mapping from C++ objects > to python objects. You can see that > here : > > http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/WrapperToPython.h > > I suspect the two solutions could be combined into one general solution, > but currently it's working just > well enough so we've left it there... Here, first I need to understand the problem, then I pass to your solution. Is it a general problem, or something relative to intrusive_ptr, that would not be there, had you used boost::shared_ptr? Thanks very much. Andrea From mariofutire at googlemail.com Sat Jan 19 13:03:17 2008 From: mariofutire at googlemail.com (Andrea) Date: Sat, 19 Jan 2008 12:03:17 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> Message-ID: <4791E705.1080505@googlemail.com> john haddon wrote: > It works... at least in my small proof of concept... > Our main problem was that when returning an intrusive_ptr from C++ > to python, > it would create a Python object of type Base, when what we really wanted > to do was create > a Python object of type Derived, by downcasting to the most derived > type. The code I > posted deals with that successfully (and I think one other issue I don't > recall). We still have > the problem that passing the same object to python twice results in two > different objects : I am not sure I follow here. My hierarchy is base class A B : A C : B then I call a function returning an intrusive_ptr(new C()) into c >>> type(c) So Python can tell that the object is of type C even if it was returned as a pointer to the base class A. Other point: please correct me if I am wrong. I need to register every class I want to be able to cast to (i.e. every class I want to accept as argument). So in that case I need to add both INTRUSIVE_PTR_PATCH(B, B_class); and INTRUSIVE_PTR_PATCH(C, C_class); so that my functions can take intrusive_ptr of type B and C. No need to do it with A. > > We are working around this problem in two ways. Firstly we have an ugly > isSame() method bound for object > to be used instead of "is". Secondly we have another to_python converter > for use when we wrap classes. This > solves the object identity problem by keeping a mapping from C++ objects > to python objects. You can see that > here : > > http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/WrapperToPython.h > > I suspect the two solutions could be combined into one general solution, > but currently it's working just > well enough so we've left it there... Here, first I need to understand the problem, then I pass to your solution. Is it a general problem, or something relative to intrusive_ptr, that would not be there, had you used boost::shared_ptr? Thanks very much. Andrea From theboyhaddon at hotmail.com Sat Jan 19 21:18:28 2008 From: theboyhaddon at hotmail.com (john haddon) Date: Sat, 19 Jan 2008 20:18:28 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: <4791E705.1080505@googlemail.com> References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> <4791E705.1080505@googlemail.com> Message-ID: > > I am not sure I follow here. > > My hierarchy is > > base class A > B : A > C : B > > then I call a function returning an intrusive_ptr(new C()) into c > > >>> type(c) > > > So Python can tell that the object is of type C even if it was returned as a pointer to the base > class A. > > Other point: please correct me if I am wrong. > I need to register every class I want to be able to cast to (i.e. every class I want to accept as > argument). > So in that case I need to add both > > INTRUSIVE_PTR_PATCH(B, B_class); > and > INTRUSIVE_PTR_PATCH(C, C_class); > > so that my functions can take intrusive_ptr of type B and C. > No need to do it with A. correct - that should be all you need to solve the downcasting problem. everything else i mentioned relates to other problems you might see later - i've expanded on those a bit below. >> >> We are working around this problem in two ways. Firstly we have an ugly >> isSame() method bound for object >> to be used instead of "is". Secondly we have another to_python converter >> for use when we wrap classes. This >> solves the object identity problem by keeping a mapping from C++ objects >> to python objects. You can see that >> here : >> >> http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/WrapperToPython.h >> >> I suspect the two solutions could be combined into one general solution, >> but currently it's working just >> well enough so we've left it there... > > Here, first I need to understand the problem, then I pass to your solution. > Is it a general problem, or something relative to intrusive_ptr, that would not be there, had you > used boost::shared_ptr? the problem is this - say you bind this function : intrusive_ptr x( intrusive_ptr b ) { return b; } then this python code fails : b = B() b2 = x( b ) assert( b is b2 ) # fails although both b and b2 correctly refer to the same c++ object, they refer to two different python instances - what x should really do is return the same PyObject that was being used to hold b in the first place. i believe there's some magic in boost::python that solves this problem as long as you're using shared_ptr (although you might want to test that), but the patch i posted for the downcasting of intrusive_ptr doesn't address it... we've addressed the issue only for classes we wrap during binding - as we can easily store a mapping between the c++ pointer and the PyObject * and use that in our to_python function...this is what you see in this file : http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/WrapperToPython.h that file also refers to the WrapperGarbageCollector class we use in the same project. we found there were memory management issues using the boost::wrapper mechanism - i don't recall the specifics but it was either that wrapped objects would never be garbage collected, or that the python half of the wrapped object could die before the c++ half. to tie the lifetimes of the two halves of the objects together they each must have a reference to the other, but that results in a circular reference and hence no collection - WrapperGarbageCollector periodically trawls through the wrapped instances and breaks any such cycles when necessary. depending on whether or not you use wrapping you may want to incorporate something like that too... cheers... john _________________________________________________________________ From theboyhaddon at hotmail.com Sat Jan 19 22:36:45 2008 From: theboyhaddon at hotmail.com (john haddon) Date: Sat, 19 Jan 2008 21:36:45 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> <4791E705.1080505@googlemail.com> Message-ID: >> Other point: please correct me if I am wrong. >> I need to register every class I want to be able to cast to (i.e. every class I want to accept as >> argument). >> So in that case I need to add both >> >> INTRUSIVE_PTR_PATCH(B, B_class); >> and >> INTRUSIVE_PTR_PATCH(C, C_class); >> >> so that my functions can take intrusive_ptr of type B and C. >> No need to do it with A. Taking a look at our bindings again, I think it would be wise to do the same for A too. Sorry if I'm a little vague on this subject - it was a while ago and I didn't actually write all the code... _________________________________________________________________ From mariofutire at googlemail.com Sat Jan 19 22:55:42 2008 From: mariofutire at googlemail.com (Andrea) Date: Sat, 19 Jan 2008 21:55:42 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> <4791E705.1080505@googlemail.com> Message-ID: <479271DE.9000604@googlemail.com> john haddon wrote: >>> Other point: please correct me if I am wrong. >>> I need to register every class I want to be able to cast to (i.e. every class I want to accept as >>> argument). >>> So in that case I need to add both >>> >>> INTRUSIVE_PTR_PATCH(B, B_class); >>> and >>> INTRUSIVE_PTR_PATCH(C, C_class); >>> >>> so that my functions can take intrusive_ptr of type B and C. >>> No need to do it with A. > > Taking a look at our bindings again, I think it would be wise to do the same for A too. Sorry if > I'm a little vague on this subject - it was a while ago and I didn't actually write all the code... Yes, you are right. It is needed when I have a pointer to a derived class and I need to pass is as base class. It seems that I have to pair INTRUSIVE_PTR_PATCH with each class_ declaration. It would be nice if it could be added to class_ so it is done automatically. Thanks From mariofutire at googlemail.com Sat Jan 19 22:55:42 2008 From: mariofutire at googlemail.com (Andrea) Date: Sat, 19 Jan 2008 21:55:42 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> <4791E705.1080505@googlemail.com> Message-ID: <479271DE.9000604@googlemail.com> john haddon wrote: >>> Other point: please correct me if I am wrong. >>> I need to register every class I want to be able to cast to (i.e. every class I want to accept as >>> argument). >>> So in that case I need to add both >>> >>> INTRUSIVE_PTR_PATCH(B, B_class); >>> and >>> INTRUSIVE_PTR_PATCH(C, C_class); >>> >>> so that my functions can take intrusive_ptr of type B and C. >>> No need to do it with A. > > Taking a look at our bindings again, I think it would be wise to do the same for A too. Sorry if > I'm a little vague on this subject - it was a while ago and I didn't actually write all the code... Yes, you are right. It is needed when I have a pointer to a derived class and I need to pass is as base class. It seems that I have to pair INTRUSIVE_PTR_PATCH with each class_ declaration. It would be nice if it could be added to class_ so it is done automatically. Thanks From rogeeff at gmail.com Mon Jan 21 16:38:54 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Mon, 21 Jan 2008 10:38:54 -0500 Subject: [C++-sig] python->c++ ->python Message-ID: Hi, I've got a bit wierd usage scenario. My C++ library A embeds python. My library A can be loaded as a service using LoadLibrary/dlopen. My library A can be unloaded using FreeLibrary. Now there is another library B which is python extension. Library wants to use my library A on demand. For varios reasons it also wishes to unload my library A when it's done. So here is an order of events: python script test.py is started pythons extension library B loaded. Library B loads library A. Library B unloads library A. Library B loads library A again. ... In theory this load/unload can happend any number of times. But here is the problem: it crashes for me. I've tried to pla with it for a bit. My latest test eliminates any logic from my library A, but following lines: boost::python::dict d; PyInitialize(); PyRun_String( "import sys\n", Py_file_input, d.ptr(), d.ptr() ); First time it works fine. Second time around it report an error like this: "__import__ is unknown" Any advice? Thanks, Gennadiy From gjcarneiro at gmail.com Mon Jan 21 16:47:42 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Mon, 21 Jan 2008 15:47:42 +0000 Subject: [C++-sig] python->c++ ->python In-Reply-To: References: Message-ID: On 21/01/2008, Gennadiy Rozental wrote: > > Hi, > > I've got a bit wierd usage scenario. > > My C++ library A embeds python. > My library A can be loaded as a service using LoadLibrary/dlopen. > My library A can be unloaded using FreeLibrary. > > Now there is another library B which is python extension. Library wants to > use my library A on demand. For varios reasons it also wishes to unload my > library A when it's done. > > So here is an order of events: > > python script test.py is started > pythons extension library B loaded. > Library B loads library A. > Library B unloads library A. > Library B loads library A again. > ... > > In theory this load/unload can happend any number of times. But here is > the > problem: it crashes for me. I've tried to pla with it for a bit. My latest > test eliminates any logic from my library A, but following lines: > > boost::python::dict d; > PyInitialize(); > PyRun_String( "import sys\n", Py_file_input, d.ptr(), d.ptr() ); First time it works fine. Second time around it report an error like this: > "__import__ is unknown" > > Any advice? Perhaps you are calling PyInitialize(); multiple times? That would be a mistake... -- 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: From rogeeff at gmail.com Mon Jan 21 17:05:51 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Mon, 21 Jan 2008 11:05:51 -0500 Subject: [C++-sig] python->c++ ->python References: Message-ID: For one thing I don;t beleive callig n PyInitializa multiple time isa proble. But in fact I;ve tried this test both with and without PyInitialize() and with If(!Py_IsInitialized() ) PyInitialize(); result is the same. gennadiy "Gustavo Carneiro" wrote in message news:a467ca4f0801210747l5af8c522s4240c256461e0ef at mail.gmail.com... On 21/01/2008, Gennadiy Rozental wrote: Hi, I've got a bit wierd usage scenario. My C++ library A embeds python. My library A can be loaded as a service using LoadLibrary/dlopen. My library A can be unloaded using FreeLibrary. Now there is another library B which is python extension. Library wants to use my library A on demand. For varios reasons it also wishes to unload my library A when it's done. So here is an order of events: python script test.py is started pythons extension library B loaded. Library B loads library A. Library B unloads library A. Library B loads library A again. ... In theory this load/unload can happend any number of times. But here is the problem: it crashes for me. I've tried to pla with it for a bit. My latest test eliminates any logic from my library A, but following lines: boost::python::dict d; PyInitialize(); PyRun_String( "import sys\n", Py_file_input, d.ptr(), d.ptr() ); First time it works fine. Second time around it report an error like this: "__import__ is unknown" Any advice? Perhaps you are calling PyInitialize(); multiple times? That would be a mistake... -------------- next part -------------- An HTML attachment was scrubbed... URL: From progettodex at gmail.com Mon Jan 21 17:13:09 2008 From: progettodex at gmail.com (progetto dex) Date: Mon, 21 Jan 2008 17:13:09 +0100 Subject: [C++-sig] Garbage Collector and C++ objects lifetime Message-ID: <54f22e040801210813g1f3fba01s5ac7569c8871b94b@mail.gmail.com> Hi, I'm using boost.python to expose a C++ API to Python, and I'm facing a problem concerning the C++ object lifetime and python's garbage collector. The C++ API is based on a Session object that abstracts a connection towards a RDBMS, and let the client access a Documents object, which in turn exposes a DOM inspired Document object list, made up of a variable number of Element objects. Documents is owned by Session, and returns pointers to Document objects. Elements are implemented as custom smart pointers (ref-count idiom). I've successfully wrapped all this stuff, but a problem raises when I instantiate some Elements from Python: the program crashes on exit. The reason is that every Element has an internal reference to the Session (well, to an abstract Observer that happens to be the Session), and Python releases the Session object before the Element. Instantiating a static Session on the C++ side, and using that object instead of the Python's one, every thing works fine. Can I tell Python that a custodian/ward relationship exists between these two objects, given the fact that no one knows nothing about the other (except from Element expecting an abstract Observer)? Is there any other construct/technique that I could adopt to solve this problem? Regards, Gianfranco -------------- next part -------------- An HTML attachment was scrubbed... URL: From gjcarneiro at gmail.com Mon Jan 21 17:25:24 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Mon, 21 Jan 2008 16:25:24 +0000 Subject: [C++-sig] python->c++ ->python In-Reply-To: References: Message-ID: On 21/01/2008, Gennadiy Rozental wrote: > > For one thing I don;t beleive callig n PyInitializa multiple time isa > proble. But in fact I;ve tried this test both with and without > PyInitialize() and with > > If(!Py_IsInitialized() ) > PyInitialize(); > If the library that loads Python has to be unloaded, then maybe you should call Py_Finalize() when the library is unloaded. Or generally just fire a debugger and figure out where/why it crashes... result is the same. > > gennadiy > > "Gustavo Carneiro" wrote in message > news:a467ca4f0801210747l5af8c522s4240c256461e0ef at mail.gmail.com... > On 21/01/2008, Gennadiy Rozental wrote: > > > > Hi, > > > > I've got a bit wierd usage scenario. > > > > My C++ library A embeds python. > > My library A can be loaded as a service using LoadLibrary/dlopen. > > My library A can be unloaded using FreeLibrary. > > > > Now there is another library B which is python extension. Library wants > > to > > use my library A on demand. For varios reasons it also wishes to unload > > my > > library A when it's done. > > > > So here is an order of events: > > > > python script test.py is started > > pythons extension library B loaded. > > Library B loads library A. > > Library B unloads library A. > > Library B loads library A again. > > ... > > > > In theory this load/unload can happend any number of times. But here is > > the > > problem: it crashes for me. I've tried to pla with it for a bit. My > > latest > > test eliminates any logic from my library A, but following lines: > > > > boost::python::dict d; > > PyInitialize(); > > PyRun_String( "import sys\n", Py_file_input, d.ptr(), d.ptr() ); > > > > First time it works fine. Second time around it report an error like this: > > "__import__ is unknown" > > > > Any advice? > > > Perhaps you are calling PyInitialize(); multiple times? That would be a > mistake... > > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > > -- 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: From rogeeff at gmail.com Mon Jan 21 17:42:59 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Mon, 21 Jan 2008 11:42:59 -0500 Subject: [C++-sig] python->c++ ->python References: Message-ID: My library does both Initialize and Finalize in regular circumstances. When invoked from Python I disable both based on Py_IsInitialized() test. Since neither needs to be done in this case, right? My library crashes somewhere during unloading second time in depth of system calls (stack is composed completely of python dll and system libs, no my library at all. But as I said in minimal example it doesn't crash it just throw an exception indicating interpreter is corrupted. Gennadiy "Gustavo Carneiro" wrote in message news:a467ca4f0801210825p6c070dabr282a9300a4b6dd2d at mail.gmail.com... On 21/01/2008, Gennadiy Rozental wrote: For one thing I don't believe calling PyInitialize multiple time is a problem. But in fact I've tried this test both with and without PyInitialize() and with If(!Py_IsInitialized() ) PyInitialize(); If the library that loads Python has to be unloaded, then maybe you should call Py_Finalize() when the library is unloaded. Or generally just fire a debugger and figure out where/why it crashes... result is the same. Gennady "Gustavo Carneiro" wrote in message news:a467ca4f0801210747l5af8c522s4240c256461e0ef at mail.gmail.com ... On 21/01/2008, Gennadiy Rozental wrote: Hi, I've got a bit weird usage scenario. My C++ library A embeds python. My library A can be loaded as a service using LoadLibrary/dlopen. My library A can be unloaded using FreeLibrary. Now there is another library B which is python extension. Library wants to use my library A on demand. For various reasons it also wishes to unload my library A when it's done. So here is an order of events: python script test.py is started pythons extension library B loaded. Library B loads library A. Library B unloads library A. Library B loads library A again. ... In theory this load/unload can happened any number of times. But here is the problem: it crashes for me. I've tried to play with it for a bit. My latest test eliminates any logic from my library A, but following lines: boost::python::dict d; PyInitialize(); PyRun_String( "import sys\n", Py_file_input, d.ptr(), d.ptr() ); First time it works fine. Second time around it report an error like this: "__import__ is unknown" Any advice? Perhaps you are calling PyInitialize(); multiple times? That would be a mistake... _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig -- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert ------------------------------------------------------------------------------ _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Mon Jan 21 19:52:06 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 21 Jan 2008 20:52:06 +0200 Subject: [C++-sig] Garbage Collector and C++ objects lifetime In-Reply-To: <54f22e040801210813g1f3fba01s5ac7569c8871b94b@mail.gmail.com> References: <54f22e040801210813g1f3fba01s5ac7569c8871b94b@mail.gmail.com> Message-ID: <7465b6170801211052h52f93fe6qe5e0f91871f1ecc9@mail.gmail.com> 2008/1/21 progetto dex : > Hi, > I'm using boost.python to expose a C++ API to Python, and I'm facing a > problem concerning the C++ object lifetime and python's garbage collector. > The C++ API is based on a Session object that abstracts a connection towards > a RDBMS, and let the client access a Documents object, which in turn exposes > a DOM inspired Document object list, made up of a variable number of Element > objects. Documents is owned by Session, and returns pointers to Document > objects. Elements are implemented as custom smart pointers (ref-count > idiom). I've successfully wrapped all this stuff, but a problem raises when > I instantiate some Elements from Python: the program crashes on exit. The > reason is that every Element has an internal reference to the Session (well, > to an abstract Observer that happens to be the Session), and Python releases > the Session object before the Element. Instantiating a static Session on the > C++ side, and using that object instead of the Python's one, every thing > works fine. Can I tell Python that a custodian/ward relationship exists > between these two objects, given the fact that no one knows nothing about > the other (except from Element expecting an abstract Observer)? Is there any > other construct/technique that I could adopt to solve this problem? If you instantiate Elements from Python, than you can store on an element instance reference to the Session, thus preventing Python to distract the it earlier. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From Chang_Chen at sonic.com Mon Jan 21 22:23:23 2008 From: Chang_Chen at sonic.com (Chang Chen) Date: Tue, 22 Jan 2008 05:23:23 +0800 Subject: [C++-sig] reducer class member funciton Message-ID: <51C471E6CBEC8148A08E065690F4DBDF01C106D5@SHAEXC01.ent.sonic.com> Hi all I asked a question about wrapping out parameter for python access, and Roman Yakovenko gave me a perfect solution(http://mail.python.org/pipermail/c++-sig/2007-October/012992.ht ml) . But I do not know how to integrate class_ :: def() with his solution since lack of Meta programming. Any suggestion? Thanks -Chang -------------- next part -------------- An HTML attachment was scrubbed... URL: From mattjomeara at gmail.com Tue Jan 22 18:38:16 2008 From: mattjomeara at gmail.com (Matthew O'Meara) Date: Tue, 22 Jan 2008 12:38:16 -0500 Subject: [C++-sig] trouble with boost.python build (blame boost-1_34_1/boost/function/function_template.hpp?) In-Reply-To: References: Message-ID: To whom it may concern, The solution was to put the line #include wrote: > Hi all, > > > > > I've got a complicated boost.python build and I'm running into an error > that I can't figure out. I've tried to simplify it as much as I can while > still reproducing the same error. > > > For reasons outside of my control I am trying to build using g++ as > follows. > > > in /path/to/source.cc I have: > > #include "boost/python.hpp" > > > > running: > > $g++ -v -I/users/momeara/svn/mini_19621/src > -I/usr/local/include/boost-1_34_1 -I/usr/local/include/python2.5 -o > /path/to/target.o /path/to/source.cc > > gives: > > Using built-in specs. > Target: i386-redhat-linux > Configured with: ../configure --prefix=/usr --mandir=/usr/share/man > --infodir=/usr/share/info --enable-shared --enable-threads=posix > --enable-checking=relea\ > se --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions > --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk > --di\ > sable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java- > 1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile > --enable-java-maintainer-mode --with-ecj-jar=/\ > usr/share/java/eclipse-ecj.jar --with-cpu=generic --host=i386-redhat-linux > Thread model: posix > gcc version 4.1.2 20070925 (Red Hat 4.1.2-27) > /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1plus -quiet -v > -I/users/momeara/svn/mini_19621/src -I/usr/local/include/boost-1_34_1 > -I/usr/local/include/pytho\ > n2.5 -D_GNU_SOURCE /users/momeara/svn/_utility__by_hand.cc -quiet > -dumpbase _utility__by_hand.cc -mtune=generic -auxbase _utility__by_hand > -version -o /tmp/\ > ccWhDU3J.s > ignoring nonexistent directory > "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include" > #include "..." search starts here: > #include <...> search starts here: > /users/momeara/svn/mini_19621/src > /usr/local/include/boost-1_34_1 > /usr/local/include/python2.5 > /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2 > /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/i386-redhat-linux > > > /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/backward > /usr/local/include > /usr/lib/gcc/i386-redhat-linux/4.1.2/include > /usr/include > End of search list. > GNU C++ version 4.1.2 20070925 (Red Hat 4.1.2-27) (i386-redhat-linux) > compiled by GNU C version 4.1.2 20070925 (Red Hat 4.1.2-27). > GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 > Compiler executable checksum: 47ecd5b49be3842ab15019d13f95ada6 > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:242: > error: 'is_void' was not declared in this scope > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:242: > error: '::value' has not been declared > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:253: > error: template argument 1 is invalid > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:253: > error: typedef name may not be a nested-name-specifier > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:253: > error: expected ';' before 'type' > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:263: > error: 'is_void' was not declared in this scope > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:263: > error: '::value' has not been declared > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:274: > error: template argument 1 is invalid > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:274: > error: typedef name may not be a nested-name-specifier > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:274: > error: expected ';' before 'type' > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:284: > error: 'is_void' was not declared in this scope > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:284: > error: '::value' has not been declared > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:295: > error: template argument 1 is invalid > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:295: > error: typedef name may not be a nested-name-specifier > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:295: > error: expected ';' before 'type' > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp: In > instantiation of > 'boost::detail::function::get_invoker0 nction_obj_ref_tag>::apply, void, > std::allocator >': > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:783: > instantiated from 'void boost::function0::assign_to(const > Functor&\ > ) [with Functor = boost::reference_wrapper, R = void, > Allocator = std::allocator]' > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:624: > instantiated from 'boost::function0::function0(Functor, > typename b\ > oost::enable_if_c::value>::value, > int>::type) [with Functor = boost::reference_wrapper, \ > R = void, Allocator = std::allocator]' > /usr/local/include/boost-1_34_1/boost/python/errors.hpp:29: instantiated > from 'bool boost::python::handle_exception(T) [with T = void (*)()]' > /usr/local/include/boost-1_34_1/boost/python/errors.hpp:36: instantiated > from here > /usr/local/include/boost-1_34_1/boost/function/function_template.hpp:410: > error: no type named 'type' in 'struct > boost::detail::function::get_function_ref_i\ > nvoker0' > > > > > > I have seen seen posts ( > http://archives.free.net.ph/message/20071221.134700.e0c7e2b6.pl.html) > claiming that there is a bug in function_template that causes it to be > non-thread-safe. I tried the suggested patch--replacing > function_template.hpp and function_base.hpp with the most current version > from boost svn and it returns the same error. > > > For reference I downloaded the July 2007 boost 1.34.1 source from > sourceforge and built using ./configure --with-libraries=python > --with-python=python2.5. > > > I've been trying to get the quickstart builds working--but I am having > different issues. > > > If anyone has any suggestions for things to look at or try, I would be > very thankful. > > Best, > Matt > > > > > > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boost-consulting.com Wed Jan 23 00:59:38 2008 From: dave at boost-consulting.com (David Abrahams) Date: Tue, 22 Jan 2008 18:59:38 -0500 Subject: [C++-sig] Fw: Problems using wrapper.hpp to override virtual functions In-Reply-To: <894865.38262.qm@web45107.mail.sp1.yahoo.com> (Tim Spens's message of "Tue, 22 Jan 2008 12:58:28 -0800 (PST)") References: <894865.38262.qm@web45107.mail.sp1.yahoo.com> Message-ID: <87bq7dpg2d.fsf@gutsy.luannocracy.com> on Tue Jan 22 2008, Tim Spens wrote: > Sorry to forward this to you directly but I've noticed you seem to know the most about boost python > and I haven't received any responses for a while now. > All I want to accomplish is have c++ call a python function that is > defined in a .py script. There are several examples of that available in the libs/python/test directory. > If you have a minute please look at my issue. Your code doesn't look like it's trying to do anything like what you say you're trying to accomplish above. The following does that: // get the python module boost::python::object module = boost::python::import(modulename) // get the function from the module boost::python::object fn = module.attr(functionname) // call the function fn(3, 4) > Thanks, > Tim > > ----- Forwarded Message ---- > From: Tim Spens > To: Development of Python/C++ integration > Sent: Friday, January 18, 2008 4:51:09 PM > Subject: Re: [C++-sig] Problems using wrapper.hpp to override virtual functions > > > I must be missing something still. > I'm trying to get the python overridden functions to be called when the > c++ virtual function is invoked. > Here is what I have so far... > > // Class with one pure virtual function > struct A { > virtual ~A(){} > virtual char const* f() = 0; > char const* g() { return "A::g()"; } > }; > > struct ACallback : A, wrapper { > char const* f() { > return this->get_override("f")(); > } > }; > > int main() { > while(1){ > ACallback a; Never do that. class ACallback is purely for the use of the Boost.Python interface. You shouldn't be creating them. -- Dave Abrahams Boost Consulting http://boost-consulting.com From dave at boost-consulting.com Wed Jan 23 20:10:03 2008 From: dave at boost-consulting.com (David Abrahams) Date: Wed, 23 Jan 2008 14:10:03 -0500 Subject: [C++-sig] Fw: Problems using wrapper.hpp to override virtual functions In-Reply-To: <474136.27264.qm@web45104.mail.sp1.yahoo.com> (Tim Spens's message of "Wed, 23 Jan 2008 07:26:16 -0800 (PST)") References: <474136.27264.qm@web45104.mail.sp1.yahoo.com> Message-ID: <8763xk5pf8.fsf@gutsy.luannocracy.com> on Wed Jan 23 2008, Tim Spens wrote: > That's more of what I was looking for, but I'm using boost 1.33.1 for > reasons not under my control. It looks like import.hpp is only in > 1.34? It's just a wrapper around PyImport_AddModule or something; take a look at the source. > I looked at the embedding.cpp example in libs/python/test which > allows for python to be executed in c++ code. The problem is that I > need to have the c++ code call a function that is in a already running > python script. Why is that a problem? You can just import the module and call the function, right? > Basically there are callbacks occurring in the c++ > code. I need to notify a running python client of these callbacks. > After looking at what you suggested and what I saw in the > embedding.cpp test I'm not sure if either of these will do what I > need? > > Thanks, Tim > > ----- Original Message ---- From: David Abrahams > To: c++-sig at python.org Cc: Tim Spens > Sent: Tuesday, January 22, 2008 4:59:38 PM > Subject: Re: Fw: [C++-sig] Problems using wrapper.hpp to override > virtual functions > > > > on Tue Jan 22 2008, Tim Spens wrote: > >> Sorry to forward this to you directly but I've noticed you seem to > know the most about boost python and I haven't received any responses >> for a while now. All I want to accomplish is have c++ call a python >> function that is defined in a .py script. > > There are several examples of that available in the libs/python/test > directory. > >> If you have a minute please look at my issue. > > Your code doesn't look like it's trying to do anything like what you > say you're trying to accomplish above. The following does that: > > // get the python module boost::python::object module = > boost::python::import(modulename) // get the function from the module > boost::python::object fn = module.attr(functionname) // call the > function fn(3, 4) > >> Thanks, Tim >> ----- Forwarded Message ---- From: Tim Spens To: >> Development of Python/C++ integration Sent: >> Friday, January 18, 2008 4:51:09 PM Subject: Re: [C++-sig] Problems >> using wrapper.hpp to override virtual > functions >> >> I must be missing something still. I'm trying to get the python >> overridden functions to be called when > the c++ virtual function is invoked. Here is what I have so far... >> > > > >> // Class with one pure virtual function struct A { virtual ~A(){} >> virtual char const* f() = 0; char const* g() { return "A::g()"; } >> }; >> struct ACallback : A, wrapper { char const* f() { return >> this->get_override("f")(); } >> }; >> > > > >> int main() { while(1){ ACallback a; > > Never do that. class ACallback is purely for the use of the > Boost.Python interface. You shouldn't be creating them. > > > -- > Dave Abrahams > Boost Consulting > http://boost-consulting.com > > > > > > ____________________________________________________________________________________ > Looking for last minute shopping deals? > Find them fast with Yahoo! Search. http://tools.search.yahoo.com/newsearch/category.php?category=shopping > -- Dave Abrahams Boost Consulting http://boost-consulting.com From hans_meine at gmx.net Wed Jan 23 20:52:18 2008 From: hans_meine at gmx.net (Hans Meine) Date: Wed, 23 Jan 2008 20:52:18 +0100 Subject: [C++-sig] Fw: Problems using wrapper.hpp to override virtual functions In-Reply-To: <8763xk5pf8.fsf@gutsy.luannocracy.com> References: <474136.27264.qm@web45104.mail.sp1.yahoo.com> <8763xk5pf8.fsf@gutsy.luannocracy.com> Message-ID: <200801232052.23738.hans_meine@gmx.net> On Mittwoch 23 Januar 2008, David Abrahams wrote: > > I looked at the embedding.cpp example in libs/python/test which > > allows for python to be executed in c++ code. The problem is that I > > need to have the c++ code call a function that is in a already running > > python script. > > Why is that a problem? You can just import the module and call the > function, right? > > > Basically there are callbacks occurring in the c++ > > code. I need to notify a running python client of these callbacks. You can also export a C++ function that takes a bp::object parameter and pass a python function to C++ code (remember: functions are 1st class citizens in [boost::]python), if you don't have a module that you can easily import. Ciao, / / .o. /--/ ..o / / ANS ooo -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: This is a digitally signed message part. URL: From mariofutire at googlemail.com Wed Jan 23 21:28:56 2008 From: mariofutire at googlemail.com (Andrea) Date: Wed, 23 Jan 2008 20:28:56 +0000 Subject: [C++-sig] Best way to register shared_ptr Message-ID: <4797A388.2060602@googlemail.com> Hi, I've run into the problem of registering a shared_ptr. I've found a workaround using a const_cast in get_pointer. I've also read that there exist a patch to implement a proper fix. Unfortunately I cannot find more details on that. Could someone please quick list pros and cons of both approaches. I am a bit uncomfortable about const_casting, but I cannot understand if it is just a way to please the compiler or something deeper. Andrea From mariofutire at googlemail.com Wed Jan 23 21:28:56 2008 From: mariofutire at googlemail.com (Andrea) Date: Wed, 23 Jan 2008 20:28:56 +0000 Subject: [C++-sig] Best way to register shared_ptr Message-ID: <4797A388.2060602@googlemail.com> Hi, I've run into the problem of registering a shared_ptr. I've found a workaround using a const_cast in get_pointer. I've also read that there exist a patch to implement a proper fix. Unfortunately I cannot find more details on that. Could someone please quick list pros and cons of both approaches. I am a bit uncomfortable about const_casting, but I cannot understand if it is just a way to please the compiler or something deeper. Andrea From mariofutire at googlemail.com Wed Jan 23 21:34:36 2008 From: mariofutire at googlemail.com (Andrea) Date: Wed, 23 Jan 2008 20:34:36 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> <4791E705.1080505@googlemail.com> Message-ID: <4797A4DC.8010604@googlemail.com> john haddon wrote: > > the problem is this - say you bind this function : > > intrusive_ptr x( intrusive_ptr b ) { return b; } > > then this python code fails : > > b = B() > b2 = x( b ) > assert( b is b2 ) # fails > > although both b and b2 correctly refer to the same c++ object, they refer to two different python > instances - what x should really do is return the same PyObject that was being used to hold b in the > first place. i believe there's some magic in boost::python that solves this problem as long as you're > using shared_ptr (although you might want to test that), but the patch i posted for the downcasting > of intrusive_ptr doesn't address it... Yes, boost::shared_ptr<> achieve this >>> b=bo.newB() >>> c=bo.sameB(b) >>> b >>> c >>> b is c True but any other pointer fails. It looks that there is some special handling of boost::shared_ptr<> when they are returned to python. Has anybody found where this happens. Maybe the fix is as easy allowing the downcasting of custom pointers... I've tried to follow the function calls, but I got lost after a few layer of templates... Andrea From mariofutire at googlemail.com Wed Jan 23 21:34:36 2008 From: mariofutire at googlemail.com (Andrea) Date: Wed, 23 Jan 2008 20:34:36 +0000 Subject: [C++-sig] Custom smart pointer with same behaviour as shared_ptr In-Reply-To: References: <87tzvrrfri.fsf@valverde.peloton> <478FC006.6000602@googlemail.com> <47912476.9030309@googlemail.com> <4791E705.1080505@googlemail.com> Message-ID: <4797A4DC.8010604@googlemail.com> john haddon wrote: > > the problem is this - say you bind this function : > > intrusive_ptr x( intrusive_ptr b ) { return b; } > > then this python code fails : > > b = B() > b2 = x( b ) > assert( b is b2 ) # fails > > although both b and b2 correctly refer to the same c++ object, they refer to two different python > instances - what x should really do is return the same PyObject that was being used to hold b in the > first place. i believe there's some magic in boost::python that solves this problem as long as you're > using shared_ptr (although you might want to test that), but the patch i posted for the downcasting > of intrusive_ptr doesn't address it... Yes, boost::shared_ptr<> achieve this >>> b=bo.newB() >>> c=bo.sameB(b) >>> b >>> c >>> b is c True but any other pointer fails. It looks that there is some special handling of boost::shared_ptr<> when they are returned to python. Has anybody found where this happens. Maybe the fix is as easy allowing the downcasting of custom pointers... I've tried to follow the function calls, but I got lost after a few layer of templates... Andrea From seefeld at sympatico.ca Thu Jan 24 18:35:51 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 24 Jan 2008 12:35:51 -0500 Subject: [C++-sig] HOWTO: ...create shallow boost::python::object copies ? Message-ID: <4798CC77.3040506@sympatico.ca> I'm accessing python objects from within C++, using the convenient bpl::object, bpl::list, etc. types. However, I just now realize that their constructors all (?) create new Python objects. How can I prevent that ? Imagine this python code: d = o.__dict__ d['new_member'] = 42 print o.new_member which I would like to write in C++: bpl::dict d = o.attr("__dict__") d["new_member"] = 42 However, as 'd' is a copy of o's __dict__ attribute, the new_member never makes it into o. How can I do what I want ? (Yes, in this case I obviously wouldn't need the indirection over 'd'; this is just a minimal test case...) Thanks, Stefan -- ...ich hab' noch einen Koffer in Berlin... From rwgk at yahoo.com Thu Jan 24 19:46:11 2008 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Thu, 24 Jan 2008 10:46:11 -0800 (PST) Subject: [C++-sig] HOWTO: ...create shallow boost::python::object copies ? Message-ID: <924600.7133.qm@web31108.mail.mud.yahoo.com> Hi Stefan, look for "astute" on this page: http://www.boost.org/libs/python/doc/tutorial/doc/html/python/object.html Ralf ----- Original Message ---- From: Stefan Seefeld To: c++-sig at python.org Sent: Thursday, January 24, 2008 9:35:51 AM Subject: [C++-sig] HOWTO: ...create shallow boost::python::object copies ? I'm accessing python objects from within C++, using the convenient bpl::object, bpl::list, etc. types. However, I just now realize that their constructors all (?) create new Python objects. How can I prevent that ? Imagine this python code: d = o.__dict__ d['new_member'] = 42 print o.new_member which I would like to write in C++: bpl::dict d = o.attr("__dict__") d["new_member"] = 42 However, as 'd' is a copy of o's __dict__ attribute, the new_member never makes it into o. How can I do what I want ? (Yes, in this case I obviously wouldn't need the indirection over 'd'; this is just a minimal test case...) Thanks, Stefan -- ...ich hab' noch einen Koffer in Berlin... _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig From seefeld at sympatico.ca Thu Jan 24 19:56:59 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 24 Jan 2008 13:56:59 -0500 Subject: [C++-sig] HOWTO: ...create shallow boost::python::object copies ? In-Reply-To: <924600.7133.qm@web31108.mail.mud.yahoo.com> References: <924600.7133.qm@web31108.mail.mud.yahoo.com> Message-ID: <4798DF7B.50307@sympatico.ca> Ralf W. Grosse-Kunstleve wrote: > Hi Stefan, look for "astute" on this page: > http://www.boost.org/libs/python/doc/tutorial/doc/html/python/object.html Hi Ralf, yes, I have seen that. However, unfortunately that only tells me that a copy is to be expected there. It doesn't tell me how I can make a reference. The reason for me creating a bpl::dict object is to be able to access the dict-specific methods (such as get()) which aren't available when I access the object. So, how can I cast to a derived type without a temporary ? bpl::dict d = some_cast(o.attr("a_dict_member")); ?? Thanks, Stefan -- ...ich hab' noch einen Koffer in Berlin... From seefeld at sympatico.ca Thu Jan 24 19:59:18 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 24 Jan 2008 13:59:18 -0500 Subject: [C++-sig] HOWTO: ...create shallow boost::python::object copies ? In-Reply-To: <924600.7133.qm@web31108.mail.mud.yahoo.com> References: <924600.7133.qm@web31108.mail.mud.yahoo.com> Message-ID: <4798E006.50107@sympatico.ca> Ralf W. Grosse-Kunstleve wrote: > Hi Stefan, look for "astute" on this page: > http://www.boost.org/libs/python/doc/tutorial/doc/html/python/object.html ...and just after hitting I noticed that I hadn't read that page carefully enough. Now I see the other remark about using extract<>. Thanks ! Stefan -- ...ich hab' noch einen Koffer in Berlin... From roman.yakovenko at gmail.com Thu Jan 24 20:05:27 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 24 Jan 2008 21:05:27 +0200 Subject: [C++-sig] Best way to register shared_ptr In-Reply-To: <4797A388.2060602@googlemail.com> References: <4797A388.2060602@googlemail.com> Message-ID: <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> On Jan 23, 2008 10:28 PM, Andrea wrote: > Hi, > > I've run into the problem of registering a shared_ptr. > I've found a workaround using a const_cast in get_pointer. > > I've also read that there exist a patch to implement a proper fix. > > Unfortunately I cannot find more details on that. Read this thread: http://mail.python.org/pipermail/c++-sig/2006-November/011664.html > Could someone please quick list pros and cons of both approaches. > I am a bit uncomfortable about const_casting, but I cannot understand if it is just a way to please > the compiler or something deeper. I attach the original patch -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- A non-text attachment was scrubbed... Name: pointer_holder.hpp.patch Type: text/x-patch Size: 3094 bytes Desc: not available URL: From mariofutire at googlemail.com Thu Jan 24 22:37:26 2008 From: mariofutire at googlemail.com (Andrea) Date: Thu, 24 Jan 2008 21:37:26 +0000 Subject: [C++-sig] Best way to register shared_ptr In-Reply-To: <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> References: <4797A388.2060602@googlemail.com> <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> Message-ID: <47990516.9040206@googlemail.com> Roman Yakovenko wrote: > On Jan 23, 2008 10:28 PM, Andrea wrote: >> Hi, >> >> I've run into the problem of registering a shared_ptr. >> I've found a workaround using a const_cast in get_pointer. >> >> I've also read that there exist a patch to implement a proper fix. >> >> Unfortunately I cannot find more details on that. > > Read this thread: > http://mail.python.org/pipermail/c++-sig/2006-November/011664.html > Thank you > > I attach the original patch I guess I need to recompile boost_python, don't I? It looks you know a lot about it... would you be able to help me on the other issue I posted recently: http://mail.python.org/pipermail/c++-sig/2008-January/013254.html somehow boost::python is able to tell when 2 different instances of a boost::shared_ptr<> point to the same C++ object. This is not the case for custom smart pointers. I'm happy to investigate, but I could not find where to start. Do you know how it works? Cheers From mariofutire at googlemail.com Thu Jan 24 22:37:26 2008 From: mariofutire at googlemail.com (Andrea) Date: Thu, 24 Jan 2008 21:37:26 +0000 Subject: [C++-sig] Best way to register shared_ptr In-Reply-To: <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> References: <4797A388.2060602@googlemail.com> <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> Message-ID: <47990516.9040206@googlemail.com> Roman Yakovenko wrote: > On Jan 23, 2008 10:28 PM, Andrea wrote: >> Hi, >> >> I've run into the problem of registering a shared_ptr. >> I've found a workaround using a const_cast in get_pointer. >> >> I've also read that there exist a patch to implement a proper fix. >> >> Unfortunately I cannot find more details on that. > > Read this thread: > http://mail.python.org/pipermail/c++-sig/2006-November/011664.html > Thank you > > I attach the original patch I guess I need to recompile boost_python, don't I? It looks you know a lot about it... would you be able to help me on the other issue I posted recently: http://mail.python.org/pipermail/c++-sig/2008-January/013254.html somehow boost::python is able to tell when 2 different instances of a boost::shared_ptr<> point to the same C++ object. This is not the case for custom smart pointers. I'm happy to investigate, but I could not find where to start. Do you know how it works? Cheers From amohr at pixar.com Thu Jan 24 23:00:02 2008 From: amohr at pixar.com (Alex Mohr) Date: Thu, 24 Jan 2008 14:00:02 -0800 Subject: [C++-sig] Best way to register shared_ptr In-Reply-To: <47990516.9040206@googlemail.com> References: <4797A388.2060602@googlemail.com> <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> <47990516.9040206@googlemail.com> Message-ID: <47990A62.8080304@pixar.com> > It looks you know a lot about it... would you be able to help me on the other issue I posted recently: > http://mail.python.org/pipermail/c++-sig/2008-January/013254.html > > somehow boost::python is able to tell when 2 different instances of a boost::shared_ptr<> point to > the same C++ object. > > This is not the case for custom smart pointers. > > I'm happy to investigate, but I could not find where to start. Do you know how it works? Boost.python has special-case code for shared_ptr. When boost.python creates a shared_ptr for a class, it supplies a special custom deleter that tracks the python object identified with the c++ object. When a shared_ptr is converted to_python, boost.python checks to see if it has its special deleter, and if so, it can get the existing python object from it. That's how "object identity" is maintained for shared_ptr. It is possible to make object identity work for custom smart pointers, but it is hard. What we're doing is totally gross. After a class_<> is registered with boost.python, I manually replace the to_python converter that boost.python registered with my own that wraps it but also does custom object-identity mapping. This requires digging into the conversion registry using private api, and doing nasty stuff, but you *can* make it work. Alex From meine at informatik.uni-hamburg.de Fri Jan 25 10:03:48 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Fri, 25 Jan 2008 10:03:48 +0100 Subject: [C++-sig] Best way to register shared_ptr In-Reply-To: <47990A62.8080304@pixar.com> References: <4797A388.2060602@googlemail.com> <47990516.9040206@googlemail.com> <47990A62.8080304@pixar.com> Message-ID: <200801251003.49386.meine@informatik.uni-hamburg.de> Am Donnerstag, 24. Januar 2008 23:00:02 schrieb Alex Mohr: > Boost.python has special-case code for shared_ptr. When boost.python > creates a shared_ptr for a class, it supplies a special custom deleter > that tracks the python object identified with the c++ object. For the first time, I begin to see this clear. So.. this only works if boost::python creates the shared_ptr? E.g. if I have a method shared_ptr createFoo() { ... } Will boost::python be able to retroactively add the custom deleter? AFAICS, this is impossible, because the shared_ptr might be actually shared, so BPL cannot re-wrap the pointer in another shared_ptr... yes it can, if the custom deleter stores a reference to the original shared_ptr? Ciao, / / /--/ / / ANS From roman.yakovenko at gmail.com Fri Jan 25 11:25:09 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 25 Jan 2008 12:25:09 +0200 Subject: [C++-sig] Best way to register shared_ptr In-Reply-To: <47990A62.8080304@pixar.com> References: <4797A388.2060602@googlemail.com> <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> <47990516.9040206@googlemail.com> <47990A62.8080304@pixar.com> Message-ID: <7465b6170801250225v8a83ee9pc96d0962648af429@mail.gmail.com> On Jan 25, 2008 12:00 AM, Alex Mohr wrote: > > It looks you know a lot about it... would you be able to help me on the other issue I posted recently: > > http://mail.python.org/pipermail/c++-sig/2008-January/013254.html > > > > somehow boost::python is able to tell when 2 different instances of a boost::shared_ptr<> point to > > the same C++ object. > > > > This is not the case for custom smart pointers. > > > > I'm happy to investigate, but I could not find where to start. Do you know how it works? > > Boost.python has special-case code for shared_ptr. When boost.python > creates a shared_ptr for a class, it supplies a special custom deleter > that tracks the python object identified with the c++ object. > > When a shared_ptr is converted to_python, boost.python checks to see if > it has its special deleter, and if so, it can get the existing python > object from it. That's how "object identity" is maintained for shared_ptr. > > It is possible to make object identity work for custom smart pointers, > but it is hard. What we're doing is totally gross. After a class_<> is > registered with boost.python, I manually replace the to_python > converter that boost.python registered with my own that wraps it but > also does custom object-identity mapping. This requires digging into > the conversion registry using private api, and doing nasty stuff, but > you *can* make it work. Can you post some complete example? Thank you -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From amohr at pixar.com Fri Jan 25 21:16:17 2008 From: amohr at pixar.com (Alex Mohr) Date: Fri, 25 Jan 2008 12:16:17 -0800 Subject: [C++-sig] Best way to register shared_ptr In-Reply-To: <7465b6170801250225v8a83ee9pc96d0962648af429@mail.gmail.com> References: <4797A388.2060602@googlemail.com> <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> <47990516.9040206@googlemail.com> <47990A62.8080304@pixar.com> <7465b6170801250225v8a83ee9pc96d0962648af429@mail.gmail.com> Message-ID: <479A4391.2050000@pixar.com> > Can you post some complete example? No, I can't. It's way too much code and very specific to our smart pointers. But I can give you a sense of what it could look like for what I imagine is a common case. Alex Suppose you have: // manages pointee's lifetime using an intrusive reference count, // like intrusive_ptr. T must derive from ref_base, which contains // the reference count. template class ref_ptr; // a smart pointer that can detect when its pointee has been destroyed, // but does not influence the lifetime of the pointee. also, // weak_ptr can detect when its pointee (of any type) has been // destroyed. assume that ref_ptr may be implicitly converted to // weak_ptr and weak_ptr. template class weak_ptr; // some object which may be pointed to by ref_ptr and weak_ptr. class foo : public ref_base {}; // wrap foo, and enable identity tracking. bp::class_ >("foo") .def(track_identity()) ; // The key here is to implement some sort of mapping between python // objects and c++ objects. The code below assumes an api like this: void set_python_identity(weak_ptr const &cpp, PyObject *python); PyObject * get_python_identity(weak_ptr const &cpp); // I'll leave the issue of lifetime management wrt this mapping up to // you. I think in this case, the right thing might be to keep python // weak references for the python objects. Another challenge is how to // clean expired objects from the mapping. Of course, if you're willing // to store the python object weak reference inside the C++ object // itself (say, alongside the ref count), then this is much easier. // This code is similar to the code we have in production use, though // I may have forgotten some things, have typos, and certainly I haven't // tried compiling it. template struct ref_ptr_from_python { typedef typename get_pointee::type pointee; ref_ptr_from_python() { bp::converter::registry::insert(&convertible, &construct, bp::type_id()); } private: static void *convertible(PyObject *p) { if (p == Py_None) return p; return bp::converter::get_lvalue_from_python (p, bp::converter::registered::converters); } static void construct(PyObject *source, bp::converter:: rvalue_from_python_stage1_data *data) { void *const storage = ((bp::converter:: rvalue_from_python_storage*)data)->storage.bytes; // None case if (data->convertible == source) new (storage) Ptr(); else { Ptr ptr(static_cast(data->convertible)); new (storage) Ptr(ptr); // set python identity set_python_identity(ptr, source); } data->convertible = storage; } }; template struct ref_ptr_to_python_replacement { // store the original to-python converter. we do this in a static // member since there is only one converter for a Ptr type. static bp::converter::to_python_function_t original_converter; // this signature has to match to_python_function_t. static PyObject *convert(void const *x) { // see boost/python/converter/as_to_python_function.hpp Ptr const &ptr = *const_cast(static_cast(x)); // try to get an existing python identity. if (PyObject *id = get_python_identity(ptr)) return id; // otherwise, use the original converter, and set that as the // identity. PyObject *ret = original_converter(x); set_python_identity(ptr, ret); return ret; } }; template bp::converter::to_python_function_t ret_ptr_to_python_replacement::original_converter = 0; template struct ref_ptr_to_python { ref_ptr_to_python() { bp::to_python_converter >(); } static PyObject *convert(Ptr const &p) { typedef typename get_pointee::type pointee; // null -> None if (not p) return bp::detail::none(); if (PyObject *id = get_python_identity(p)) return id; // otherwise make a new python object, and set identity. PyObject *ret = bp::objects::make_ptr_instance >::execute(p); set_python_identity(p, ret); return ret; } }; // the identity tracking mechanism. struct track_identity : bp::def_visitor { friend class bp::def_visitor_access; template static void register_conversions(Wrapper *, T *) { // need to rebind the smart pointer template with T, // in case T is different from Wrapper (python virtual dispatch) // I'll just assume ref_ptr here. typedef ref_ptr ptr_type; // register from-python conversions ref_ptr_from_python(); // replace the to_python conversion that boost.python registered // for this class. we do this to support object-identity // tracking. // // note that we do this for the "wrapped type", since that's all // that boost.python's registered a conversion for. the // unwrapped type is handled separately -- we don't have to // replace an existing converter, we just register our own. bp::converter::registration *r = const_cast (bp::converter::registry::query(bp::type_id())); assert(r); ref_ptr_to_python_replacement::original_converter = r->m_to_python; r->m_to_python = ref_ptr_to_python_replacement::convert; if (not is_same::value) ref_ptr_to_python(); } template void visit(CLS &c) const { typedef typename CLS::wrapped_type type; typedef typename CLS::metadata::held_type_arg ptr_type; register_conversions ((type *)0, bp::detail::unwrap_wrapper((type *)0)); } }; From roman.yakovenko at gmail.com Sat Jan 26 12:28:12 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 26 Jan 2008 13:28:12 +0200 Subject: [C++-sig] Best way to register shared_ptr In-Reply-To: <479A4391.2050000@pixar.com> References: <4797A388.2060602@googlemail.com> <7465b6170801241105u108cd910l5f965499bab83194@mail.gmail.com> <47990516.9040206@googlemail.com> <47990A62.8080304@pixar.com> <7465b6170801250225v8a83ee9pc96d0962648af429@mail.gmail.com> <479A4391.2050000@pixar.com> Message-ID: <7465b6170801260328u23f82a9aha8ce82d96fd639d8@mail.gmail.com> On Jan 25, 2008 10:16 PM, Alex Mohr wrote: > > Can you post some complete example? > > No, I can't. It's way too much code and very specific to our smart > pointers. But I can give you a sense of what it could look like for > what I imagine is a common case. > Thank you very much! -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From Tim.Norman at dreamworks.com Wed Jan 30 20:22:37 2008 From: Tim.Norman at dreamworks.com (Norman, Tim) Date: Wed, 30 Jan 2008 11:22:37 -0800 Subject: [C++-sig] Dreamworks Animation Opportunity--Pipeline Engineer Message-ID: <57E24B0263A3594EB66EEABC419CA407152FA4C3FC@EXCLUSGLD1.win.dreamworks.com> Production or Pipeline (Python) Engineer-Maya or Houdini The Production Engineering group designs, implements, and maintains software that "glues" together the software and data used in the creation of animated feature films. Production engineers understand the entire computer animation process and use that knowledge to develop the DreamWorks global animation pipeline. We are seeking a highly motivated person with experience in both software development and in computer animation production. As a production engineer, you will: * Maintain the existing software systems used in the production of our films * Enhance, modify, and redesign the systems to accommodate the ever-changing requirements of production * Interact with animators and technical directors to solve technical production issues * Use your understanding of computer animation to come up with creative solutions to difficult technical problems Your software development experience must include: * Large software systems * Extensive scripting in Python or Perl (preferably both) * Object oriented C++ experience * UNIX/Linux development Additionally, you must have: * Excellent problem solving and customer service skills * Outstanding attention to detail and following through on tasks * Ability to handle a variety of tasks and personality types * Strong verbal and written communication skills * Enthusiasm for computer animation and/or CG in films We also prefer: * Experience in the computer animation industry * Knowledge of CG software such as Maya, Houdini, or Shake * Shell scripting (csh, tcsh, sh) ability * Knowledge of relational database concepts * System administration skills You must have achieved at least a bachelors degree in computer science. A masters is preferred. Tim Norman Dreamworks Animation Ph 818. 695. 7801 Fax 818. 695. 6210 www.dreamworksanimation.com [http://www.dreamworksanimation.com/interact/signatures/shrek_puss1.gif] -------------- next part -------------- An HTML attachment was scrubbed... URL: From progettodex at gmail.com Thu Jan 31 17:43:31 2008 From: progettodex at gmail.com (progetto dex) Date: Thu, 31 Jan 2008 17:43:31 +0100 Subject: [C++-sig] missing to_python converter for wchar_t Message-ID: <54f22e040801310843n59a56a5cx4e2768d2cdea0a28@mail.gmail.com> Hi all, it seems that I'm facing a well-known problem (I've found some messages dated 2006 or earlier), but maybe in the near past something has changed... I'm wrapping a third party API that uses "const wchar_t* const" arguments, and Python says that a converter is missing. Before creating a layer to convert from/to std::wstring, I post a stripped down example. First, the C++ stuff: // C++ #include using namespace boost::python; class Receiver { public: virtual void Receive(const wchar_t* const message) { } }; class Sender { Receiver& receiver; public: Sender(Receiver& r) : receiver(r) { } void Send() { receiver.Receive(L"test message"); } }; struct PyReceiver : Receiver { PyObject* self; PyReceiver(PyObject* s) : self(s) { } void Receive(const wchar_t* const message) { return call_method(self, "Receive", message); } void Receive_default(const wchar_t* const message) { return this->Receiver::Receive(message); } }; BOOST_PYTHON_MODULE(MyModule) { class_("Receiver") .def("Receive", &Receiver::Receive, &PyReceiver::Receive_default) ; class_("Sender", init()) .def("Send", &Sender::Send) ; } Now, a trivial Python test case: #python import MyModule class Receiver(MyModule.Receiver): def Receive (self, msg): print "MSG: " + msg r = Receiver() s = MyModule.Sender(r) s.Send() If I supply a to_python wchar_t converter, only the first letter of the message is printed. I'm I missing something? Best regards, Dex -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Thu Jan 31 20:20:11 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 31 Jan 2008 21:20:11 +0200 Subject: [C++-sig] missing to_python converter for wchar_t In-Reply-To: <54f22e040801310843n59a56a5cx4e2768d2cdea0a28@mail.gmail.com> References: <54f22e040801310843n59a56a5cx4e2768d2cdea0a28@mail.gmail.com> Message-ID: <7465b6170801311120r4a8c5141y3fb09cc3e1af25df@mail.gmail.com> 2008/1/31 progetto dex : > Hi all, > it seems that I'm facing a well-known problem (I've found some messages > dated 2006 or earlier), but maybe in the near past something has changed... > > I'm wrapping a third party API that uses "const wchar_t* const" arguments, > and Python says that a converter is missing. > Before creating a layer to convert from/to std::wstring, I post a stripped > down example. First, the C++ stuff: > > // C++ > #include > > using namespace boost::python; > > class Receiver > { > public: > virtual void Receive(const wchar_t* const message) { } > }; > > class Sender > { > Receiver& receiver; > > public: > Sender(Receiver& r) : receiver(r) { } > void Send() { receiver.Receive(L"test message"); } > }; > > struct PyReceiver : Receiver > { > PyObject* self; > PyReceiver(PyObject* s) : self(s) { } > > void Receive(const wchar_t* const message) > { return call_method(self, "Receive", message); } > > void Receive_default(const wchar_t* const message) > { return this->Receiver::Receive(message); } > }; > > BOOST_PYTHON_MODULE(MyModule) > { > class_("Receiver") > .def("Receive", &Receiver::Receive, &PyReceiver::Receive_default) > ; > > class_("Sender", init()) > .def("Send", &Sender::Send) > ; > } > > Now, a trivial Python test case: > > #python > import MyModule > > class Receiver(MyModule.Receiver): > def Receive (self, msg): > print "MSG: " + msg > > r = Receiver() > s = MyModule.Sender(r) > s.Send() > > If I supply a to_python wchar_t converter, only the first letter of the > message is printed. > I'm I missing something? Untested: I think that replacing "const wchar_t* const" with "const wchar_t* " should work. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/