From paul.bilokon at lehman.com Wed Sep 3 10:08:36 2008 From: paul.bilokon at lehman.com (Bilokon, Paul) Date: Wed, 3 Sep 2008 09:08:36 +0100 Subject: [C++-sig] Attempt to Return Dangling Reference Error -- What'sthe solution? In-Reply-To: <87hc93ho04.fsf@mcbain.luannocracy.com> References: <87hc93ho04.fsf@mcbain.luannocracy.com> Message-ID: Hi Dave, Thanks a lot. Changing clone's signature to... virtual boost::shared_ptr clone() const did the trick. However, since I'm wrapping an existing library, I'm trying to find a non-intrusive solution. I'd rather keep the current signature of clone. Are there any other options? Many thanks, Paul -----Original Message----- From: c++-sig-bounces+paul.bilokon=lehman.com at python.org [mailto:c++-sig-bounces+paul.bilokon=lehman.com at python.org] On Behalf Of David Abrahams Sent: Friday, August 29, 2008 3:53 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] Attempt to Return Dangling Reference Error -- What'sthe solution? on Fri Aug 29 2008, "Bilokon, Paul" wrote: > Hi, > > I'm looking at this: > > http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html#dangl > in > g > > ('I'm getting the "attempt to return dangling reference" error. What > am I doing wrong?') > > I understand the statement of the problem. However, suppose I need to > hold on to the new Python object and the C++ object contained therein. > How do I achieve that? > > Let me give you the specific context. > > > void takesAnimalRawPtr(Animal * animalRawPtr) > { > cout << "This is " << animalRawPtr->whatIsIt() << endl; > Animal * clone = animalRawPtr->clone(); > cout << "Now this is " << clone->whatIsIt() << endl; > delete clone; > } I suggest you make your clone() functions return a boost::shared_ptr. That would make all your problems disappear. HTH, -- Dave Abrahams BoostPro Computing http://www.boostpro.com _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice. From paul.bilokon at lehman.com Wed Sep 3 11:27:02 2008 From: paul.bilokon at lehman.com (Bilokon, Paul) Date: Wed, 3 Sep 2008 10:27:02 +0100 Subject: [C++-sig] Attempt to Return Dangling Reference Error -- What'sthe solution? In-Reply-To: References: <87hc93ho04.fsf@mcbain.luannocracy.com> Message-ID: Hi Dave, Thought I should give more background. The library I'm wrapping is using a custom smart pointer, which is really unfortunate. It would be difficult (though desirable) to replace these custom shared pointers with shared_ptr. Therefore I'm trying to provide a mechanism for calling the functions expecting CustomPtr with a CustomPtr-to-copy by calling clone and wrapping the copy into CustomPtr, wherever this has reasonable semantics. Therefore I cannot wrap the return of clone into shared_ptr. Is there any way to cheat the dangling reference detection mechanism? (Of course, shared_ptr does not do release().) Many thanks, Paul -----Original Message----- From: Bilokon, Paul Sent: Wednesday, September 03, 2008 9:09 AM To: 'Development of Python/C++ integration' Subject: RE: [C++-sig] Attempt to Return Dangling Reference Error -- What'sthe solution? Hi Dave, Thanks a lot. Changing clone's signature to... virtual boost::shared_ptr clone() const did the trick. However, since I'm wrapping an existing library, I'm trying to find a non-intrusive solution. I'd rather keep the current signature of clone. Are there any other options? Many thanks, Paul -----Original Message----- From: c++-sig-bounces+paul.bilokon=lehman.com at python.org [mailto:c++-sig-bounces+paul.bilokon=lehman.com at python.org] On Behalf Of David Abrahams Sent: Friday, August 29, 2008 3:53 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] Attempt to Return Dangling Reference Error -- What'sthe solution? on Fri Aug 29 2008, "Bilokon, Paul" wrote: > Hi, > > I'm looking at this: > > http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html#dangl > in > g > > ('I'm getting the "attempt to return dangling reference" error. What > am I doing wrong?') > > I understand the statement of the problem. However, suppose I need to > hold on to the new Python object and the C++ object contained therein. > How do I achieve that? > > Let me give you the specific context. > > > void takesAnimalRawPtr(Animal * animalRawPtr) > { > cout << "This is " << animalRawPtr->whatIsIt() << endl; > Animal * clone = animalRawPtr->clone(); > cout << "Now this is " << clone->whatIsIt() << endl; > delete clone; > } I suggest you make your clone() functions return a boost::shared_ptr. That would make all your problems disappear. HTH, -- Dave Abrahams BoostPro Computing http://www.boostpro.com _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice. From roman.yakovenko at gmail.com Wed Sep 3 11:40:33 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 3 Sep 2008 12:40:33 +0300 Subject: [C++-sig] Attempt to Return Dangling Reference Error -- What'sthe solution? In-Reply-To: References: <87hc93ho04.fsf@mcbain.luannocracy.com> Message-ID: <7465b6170809030240k5458faa4rd6bbe8d801eda4b7@mail.gmail.com> On Wed, Sep 3, 2008 at 12:27 PM, Bilokon, Paul wrote: > Hi Dave, > > Thought I should give more background. The library I'm wrapping is using > a custom smart pointer, which is really unfortunate. It would be > difficult (though desirable) to replace these custom shared pointers > with shared_ptr. Therefore I'm trying to provide a mechanism for calling > the functions expecting CustomPtr with a CustomPtr-to-copy by calling > clone and wrapping the copy into CustomPtr, wherever this has reasonable > semantics. Therefore I cannot wrap the return of clone into shared_ptr. > Is there any way to cheat the dangling reference detection mechanism? > (Of course, shared_ptr does not do release().) May be my troubleshooting guide can help you: http://language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From ope-devel at gmx.de Wed Sep 3 21:14:00 2008 From: ope-devel at gmx.de (Olaf Peter) Date: Wed, 03 Sep 2008 21:14:00 +0200 Subject: [C++-sig] embbed simple example failed Message-ID: <48BEE1F8.1020102@gmx.de> Hi, I get mad on this snipped; I've got: $ g++ -I /usr/include/python2.5 bpl.cpp -o bpl -l boost_python -l python2.5 && ./bpl File "", line 1 print("Hello World\n") ^ SyntaxError: invalid syntax something went wrong What did I wrong??? Thanks, Olaf -------------- next part -------------- A non-text attachment was scrubbed... Name: bpl.cpp Type: text/x-c++src Size: 1897 bytes Desc: not available URL: From seefeld at sympatico.ca Wed Sep 3 21:40:58 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 03 Sep 2008 15:40:58 -0400 Subject: [C++-sig] embbed simple example failed In-Reply-To: <48BEE1F8.1020102@gmx.de> References: <48BEE1F8.1020102@gmx.de> Message-ID: <48BEE84A.8020002@sympatico.ca> Olaf Peter wrote: > Hi, > > I get mad on this snipped; I've got: > > $ g++ -I /usr/include/python2.5 bpl.cpp -o bpl -l boost_python -l > python2.5 && ./bpl > File "", line 1 > print("Hello World\n") > ^ > SyntaxError: invalid syntax > The compiler is right. What you probably meant is 'printf', not 'print'. It is usually a good idea to narrow down the error by removing bits from the sources until the problem disappears. Doing that you would have realized that your problem has nothing to do with boost, neither with python. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From amohr at pixar.com Wed Sep 3 22:51:10 2008 From: amohr at pixar.com (Alex Mohr) Date: Wed, 03 Sep 2008 13:51:10 -0700 Subject: [C++-sig] embbed simple example failed In-Reply-To: <48BEE1F8.1020102@gmx.de> References: <48BEE1F8.1020102@gmx.de> Message-ID: <48BEF8BE.9080501@pixar.com> > I get mad on this snipped; I've got: > > $ g++ -I /usr/include/python2.5 bpl.cpp -o bpl -l boost_python -l > python2.5 && ./bpl > File "", line 1 > print("Hello World\n") > ^ > SyntaxError: invalid syntax > something went wrong The problem is that in python, 'print' is a statement, not an expression. When you call PyRun_String, you pass Py_eval_input as the start token, which means python expects the string you pass to be an expression, not a statement. I think you can fix this by passing Py_single_input to PyRun_String rather than Py_eval_input. Check out the docs for PyRun_String, Py_eval_input, and Py_single_input here: http://docs.python.org/api/veryhigh.html Alex From seefeld at sympatico.ca Wed Sep 3 22:58:05 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Wed, 03 Sep 2008 16:58:05 -0400 Subject: [C++-sig] embbed simple example failed In-Reply-To: <48BEF8BE.9080501@pixar.com> References: <48BEE1F8.1020102@gmx.de> <48BEF8BE.9080501@pixar.com> Message-ID: <48BEFA5D.8020209@sympatico.ca> Alex Mohr wrote: >> I get mad on this snipped; I've got: >> >> $ g++ -I /usr/include/python2.5 bpl.cpp -o bpl -l boost_python -l >> python2.5 && ./bpl >> File "", line 1 >> print("Hello World\n") >> ^ >> SyntaxError: invalid syntax >> something went wrong > > The problem is that in python, 'print' is a statement, not an > expression. When you call PyRun_String, you pass Py_eval_input as the > start token, which means python expects the string you pass to be an > expression, not a statement. Olaf, I'm really sorry I replied after only a casual look at the error message. Let me try to rectify that by suggesting that you use the boost.python embedding API instead. Something like namespace bpl = boost::python; bpl::object result = bpl::exec("print 'Hello World\n'"); Instead of what you originally wrote, and bpl::eval() for actual expressions. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From ope-devel at gmx.de Thu Sep 4 05:06:55 2008 From: ope-devel at gmx.de (Olaf Peter) Date: Thu, 04 Sep 2008 05:06:55 +0200 Subject: [C++-sig] embbed simple example failed In-Reply-To: <48BEF8BE.9080501@pixar.com> References: <48BEE1F8.1020102@gmx.de> <48BEF8BE.9080501@pixar.com> Message-ID: <48BF50CF.4050603@gmx.de> > I think you can fix this by passing Py_single_input to PyRun_String > rather than Py_eval_input. > > Check out the docs for PyRun_String, Py_eval_input, and Py_single_input > here: > > http://docs.python.org/api/veryhigh.html > > Alex Thanks, that's. Some time ago I did introduce this bug and saw what I did expect to see. Thanks, Olaf From rogeeff at gmail.com Thu Sep 4 08:29:15 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Thu, 4 Sep 2008 06:29:15 +0000 (UTC) Subject: [C++-sig] how t o specify and/or update global dictionary for bp::call call Message-ID: Hi, I've got class implemented in python, inherited from C++ class. I am using wrapper/get_overload to invoke it's methods: bp::override o = this->get_override( "update" ); bp::call< void >( o.ptr(), arg1, arg2 ); Now in update method I need to refer to the function foo that reside in module moo. I've tried to add from moo import foo and from moo import * before class definition: class MyPyDerivedClass( MyCPPBase ): def update( s, arg1, ARG2 ): foo() Both attempts were futile. Looks like global dictionary during update call does not contain foo. So, now I am looking for a way for either update it somehow or specify my own. Please advise. Gennadiy P.S. solution to add import method inside update method is unacceptable for various (mostly political) reasons, though it obviously will work. I definitely need this outside class definition P.P.S. Instance of class MyPyDerivedClass is created on C++ side based on pre-cached python type: cpp_derived = m_py_type(); From seefeld at sympatico.ca Thu Sep 4 15:00:54 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 04 Sep 2008 09:00:54 -0400 Subject: [C++-sig] how t o specify and/or update global dictionary for bp::call call In-Reply-To: References: Message-ID: <48BFDC06.1010706@sympatico.ca> Gennadiy Rozental wrote: > Hi, > > I've got class implemented in python, inherited from C++ class. I am using > wrapper/get_overload to invoke it's methods: > > bp::override o = this->get_override( "update" ); > bp::call< void >( o.ptr(), arg1, arg2 ); > > Now in update method I need to refer to the function foo that reside > in module moo. > > I've tried to add > from moo import foo > and > from moo import * > > before class definition: > > class MyPyDerivedClass( MyCPPBase ): > def update( s, arg1, ARG2 ): > foo() > > Both attempts were futile. Looks like global dictionary during update call does > not contain foo. > For avoidance of doubt: you are saying the import itself was successful, but there was no 'foo' symbol in the global namespace after that point ? I just modified the exec.cpp test (libs/python/tests/exec.cpp), and it all works like a charm. (For reference: go to libs/python/tests/exec.cpp and modify the 'exec_test' function to simulate what you want. Here is the modified script I use: python::object main = python::import("__main__"); python::object global(main.attr("__dict__")); python::object result = python::exec( "from embedded_hello import * \n" "from sys import * \n" //<-- added just to show how to populate the global namespace of the script "class PythonDerived(Base): \n" " def hello(self): \n" " print platform \n" //<-- use it " return 'Hello from Python!' \n", global, global); python::object PythonDerived = global["PythonDerived"]; python::object py_base = PythonDerived(); Base& py = python::extract(py_base); assert(py.hello() == "Hello from Python!"); > P.S. solution to add import method inside update method is unacceptable for > various (mostly political) reasons, though it obviously will work. I definitely > need this outside class definition > > P.P.S. Instance of class MyPyDerivedClass is created on C++ side based on > pre-cached python type: > > cpp_derived = m_py_type(); > Sounds like what you are doing is very similar to what the test I just exerimented with does. Right ? HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From rogeeff at gmail.com Thu Sep 4 18:38:05 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Thu, 4 Sep 2008 16:38:05 +0000 (UTC) Subject: [C++-sig] how t o specify and/or update global dictionary for bp::call call References: <48BFDC06.1010706@sympatico.ca> Message-ID: Stefan Seefeld sympatico.ca> writes: > For avoidance of doubt: you are saying the import itself was successful, > but there was no 'foo' symbol in the global namespace after that point ? Yes. That's true > I just modified the exec.cpp test (libs/python/tests/exec.cpp), and it > all works like a charm. > (For reference: go to libs/python/tests/exec.cpp and modify the > 'exec_test' function to simulate what you want. Here is the modified > script I use: > > python::object main = python::import("__main__"); > python::object global(main.attr("__dict__")); > python::object result = python::exec( > "from embedded_hello import * \n" > "from sys import * \n" > //<-- added just to show how to populate the global namespace of the script > "class PythonDerived(Base): \n" > " def hello(self): \n" > " print platform \n" > //<-- use it > " return 'Hello from Python!' \n", > global, global); This only works because you used global dictionary as local. I can't do this for various reasons, main of which is that I am trying to mimic namespacing, which is unavailable directly in python. So the local dictionary is the dictionary of the module I am trying to put this class in. > python::object PythonDerived = global["PythonDerived"]; > python::object py_base = PythonDerived(); > Base& py = python::extract(py_base); > assert(py.hello() == "Hello from Python!"); So what I need to update global dictionary before hello call here even if it was not used as local during exec call. Gennadiy From william.marie at gmail.com Fri Sep 5 16:01:21 2008 From: william.marie at gmail.com (=?UTF-8?Q?William_Mari=C3=A9?=) Date: Fri, 5 Sep 2008 07:01:21 -0700 (PDT) Subject: [C++-sig] Calling Object virtual function from C++ Message-ID: <19331891.post@talk.nabble.com> Hi, I'm trying to call a object virtual function from the C++ side but if the python function is not defined into the Python class it doesn't find the one defined in the C++ object : ------- C++ class Base { public: Base() {}; virtual ~Base() {}; virtual void testVirtual() { itn a = 0; } }; struct BaseWrap : Base, bp::wrapper { void testVirtual() { if (bp::override fun = this->get_override("testVirtual")) fun(); // *note* Base::testVirtual(); } void default_testVirtual() { this->Base::testVirtual(); } }; BOOST_PYTHON_MODULE(MyModule) { bp::class_("Base") .def("testVirtual", &Base::testVirtual, &BaseWrap::default_testVirtual) ; bp::class_("Base", bp::init<>()) ; } ------- Python ( testvirtual.py ) import MyModule class TestVirtual( MyModule.Base ): ## Constructor def __init__( self ) : pass ------------ Now from a C++ side : bp::object module = bp::import("testvirtual"); bp::object modDict(module.attr("__dict__")); bp::object bpClass = modDict["TestVirtual"]; bp::object bpTestVirtual = pbClass(); # Doesn't call the Base class virtual function and tell that the testVirtual function doesn't exist bp::object bpFun = bpTestVirtual.attr( "testVirtual" ); bpFun(); # Doesn't call the Base class virtual function and tell that the testVirtual function doesn't exist bp::call(bpFun.ptr()); # Doesn't call the Base class virtual function and tell that the testVirtual function doesn't exist bp::call_method(bpTestVirtual.ptr(), "testVirtual" ); # Always call the Base::testVirtual even if the function is overidden in Python Base& pTestVirtual = BP_EXTRACT( bpTestVirtual, Base& ); pTestVirtual->testVirtual() Here we go, so i'm sure there is an easy way to do that but i don't understand what i'm doing wrong. Could someone help me please ? Thanks William -- View this message in context: http://www.nabble.com/Calling-Object-virtual-function-from-C%2B%2B-tp19331891p19331891.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From wladwig at wdtinc.com Fri Sep 5 17:50:25 2008 From: wladwig at wdtinc.com (William Ladwig) Date: Fri, 5 Sep 2008 10:50:25 -0500 Subject: [C++-sig] Calling Object virtual function from C++ In-Reply-To: <19331891.post@talk.nabble.com> References: <19331891.post@talk.nabble.com> Message-ID: <765CBD9053EA2B438625895F30D1856FCA025885@storm.wdtinc.com> In your BOOST_PYTHON_MODULE you have "Base" defined twice. Try removing: bp::class_("Base", bp::init<>()) ; since you already exposed your "Base" class using BaseWrapper. Also, in your python module, try adding the base class constructor: MyModule.Base.__init__(self) to the first line of your TestVirtual __init__ function. These are the first two places I would start. Hope this helps. Bill -----Original Message----- From: c++-sig-bounces+wladwig=wdtinc.com at python.org [mailto:c++-sig-bounces+wladwig=wdtinc.com at python.org] On Behalf Of William Mari? Sent: Friday, September 05, 2008 9:01 AM To: c++-sig at python.org Subject: [C++-sig] Calling Object virtual function from C++ Hi, I'm trying to call a object virtual function from the C++ side but if the python function is not defined into the Python class it doesn't find the one defined in the C++ object : ------- C++ class Base { public: Base() {}; virtual ~Base() {}; virtual void testVirtual() { itn a = 0; } }; struct BaseWrap : Base, bp::wrapper { void testVirtual() { if (bp::override fun = this->get_override("testVirtual")) fun(); // *note* Base::testVirtual(); } void default_testVirtual() { this->Base::testVirtual(); } }; BOOST_PYTHON_MODULE(MyModule) { bp::class_("Base") .def("testVirtual", &Base::testVirtual, &BaseWrap::default_testVirtual) ; bp::class_("Base", bp::init<>()) ; } ------- Python ( testvirtual.py ) import MyModule class TestVirtual( MyModule.Base ): ## Constructor def __init__( self ) : pass ------------ Now from a C++ side : bp::object module = bp::import("testvirtual"); bp::object modDict(module.attr("__dict__")); bp::object bpClass = modDict["TestVirtual"]; bp::object bpTestVirtual = pbClass(); # Doesn't call the Base class virtual function and tell that the testVirtual function doesn't exist bp::object bpFun = bpTestVirtual.attr( "testVirtual" ); bpFun(); # Doesn't call the Base class virtual function and tell that the testVirtual function doesn't exist bp::call(bpFun.ptr()); # Doesn't call the Base class virtual function and tell that the testVirtual function doesn't exist bp::call_method(bpTestVirtual.ptr(), "testVirtual" ); # Always call the Base::testVirtual even if the function is overidden in Python Base& pTestVirtual = BP_EXTRACT( bpTestVirtual, Base& ); pTestVirtual->testVirtual() Here we go, so i'm sure there is an easy way to do that but i don't understand what i'm doing wrong. Could someone help me please ? Thanks William -- View this message in context: http://www.nabble.com/Calling-Object-virtual-function-from-C%2B%2B-tp19331891p19331891.html Sent from the Python - c++-sig mailing list archive at Nabble.com. _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig From snemeth at houston.rr.com Sat Sep 6 23:48:47 2008 From: snemeth at houston.rr.com (Rocketman@JSC) Date: Sat, 6 Sep 2008 14:48:47 -0700 (PDT) Subject: [C++-sig] Base class defined in other header file not includedin bp::bases<> In-Reply-To: <7465b6170607140134r5534654dtb54cec53cd930fa7@mail.gmail.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA880152C02F@NAEX11.na.qualcomm.com> <7465b6170607122218j6aeb97c4y2d1c60cff878037f@mail.gmail.com> <5383A81D5EB8F14FBDB487D9B5FBEA880152C24D@NAEX11.na.qualcomm.com> <7465b6170607140134r5534654dtb54cec53cd930fa7@mail.gmail.com> Message-ID: <19351675.post@talk.nabble.com> I have a few questions about this thread. Basically I have the same problem, my bases for a binding are not getting picked up cause they are in a separate library. I've tried solution #2 and here's a snippet from the Py++ generator file: < baseModel_creators = code_creators.creator_finder.find_by_declaration( lambda decl: decl is baseModel, mb.code_creator, False ) File "/data/apps/oss/lib/python/pyplusplus/code_creators/algorithm.py", line 87, in find_by_declaration , search_area ) TypeError: 'module_t' object is not iterable I'm sure this is something I'm doing wrong, but I looked at the API's and could not figure it out. Does anyone have an idea what I'm doing wrong here? Thanks in advance for any help. Scott Roman Yakovenko wrote: > > On 7/13/06, Haridev, Meghana wrote: >> In other words, I want the generated code to look like this: >> /* bp::class_< baseB >( "baseB" ); - Do not want to generate */ >> >> bp::class_< baseA >( "baseA" ); >> >> bp::class_< derivedC, bp::bases< baseA, baseB > >( "derivedC" ); > > It is possible, but you will have to understand a little how code > creators works. > There are few ways to solve the problem. > > 1. Exclude baseB from being generated, and add it manually to derivedC. > Take a look on pyplusplus/code_creators/class_declaration.py - > class "class_t", > method - _generate_bases. You will have to redefine this method, > something > like this: > > def my_generate_bases( self, base_creators ): > if self.declaration.Name != 'derivedC': > run current code > else: > your logic is going her > > pyplusplus.code_creators.class_t._generate_bases = my_generate_bases > > 2. You can include baseB to be generated, but instead of generating code > it > will generate empty string: > > mb = module_builder_t( ... ) > mb.build_code_creators( ... ) > > from pyplusplus import code_creators > > baseB = mb.class_( 'baseB' ) > > baseB_creators \ > = code_creators.creator_finder.find_by_declaration( > lambda decl: decl is baseB #what to look > , mb.code_creator ) #where > #now, we have to leave only class_t and class_wrapper_t code creators > relevant_clss = ( code_creators.class_t, > code_creators.class_wrapper_t ) > baseB_creators = filters( lambda c: isinstance( c, relevant_clss ) ) > for c in baseB_creators: > c.create = lambda self: '' > > Second approach is better, because in this case pyplusplus will take > into account > all data available from baseB, but will not generate code. > > > -- > 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 > > -- View this message in context: http://www.nabble.com/-C%2B%2B-sig--Base-class-defined-in-other-header-file-not-included-in-bp%3A%3Abases%3C%3E-tp5299734p19351675.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From j.reid at mail.cryst.bbk.ac.uk Wed Sep 10 13:51:18 2008 From: j.reid at mail.cryst.bbk.ac.uk (John Reid) Date: Wed, 10 Sep 2008 12:51:18 +0100 Subject: [C++-sig] [Fwd: NumPy arrays that use memory allocated from other libraries or tools] Message-ID: I saw this on the rpy list and thought it might be of interest to some here. John. -------- Original Message -------- Subject: NumPy arrays that use memory allocated from other libraries or tools Date: Tue, 09 Sep 2008 23:39:26 -0500 From: Travis Oliphant To: python-list at python.org CC: rpy-list at lists.sourceforge.net, ctypes-users at lists.sourceforge.net Newsgroups: gmane.comp.python.general,gmane.comp.python.rpy,gmane.comp.python.ctypes I wanted to point anybody interested to a blog post that describes a useful pattern for having a NumPy array that points to the memory created by a different memory manager than the standard one used by NumPy. The pattern shows how to create a NumPy array that points to previously allocated memory and then shows how to construct an object that allows the correct deallocator to be called when the NumPy array is freed. This may be useful if you are wrapping code that has it's own memory management scheme. Comments and feedback is welcome. The post is http://blog.enthought.com/?p=62 Best regards, -Travis Oliphant -- http://mail.python.org/mailman/listinfo/python-list From william.marie at gmail.com Thu Sep 11 09:32:04 2008 From: william.marie at gmail.com (=?ISO-8859-1?Q?William_Mari=E9?=) Date: Thu, 11 Sep 2008 08:32:04 +0100 Subject: [C++-sig] Calling Object virtual function from C++ In-Reply-To: <765CBD9053EA2B438625895F30D1856FCA025885@storm.wdtinc.com> References: <19331891.post@talk.nabble.com> <765CBD9053EA2B438625895F30D1856FCA025885@storm.wdtinc.com> Message-ID: <48C8C974.1030603@gmail.com> Hi Bill, Thanks for your answer it was exactly that, no need to declare both the wrapper and the class ... Have a good day and thank a lot William William Ladwig a ?crit : > In your BOOST_PYTHON_MODULE you have "Base" defined twice. Try removing: > > bp::class_("Base", bp::init<>()) > ; > > since you already exposed your "Base" class using BaseWrapper. > > Also, in your python module, try adding the base class constructor: > > MyModule.Base.__init__(self) > > to the first line of your TestVirtual __init__ function. > > These are the first two places I would start. Hope this helps. > > Bill > > -----Original Message----- > From: c++-sig-bounces+wladwig=wdtinc.com at python.org [mailto:c++-sig-bounces+wladwig=wdtinc.com at python.org] On Behalf Of William Mari? > Sent: Friday, September 05, 2008 9:01 AM > To: c++-sig at python.org > Subject: [C++-sig] Calling Object virtual function from C++ > > > Hi, > > I'm trying to call a object virtual function from the C++ side but if the > python function is not defined into the Python class it doesn't find the one > defined in the C++ object : > > ------- C++ > class Base > { > public: > Base() {}; > virtual ~Base() {}; > virtual void testVirtual() > { > itn a = 0; > } > }; > > struct BaseWrap : Base, bp::wrapper > { > void testVirtual() > { > if (bp::override fun = this->get_override("testVirtual")) > fun(); // *note* > Base::testVirtual(); > } > void default_testVirtual() { this->Base::testVirtual(); } > }; > > BOOST_PYTHON_MODULE(MyModule) > { > bp::class_("Base") > .def("testVirtual", &Base::testVirtual, &BaseWrap::default_testVirtual) > ; > > bp::class_("Base", bp::init<>()) > ; > } > ------- Python ( testvirtual.py ) > import MyModule > > class TestVirtual( MyModule.Base ): > > ## Constructor > def __init__( self ) : > pass > > ------------ Now from a C++ side : > bp::object module = bp::import("testvirtual"); > bp::object modDict(module.attr("__dict__")); > bp::object bpClass = modDict["TestVirtual"]; > bp::object bpTestVirtual = pbClass(); > > # Doesn't call the Base class virtual function and tell that the testVirtual > function doesn't exist > bp::object bpFun = bpTestVirtual.attr( "testVirtual" ); > bpFun(); > > # Doesn't call the Base class virtual function and tell that the testVirtual > function doesn't exist bp::call(bpFun.ptr()); > > # Doesn't call the Base class virtual function and tell that the testVirtual > function doesn't exist > bp::call_method(bpTestVirtual.ptr(), "testVirtual" ); > > # Always call the Base::testVirtual even if the function is overidden in > Python > Base& pTestVirtual = BP_EXTRACT( bpTestVirtual, Base& ); > pTestVirtual->testVirtual() > > > Here we go, so i'm sure there is an easy way to do that but i don't > understand what i'm doing wrong. > Could someone help me please ? > Thanks > > William > > > -- > View this message in context: http://www.nabble.com/Calling-Object-virtual-function-from-C%2B%2B-tp19331891p19331891.html > Sent from the Python - c++-sig mailing list archive at Nabble.com. > > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > From william.marie at gmail.com Thu Sep 11 09:39:31 2008 From: william.marie at gmail.com (=?UTF-8?Q?William_Mari=C3=A9?=) Date: Thu, 11 Sep 2008 00:39:31 -0700 (PDT) Subject: [C++-sig] Call Python functions generically Message-ID: <19429482.post@talk.nabble.com> Hi, I would like to know if there is an easy way to create generic python function calls using a tuple or something like that. ex : void call( const char* szFunName, bp::tuple bpTuple ) { // Something like that bp::call( szFunName, bpTuple ); } bpTuple being the args list... In Python to do that we can use something like that : arglist = Py_BuildValue("(i)", arg); result = PyEval_CallObject(my_callback, arglist); but i don't if a similar mechanism exists in boost python. Thanks for your help William -- View this message in context: http://www.nabble.com/Call-Python-functions-generically-tp19429482p19429482.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From seefeld at sympatico.ca Thu Sep 11 14:01:40 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 11 Sep 2008 08:01:40 -0400 Subject: [C++-sig] Call Python functions generically In-Reply-To: <19429482.post@talk.nabble.com> References: <19429482.post@talk.nabble.com> Message-ID: <48C908A4.1070403@sympatico.ca> William Mari? wrote: > Hi, > > I would like to know if there is an easy way to create generic python > function calls using a tuple or something like that. > I have checked in support for that into trunk a couple of weeks ago. I'm not sure when this will make it into a release. > ex : > > void call( const char* szFunName, bp::tuple bpTuple ) > { > // Something like that > bp::call( szFunName, bpTuple ); > } > > bpTuple being the args list... > Assuming you have a callable object, you simply invoke the call operator, passing an argument sequence, and optionally, a keyword dictionary, using the same syntax as in Python, i.e. unary '*' and '**' operators: object args = ...; object kwds = ...; object callable = ...; callable(*args, **kwds); Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From william.marie at gmail.com Fri Sep 12 09:57:53 2008 From: william.marie at gmail.com (=?ISO-8859-1?Q?William_Mari=E9?=) Date: Fri, 12 Sep 2008 08:57:53 +0100 Subject: [C++-sig] Call Python functions generically In-Reply-To: <48C908A4.1070403@sympatico.ca> References: <19429482.post@talk.nabble.com> <48C908A4.1070403@sympatico.ca> Message-ID: <48CA2101.8010301@gmail.com> Hi Stefan, Ok this would be a very pretty way. Do you know when this feature will be commited ? Stefan Seefeld a ?crit : > William Mari? wrote: >> Hi, >> >> I would like to know if there is an easy way to create generic python >> function calls using a tuple or something like that. >> > > I have checked in support for that into trunk a couple of weeks ago. > I'm not sure when this will make it into a release. >> ex : >> void call( const char* szFunName, bp::tuple bpTuple ) >> { >> // Something like that >> bp::call( szFunName, bpTuple ); >> } >> >> bpTuple being the args list... >> > > Assuming you have a callable object, you simply invoke the call > operator, passing an argument sequence, and optionally, a keyword > dictionary, using the same syntax as in Python, i.e. unary '*' and > '**' operators: > > object args = ...; > object kwds = ...; > > object callable = ...; > > callable(*args, **kwds); > > > Regards, > Stefan > From seefeld at sympatico.ca Fri Sep 12 12:59:08 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 12 Sep 2008 06:59:08 -0400 Subject: [C++-sig] Call Python functions generically In-Reply-To: <48CA2101.8010301@gmail.com> References: <19429482.post@talk.nabble.com> <48C908A4.1070403@sympatico.ca> <48CA2101.8010301@gmail.com> Message-ID: <48CA4B7C.7010307@sympatico.ca> William Mari? wrote: > Hi Stefan, > > Ok this would be a very pretty way. Do you know when this feature will > be commited ? As I indicated, it already is (to trunk). Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From surye80 at gmail.com Sat Sep 13 08:56:59 2008 From: surye80 at gmail.com (Vincent Castellano) Date: Fri, 12 Sep 2008 23:56:59 -0700 Subject: [C++-sig] Basic example segfaults in PyInt_FromLong Message-ID: <8628ae450809122356w19a92b27o88539f3713946c17@mail.gmail.com> I'm attempting to compile and run the following: #include #include using namespace boost::python; using namespace std; int main(int argc, char* argv[]) { object result; int number; result = eval("5 ** 2"); number = extract(result); cout << number << endl; return 0; } g++ boostpythontest.cpp -o boostpython -I /usr/include/python2.5/ -lboost_python -lpython2.5 It's segfaulting with the following backtrace: #0 0x00002aad3d27cb6c in PyInt_FromLong () from /usr/lib/libpython2.5.so.1.0 #1 0x00002aad3d2ca80b in ?? () from /usr/lib/libpython2.5.so.1.0 #2 0x00002aad3d2ccf01 in ?? () from /usr/lib/libpython2.5.so.1.0 #3 0x00002aad3d2cb702 in ?? () from /usr/lib/libpython2.5.so.1.0 #4 0x00002aad3d2cf835 in PyAST_FromNode () from /usr/lib/libpython2.5.so.1.0 #5 0x00002aad3d2fcb74 in PyParser_ASTFromString () from /usr/lib/libpython2.5.so.1.0 #6 0x00002aad3d2fcd6c in PyRun_StringFlags () from /usr/lib/libpython2.5.so.1.0 #7 0x00002aad3d0135d3 in boost::python::eval () from /usr/lib/libboost_python-gcc42-mt-1_34_1.so.1.34.1 #8 0x00000000004012a5 in main (argc=1, argv=0x7fff6dcee1a8) at boostpythontest.cpp:10 I set this up as a basic test of the library, taken pretty much from the tutorial. boost and python on Hardy Ubuntu. Any pointers would be really appreciated, I cannot figure out where this could be failing so early. From roman.yakovenko at gmail.com Sun Sep 14 08:32:43 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sun, 14 Sep 2008 09:32:43 +0300 Subject: [C++-sig] Base class defined in other header file not includedin bp::bases<> In-Reply-To: <19351675.post@talk.nabble.com> References: <5383A81D5EB8F14FBDB487D9B5FBEA880152C02F@NAEX11.na.qualcomm.com> <7465b6170607122218j6aeb97c4y2d1c60cff878037f@mail.gmail.com> <5383A81D5EB8F14FBDB487D9B5FBEA880152C24D@NAEX11.na.qualcomm.com> <7465b6170607140134r5534654dtb54cec53cd930fa7@mail.gmail.com> <19351675.post@talk.nabble.com> Message-ID: <7465b6170809132332m1b43469dsdf6083ceec5de0e2@mail.gmail.com> On Sun, Sep 7, 2008 at 12:48 AM, Rocketman at JSC wrote: > Roman Yakovenko wrote: >> >> On 7/13/06, Haridev, Meghana wrote: >>> In other words, I want the generated code to look like this: >>> /* bp::class_< baseB >( "baseB" ); - Do not want to generate */ >>> >>> bp::class_< baseA >( "baseA" ); >>> >>> bp::class_< derivedC, bp::bases< baseA, baseB > >( "derivedC" ); Did you read the following document http://language-binding.net/pyplusplus/documentation/multi_module_development.html ? I think, it will give you easy and clear way to achieve what you want. HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From mattias.brandstrom at todos.se Wed Sep 17 09:58:14 2008 From: mattias.brandstrom at todos.se (=?iso-8859-1?Q?Mattias_Br=E4ndstr=F6m?=) Date: Wed, 17 Sep 2008 09:58:14 +0200 Subject: [C++-sig] Attributes of type char[x]... Message-ID: Hello! I'm trying to expose a C++ (using Boost.Python) class containing public char[] members to python. I would have guessed that I would be able to do this using def_readwrite(). However, I get a compilation error when trying to do so. Here is a minimal example of what I'm trying to do: #include class A { public: char foo[10]; }; BOOST_PYTHON_MODULE(pyconfdb) { using namespace boost::python; class_("A") .def_readwrite("foo", &A::foo) ; } This is the compilation error message: c:\3pty\boost_1_35_0\boost\python\data_members.hpp(64) : error C2440: '=' : cannot convert from 'const char [10]' to 'char [10]' I have not used the Boost.Python library for that long so there might be something obvious I'm missing. Any tips will be greatly appreciated! Regards, Mattias From roman.yakovenko at gmail.com Wed Sep 17 10:19:25 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 17 Sep 2008 11:19:25 +0300 Subject: [C++-sig] Attributes of type char[x]... In-Reply-To: References: Message-ID: <7465b6170809170119u551eede2tec343e45703d533a@mail.gmail.com> On Wed, Sep 17, 2008 at 10:58 AM, Mattias Br?ndstr?m wrote: > Hello! > > I'm trying to expose a C++ (using Boost.Python) class containing public char[] members to python. I would have guessed that I would be able to do this using def_readwrite(). However, I get a compilation error when trying to do so. Here is a minimal example of what I'm trying to do: > > #include > > class A > { > public: > char foo[10]; > }; You have 3 approaches to expose such code: * to create a wrapper like this http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/pyplusplus/code_repository/array_1.py?revision=1097&view=markup and the usage code attached * to use ctypes module and expose "foo" address * to use Py++ and have both possibilities :-) HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: usage.cpp URL: From ope-devel at gmx.de Thu Sep 18 07:50:03 2008 From: ope-devel at gmx.de (Olaf Peter) Date: Thu, 18 Sep 2008 07:50:03 +0200 Subject: [C++-sig] [boost.python] Debug build on win32 runtime trouble Message-ID: <48D1EC0B.5030405@gmx.de> Hi, I run into trouble on my debug build on windows, which works fine on linux. The interesting point is, that it does crash in python.dll and not python_d.dll. Depency walker shows: d:\work\...\build\debug\PYTHON25_D.DLL d:\work\...\build\debug\BOOST_PYTHON-VC80-MT-GD-1_35.DLL + c:\windows\system32\PYTHON25.DLL Here we are. I've used cmake and compile flags: /Od /I .... /D "WIN32" /D "_WINDOWS" /D "_DEBUG" /D "BOOST_ENABLE_ASSERT_HANDLER=1" /D "BOOST_ALL_DYN_LINK" /D "BOOST_LIB_DIAGNOSTIC" /D "BOOST_DEBUG_PYTHON" /D "QT_DLL" /D "QT_GUI_LIB" /D "QT_CORE_LIB" /D "QT_DEBUG" /D "CMAKE_INTDIR=\"Debug\"" /D "_MBCS" /FD /EHsc /RTC1 /MDd /Fo"eda.dir\Debug\\" /Fd"D:/work/.../build/Debug/eda.pdb" /W3 /nologo /c /Zi /TP /errorReport:prompt Note the BOOST_DEBUG_PYTHON define. As linker flags I've used: /OUT:"D:\work\...\build\Debug\eda.exe" /VERSION:0.0 /INCREMENTAL /NOLOGO /LIBPATH:"optimized\Debug" /LIBPATH:"optimized" /LIBPATH:"C:\Cpp\boost_1_35_0\lib\boost_iostreams-vc80-mt-1_35.lib\Debug" /LIBPATH:"C:\Cpp\boost_1_35_0\lib\boost_iostreams-vc80-mt-1_35.lib" /LIBPATH:"debug\Debug" /LIBPATH:"debug" /LIBPATH:"C:\Cpp\boost_1_35_0\lib\Debug" /LIBPATH:"C:\Cpp\boost_1_35_0\lib" /LIBPATH:"C:\Programme\Python25\libs\Debug" /MANIFEST /MANIFESTFILE:"eda.dir\Debug\eda.exe.intermediate.manifest" /DEBUG /PDB:"D:/work/.../build/Debug/eda.pdb" /SUBSYSTEM:WINDOWS /IMPLIB:"D:\work\...\build\Debug\eda.lib" /ERRORREPORT:PROMPT kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib C:\Cpp\qt-win-opensource-4.4.1-msvc2005\lib\qtmaind.lib C:\Cpp\qt-win-opensource-4.4.1-msvc2005\lib\QtGuid4.lib imm32.lib winmm.lib C:\Cpp\qt-win-opensource-4.4.1-msvc2005\lib\QtCored4.lib ws2_32.lib python25_d.lib C:\Cpp\boost_1_35_0\lib\boost_python-vc80-mt-gd-1_35.lib C:\Cpp\boost_1_35_0\lib\boost_program_options-vc80-mt-gd-1_35.lib C:\Cpp\boost_1_35_0\lib\boost_thread-vc80-mt-gd-1_35.lib C:\Cpp\boost_1_35_0\lib\boost_filesystem-vc80-mt-gd-1_35.lib C:\Cpp\boost_1_35_0\lib\boost_iostreams-vc80-mt-gd-1_35.lib for MS VS 2005 (MSVC 8.0) Did I miss something on win32?? Thanks, Olaf From william.marie at gmail.com Thu Sep 18 14:21:38 2008 From: william.marie at gmail.com (=?UTF-8?Q?William_Mari=C3=A9?=) Date: Thu, 18 Sep 2008 05:21:38 -0700 (PDT) Subject: [C++-sig] How to use BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID ? Message-ID: <19551601.post@talk.nabble.com> Hi, I would need to create an opaque boost python type, i had a look on the internet, here is what i found for instance to declare a void* opaque type : struct void_ {}; BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(void_); This works well if you have to call C++ function from Python but how do i do if i want to call a python function from C++ with a void* parameter : ex : void* pFoo = 0x78985465; object callable = ....; callable( (void_*)pFoo ); I can't find out how to make this work properly i always have a runtime error : TypeError: No to_python (by-value) converter found for C++ type: struct void_ * Thanks for your help William -- View this message in context: http://www.nabble.com/How-to-use-BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID---tp19551601p19551601.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From merlin66b at gmail.com Thu Sep 18 20:44:03 2008 From: merlin66b at gmail.com (Thomas Berg) Date: Thu, 18 Sep 2008 20:44:03 +0200 Subject: [C++-sig] [boost.python] Debug build on win32 runtime trouble In-Reply-To: <48D1EC0B.5030405@gmx.de> References: <48D1EC0B.5030405@gmx.de> Message-ID: On Thu, Sep 18, 2008 at 7:50 AM, Olaf Peter wrote: > Hi, > > I run into trouble on my debug build on windows, which works fine on linux. > The interesting point is, that it does crash in python.dll and not > python_d.dll. > > Depency walker shows: > > d:\work\...\build\debug\PYTHON25_D.DLL > d:\work\...\build\debug\BOOST_PYTHON-VC80-MT-GD-1_35.DLL > + c:\windows\system32\PYTHON25.DLL > > Here we are. > > I've used cmake and compile flags: > > /Od /I .... /D "WIN32" /D "_WINDOWS" /D "_DEBUG" /D > "BOOST_ENABLE_ASSERT_HANDLER=1" /D "BOOST_ALL_DYN_LINK" /D > "BOOST_LIB_DIAGNOSTIC" /D "BOOST_DEBUG_PYTHON" /D "QT_DLL" /D "QT_GUI_LIB" > /D "QT_CORE_LIB" /D "QT_DEBUG" /D "CMAKE_INTDIR=\"Debug\"" /D "_MBCS" /FD > /EHsc /RTC1 /MDd /Fo"eda.dir\Debug\\" /Fd"D:/work/.../build/Debug/eda.pdb" > /W3 /nologo /c /Zi /TP /errorReport:prompt > > Note the BOOST_DEBUG_PYTHON define. As linker flags I've used: > > /OUT:"D:\work\...\build\Debug\eda.exe" /VERSION:0.0 /INCREMENTAL /NOLOGO > /LIBPATH:"optimized\Debug" /LIBPATH:"optimized" > /LIBPATH:"C:\Cpp\boost_1_35_0\lib\boost_iostreams-vc80-mt-1_35.lib\Debug" > /LIBPATH:"C:\Cpp\boost_1_35_0\lib\boost_iostreams-vc80-mt-1_35.lib" > /LIBPATH:"debug\Debug" /LIBPATH:"debug" > /LIBPATH:"C:\Cpp\boost_1_35_0\lib\Debug" /LIBPATH:"C:\Cpp\boost_1_35_0\lib" > /LIBPATH:"C:\Programme\Python25\libs\Debug" /MANIFEST > /MANIFESTFILE:"eda.dir\Debug\eda.exe.intermediate.manifest" /DEBUG > /PDB:"D:/work/.../build/Debug/eda.pdb" /SUBSYSTEM:WINDOWS > /IMPLIB:"D:\work\...\build\Debug\eda.lib" /ERRORREPORT:PROMPT kernel32.lib > user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib > uuid.lib comdlg32.lib advapi32.lib > C:\Cpp\qt-win-opensource-4.4.1-msvc2005\lib\qtmaind.lib > C:\Cpp\qt-win-opensource-4.4.1-msvc2005\lib\QtGuid4.lib imm32.lib winmm.lib > C:\Cpp\qt-win-opensource-4.4.1-msvc2005\lib\QtCored4.lib ws2_32.lib > python25_d.lib C:\Cpp\boost_1_35_0\lib\boost_python-vc80-mt-gd-1_35.lib > C:\Cpp\boost_1_35_0\lib\boost_program_options-vc80-mt-gd-1_35.lib > C:\Cpp\boost_1_35_0\lib\boost_thread-vc80-mt-gd-1_35.lib > C:\Cpp\boost_1_35_0\lib\boost_filesystem-vc80-mt-gd-1_35.lib > C:\Cpp\boost_1_35_0\lib\boost_iostreams-vc80-mt-gd-1_35.lib > > for MS VS 2005 (MSVC 8.0) > > Did I miss something on win32?? > > Thanks, > Olaf > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > To get a pure python-debug build you need to explicitly tell boost to link to the debug version of python, by passing the option python-debugging=on to bjam when building boost. You will ghen get boost_python-vc80-mt-gyd-1_35.lib and .dll instead, and need to link to that. You also need to define BOOST_DEBUG_PYTHON when you build your application, see this url: http://www.boost.org/doc/libs/1_36_0/libs/python/doc/building.html#python-debugging-builds Hope this helps! Thomas From s_sourceforge at nedprod.com Thu Sep 18 21:24:59 2008 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Thu, 18 Sep 2008 20:24:59 +0100 Subject: [C++-sig] How to use BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID ? In-Reply-To: <19551601.post@talk.nabble.com> References: <19551601.post@talk.nabble.com> Message-ID: <48D2B91B.13559.16935278@s_sourceforge.nedprod.com> On 18 Sep 2008 at 5:21, William Mari? wrote: > I would need to create an opaque boost python type, i had a look on the > internet, here is what i found for instance to declare a void* opaque type : > > struct void_ {}; > BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(void_); No, that's trickery from before my 2005 patch implementing native void * support. > This works well if you have to call C++ function from Python but how do i do > if i want to call a python function from C++ with a void* parameter : > > ex : > void* pFoo = 0x78985465; > object callable = ....; > callable( (void_*)pFoo ); > > I can't find out how to make this work properly i always have a runtime > error : > TypeError: No to_python (by-value) converter found for C++ type: struct > void_ * It just automatically works as an opaque pointer. Python cannot access the value, but can pass it into functions. There should be an example in the regression suite. Cheers, Niall From ope-devel at gmx.de Fri Sep 19 07:21:30 2008 From: ope-devel at gmx.de (Olaf Peter) Date: Fri, 19 Sep 2008 07:21:30 +0200 Subject: [C++-sig] [boost.python] Debug build on win32 runtime trouble In-Reply-To: References: <48D1EC0B.5030405@gmx.de> Message-ID: <48D336DA.5000005@gmx.de> >> Depency walker shows: >> >> d:\work\...\build\debug\PYTHON25_D.DLL >> d:\work\...\build\debug\BOOST_PYTHON-VC80-MT-GD-1_35.DLL >> + c:\windows\system32\PYTHON25.DLL [...] >> Note the BOOST_DEBUG_PYTHON define. As linker flags I've used: > To get a pure python-debug build you need to explicitly tell boost to > link to the debug version of python, by passing the option > python-debugging=on to bjam when building boost. You will ghen get > boost_python-vc80-mt-gyd-1_35.lib and .dll instead, and need to link > to that. > > You also need to define BOOST_DEBUG_PYTHON when you build your > application, see this url: > http://www.boost.org/doc/libs/1_36_0/libs/python/doc/building.html#python-debugging-builds As first thank you for the answer. Anyway, I assume the debug build of boost_python dll ist wrong and points to the wrong python25.dll, should be python25_d.dll. Could it be? I've used the boostpro windows installer 1.35.0. Thanks, Olaf PS: My code works without defined BOOST_DEBUG_PYTHON; my crash results really from different memory layout of the python25{_d}.dll used by BOOST_PYTHON-VC80-MT-GD-1_35.DLL. From dave at boostpro.com Fri Sep 19 08:16:56 2008 From: dave at boostpro.com (David Abrahams) Date: Thu, 18 Sep 2008 23:16:56 -0700 Subject: [C++-sig] [boost.python] Debug build on win32 runtime trouble In-Reply-To: <48D336DA.5000005@gmx.de> (Olaf Peter's message of "Fri, 19 Sep 2008 07:21:30 +0200") References: <48D1EC0B.5030405@gmx.de> <48D336DA.5000005@gmx.de> Message-ID: <87zlm4mzhj.fsf@mcbain.luannocracy.com> on Thu Sep 18 2008, Olaf Peter wrote: >>> Depency walker shows: >>> >>> d:\work\...\build\debug\PYTHON25_D.DLL >>> d:\work\...\build\debug\BOOST_PYTHON-VC80-MT-GD-1_35.DLL >>> + c:\windows\system32\PYTHON25.DLL > [...] >>> Note the BOOST_DEBUG_PYTHON define. As linker flags I've used: > >> To get a pure python-debug build you need to explicitly tell boost to >> link to the debug version of python, by passing the option >> python-debugging=on to bjam when building boost. You will ghen get >> boost_python-vc80-mt-gyd-1_35.lib and .dll instead, and need to link >> to that. >> >> You also need to define BOOST_DEBUG_PYTHON when you build your >> application, see this url: >> > http://www.boost.org/doc/libs/1_36_0/libs/python/doc/building.html#python-debugging-builds > > As first thank you for the answer. Anyway, I assume the debug build of > boost_python dll ist wrong and points to the wrong python25.dll, should be > python25_d.dll. Could it be? No, the build is right, i.e. intentional. Please read the URL as suggested by Olaf, it really does explain everything. Also please follow the advice in http://www.boost.org/doc/libs/1_36_0/libs/python/doc/building.html#no-install-quickstart for people like you who want to use build systems other than bjam. It will potentially save you a great deal of pain. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From renjipanicker at gmail.com Fri Sep 19 12:35:14 2008 From: renjipanicker at gmail.com (Renji Panicker) Date: Fri, 19 Sep 2008 16:05:14 +0530 Subject: [C++-sig] Returning None from __getitem__ Message-ID: <3c0065cc0809190335s7219b866hada4f7d71eb48458@mail.gmail.com> Hi all, I am using boost.python to wrap a map class. My get function looks like this (edited): std::map _values; class PropSet { std::wstring get(const std::wstring& key) { if(_values.find(key) == _values.end()) throw InvalidKeyException(); return _values[key]; } }; In my python code, I would like to be able to do this: if ps["ssl"] == "yes": print "ssl enabled" My requirement in brief is, if the key 'ssl' exists in the map, and its value is 'yes', turn on SSL. If the key does not exist, or has any value other than 'yes', do not turn SSL on. To that extent, I would like to return None from the get function if the key does not exist, instead of throwing an exception. My obvious workaround is to use an empty string, but I am expecting use cases where the empty string can be a valid value for a key. So I will have to consider using some other string which cannot be a potential value, to indicate an empty value. But before I go with this workaround, I would like to know if there is any standard way of achieving this. Thanks in advance. -/renji From meine at informatik.uni-hamburg.de Fri Sep 19 13:11:40 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Fri, 19 Sep 2008 13:11:40 +0200 Subject: [C++-sig] pybindgen and PyQt In-Reply-To: <1f4b35e00808210532g252d2df0mf4885bc972feda66@mail.gmail.com> References: <1f4b35e00808210006r66135dcfr1d52678adc0eac1@mail.gmail.com> <1f4b35e00808210532g252d2df0mf4885bc972feda66@mail.gmail.com> Message-ID: <200809191311.41721.meine@informatik.uni-hamburg.de> On Thursday 21 August 2008 14:32:00 Andreas Eriksson wrote: > We have a C++ class that derives from the C++ Qt class QWidget. Lets > call it OwnWidget. Several Qt methods expects pointers to subclasses > of QWidget, such as QLayout::addWidget(QWidget*). Using the PyQt > bindings I can create a python QLayout object. Now, what I want to do > is create a pybindgen wrapped OwnWidget instance and pass it to the > addWidget method of the PyQt QLayout object. That's a very common need and has been discussed on this list before. I have done that in the past (and posted on it); basically I used SIP's functions to (un)wrap the C++ pointer within my BPL code. Then, I simply returned the PyObject * and exported that using B::P. Please try to find previous discussions, if you don't find anything I can try. HTH, Hans From seefeld at sympatico.ca Fri Sep 19 14:07:46 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 19 Sep 2008 08:07:46 -0400 Subject: [C++-sig] Returning None from __getitem__ In-Reply-To: <3c0065cc0809190335s7219b866hada4f7d71eb48458@mail.gmail.com> References: <3c0065cc0809190335s7219b866hada4f7d71eb48458@mail.gmail.com> Message-ID: <48D39612.3060303@sympatico.ca> Hi Renji, From a language / API design point of view, I think it would be best (i.e., least surprising) to follow the expected behavior. Python's dict type does have ways to handle what you want (and you can even set default values to be returned for non-existent keys). As for your particular case, where your C++ map throws an exception, but you want the wrapper to return None, that's simple, too: boost::python::object my_get_wrapper(PropSet &p, std::wstring const &k) { try { return p->get(k);} // implicitly convert valid value to python object catch (InvalidKeyException const &) { return boost::python::object();} // on failure, return None } Then, you bind that as 'get': class_ (...) .def("get", my_get_wrapper); HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From aeriksson at gmail.com Fri Sep 19 17:01:45 2008 From: aeriksson at gmail.com (Andreas Eriksson) Date: Fri, 19 Sep 2008 17:01:45 +0200 Subject: [C++-sig] pybindgen and PyQt In-Reply-To: <200809191311.41721.meine@informatik.uni-hamburg.de> References: <1f4b35e00808210006r66135dcfr1d52678adc0eac1@mail.gmail.com> <1f4b35e00808210532g252d2df0mf4885bc972feda66@mail.gmail.com> <200809191311.41721.meine@informatik.uni-hamburg.de> Message-ID: <1f4b35e00809190801s1309df26h2bc23f78dd6f25c3@mail.gmail.com> > > We have a C++ class that derives from the C++ Qt class QWidget. Lets > > call it OwnWidget. Several Qt methods expects pointers to subclasses > > of QWidget, such as QLayout::addWidget(QWidget*). Using the PyQt > > bindings I can create a python QLayout object. Now, what I want to do > > is create a pybindgen wrapped OwnWidget instance and pass it to the > > addWidget method of the PyQt QLayout object. > That's a very common need and has been discussed on this list before. > > I have done that in the past (and posted on it); basically I used SIP's > functions to (un)wrap the C++ pointer within my BPL code. Then, I simply > returned the PyObject * and exported that using B::P. > > Please try to find previous discussions, if you don't find anything I can try. Thanks, are you referring to the following from http://mail.python.org/pipermail/c++-sig/2005-November/009865.html? (Included at bottom of post) I'll have a look at this and probably come back with more questions... Regards, Andreas > > > Can I tell boost to use qt objects from PyQt instead of generating it's > > > own version of them? > > > > That's probably something you'd have to figure out yourself. A few months > > ago someone asked a similar question regarding SWIG. It was surprisingly > > easy to support passing SWIG-wrapped objects to Boost.Python wrapped > > functions: > > > > http://cvs.sourceforge.net/viewcvs.py/cctbx/boost_adaptbx/swig_arg.hpp?view > >=markup > > > > http://cvs.sourceforge.net/viewcvs.py/cctbx/boost_adaptbx/swig_args_ext.cpp > >?view=markup > > > > If you are lucky, maybe something similar works for SIP. You'd have to find > > out how the SIP wrapping mechanism works. > > Yes, it is possible; for example I did it with code fragments like these (this > is also a hint for Jan Langer how such a thing could look like): > > #include // my SIP module was called vigraqt > > PyObject *getViewer() > { > return sipMapCppToSelfSubClass( > somehowGetViewer(), sipClass_QImageViewer); > } > > BOOST_PYTHON_MODULE_INIT(sipbridge) > { > def("viewer", &getViewer); > } Btw, the URLs above seem to have changed into the following: http://cctbx.svn.sourceforge.net/viewvc/cctbx/trunk/boost_adaptbx/swig_arg.hpp?view=markup http://cctbx.svn.sourceforge.net/viewvc/cctbx/trunk/boost_adaptbx/swig_args_ext.cpp?view=markup From hans_meine at gmx.net Fri Sep 19 20:20:48 2008 From: hans_meine at gmx.net (Hans Meine) Date: Fri, 19 Sep 2008 20:20:48 +0200 Subject: [C++-sig] pybindgen and PyQt In-Reply-To: <1f4b35e00809190801s1309df26h2bc23f78dd6f25c3@mail.gmail.com> References: <1f4b35e00808210006r66135dcfr1d52678adc0eac1@mail.gmail.com> <200809191311.41721.meine@informatik.uni-hamburg.de> <1f4b35e00809190801s1309df26h2bc23f78dd6f25c3@mail.gmail.com> Message-ID: <200809192020.58995.hans_meine@gmx.net> On Freitag 19 September 2008, Andreas Eriksson wrote: > are you referring to the following from > http://mail.python.org/pipermail/c++-sig/2005-November/009865.html? > [...] > > #include // my SIP module was called vigraqt > > > > PyObject *getViewer() > > { > > return sipMapCppToSelfSubClass( > > somehowGetViewer(), sipClass_QImageViewer); > > } > > > > BOOST_PYTHON_MODULE_INIT(sipbridge) > > { > > def("viewer", &getViewer); > > } Correct, that's how I did the to-python conversion. Of course, the C++ object must not be destroyed before the python one, but I am not so sure about the lifecycle management in SIP and at least this is a working start. Probably you cannot even do much better. -- Ciao, / / .o. /--/ ..o / / ANS ooo -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 197 bytes Desc: This is a digitally signed message part. URL: From agrimstrup at gmail.com Fri Sep 19 22:37:34 2008 From: agrimstrup at gmail.com (Arne Grimstrup) Date: Fri, 19 Sep 2008 14:37:34 -0600 Subject: [C++-sig] .def( bp::self == bp::self ); causes compile problems for Py++ generated wrapper Message-ID: <48D40D8E.4040604@gmail.com> I'm having some trouble with Py++ generating a wrapper for a class that defines a == operator. The class definition looks like this: class MyFrac { private: int numerator; int denominator; friend std::ostream& operator<<(std::ostream&, MyFrac&); public: MyFrac(void) : numerator(1), denominator(1) {} MyFrac(int n, int d) : numerator(n), denominator(d) {} bool operator==(MyFrac& b) const { return numerator == b.numerator && denominator == b.denominator; } }; When the code is generated using pyplusplus_gui, the resulting C++ bindings look like this: #include "boost/python.hpp" #include "MyFrac.h" namespace bp = boost::python; BOOST_PYTHON_MODULE(MyFrac){ bp::class_< MyFrac >( "MyFrac" ) .def( bp::init< >() ) .def( bp::init< int, int >(( bp::arg("n"), bp::arg("d") )) ) .def( bp::self == bp::self ); } Attempting to compile the bindings gives: operators.hpp:226: error: no match for ?operator==? in ?l == r? MyFrac.h:14: note: candidates are: bool MyFrac::operator==(MyFrac&) const function/function_base.hpp:577: note: bool boost::operator==(const boost::function_base&, boost::detail::function::useless_clear_type*) function/function_base.hpp:589: note: bool boost::operator==(boost::detail::function::useless_clear_type*, const boost::function_base&) Removing the line ".def( bp::self == bp::self );" from the bindings allows it to compile cleanly and the resulting code works correctly from what I can tell. Could anyone shed light on what I've done wrong and how to fix the error? Thanks, Arne From wladwig at wdtinc.com Fri Sep 19 23:37:09 2008 From: wladwig at wdtinc.com (William Ladwig) Date: Fri, 19 Sep 2008 16:37:09 -0500 Subject: [C++-sig] .def( bp::self == bp::self ); causes compile problems for Py++ generated wrapper In-Reply-To: <48D40D8E.4040604@gmail.com> References: <48D40D8E.4040604@gmail.com> Message-ID: <765CBD9053EA2B438625895F30D1856FCBC2C537@storm.wdtinc.com> If you can change the C++ code, try declaring the operator== argument as a const reference (should probably do this anyhow since you aren't modifying the object): bool operator==(const MyFrac& b) const { return numerator == b.numerator && denominator == b.denominator;} Hope this helps, Bill -----Original Message----- From: c++-sig-bounces+wladwig=wdtinc.com at python.org [mailto:c++-sig-bounces+wladwig=wdtinc.com at python.org] On Behalf Of Arne Grimstrup Sent: Friday, September 19, 2008 3:38 PM To: c++-sig at python.org Subject: [C++-sig] .def( bp::self == bp::self ); causes compile problems for Py++ generated wrapper I'm having some trouble with Py++ generating a wrapper for a class that defines a == operator. The class definition looks like this: class MyFrac { private: int numerator; int denominator; friend std::ostream& operator<<(std::ostream&, MyFrac&); public: MyFrac(void) : numerator(1), denominator(1) {} MyFrac(int n, int d) : numerator(n), denominator(d) {} bool operator==(MyFrac& b) const { return numerator == b.numerator && denominator == b.denominator; } }; When the code is generated using pyplusplus_gui, the resulting C++ bindings look like this: #include "boost/python.hpp" #include "MyFrac.h" namespace bp = boost::python; BOOST_PYTHON_MODULE(MyFrac){ bp::class_< MyFrac >( "MyFrac" ) .def( bp::init< >() ) .def( bp::init< int, int >(( bp::arg("n"), bp::arg("d") )) ) .def( bp::self == bp::self ); } Attempting to compile the bindings gives: operators.hpp:226: error: no match for 'operator==' in 'l == r' MyFrac.h:14: note: candidates are: bool MyFrac::operator==(MyFrac&) const function/function_base.hpp:577: note: bool boost::operator==(const boost::function_base&, boost::detail::function::useless_clear_type*) function/function_base.hpp:589: note: bool boost::operator==(boost::detail::function::useless_clear_type*, const boost::function_base&) Removing the line ".def( bp::self == bp::self );" from the bindings allows it to compile cleanly and the resulting code works correctly from what I can tell. Could anyone shed light on what I've done wrong and how to fix the error? Thanks, Arne _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig From jean-sebastien.guay at cm-labs.com Sat Sep 20 06:24:14 2008 From: jean-sebastien.guay at cm-labs.com (=?ISO-8859-1?Q?Jean-S=E9bastien_Guay?=) Date: Sat, 20 Sep 2008 00:24:14 -0400 Subject: [C++-sig] Wrapping a class whose constructor takes a pointer to an abstract class Message-ID: <48D47AEE.6050405@cm-labs.com> Hi all, I've just started trying to use boost.python on a personal project of mine, and I decided to start with the hardest part :-) I'm trying to wrap OpenSceneGraph... It's going really well for the most part, and I do mean really well. In little time I've got a good number of classes working and tested. But I've hit a roadblock. I've done a lot of searching and have found lots of posts on this list dealing with pure virtual methods, but never anything about a method taking a pointer to an abstract class as a parameter. I have to wrap the following structure: class osg::Shape : public osg::Object { // has some pure virtual methods, none of which I need to // expose to python at this point }; class osg::Sphere : public osg::Shape { // fully concrete }; class osg::ShapeDrawable { ShapeDrawable(osg::Shape*, TessellationHints) { /* ... */ } }; So the constructor to ShapeDrawable takes a pointer to the abstract base class. My attempt to wrap this (leaving out some details) is: // Abstract class, so noncopyable and no_init. class_, ref_ptr, boost::noncopyable >("Shape", no_init); // Concrete derived class. class_, ref_ptr >("Sphere"); // Details of this class not important for now. class_, ref_ptr >("TessellationHints"); class_, ref_ptr >("ShapeDrawable") .def(init()) ; The first three classes wrap correctly, but the fourth fails with a compiler error saying it cannot instantiate abstract class Shape. OK, I understand that, but what can I do about it? Thanks in advance for any help, J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From renjipanicker at gmail.com Sat Sep 20 13:08:03 2008 From: renjipanicker at gmail.com (Renji Panicker) Date: Sat, 20 Sep 2008 16:38:03 +0530 Subject: [C++-sig] Returning None from __getitem__ In-Reply-To: <48D39612.3060303@sympatico.ca> References: <3c0065cc0809190335s7219b866hada4f7d71eb48458@mail.gmail.com> <48D39612.3060303@sympatico.ca> Message-ID: <3c0065cc0809200408q2fd7aa6dnaf8604cf11c57c30@mail.gmail.com> Exactly what I wanted! Thanks a lot, Stefan! On Fri, Sep 19, 2008 at 5:37 PM, Stefan Seefeld wrote: > Hi Renji, > > From a language / API design point of view, I think it would be best (i.e., > least surprising) to follow the expected behavior. Python's dict type does > have ways to handle what you want (and you can even set default values to be > returned for non-existent keys). > > As for your particular case, where your C++ map throws an exception, but you > want the wrapper to return None, that's simple, too: > > boost::python::object my_get_wrapper(PropSet &p, std::wstring const &k) > { > try { return p->get(k);} // implicitly convert valid value to python object > catch (InvalidKeyException const &) { return boost::python::object();} // > on failure, return None > } > > Then, you bind that as 'get': > > class_ (...) > .def("get", my_get_wrapper); > > > HTH, > 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 merlin66b at gmail.com Sat Sep 20 15:57:35 2008 From: merlin66b at gmail.com (Thomas Berg) Date: Sat, 20 Sep 2008 15:57:35 +0200 Subject: [C++-sig] [boost.python] Debug build on win32 runtime trouble In-Reply-To: <87zlm4mzhj.fsf@mcbain.luannocracy.com> References: <48D1EC0B.5030405@gmx.de> <48D336DA.5000005@gmx.de> <87zlm4mzhj.fsf@mcbain.luannocracy.com> Message-ID: On Fri, Sep 19, 2008 at 7:21 AM, Olaf Peter wrote: > As first thank you for the answer. Anyway, I assume the debug build of > boost_python dll ist wrong and points to the wrong python25.dll, should be > python25_d.dll. Could it be? I've used the boostpro windows installer > 1.35.0. > > Thanks, > Olaf > > PS: My code works without defined BOOST_DEBUG_PYTHON; my crash results > really from different memory layout of the python25{_d}.dll used by > BOOST_PYTHON-VC80-MT-GD-1_35.DLL. Sorry Olaf, I assumed that you had built boost yourself, as I always do... As David says this is the intentional default debug variant of boost python. I don't know if the boostpro installer is capable of installing the proper python-debug versions of the libraries. If not, I guess you will have to build it yourself with bjam, passing python-debugging=on. Notice that you get a different name: "gyd" instead of "gd" in the lib / dll name. Believe me, you do want to define BOOST_DEBUG_PYTHON when building your application, otherwise one of the boost python headers will undefine _DEBUG before including "python.h". See boost/python/detail/wrap_python.hpp, or the documentation. It might accidentaly work if you "illegaly" include python.h yourself. You may also want to disable the autolinking of boost, since it doesn't handle the python debug naming scheme (at least in earlier versions of boost it doesn't), and pulls in the wrong library. Do that by defining BOOST_ALL_NO_LIB in your project. Thomas From ope-devel at gmx.de Sat Sep 20 19:50:22 2008 From: ope-devel at gmx.de (Olaf Peter) Date: Sat, 20 Sep 2008 19:50:22 +0200 Subject: [C++-sig] [boost.python] Debug build on win32 runtime trouble In-Reply-To: References: <48D1EC0B.5030405@gmx.de> <48D336DA.5000005@gmx.de> <87zlm4mzhj.fsf@mcbain.luannocracy.com> Message-ID: <48D537DE.80506@gmx.de> Thomas Berg schrieb: > On Fri, Sep 19, 2008 at 7:21 AM, Olaf Peter wrote: >> As first thank you for the answer. Anyway, I assume the debug build of >> boost_python dll ist wrong and points to the wrong python25.dll, should be >> python25_d.dll. Could it be? I've used the boostpro windows installer >> 1.35.0. >> >> Thanks, >> Olaf >> >> PS: My code works without defined BOOST_DEBUG_PYTHON; my crash results >> really from different memory layout of the python25{_d}.dll used by >> BOOST_PYTHON-VC80-MT-GD-1_35.DLL. > > > Sorry Olaf, I assumed that you had built boost yourself, as I always > do... As David says this is the intentional default debug variant of > boost python. I don't know if the boostpro installer is capable of > installing the proper python-debug versions of the libraries. If not, > I guess you will have to build it yourself with bjam, passing > python-debugging=on. Notice that you get a different name: "gyd" > instead of "gd" in the lib / dll name. > > Believe me, you do want to define BOOST_DEBUG_PYTHON when building > your application, otherwise one of the boost python headers will > undefine _DEBUG before including "python.h". See > boost/python/detail/wrap_python.hpp, or the documentation. It might > accidentaly work if you "illegaly" include python.h yourself. > > You may also want to disable the autolinking of boost, since it > doesn't handle the python debug naming scheme (at least in earlier > versions of boost it doesn't), and pulls in the wrong library. Do that > by defining BOOST_ALL_NO_LIB in your project. Thanks for your explanation and patience. Regards, Olaf From surye80 at gmail.com Mon Sep 22 02:49:18 2008 From: surye80 at gmail.com (Vincent Castellano) Date: Sun, 21 Sep 2008 17:49:18 -0700 Subject: [C++-sig] Exporting Classes to Embedded Interpreter Message-ID: <8628ae450809211749o703b1059gb02d05cd205a7f9b@mail.gmail.com> I can create a shared library that exports C++ classes to python. And I have the embedded interpreter working for boost:python::exec() to call python code (as well as pulling out objects and function from globals, and interacting with them in C. What I am trying to is combine these techniques to enable classes written in C++ to be accessible to the embedded interpreter, and more to the point, instantiate instances of those classes in C++, and pass them into the globals of python to interact with. I have tried to find documentation on this but it eludes me. Any pointers on how to accomplish this, or is it possible at all? Thanks, Vince Castellano From seefeld at sympatico.ca Mon Sep 22 03:24:15 2008 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Sun, 21 Sep 2008 21:24:15 -0400 Subject: [C++-sig] Exporting Classes to Embedded Interpreter In-Reply-To: <8628ae450809211749o703b1059gb02d05cd205a7f9b@mail.gmail.com> References: <8628ae450809211749o703b1059gb02d05cd205a7f9b@mail.gmail.com> Message-ID: <48D6F3BF.4090404@sympatico.ca> Vincent Castellano wrote: > I can create a shared library that exports C++ classes to python. And > I have the embedded interpreter working for boost:python::exec() to > call python code (as well as pulling out objects and function from > globals, and interacting with them in C. > > What I am trying to is combine these techniques to enable classes > written in C++ to be accessible to the embedded interpreter, and more > to the point, instantiate instances of those classes in C++, and pass > them into the globals of python to interact with. I have tried to find > documentation on this but it eludes me. Any pointers on how to > accomplish this, or is it possible at all? > It's easy, really: the boost::python::exec() function takes a dictionary, providing the environment that the script will see. You can push whatever you want into that dictionary, inclusively objects of types you previously exported to python. E.g.: somewhere in your code: class_....; somewhere else in your code: your_class &an_object = ...; python::object main = python::import("__main__"); python::object global(main.attr("__dict__")); global["an_object"] = an_object; // <- this injects a python wrapper around 'an_object' into the 'global' namespace. python::object::exec(your_script, global); // <- this runs a script, exposing 'an_object' to it. HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin... From renjipanicker at gmail.com Mon Sep 22 07:20:29 2008 From: renjipanicker at gmail.com (Renji Panicker) Date: Mon, 22 Sep 2008 10:50:29 +0530 Subject: [C++-sig] Exporting Classes to Embedded Interpreter In-Reply-To: <8628ae450809211749o703b1059gb02d05cd205a7f9b@mail.gmail.com> References: <8628ae450809211749o703b1059gb02d05cd205a7f9b@mail.gmail.com> Message-ID: <3c0065cc0809212220rdea7008p21fe441985cc6149@mail.gmail.com> You need to introduce an instance of the object into the namespace of your main module. 1. Get reference to main module boost::python::object main_module((boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__"))))); 2. Get reference to main namespace boost::python::object main_namespace = main_module.attr("__dict__"); 3. Create an instance of MyObj, which you want to expose to your Python code MyObj myObj; 4. Insert reference to myObj into the main namespace main_namespace["myObj"] = boost::python::ptr(&myObj); 5. Code to be executed. This code can access myObj const char* code = "print 'hello'"; 6. Execute the code boost::python::handle<> ignored(( PyRun_String(code, Py_file_input, main_namespace.ptr(), main_namespace.ptr() ) )); HTH, -/renji On Mon, Sep 22, 2008 at 6:19 AM, Vincent Castellano wrote: > I can create a shared library that exports C++ classes to python. And > I have the embedded interpreter working for boost:python::exec() to > call python code (as well as pulling out objects and function from > globals, and interacting with them in C. > > What I am trying to is combine these techniques to enable classes > written in C++ to be accessible to the embedded interpreter, and more > to the point, instantiate instances of those classes in C++, and pass > them into the globals of python to interact with. I have tried to find > documentation on this but it eludes me. Any pointers on how to > accomplish this, or is it possible at all? > > Thanks, > Vince Castellano > _______________________________________________ > C++-sig mailing list > C++-sig at python.org > http://mail.python.org/mailman/listinfo/c++-sig > From arne at ras.ucalgary.ca Mon Sep 22 18:28:32 2008 From: arne at ras.ucalgary.ca (Arne Grimstrup) Date: Mon, 22 Sep 2008 10:28:32 -0600 Subject: [C++-sig] .def( bp::self == bp::self ); causes compile problems for Py++ generated wrapper In-Reply-To: References: Message-ID: <48D7C7B0.5000700@ras.ucalgary.ca> On Friday, September 19th, William Ladwig wrote: > > If you can change the C++ code, try declaring the operator== argument > as a const reference (should probably do this anyhow since you aren't > modifying the object): > > bool operator==(const MyFrac& b) const { > return numerator == b.numerator && denominator == b.denominator;} > > > Hope this helps, > Bill Thanks very much, Bill. Adding the const reference to the argument fixed the problem. Regards, Arne From dave at boostpro.com Mon Sep 22 19:59:49 2008 From: dave at boostpro.com (David Abrahams) Date: Mon, 22 Sep 2008 10:59:49 -0700 Subject: [C++-sig] Wrapping a class whose constructor takes a pointer to an abstract class In-Reply-To: <48D47AEE.6050405@cm-labs.com> (=?utf-8?Q?=22Jean-S=C3=A9bast?= =?utf-8?Q?ien?= Guay"'s message of "Sat, 20 Sep 2008 00:24:14 -0400") References: <48D47AEE.6050405@cm-labs.com> Message-ID: <87myi0f4dm.fsf@mcbain.luannocracy.com> on Fri Sep 19 2008, Jean-S?bastien Guay wrote: > class_, > ref_ptr >("ShapeDrawable") > .def(init()) > ; > > The first three classes wrap correctly, but the fourth fails with a compiler > error saying it cannot instantiate abstract class Shape. OK, I understand that, > but what can I do about it? I can't promise that this is enough to make everything work, but you could start by making the init signature match your constructor signature: class_, ref_ptr >("ShapeDrawable") .def(init()) ^ here--------------------^ It will at least compile, then! -- Dave Abrahams BoostPro Computing http://www.boostpro.com From jean-sebastien.guay at cm-labs.com Mon Sep 22 20:06:59 2008 From: jean-sebastien.guay at cm-labs.com (=?UTF-8?B?SmVhbi1Tw6liYXN0aWVuIEd1YXk=?=) Date: Mon, 22 Sep 2008 14:06:59 -0400 Subject: [C++-sig] Wrapping a class whose constructor takes a pointer to an abstract class In-Reply-To: <87myi0f4dm.fsf@mcbain.luannocracy.com> References: <48D47AEE.6050405@cm-labs.com> <87myi0f4dm.fsf@mcbain.luannocracy.com> Message-ID: <48D7DEC3.3070108@cm-labs.com> Hi David, > I can't promise that this is enough to make everything work, but you > could start by making the init signature match your constructor > signature: [...] > It will at least compile, then! Hmmm, you're right of course. The only thing I can say in my defense is that I'm still confused as to when I need to use "python" syntax and when I need to use "C++" syntax... I bet there are a lot more places where I made the same mistake. :-) Thanks a lot, and I'm sure I'll have many more such newbie questions in the near future. J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From jean-sebastien.guay at cm-labs.com Tue Sep 23 05:21:40 2008 From: jean-sebastien.guay at cm-labs.com (=?ISO-8859-1?Q?Jean-S=E9bastien_Guay?=) Date: Mon, 22 Sep 2008 23:21:40 -0400 Subject: [C++-sig] Reference counted object return value policy Message-ID: <48D860C4.6000300@cm-labs.com> Hi all, Thanks to David for pointing out my mistake earlier, I am progressing again on my OSG boost.python wrappers. Now, you may or may not know that OSG uses its own reference counting mechanism. All ref counted objects inherit from an osg::Referenced base class which has the reference count, and a smart pointer template class ref_ptr manages these counts. I've thus far been able to use pointee< ref_ptr > as heldtype to get around the fact that destructors are protected, but now I want to expose the file loading method: osg::Node* osgDB::readNodeFile(const std::string& filename, /*...*/) I've got this: def("readNodeFile", readNodeFile1, /* some return value policy */); I'm stuck at the choice of return value policy. I guess the most logical would be return_value_policy(), but this gives a compilation error because the actual C++ return type of the function has a protected destructor. I also tried (in true newbie fashion) return_value_policy(), return_value_policy() and even return_internal_reference<>(), and all three cause a crash of the python interpreter (though in the second and third case I can understand why, after all no reference to that object is kept by the function itself). a) Am I just looking at this the wrong way? What would be the right return value policy in this case? b) I have a hunch that if I had a way to tell boost.python *how* to manage the new object, i.e. using a ref_ptr, that might solve my problem... c) I imagine the only way to debug and find out *why* the python interpreter crashes in the other cases (other than just being not applicable) is to compile Python in debug and run it in the debugger? Do I have other options? On a side note, there seem to be other people who have attempted to wrap OSG with boost.python, but if any of them succeeded, they haven't - to my knowledge - made their work public. This is just a "free time" project for me. So I won't be too disappointed if I don't end up with something usable, though until now I like what boost.python provides. It seems expressive enough to capture pretty much any C++ API, I just have to get to know it more. Thanks in advance for any guidance you can provide, J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From roman.yakovenko at gmail.com Tue Sep 23 05:26:24 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 23 Sep 2008 06:26:24 +0300 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48D860C4.6000300@cm-labs.com> References: <48D860C4.6000300@cm-labs.com> Message-ID: <7465b6170809222026i4d91a77bm26f1d6a6c33ffe3@mail.gmail.com> On Tue, Sep 23, 2008 at 6:21 AM, Jean-S?bastien Guay wrote: > Hi all, > ... > On a side note, there seem to be other people who have attempted to wrap OSG > with boost.python, but if any of them succeeded, they haven't - to my > knowledge - made their work public. This is just a "free time" project for > me. So I won't be too disappointed if I don't end up with something usable, > though until now I like what boost.python provides. It seems expressive > enough to capture pretty much any C++ API, I just have to get to know it > more. I am not sure whether OSG and OpenSG are same project, but working Python wrapper for the latest one you can find here: https://realityforge.vrsource.org/trac/pyopensg (Of course it uses boost.python ). -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From jean-sebastien.guay at cm-labs.com Tue Sep 23 14:31:19 2008 From: jean-sebastien.guay at cm-labs.com (=?ISO-8859-1?Q?Jean-S=E9bastien_Guay?=) Date: Tue, 23 Sep 2008 08:31:19 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <7465b6170809222026i4d91a77bm26f1d6a6c33ffe3@mail.gmail.com> References: <48D860C4.6000300@cm-labs.com> <7465b6170809222026i4d91a77bm26f1d6a6c33ffe3@mail.gmail.com> Message-ID: <48D8E197.60504@cm-labs.com> Hello Roman, > I am not sure whether OSG and OpenSG are same project, but working > Python wrapper for the latest one you can find here: > https://realityforge.vrsource.org/trac/pyopensg > (Of course it uses boost.python ). No, they are different projects, though of course they are often confused because of their names. OSG stands for OpenSceneGraph: http://www.openscenegraph.org/ OpenSG is different: http://www.opensg.org/ Though if people were able to make wrappers with boosy.python for the latter, it at least gives me hope that it should be possible to do the same for the former. J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From dave at boostpro.com Tue Sep 23 18:04:19 2008 From: dave at boostpro.com (David Abrahams) Date: Tue, 23 Sep 2008 12:04:19 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48D860C4.6000300@cm-labs.com> (=?utf-8?Q?=22Jean-S=C3=A9bast?= =?utf-8?Q?ien?= Guay"'s message of "Mon, 22 Sep 2008 23:21:40 -0400") References: <48D860C4.6000300@cm-labs.com> Message-ID: <87wsh2c0ho.fsf@mcbain.luannocracy.com> on Mon Sep 22 2008, Jean-S?bastien Guay wrote: > Hi all, > > Thanks to David for pointing out my mistake earlier, I am progressing again on > my OSG boost.python wrappers. > > Now, you may or may not know that OSG uses its own reference counting > mechanism. All ref counted objects inherit from an osg::Referenced > base class which has the reference count, and a smart pointer template > class ref_ptr manages these counts. I've thus far been able to use > pointee< ref_ptr > as heldtype to get around the fact that > destructors are protected, but now I want to expose the file loading > method: > > osg::Node* osgDB::readNodeFile(const std::string& filename, /*...*/) > > I've got this: > > def("readNodeFile", readNodeFile1, /* some return value policy */); > > I'm stuck at the choice of return value policy. I guess the most > logical would be return_value_policy(), but this > gives a compilation error because the actual C++ return type of the > function has a protected destructor. As long as it's protected, you can break in by: struct Hole : osg::Node { struct deleter { void operator()(osg::Node* n) const { delete n; } }; }; and then expose a thin wrapper around readNodeFile that returns boost::shared_ptr(readNodeFile(...), Hole::deleter()); Or use your own smart pointer if that's what you have. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From jean-sebastien.guay at cm-labs.com Tue Sep 23 18:25:26 2008 From: jean-sebastien.guay at cm-labs.com (=?UTF-8?B?SmVhbi1Tw6liYXN0aWVuIEd1YXk=?=) Date: Tue, 23 Sep 2008 12:25:26 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <87wsh2c0ho.fsf@mcbain.luannocracy.com> References: <48D860C4.6000300@cm-labs.com> <87wsh2c0ho.fsf@mcbain.luannocracy.com> Message-ID: <48D91876.1080401@cm-labs.com> Hello David, > Or use your own smart pointer if that's what you have. So you're saying that exposing a thin wrapper that would return a ref_ptr instead of osg::Node would work? What would be the return value policy for that? (since it's returning an object on the stack instead of one created with new, I would think return_value_policy? Thanks again for your help, J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From rogeeff at gmail.com Tue Sep 23 21:23:30 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Tue, 23 Sep 2008 19:23:30 +0000 (UTC) Subject: [C++-sig] export of dictionary like class Message-ID: What is the best way to export dictionary like class so it support python dictionary API. Pretty much what I want is to inherit from UserDict.DictMixin in C++ Thanks, Gennadiy From rwgk at yahoo.com Tue Sep 23 21:36:02 2008 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Tue, 23 Sep 2008 12:36:02 -0700 (PDT) Subject: [C++-sig] export of dictionary like class Message-ID: <118782.65438.qm@web31403.mail.mud.yahoo.com> There is the map_indexing_suite that comes with Boost.Python #include or you can just roll the wrapper yourself like this: http://cctbx.svn.sourceforge.net/viewvc/cctbx/trunk/scitbx/stl/map_wrapper.h?view=markup ----- Original Message ---- From: Gennadiy Rozental To: c++-sig at python.org Sent: Tuesday, September 23, 2008 12:23:30 PM Subject: [C++-sig] export of dictionary like class What is the best way to export dictionary like class so it support python dictionary API. Pretty much what I want is to inherit from UserDict.DictMixin in C++ Thanks, Gennadiy From rogeeff at gmail.com Tue Sep 23 22:00:43 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Tue, 23 Sep 2008 20:00:43 +0000 (UTC) Subject: [C++-sig] export of dictionary like class References: <118782.65438.qm@web31403.mail.mud.yahoo.com> Message-ID: Ralf W. Grosse-Kunstleve yahoo.com> writes: > > There is the map_indexing_suite that comes with Boost.Python > > #include Class I have export is quite crippled. One of the problem is that value is smart_ptr, which seems to prevent above template usage. Gennadiy From rwgk at yahoo.com Tue Sep 23 22:30:01 2008 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Tue, 23 Sep 2008 13:30:01 -0700 (PDT) Subject: [C++-sig] export of dictionary like class Message-ID: <87237.64852.qm@web31401.mail.mud.yahoo.com> > Class I have export is quite crippled. One of the problem is that value is > smart_ptr, which seems to prevent above template usage. In that case I'd go the hand-rolled route. Usually, it isn't much work. Ralf From rogeeff at gmail.com Tue Sep 23 22:58:36 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Tue, 23 Sep 2008 20:58:36 +0000 (UTC) Subject: [C++-sig] export of dictionary like class References: <87237.64852.qm@web31401.mail.mud.yahoo.com> Message-ID: Ralf W. Grosse-Kunstleve yahoo.com> writes: > > > Class I have export is quite crippled. One of the problem is that value is > > > smart_ptr, which seems to prevent above template usage. > > In that case I'd go the hand-rolled route. Usually, it isn't much work. > Ralf > I've decided on mixed C++/python approach. on c++ side: class_, boost::noncopyable>( "Map", bp::no_init ) .def( "keys", &object_op::get_map_keys ) .def( "items", &object_op::get_map_items ) ; class_, boost::noncopyable>( "ReadableMap", bp::no_init ) .def( "__getitem__", &IReadableMap::get ) ; class_, boost::noncopyable>( "WriteableMap", bp::no_init ) .def( "__setitem__", &IWriteableMap::put ) ; on python side during library init phase import UserDict add_mixin( Map, UserDict.DictMixin ) where def add_mixin( clazz, mixin_clazz ): bases = inspect.getmro( clazz ) if mixin_clazz not in bases: clazz.__bases__ = (mixin_clazz,) + clazz.__bases__ Gennadiy From jean-sebastien.guay at cm-labs.com Wed Sep 24 04:09:10 2008 From: jean-sebastien.guay at cm-labs.com (=?UTF-8?B?SmVhbi1Tw6liYXN0aWVuIEd1YXk=?=) Date: Tue, 23 Sep 2008 22:09:10 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48D91876.1080401@cm-labs.com> References: <48D860C4.6000300@cm-labs.com> <87wsh2c0ho.fsf@mcbain.luannocracy.com> <48D91876.1080401@cm-labs.com> Message-ID: <48D9A146.1040000@cm-labs.com> Hello again, > So you're saying that exposing a thin wrapper that would return a > ref_ptr instead of osg::Node would work? Continuing my (mostly bind) experimentation, I tried this: osg::ref_ptr readNodeFileWrapper(const std::string& filename) { return osg::ref_ptr(osgDB::readNodeFile(filename)); } BOOST_PYTHON_MODULE(_osgDB) { def("readNodeFile", readNodeFileWrapper); } That gets me a python.exe crash like the other experiments did, running this way from the Python interpreter: import osg import osgDB n = osgDB.readNodeFile("cow.osg") Note that this works: import osg n = osg.Node() so I assume my osg::Node wrapper is at least minimally functional... Though I have no way of knowing if n is actually properly constructed or if it's enclosed in a ref_ptr... My osg::Node wrapper starts like this, for reference: class_, ref_ptr >("Node") /* ... */ ; Another weird thing, if I change the above thin wrapper to hard-code the filename: osg::ref_ptr readNodeFileWrapper() { return osg::ref_ptr(osgDB::readNodeFile("cow.osg")); } I don't get a crash, but OSG reports it can't load a file called "?" (lowercase greek alpha), which seems to indicate that somewhere, the string got mangled somehow? I don't understand how that can be since it's a direct call into OSG from C++ code, nothing else involved. Has anyone seen something like that before? Thanks in advance, J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From dave at boostpro.com Wed Sep 24 05:27:50 2008 From: dave at boostpro.com (David Abrahams) Date: Tue, 23 Sep 2008 23:27:50 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48D9A146.1040000@cm-labs.com> (=?utf-8?Q?=22Jean-S=C3=A9bast?= =?utf-8?Q?ien?= Guay"'s message of "Tue, 23 Sep 2008 22:09:10 -0400") References: <48D860C4.6000300@cm-labs.com> <87wsh2c0ho.fsf@mcbain.luannocracy.com> <48D91876.1080401@cm-labs.com> <48D9A146.1040000@cm-labs.com> Message-ID: <87wsh2te89.fsf@mcbain.luannocracy.com> on Tue Sep 23 2008, Jean-S?bastien Guay wrote: > Hello again, > >> So you're saying that exposing a thin wrapper that would return a >> ref_ptr instead of osg::Node would work? I don't think I said that. I don't know anything about your ref_ptr, its relationship to osg::Node, or the reference count that you might have for your new Node, so I wouldn't make such an assertion. > Continuing my (mostly bind) experimentation, I tried this: > > osg::ref_ptr readNodeFileWrapper(const std::string& filename) > { > return osg::ref_ptr(osgDB::readNodeFile(filename)); > } > > BOOST_PYTHON_MODULE(_osgDB) > { > def("readNodeFile", readNodeFileWrapper); > } > > That gets me a python.exe crash like the other experiments did, running this way > from the Python interpreter: > > import osg > import osgDB > n = osgDB.readNodeFile("cow.osg") > > Note that this works: > > import osg > n = osg.Node() > > so I assume my osg::Node wrapper is at least minimally functional... Though I > have no way of knowing if n is actually properly constructed or if it's enclosed > in a ref_ptr... My osg::Node wrapper starts like this, for reference: > > class_, ref_ptr >("Node") > /* ... */ > ; > > Another weird thing, if I change the above thin wrapper to hard-code the > filename: > > osg::ref_ptr readNodeFileWrapper() > { > return osg::ref_ptr(osgDB::readNodeFile("cow.osg")); > } > > I don't get a crash, but OSG reports it can't load a file called "?" (lowercase > greek alpha), which seems to indicate that somewhere, the string got mangled > somehow? I don't understand how that can be since it's a direct call into OSG > from C++ code, nothing else involved. It sounds like you are casting about for something that seems to work without really understanding what you're doing. I suggest you slow down and analyze. Remember that "seems to work" and "works" can be widely separated ;-) -- Dave Abrahams BoostPro Computing http://www.boostpro.com From rogeeff at gmail.com Wed Sep 24 07:02:30 2008 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Wed, 24 Sep 2008 05:02:30 +0000 (UTC) Subject: [C++-sig] once again about global dictionary replacement Message-ID: Hi, This is an example that illustrate problem similar to what I have. Let's say I have c++ function bp::object s_save; void foo( bp::object const& obj ) { s_save = obj; } exported with the same name in python. Now let say in one place of my program I execute following string (this is an input to my program for example) " import some_module_abc def boo() .... do something here and use some_module_abc foo( boo ) " when I execute this string I use command like exec( code, my_module_dictionary ) I specify my_module_dictionary as local context because I want all the symbols defined in string (functions and classes) to become members of my module. Some time later I execute stored function like this s_save(); Now here is a punchline: python complains it does not know about some_module_abc The reason is that import occurred in local context of first exec call, resulting in all symbols to be imported into my module dictionary, not global dictionary. At execution point global dictionary is the same (not sure now, but it seems bound to the python object). Local dictionary is temporary containing function arguments (if any). Both know nothing about some_module_abc. It would help if I can as the point of invocation do one of these a) replace the bound global dictionary b) update the bound global dictionary c) update local dictionary that is going to be used Can anyone suggest the solution? Thanks, Gennadiy From meine at informatik.uni-hamburg.de Wed Sep 24 11:07:15 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Wed, 24 Sep 2008 11:07:15 +0200 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48D860C4.6000300@cm-labs.com> References: <48D860C4.6000300@cm-labs.com> Message-ID: <200809241107.16769.meine@informatik.uni-hamburg.de> Am Dienstag, 23. September 2008 05:21:40 schrieb Jean-S?bastien Guay: > Now, you may or may not know that OSG uses its own reference counting > mechanism. All ref counted objects inherit from an osg::Referenced base > class which has the reference count, and a smart pointer template class > ref_ptr manages these counts. Do you know that people have more or less successfully used custom smart pointers with BP? IIRC Roman has a nice web page about that.. Seems to be here: http://www.language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html -- Ciao, / / /--/ / / ANS From jean-sebastien.guay at cm-labs.com Wed Sep 24 15:03:52 2008 From: jean-sebastien.guay at cm-labs.com (=?UTF-8?B?SmVhbi1Tw6liYXN0aWVuIEd1YXk=?=) Date: Wed, 24 Sep 2008 09:03:52 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <87wsh2te89.fsf@mcbain.luannocracy.com> References: <48D860C4.6000300@cm-labs.com> <87wsh2c0ho.fsf@mcbain.luannocracy.com> <48D91876.1080401@cm-labs.com> <48D9A146.1040000@cm-labs.com> <87wsh2te89.fsf@mcbain.luannocracy.com> Message-ID: <48DA3AB8.4070309@cm-labs.com> Hello David, > I don't think I said that. I don't know anything about your ref_ptr, > its relationship to osg::Node, or the reference count that you might > have for your new Node, so I wouldn't make such an assertion. Of course, I was interpreting what you said (perhaps too enthusiastically). I've really been impressed at how boost.python seemed to just do the right thing automatically in many cases, so I thought this was another instance where I thought something complex was required but something simple actually worked better. Sorry for putting words in your mouth. > It sounds like you are casting about for something that seems to work > without really understanding what you're doing. I suggest you slow down > and analyze. Remember that "seems to work" and "works" can be widely > separated ;-) Sure, that's a valid assessment. I'll do more reading and see if I can make a new start. Thanks, J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From jean-sebastien.guay at cm-labs.com Wed Sep 24 15:08:59 2008 From: jean-sebastien.guay at cm-labs.com (=?ISO-8859-1?Q?Jean-S=E9bastien_Guay?=) Date: Wed, 24 Sep 2008 09:08:59 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <200809241107.16769.meine@informatik.uni-hamburg.de> References: <48D860C4.6000300@cm-labs.com> <200809241107.16769.meine@informatik.uni-hamburg.de> Message-ID: <48DA3BEB.9030608@cm-labs.com> Hello Hans, > IIRC Roman has a nice web page about that.. Seems to be > here: > http://www.language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html I've seen that and will try to apply it. Thanks. Actually, I think my problems with boost.python can be summed up thus: * I don't know when such and such strategy is required, and when it's just useless code that adds to the maintenance overhead of my wrappers * I don't know how to debug the wrappers if they don't give the intended results (other than blindly trying something else). The first point, I can work on by reading the documentation more thoroughly. But I would appreciate tips on the second. I haven't seen any documentation on that. In particular, if some code makes the python interpreter crash, what can I do to get a stack trace or go into it with a debugger (on windows in my case)? Do I need to compile Python and/or boost in debug? Thanks in advance, J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From gjcarneiro at gmail.com Wed Sep 24 15:15:29 2008 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Wed, 24 Sep 2008 14:15:29 +0100 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48DA3BEB.9030608@cm-labs.com> References: <48D860C4.6000300@cm-labs.com> <200809241107.16769.meine@informatik.uni-hamburg.de> <48DA3BEB.9030608@cm-labs.com> Message-ID: 2008/9/24 Jean-S?bastien Guay > Hello Hans, > > IIRC Roman has a nice web page about that.. Seems to be here: >> >> http://www.language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html >> > > I've seen that and will try to apply it. Thanks. > > Actually, I think my problems with boost.python can be summed up thus: > * I don't know when such and such strategy is required, and when it's > just useless code that adds to the maintenance overhead of my wrappers > * I don't know how to debug the wrappers if they don't give the intended > results (other than blindly trying something else). > > The first point, I can work on by reading the documentation more > thoroughly. But I would appreciate tips on the second. I haven't seen any > documentation on that. > > In particular, if some code makes the python interpreter crash, what can I > do to get a stack trace or go into it with a debugger (on windows in my > case)? Do I need to compile Python and/or boost in debug? Shameless plug... :-) I think PyBindGen is better in that respect. It may not have all the features of BP yet, or be as mature, but it generates reasonably clean C/C++ wrapping code without templates or macros or funny variable names, so you can debug it better. Regards, -- 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 meine at informatik.uni-hamburg.de Wed Sep 24 15:17:15 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Wed, 24 Sep 2008 15:17:15 +0200 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48DA3BEB.9030608@cm-labs.com> References: <48D860C4.6000300@cm-labs.com> <200809241107.16769.meine@informatik.uni-hamburg.de> <48DA3BEB.9030608@cm-labs.com> Message-ID: <200809241517.17305.meine@informatik.uni-hamburg.de> Am Mittwoch, 24. September 2008 15:08:59 schrieb Jean-S?bastien Guay: > Actually, I think my problems with boost.python can be summed up thus: > * I don't know when such and such strategy is required, and when it's > just useless code that adds to the maintenance overhead of my wrappers My strategy for preventing useless code usually is to leave out as much code as possible and see what breaks (not). > * I don't know how to debug the wrappers if they don't give the intended > results (other than blindly trying something else). That's tough indeed. > In particular, if some code makes the python interpreter crash, what can > I do to get a stack trace or go into it with a debugger (on windows in > my case)? Do I need to compile Python and/or boost in debug? AFAIK, yes. But I have nearly zero experience with that. I occasionally run python through valgrind (with the supplied suppressions-file) in order to find memleaks/similar memory errors though. It's slow, but works fairly well. -- Ciao, / / /--/ / / ANS From jean-sebastien.guay at cm-labs.com Wed Sep 24 15:28:03 2008 From: jean-sebastien.guay at cm-labs.com (=?UTF-8?B?SmVhbi1Tw6liYXN0aWVuIEd1YXk=?=) Date: Wed, 24 Sep 2008 09:28:03 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: References: <48D860C4.6000300@cm-labs.com> <200809241107.16769.meine@informatik.uni-hamburg.de> <48DA3BEB.9030608@cm-labs.com> Message-ID: <48DA4063.8090101@cm-labs.com> Hi Gustavo, > I think PyBindGen is better in that respect. Looks interesting, but from the features page, it doesn't support multiple inheritance, which OSG uses a lot. So it's probably a no go. I'll check it out though. Thanks for the suggestion, J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From jean-sebastien.guay at cm-labs.com Wed Sep 24 15:32:12 2008 From: jean-sebastien.guay at cm-labs.com (=?ISO-8859-1?Q?Jean-S=E9bastien_Guay?=) Date: Wed, 24 Sep 2008 09:32:12 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <200809241517.17305.meine@informatik.uni-hamburg.de> References: <48D860C4.6000300@cm-labs.com> <200809241107.16769.meine@informatik.uni-hamburg.de> <48DA3BEB.9030608@cm-labs.com> <200809241517.17305.meine@informatik.uni-hamburg.de> Message-ID: <48DA415C.2030107@cm-labs.com> Hello Hans, >> * I don't know when such and such strategy is required, and when it's >> just useless code that adds to the maintenance overhead of my wrappers > > My strategy for preventing useless code usually is to leave out as much code > as possible and see what breaks (not). That's what I've been doing until now, but it doesn't seem that successful. > AFAIK, yes. But I have nearly zero experience with that. I occasionally run > python through valgrind (with the supplied suppressions-file) in order to > find memleaks/similar memory errors though. It's slow, but works fairly > well. I'm not talking about memory leaks per se, I'm talking about the actual wrapper code causing crashes of the interpreter. I've seen a few instances of choosing the wrong return value policy making the interpreter crash, and I don't see a way of finding out what code was being called to cause the crash. Anyways, I'll read up on the actual low-level process that's going on and hopefully that will help me understand what I'm doing. Thanks, J-S -- ______________________________________________________ Jean-Sebastien Guay jean-sebastien.guay at cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ From meine at informatik.uni-hamburg.de Wed Sep 24 15:42:08 2008 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Wed, 24 Sep 2008 15:42:08 +0200 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48DA415C.2030107@cm-labs.com> References: <48D860C4.6000300@cm-labs.com> <200809241517.17305.meine@informatik.uni-hamburg.de> <48DA415C.2030107@cm-labs.com> Message-ID: <200809241542.10101.meine@informatik.uni-hamburg.de> Am Mittwoch, 24. September 2008 15:32:12 schrieb Jean-S?bastien Guay: > I'm not talking about memory leaks per se, I'm talking about the actual > wrapper code causing crashes of the interpreter. That's what I meant with "similar memory errors". My guess is that you're using a policy that allows the C++ object to be deleted although a python object still references it. -- Ciao, / / /--/ / / ANS From dave at boostpro.com Wed Sep 24 16:20:25 2008 From: dave at boostpro.com (David Abrahams) Date: Wed, 24 Sep 2008 10:20:25 -0400 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48DA3BEB.9030608@cm-labs.com> (=?utf-8?Q?=22Jean-S=C3=A9bast?= =?utf-8?Q?ien?= Guay"'s message of "Wed, 24 Sep 2008 09:08:59 -0400") References: <48D860C4.6000300@cm-labs.com> <200809241107.16769.meine@informatik.uni-hamburg.de> <48DA3BEB.9030608@cm-labs.com> Message-ID: <874p4562xi.fsf@mcbain.luannocracy.com> on Wed Sep 24 2008, Jean-S?bastien Guay wrote: > Hello Hans, > >> IIRC Roman has a nice web page about that.. Seems to be here: >> > http://www.language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html > > I've seen that and will try to apply it. Thanks. > > Actually, I think my problems with boost.python can be summed up thus: > * I don't know when such and such strategy is required, and when it's > just useless code that adds to the maintenance overhead of my wrappers > * I don't know how to debug the wrappers if they don't give the intended > results (other than blindly trying something else). > > The first point, I can work on by reading the documentation more thoroughly. But > I would appreciate tips on the second. I haven't seen any documentation on that. > > In particular, if some code makes the python interpreter crash, what can I do to > get a stack trace or go into it with a debugger (on windows in my case)? Do I > need to compile Python and/or boost in debug? That's what I do. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From roman.yakovenko at gmail.com Wed Sep 24 19:44:00 2008 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 24 Sep 2008 20:44:00 +0300 Subject: [C++-sig] Reference counted object return value policy In-Reply-To: <48DA415C.2030107@cm-labs.com> References: <48D860C4.6000300@cm-labs.com> <200809241107.16769.meine@informatik.uni-hamburg.de> <48DA3BEB.9030608@cm-labs.com> <200809241517.17305.meine@informatik.uni-hamburg.de> <48DA415C.2030107@cm-labs.com> Message-ID: <7465b6170809241044v193d9148i3ac83195f507ac49@mail.gmail.com> On Wed, Sep 24, 2008 at 4:32 PM, Jean-S?bastien Guay wrote: > I'm not talking about memory leaks per se, I'm talking about the actual > wrapper code causing crashes of the interpreter. I've seen a few instances > of choosing the wrong return value policy making the interpreter crash, and > I don't see a way of finding out what code was being called to cause the > crash. > >From my experience you can assume that "boost.python" just works. After you accepted such assumption, it takes some time, - your life is a little bit easier. If your wrapper consists from "registration code" only, than simple code review usually helps. If you use visual studio you can build your wrapper in release mode with debug information. In most cases this is enough to debug your problems. Also, building small test case that reproduce the problem and asking for help here works too and quite well. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From william.marie at gmail.com Wed Sep 24 20:26:33 2008 From: william.marie at gmail.com (=?UTF-8?Q?William_Mari=C3=A9?=) Date: Wed, 24 Sep 2008 11:26:33 -0700 (PDT) Subject: [C++-sig] How to use an opaque pointer ? Message-ID: <19655049.post@talk.nabble.com> Hi, I would like to pass a CObject from C++ to Python without exposing the class i've seen the opaque pointer feature but i can't make it work. Here is what i would like to do : CObject* pObj = ..... ; bp::object callable = .... ; callable( (bpCObject*)pObj ); I tried to do like in the documentation : typedef struct CObject_ *bpCObject; BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(CObject_) but i always have this error at runtime : TypeError: No to_python (by-value) converter found for C++ type: struct bpCObject * and i don't understand, could someone tell me why i have that and what do i have to do to fix the problem ? Thanks a lot William -- View this message in context: http://www.nabble.com/How-to-use-an-opaque-pointer---tp19655049p19655049.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From renatox at gmail.com Sun Sep 28 17:30:49 2008 From: renatox at gmail.com (Renato Araujo) Date: Sun, 28 Sep 2008 12:30:49 -0300 Subject: [C++-sig] downcasting problems Message-ID: <95291a80809280830y40a01a47l45f6b204212a70f1@mail.gmail.com> Hi guys, I'm looking for a solution to my problem with python and c++ objects. I will show this little sample code to introduce my problem: I have this c++ code: ======================== class Shape { public: Shape(Shape *parent=0) { this->_parent = parent; }; Shape* parent() { return this->_parent; }; private: Shape *_parent; }; class Circle : public Shape { public: Circle(Shape *parent=NULL) : Shape(parent) { }; }; ====================== and this python.boost code: ====================== BOOST_PYTHON_MODULE(shapes) { class_ ("Shape", init > ()) .def("parent", &Shape::parent, return_internal_reference<>()) ; class_ >("Circle",init >()) ; } ======================================= the python test is this, and this always output "FAIL": ======================================= if __name__ == '__main__': shape = Shape() circle = Circle(shape) p = circle.parent() if (p == shape): print "OK" else: print "FAIL" How I can fix this without change c++ code? -- Renato Araujo Oliveira Filho From entropydevice at gmail.com Mon Sep 29 15:21:23 2008 From: entropydevice at gmail.com (Frederick Heckel) Date: Mon, 29 Sep 2008 09:21:23 -0400 Subject: [C++-sig] static arrays to Python Message-ID: <2d0df3380809290621y54463a62ib059a0ab1ab4b696@mail.gmail.com> Hi, I have a series of structs which contain static arrays of more custom structs and some built-in types. One example: struct connectivity { int connections[SSPS_MAX_GATEWAYS]; gateway gateways[SSPS_MAX_GATEWAYS]; int adjacents[SSPS_MAX_ADJACENTS]; }; In every case, the arrays are static, as in this case, where SSPS_MAX_GATEWAYS is #defined to 16. Previously, these were moved around via shared memory in C. We're now moving to Python, and find ourselves having to use named pipes in windows, because of the general weirdness of Vista. But we are still using some of our old services, including the one that generates this struct (and several more like it); it seemed as though it should be fairly straightforward to wrap all the functionality in a class, and invoke that via Python. But dealing with these static arrays is turning out to be an enormous pain. I initially tried iterating over them to turn them into boost::python lists, but we'd like to be able to use pickle to turn them into strings to send over the pipe. Pickle is having none of it. So my question is, what am I missing? These are contiguous blocks of memory, boost::python has no trouble wrapping the individual structs, so long as I use accessors for the static arrays, but it looks like the task of getting them set up for pickle is going to be a massive PITA. It seems like there should be some [relatively] easy, efficient way to get static arrays into python (even discarding the use of pickle for now, we'll assume they don't need to be mutable from python), but I'm not finding it. I'll rewrite the whole silly service in Python if I have to, but it seems like there should be a Better Way. -- (fwph) Frederick Heckel From rwgk at yahoo.com Mon Sep 29 17:29:37 2008 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 29 Sep 2008 08:29:37 -0700 (PDT) Subject: [C++-sig] static arrays to Python Message-ID: <422250.12801.qm@web31402.mail.mud.yahoo.com> > gateway gateways[SSPS_MAX_GATEWAYS]; > we'd like to be able to use pickle to turn > them into strings to send over the pipe. You have to provide pickle support for your gateway type. See http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/pickle.html What are the data members of your gateway type? I often ended up using the simple __getinitargs__ approach, where you just have to construct a tuple that works as an argument list for one of your constructors. Ralf From renatox at gmail.com Mon Sep 29 20:46:50 2008 From: renatox at gmail.com (Renato Araujo) Date: Mon, 29 Sep 2008 15:46:50 -0300 Subject: [C++-sig] downcasting problems In-Reply-To: <95291a80809280830y40a01a47l45f6b204212a70f1@mail.gmail.com> References: <95291a80809280830y40a01a47l45f6b204212a70f1@mail.gmail.com> Message-ID: <95291a80809291146j2885674eo618a959da5b3704b@mail.gmail.com> Hi all, After some test I got this working but with some extra code. I created 2 wrapper classes to Shape and Circle like that: =========== wrapper code: =========== class ShapeWrapper : public Shape, public wrapper { .... }; class CircleWrapper : public Cirlce, public wrapper { ..... }; and used this classes in boost code. And all works fine. But is this the best way to do this?? I always need create theses wrapper class?? BR On Sun, Sep 28, 2008 at 12:30 PM, Renato Araujo wrote: > Hi guys, > I'm looking for a solution to my problem with python and c++ objects. > > I will show this little sample code to introduce my problem: > > I have this c++ code: > ======================== > > class Shape > { > public: > Shape(Shape *parent=0) > { > this->_parent = parent; > }; > > Shape* parent() > { > return this->_parent; > }; > > private: > Shape *_parent; > }; > > class Circle : public Shape > { > public: > > Circle(Shape *parent=NULL) > : Shape(parent) > { > }; > }; > > ====================== > and this python.boost code: > ====================== > > BOOST_PYTHON_MODULE(shapes) > { > class_ ("Shape", init > ()) > .def("parent", &Shape::parent, return_internal_reference<>()) > ; > > class_ >>("Circle",init >()) > ; > } > > ======================================= > the python test is this, and this always output "FAIL": > ======================================= > > if __name__ == '__main__': > shape = Shape() > circle = Circle(shape) > p = circle.parent() > if (p == shape): > print "OK" > else: > print "FAIL" > > > How I can fix this without change c++ code? > > -- > Renato Araujo Oliveira Filho > -- Renato Araujo Oliveira Filho From lists_ravi at lavabit.com Mon Sep 29 20:17:25 2008 From: lists_ravi at lavabit.com (Ravi) Date: Mon, 29 Sep 2008 14:17:25 -0400 Subject: [C++-sig] Return value policies for objects with custom to-python converters Message-ID: <200809291417.26620.lists_ravi@lavabit.com> Hi, The return value policies from boost.python do not seem to handle objects with custom converters; they seem to handle only those types exposed via boost.python. Attached is some code that handles reference_existing_object for objects with custom converters. However, the interface is unsatisying: 1. The _1 subscripts need to be removed. However, I could not find a way to disambiguate types not exposed through boost.python and those with custom converters. From the perspective of a library user, both the reference_existing_object from boost.python and reference_existing_object_1 from the attached file accomplish exactly the same thing, but the user needs to remember (and know) that certain classes need one vs the other. Could a guru tell me how to plug my converter into boost.python's reference_existing_object? 2. So far, I have used only reference_existing_object. Do the other return value policies need similar special handling for objects with custom to-python converters? How do I figure out which ones do and which ones don't? 3. If there is a satisfactory answer to 1, the resulting technique could be added to boost.python. No new documentation would be necessary, since the existing documentation would convey all the necessary information. In order to submit a patch though, I suppose that some tests are needed. How would one write tests for this functionality? Regards, Ravi -------------- next part -------------- A non-text attachment was scrubbed... Name: reference_existing_object_1.hpp Type: text/x-c++hdr Size: 1104 bytes Desc: not available URL: From petela at gocougs.wsu.edu Mon Sep 29 21:20:06 2008 From: petela at gocougs.wsu.edu (Peter LaDow) Date: Mon, 29 Sep 2008 12:20:06 -0700 Subject: [C++-sig] Importing and internal modules Message-ID: <9e264e780809291220i15f0557aq581af0ca58e9e0f4@mail.gmail.com> I'm not sure if my subject is clear (probably not), but I'll try to describe what I am trying to do here. A commercial tool we are using is capable of loading a shared library as an extension, and we'd like to use Python to script much of the extension. To that end, I am embedding Python in our own shared library. The scripted part of the extension is a collection of callback functions from the shared library into Python. I also want to expose an API to the extension. So, I have created an extension using Boost and the BOOST_PYTHON_MODULE, i.e.: namespace bp = boost::python; BOOST_PYTHON_MODULE(foo) { bp::def("foo1", foo1); bp::def("foo2", foo2); } And then I make sure to append foo to the inittab before calling Py_Initialize(): PyImport_AppendInittab("foo", initfoo); Py_Initialize(); No problems so far. In fact, I can use foo in the Python script as long as I import it, i.e. ("from foo import *"). However, here's were I am running into trouble. The scriptable part is a module. It exposes a set of functions, so I'd really like to just import that module, and not execute it. So, I do the following: bp::object main = bp::import("__main__"); bp::object globals(main.attr("__dict__")); // Import "foo" bp::exec("from foo import *", globals, globals); // Import the scriptable code bp::exec("from script import *", globals, globals); And here's an example of a script I am trying to import: def bar1(): foo1() print "Called bar1!" def bar2(): foo2() print "Called bar2!" Now, the import for this fails with: NameError: global name 'foo1" is not defined Unless I put "from foo import *" at the top of the script. I want the 'foo' module already imported into the script. That's the point of the first of the imports. I'm sure I'm missing something obvious here. Any help would be greatly appreciated. Thanks, Pete -- -- "To love for the sake of being loved is human; to love for the sake of loving is Angelic." -- Alphonse de Lamartine From lists_ravi at lavabit.com Mon Sep 29 23:07:58 2008 From: lists_ravi at lavabit.com (Ravi) Date: Mon, 29 Sep 2008 17:07:58 -0400 Subject: [C++-sig] Importing and internal modules In-Reply-To: <9e264e780809291220i15f0557aq581af0ca58e9e0f4@mail.gmail.com> References: <9e264e780809291220i15f0557aq581af0ca58e9e0f4@mail.gmail.com> Message-ID: <200809291708.00340.lists_ravi@lavabit.com> On Monday 29 September 2008 15:20:06 Peter LaDow wrote: > The scriptable part is a module. ?It exposes a set of functions, so > I'd really like to just import that module, and not execute it. ?So, I > do the following: [snip] > // Import "foo" > bp::exec("from foo import *", globals, globals); > > // Import the scriptable code > bp::exec("from script import *", globals, globals); > > And here's an example of a script I am trying to import: > > def bar1(): > ? foo1() > ? print "Called bar1!" [snip] > Now, the import for this fails with: > > NameError: ?global name 'foo1" is not defined > > Unless I put "from foo import *" at the top of the script. ?I want the > 'foo' module already imported into the script. ?That's the point of > the first of the imports. Think about how this would work in pure python. When you import a module X, all modules/functions X depends on must be explicitly imported in X.py. You cannot import Y in the global interpreter namespace and somehow expect to inject the contents of X into Y even if X has already been imported into the global namespace. So what you are trying to do is impossible even in the pure python case. Why would you expect it to work here? Example: foo.py def foo(): print "foo" bar.py # import foo <-- note that this is commented out def bar(): foo() main.py from foo import * import bar bar.bar() # <-- you will get a NameError here! That said, you could inject the contents of foo into the dictionary of bar, but you will run into subtle problems later. Try prototyping this in pure python before attempting this from an extension module. Regards, Ravi From petela at gocougs.wsu.edu Mon Sep 29 23:32:37 2008 From: petela at gocougs.wsu.edu (Peter LaDow) Date: Mon, 29 Sep 2008 14:32:37 -0700 Subject: [C++-sig] Importing and internal modules In-Reply-To: <200809291708.00340.lists_ravi@lavabit.com> References: <9e264e780809291220i15f0557aq581af0ca58e9e0f4@mail.gmail.com> <200809291708.00340.lists_ravi@lavabit.com> Message-ID: <9e264e780809291432h2144788fg22bbd13d483b8c20@mail.gmail.com> On Mon, Sep 29, 2008 at 2:07 PM, Ravi wrote: > Think about how this would work in pure python. When you import a module X, > all modules/functions X depends on must be explicitly imported in X.py. You > cannot import Y in the global interpreter namespace and somehow expect to > inject the contents of X into Y even if X has already been imported into the > global namespace. So what you are trying to do is impossible even in the pure > python case. Why would you expect it to work here? I was thinking that by passing 'globals' to both calls to import(), symbols would be injected into the global namespace. The goal is to make a set of functions exported by the extension available to Python scripting modules without the need for an explicit import statement. It isn't a killer if that cannot occur, I was just attempting to make things easier for the users of the extension. > That said, you could inject the contents of foo into the dictionary of bar, > but you will run into subtle problems later. Try prototyping this in pure > python before attempting this from an extension module. Fair enough. I tried your example. I'm no Python expert, and chose Python because it was already popular amongst many people here at work. The difference with the Boost method, I thought, was the passing of the 'globals' object to each of the imports, thinking that symbols were injected into the dictionary, and that later imports would see any previous imports. I see your point. Thanks for the feedback. It appears my goal of exporting a set of global "built-in" functions (a la TCL) is not possible. Pete -- -- "To love for the sake of being loved is human; to love for the sake of loving is Angelic." -- Alphonse de Lamartine From lists_ravi at lavabit.com Tue Sep 30 01:32:29 2008 From: lists_ravi at lavabit.com (Ravi) Date: Mon, 29 Sep 2008 19:32:29 -0400 Subject: [C++-sig] Importing and internal modules In-Reply-To: <9e264e780809291432h2144788fg22bbd13d483b8c20@mail.gmail.com> References: <9e264e780809291220i15f0557aq581af0ca58e9e0f4@mail.gmail.com> <200809291708.00340.lists_ravi@lavabit.com> <9e264e780809291432h2144788fg22bbd13d483b8c20@mail.gmail.com> Message-ID: <200809291932.30996.lists_ravi@lavabit.com> On Monday 29 September 2008 17:32:37 Peter LaDow wrote: > Fair enough. ?I tried your example. ?I'm no Python expert, and chose > Python because it was already popular amongst many people here at > work. ?The difference with the Boost method, I thought, was the > passing of the 'globals' object to each of the imports, thinking that > symbols were injected into the dictionary, and that later imports > would see any previous imports. ?I see your point. I see. The trouble is that importing modules in a namespace adds that module to the namespace rather than providing a namespace for the module being imported. I can't think of a way to accomplish what you want easily. > Thanks for the feedback. I re-read my email and now apologize for sounding less than helpful. My frustration from battling all day long with boost.python trying to get custom lvalue from-python conversions working shows :-) > It appears my goal of exporting a set of > global "built-in" functions (a la TCL) is not possible. The possibility I alluded to in my previous email is as follows. Using python, parse the file you would have imported and convert it to an AST. Then, modify the AST in order to inject stuff. Alternatively, you could simply read in the file as a string and prepend the import statement. After that you could run it using bp::exec or PyRun_SimpleString. However, that still has the drawback that script code is still executed; the drawback has a workaround: don't let your module files contain statements for immediate execution. Regards, Ravi From troy at resophonic.com Tue Sep 30 02:21:34 2008 From: troy at resophonic.com (troy d. straszheim) Date: Mon, 29 Sep 2008 20:21:34 -0400 Subject: [C++-sig] Importing and internal modules In-Reply-To: <200809291932.30996.lists_ravi@lavabit.com> References: <9e264e780809291220i15f0557aq581af0ca58e9e0f4@mail.gmail.com> <200809291708.00340.lists_ravi@lavabit.com> <9e264e780809291432h2144788fg22bbd13d483b8c20@mail.gmail.com> <200809291932.30996.lists_ravi@lavabit.com> Message-ID: <48E1710E.40908@resophonic.com> Ravi wrote: > On Monday 29 September 2008 17:32:37 Peter LaDow wrote: >> Fair enough. I tried your example. I'm no Python expert, and chose >> Python because it was already popular amongst many people here at >> work. The difference with the Boost method, I thought, was the >> passing of the 'globals' object to each of the imports, thinking that >> symbols were injected into the dictionary, and that later imports >> would see any previous imports. I see your point. > > I see. The trouble is that importing modules in a namespace adds that module > to the namespace rather than providing a namespace for the module being > imported. I can't think of a way to accomplish what you want easily. How about this: #### foo.py #### def foo1(): print "foo1()" def foo2(): print "foo2()" #### bar.py #### def bar1(): foo1() print "called bar1" def bar2(): foo2() print "called bar2" #### driver.py #### mod = __import__("foo") for symbol in mod.__dict__: if symbol not in __builtins__.__dict__.keys() + ['__builtins__', '__file__']: __builtins__.__dict__[symbol] = mod.__dict__[symbol] print 'Injecting symbol', symbol import bar bar.bar1() #### at the command line: % ./driver.py Injecting symbol foo1 Injecting symbol foo2 foo1() called bar1 From ralvek at yahoo.it Tue Sep 30 14:17:56 2008 From: ralvek at yahoo.it (k k) Date: Tue, 30 Sep 2008 12:17:56 +0000 (GMT) Subject: [C++-sig] Building "hello world" with Boost.Python Message-ID: <161806.85421.qm@web27803.mail.ukl.yahoo.com> Hi all, i want to use a C++ dll (containing overloaded functions) with Python. I'm trying to use Boost.Python. I dowloaded it and now I'm doing the hello world tutorial situated in the boost installation folder in \libs\python\example\tutorial I got also bjam and configured it to work with gcc and python 2.5. This is the hello.cpp file to export: #include #include char const* greet() { return "hello, world"; } BOOST_PYTHON_MODULE(hello_ext) { using namespace boost::python; def("greet", greet); } I use this Jamroot file (that is in the same folder as hello.cpp): # Copyright David Abrahams 2006. Distributed under the Boost # Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) # Specify the path to the Boost project. If you move this project # adjust this path to refer to the Boost root directory. use-project boost : ../../../.. ; # Set up the project-wide requirements that everything uses the # boost_python library from the project whose global ID is # /boost/python. project : requirements /boost/python//boost_python ; # Declare the three extension modules. You can specify multiple # source files after the colon separated by spaces. python-extension hello_ext : hello.cpp ; # A little "rule" (function) to clean up the syntax of declaring tests # of these extension modules. local rule run-test ( test-name : sources + ) { import testing ; testing.make-test run-pyd : $(sources) : : $(test-name) ; } # Declare test targets run-test hello : hello_ext hello.py ; In the Jamroot file i changed only this: use-project boost : ../../../.. ;with this(the boost root folder on on my disk): use-project boost : C:/boost/boost_1_35_0 ;When i run bjam it seams that it builds well, only with some warning, but without any error. In the tutorial they say that if all is well i should now have built the DLLs. But the only things in there are 2 files: hello.o and hello.lib I can't use these as python modules, how can i get the DLL? and the .pyd? Thanks in advance. Stefano __________________________________________________ Do You Yahoo!? Poco spazio e tanto spam? Yahoo! Mail ti protegge dallo spam e ti da tanto spazio gratuito per i tuoi file e i messaggi http://mail.yahoo.it -------------- next part -------------- An HTML attachment was scrubbed... URL: From ralvek at yahoo.it Tue Sep 30 14:20:25 2008 From: ralvek at yahoo.it (k k) Date: Tue, 30 Sep 2008 12:20:25 +0000 (GMT) Subject: [C++-sig] Building "hello world" with Boost.Python Message-ID: <189120.72518.qm@web27805.mail.ukl.yahoo.com> Correction: the 2 output files are hello.o and hello_ext.lib ----- Messaggio originale ----- Da: k k A: c++-sig at python.org Inviato: Marted? 30 settembre 2008, 14:17:56 Oggetto: Building "hello world" with Boost.Python Hi all, i want to use a C++ dll (containing overloaded functions) with Python. I'm trying to use Boost.Python. I dowloaded it and now I'm doing the hello world tutorial situated in the boost installation folder in \libs\python\example\tutorial I got also bjam and configured it to work with gcc and python 2.5. This is the hello.cpp file to export: #include #include char const* greet() { return "hello, world"; } BOOST_PYTHON_MODULE(hello_ext) { using namespace boost::python; def("greet", greet); } I use this Jamroot file (that is in the same folder as hello.cpp): # Copyright David Abrahams 2006. Distributed under the Boost # Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) # Specify the path to the Boost project. If you move this project # adjust this path to refer to the Boost root directory. use-project boost : ../../../.. ; # Set up the project-wide requirements that everything uses the # boost_python library from the project whose global ID is # /boost/python. project : requirements /boost/python//boost_python ; # Declare the three extension modules. You can specify multiple # source files after the colon separated by spaces. python-extension hello_ext : hello.cpp ; # A little "rule" (function) to clean up the syntax of declaring tests # of these extension modules. local rule run-test ( test-name : sources + ) { import testing ; testing.make-test run-pyd : $(sources) : : $(test-name) ; } # Declare test targets run-test hello : hello_ext hello.py ; In the Jamroot file i changed only this: use-project boost : ../../../.. ;with this(the boost root folder on on my disk): use-project boost : C:/boost/boost_1_35_0 ;When i run bjam it seams that it builds well, only with some warning, but without any error. In the tutorial they say that if all is well i should now have built the DLLs. But the only things in there are 2 files: hello.o and hello.lib I can't use these as python modules, how can i get the DLL? and the .pyd? Thanks in advance. Stefano __________________________________________________ Do You Yahoo!? Poco spazio e tanto spam? Yahoo! Mail ti protegge dallo spam e ti da tanto spazio gratuito per i tuoi file e i messaggi http://mail.yahoo.it Scopri il blog di Yahoo! Mail: Trucchi, novit? e la tua opinione. http://www.ymailblogit.com/blog -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boostpro.com Tue Sep 30 16:44:44 2008 From: dave at boostpro.com (David Abrahams) Date: Tue, 30 Sep 2008 10:44:44 -0400 Subject: [C++-sig] Building "hello world" with Boost.Python In-Reply-To: <189120.72518.qm@web27805.mail.ukl.yahoo.com> (k. k.'s message of "Tue, 30 Sep 2008 12:20:25 +0000 (GMT)") References: <189120.72518.qm@web27805.mail.ukl.yahoo.com> Message-ID: <871vz1bsmb.fsf@mcbain.luannocracy.com> on Tue Sep 30 2008, k k wrote: > When i run bjam it seams that it builds well, only with some warning, but without > any error. > In the tutorial they say that if all is well i should now have built the DLLs. > But the only things in there are 2 files: hello.o and hello.lib > I can't use these as python modules, how can i get the DLL? and the .pyd? Try running bjam with -n -a. The output will be all the build commands it used. Can you see where it created the files you are looking for? -- Dave Abrahams BoostPro Computing http://www.boostpro.com From ralvek at yahoo.it Tue Sep 30 17:40:06 2008 From: ralvek at yahoo.it (k k) Date: Tue, 30 Sep 2008 15:40:06 +0000 (GMT) Subject: [C++-sig] Building "hello world" with Boost.Python Message-ID: <43701.24155.qm@web27805.mail.ukl.yahoo.com> Yes, running bjam -n -a i can see that it really creates hello_ext.pyd: gcc.link.dll bin\gcc-mingw-3.4.5\debug\hello_ext.lib "g++" -L"C:\Python25\libs" -Wl,-R -Wl,"C:\Python25" -Wl,-R -Wl,"C:\Python25\ libs" "-Wl,--out-implib,bin\gcc-mingw-3.4.5\debug\hello_ext.lib" -o "bin\gcc-min gw-3.4.5\debug\hello_ext.pyd" -shared -Wl,--start-group "bin\gcc-mingw-3.4.5\de bug\hello.o" "..\..\..\..\bin.v2\libs\python\build\gcc-mingw-3.4.5\debug\boost_p ython-mgw34-d-1_35.lib" -Wl,-Bstatic -Wl,-Bdynamic -lpython25 -Wl,--end-group -g but after some other operations it deletes this file(as i understand it): RmTemps bin\hello.test\gcc-mingw-3.4.5\debug\hello del /f /q "bin\gcc-mingw-3.4.5\debug\hello_ext.pyd" this is strange, isn't it? how can i tell it to do not delete it? ----- Messaggio originale ----- Da: David Abrahams A: Development of Python/C++ integration Inviato: Marted? 30 settembre 2008, 16:44:44 Oggetto: Re: [C++-sig] Building "hello world" with Boost.Python on Tue Sep 30 2008, k k wrote: > When i run bjam it seams that it builds well, only with some warning, but without > any error. > In the tutorial they say that if all is well i should now have built the DLLs. > But the only things in there are 2 files: hello.o and hello.lib > I can't use these as python modules, how can i get the DLL? and the .pyd? Try running bjam with -n -a. The output will be all the build commands it used. Can you see where it created the files you are looking for? -- Dave Abrahams BoostPro Computing http://www.boostpro.com _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig Scopri il blog di Yahoo! Mail: Trucchi, novit? e la tua opinione. http://www.ymailblogit.com/blog -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boostpro.com Tue Sep 30 17:50:30 2008 From: dave at boostpro.com (David Abrahams) Date: Tue, 30 Sep 2008 11:50:30 -0400 Subject: [C++-sig] Building "hello world" with Boost.Python In-Reply-To: <43701.24155.qm@web27805.mail.ukl.yahoo.com> (k. k.'s message of "Tue, 30 Sep 2008 15:40:06 +0000 (GMT)") References: <43701.24155.qm@web27805.mail.ukl.yahoo.com> Message-ID: <87skrhab09.fsf@mcbain.luannocracy.com> on Tue Sep 30 2008, k k wrote: > Yes, running bjam -n -a i can see that it really creates hello_ext.pyd: > > gcc.link.dll bin\gcc-mingw-3.4.5\debug\hello_ext.lib > > "g++" -L"C:\Python25\libs" -Wl,-R -Wl,"C:\Python25" -Wl,-R -Wl,"C:\Python25\ > libs" "-Wl,--out-implib,bin\gcc-mingw-3.4.5\debug\hello_ext.lib" -o "bin\gcc-min > gw-3.4.5\debug\hello_ext.pyd" -shared -Wl,--start-group "bin\gcc-mingw-3.4.5\de > bug\hello.o" "..\..\..\..\bin.v2\libs\python\build\gcc-mingw-3.4.5\debug\boost_p > ython-mgw34-d-1_35.lib" -Wl,-Bstatic -Wl,-Bdynamic -lpython25 -Wl,--end-group > -g > > but after some other operations it deletes this file(as i understand it): > > RmTemps bin\hello.test\gcc-mingw-3.4.5\debug\hello > > del /f /q "bin\gcc-mingw-3.4.5\debug\hello_ext.pyd" > > this is strange, isn't it? how can i tell it to do not delete it? Pass --preserve-test-targets to bjam, and/or remove the test target from the Jamfile. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From ralvek at yahoo.it Tue Sep 30 18:04:33 2008 From: ralvek at yahoo.it (k k) Date: Tue, 30 Sep 2008 16:04:33 +0000 (GMT) Subject: [C++-sig] Building "hello world" with Boost.Python Message-ID: <21133.40564.qm@web27805.mail.ukl.yahoo.com> I passed --preserve-test-targets to bjam and i removed test target from Jamfile, and now it works !! Thank you very much ! ----- Messaggio originale ----- Da: David Abrahams A: Development of Python/C++ integration Inviato: Marted? 30 settembre 2008, 17:50:30 Oggetto: Re: [C++-sig] Building "hello world" with Boost.Python on Tue Sep 30 2008, k k wrote: > Yes, running bjam -n -a i can see that it really creates hello_ext.pyd: > > gcc.link.dll bin\gcc-mingw-3.4.5\debug\hello_ext.lib > > "g++" -L"C:\Python25\libs" -Wl,-R -Wl,"C:\Python25" -Wl,-R -Wl,"C:\Python25\ > libs" "-Wl,--out-implib,bin\gcc-mingw-3.4.5\debug\hello_ext.lib" -o "bin\gcc-min > gw-3.4.5\debug\hello_ext.pyd" -shared -Wl,--start-group "bin\gcc-mingw-3.4.5\de > bug\hello.o" "..\..\..\..\bin.v2\libs\python\build\gcc-mingw-3.4.5\debug\boost_p > ython-mgw34-d-1_35.lib" -Wl,-Bstatic -Wl,-Bdynamic -lpython25 -Wl,--end-group > -g > > but after some other operations it deletes this file(as i understand it): > > RmTemps bin\hello.test\gcc-mingw-3.4.5\debug\hello > > del /f /q "bin\gcc-mingw-3.4.5\debug\hello_ext.pyd" > > this is strange, isn't it? how can i tell it to do not delete it? Pass --preserve-test-targets to bjam, and/or remove the test target from the Jamfile. -- Dave Abrahams BoostPro Computing http://www.boostpro.com _______________________________________________ C++-sig mailing list C++-sig at python.org http://mail.python.org/mailman/listinfo/c++-sig __________________________________________________ Do You Yahoo!? Poco spazio e tanto spam? Yahoo! Mail ti protegge dallo spam e ti da tanto spazio gratuito per i tuoi file e i messaggi http://mail.yahoo.it -------------- next part -------------- An HTML attachment was scrubbed... URL: