From camior at gmail.com Mon Feb 1 17:36:14 2016 From: camior at gmail.com (David Sankel) Date: Mon, 1 Feb 2016 15:36:14 -0700 Subject: [C++-sig] Multiple registration safety and unit testing In-Reply-To: <56ACC8EE.30006@seefeld.name> References: <56ACC8EE.30006@seefeld.name> Message-ID: On Sat, Jan 30, 2016 at 7:30 AM, Stefan Seefeld wrote: > On 29.01.2016 19:19, David Sankel wrote: > > Hello all, > > > > I'd like to allow a class to be registered more than once and wanted > > to get your opinion if this change would be a good idea. > > > > 'boost::python::class_' always adds a class to the current scope and > > registers it in the global registry. The global registry code > > (particularly the 'insert' function in 'src/converter/registry.cpp') > > asserts that the class hasn't been registered already. This, of > > course, prevents multiple registrations of the same type. > > > > There is a use case, though, where multiple registrations of the same > > type is both necessary and safe. Generally, a c++ component which has > > a 'class_' call may not know the name of the module it will eventually > > end up in. It would still be desirable to write a unit test for this > > component. If we write a unit test and put the 'class_' into some > > dummy module we run into a problem where other unit tests might call > > 'class_' in another scope. > > > > The safety of multiple 'class_' calls stems from always using the same > > conversion function. Semantically everything is a-okay. > > > > I'm proposing to change the precondition of the > > 'boost::python::converter::registry::insert' function from: > > > > The behavior is undefined unless the specified 'type_info' object > > has not already been registered. > > > > > > to: > > > > The behavior is undefined unless the specified 'type_info' object > > has not already been registered with a semantically different > > converter > > > > Any objections? > > Yes. Can you describe your use-case ? And what do you mean exactly by > "semantically different" ? :-) > In one '.cpp' file I have something like: void addFooAssets() { boost::python::class_< Foo > ( /* etc. */ ) .def( /* etc */ ); } // testing code follows. BOOST_PYTHON_MODULE( fooTest ) { addFooAssets(); } BOOST_AUTO_TEST_CASE( fooAssets ) { // import 'fooTest' and check that all is good. } In another '.cpp' file, I have something like: void initBarPackage() { boost::python::object barPackage = boost::python::scope(); barPackage.attr( "__path__" ) = "bar"; boost::python::object barFooPackage( boost::python::handle<>( boost::python::borrowed(::PyImport_AddModule( "bar.foo" ) ) ) ); barPackage.attr( "foo" ) = barFooPackage; { const boost::python::scope fooScope( barFooPackage ); addFooAssets(); } } // testing code follows If I run the unit test for "foo" and that for "bar" in the same test run, then I'll get 'class_' called twice. It is harmless to call it twice though since the registration is identical for both calls. When I say "semantically" the same, I mean that something like the 'to_python' member could point to a different function as long as they "do" the same thing. -- David Sankel -------------- next part -------------- An HTML attachment was scrubbed... URL: From sampsa.riikonen at iki.fi Fri Feb 19 08:10:23 2016 From: sampsa.riikonen at iki.fi (Sampsa Riikonen) Date: Fri, 19 Feb 2016 15:10:23 +0200 Subject: [C++-sig] calling python object's method from c++ Message-ID: <56C7143F.7040900@iki.fi> Dear List, I am unable to solve an issue with the python c api (python 2.7). Namely, I am trying to pass a python object to c++, where the python object is then passed to a c++ class that calls the python object's method.. I only get segmentation faults. No one has been able to help with me with this problem, so I thought that a guru from this mailing list / community might know the answer. The question, with setup.py, source code, etc. has been posted here: http://stackoverflow.com/questions/35466991/calling-python-objects-method-from-c .. it is a few-liner, testing the concept of passing around python objects within c++. Regards, Sampsa From stefan at seefeld.name Sat Feb 20 09:30:39 2016 From: stefan at seefeld.name (Stefan Seefeld) Date: Sat, 20 Feb 2016 09:30:39 -0500 Subject: [C++-sig] calling python object's method from c++ In-Reply-To: <56C7143F.7040900@iki.fi> References: <56C7143F.7040900@iki.fi> Message-ID: <56C8788F.7020803@seefeld.name> On 19.02.2016 08:10, Sampsa Riikonen wrote: > Dear List, > > I am unable to solve an issue with the python c api (python 2.7). > > Namely, I am trying to pass a python object to c++, where the python > object is then passed to a c++ class that calls the python object's > method.. I only get segmentation faults. > > No one has been able to help with me with this problem, so I thought > that a guru from this mailing list / community might know the answer. > > The question, with setup.py, source code, etc. has been posted here: > > http://stackoverflow.com/questions/35466991/calling-python-objects-method-from-c > > > .. it is a few-liner, testing the concept of passing around python > objects within c++. Hi Sampsa, there are many stylistic issues with your code, and solving those would make the bug obvious. The bug is really in your testclass constructor: testclass(int* i, PyObject* po) { std::cerr << "testclass constructor! \n"; i=i; po=po; } You think you are assigning the two arguments 'i' and 'po' to the two member variables 'i', 'po', but you are not. You are assigning the variables to themselves. (The compiler may even chose to optimize away these as no-ops !) I don't know swig, but for rich mapping between Python and C++ I would suggest you consider Boost.Python, which is way more powerful. Stefan -- ...ich hab' noch einen Koffer in Berlin... From pneher at gmail.com Thu Feb 25 09:02:18 2016 From: pneher at gmail.com (Peter Neher) Date: Thu, 25 Feb 2016 15:02:18 +0100 Subject: [C++-sig] type of boost::python::numeric::array Message-ID: Hi everyone, i want to pass a numeric::array to python. This basically works fine, but the type of the resulting numpy array is always int but I need uint8. Is there a possibility to define the type of the numeric::array? Best, Peter -------------- next part -------------- An HTML attachment was scrubbed... URL: