From blake at hailmail.net Sun Jan 2 06:12:28 2011 From: blake at hailmail.net (Dane Springmeyer) Date: Sat, 1 Jan 2011 21:12:28 -0800 Subject: [C++-sig] Boost.Python and python 3.x? In-Reply-To: <452571.41742.qm@web111411.mail.gq1.yahoo.com> References: <452571.41742.qm@web111411.mail.gq1.yahoo.com> Message-ID: <2CB2ACF5-EF2F-49AF-A35A-F47C042B7FAF@hailmail.net> On Dec 30, 2010, at 9:33 AM, Ralf W. Grosse-Kunstleve wrote: > Hi Andrew, > > Yes, the GSoC project was finished. > I checked in a few Python3 related fixes after the > 1.45 release that may be important for you, in > particular the one below, which you could easily apply > manually to your installation. > > Ralf > Hi Ralf, Great stuff, thanks for doing this. I'm starting to use the new python3 support and that windows fix will surely help down the road. Curious if you could also take a look at https://svn.boost.org/trac/boost/ticket/4609 and see if it is viable for checkin? Thanks! Dane -------------- next part -------------- An HTML attachment was scrubbed... URL: From ndbecker2 at gmail.com Mon Jan 3 21:31:36 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 03 Jan 2011 15:31:36 -0500 Subject: [C++-sig] Status of Numpy support in boost python - II Message-ID: I was just re-reading these posts: Status of Numpy support in boost python http://web.archiveorange.com/archive/v/YJlerznoeRShLhlurQzC I wonder if there are any updates? Is ndarray working with current (1.45.0) boost? From rwgk at yahoo.com Mon Jan 3 22:44:09 2011 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 3 Jan 2011 13:44:09 -0800 (PST) Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: References: Message-ID: <441943.22336.qm@web111407.mail.gq1.yahoo.com> ----- Original Message ---- > From: Neal Becker > To: cplusplus-sig at python.org > Sent: Mon, January 3, 2011 12:31:36 PM > Subject: [C++-sig] Status of Numpy support in boost python - II > > I was just re-reading these posts: Status of Numpy support in boost python > http://web.archiveorange.com/archive/v/YJlerznoeRShLhlurQzC > > I wonder if there are any updates? > > Is ndarray working with current (1.45.0) boost? Boost.Python's existing numpy array support has been working all the time. It sounds like you are expecting new functionality. Could you be more specific? FWIW: recently I've added C++ copies between numpy arrays and the scitbx array library. You can find the implementation in this directory: http://cctbx.svn.sourceforge.net/viewvc/cctbx/trunk/scitbx/array_family/boost_python/ Look for numpy_bridge.hpp and numpy_bridge.cpp. You'll need something like this for other array libraries. Since there is no C++ "std" multi-dimensional array type it is difficult for Boost.Python to provide more support than it does currently. Ralf From seefeld at sympatico.ca Mon Jan 3 23:57:29 2011 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 03 Jan 2011 17:57:29 -0500 Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: <441943.22336.qm@web111407.mail.gq1.yahoo.com> References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> Message-ID: <4D225459.8050304@sympatico.ca> On 01/03/2011 04:44 PM, Ralf W. Grosse-Kunstleve wrote: > ----- Original Message ---- > >> From: Neal Becker >> To: cplusplus-sig at python.org >> Sent: Mon, January 3, 2011 12:31:36 PM >> Subject: [C++-sig] Status of Numpy support in boost python - II >> >> I was just re-reading these posts: Status of Numpy support in boost python >> http://web.archiveorange.com/archive/v/YJlerznoeRShLhlurQzC >> >> I wonder if there are any updates? >> >> Is ndarray working with current (1.45.0) boost? > Boost.Python's existing numpy array support has been working all the time. It > sounds like you are expecting new functionality. Could you be more specific? One point that I'm particularly missing (and which I'm referring to in the thread cited above) is the ability to access raw pointers around. Here is a simple use-case illustrating my needs: I have a library that uses a separation between data types ("blocks") and algorithms, akin to the C++ standard library. I would like to wrap that library in a way that allows me to access and share data with other Python packages using NumPy (notably SciPy), without the need to copy. Right now that's impossible, since the only means to allocate NumPy arrays in boost.python is via the Python interpreter. In my own wrapper, I use the much richer PyArray_New. The returned pointer is then usable via PyArray_GETPTR, allowing me to implement a "Block" type that I can bind my library to directly. Exposing such a rich API via boost.python obviously requires numpy/arrayobject.h to be available, at least during compilation of boost.python (or its numpy bindings), which may be considered inconvenient. I would be very happy for any such addition into boost.python, and would be happy to help implement it. Thanks, Stefan -- ...ich hab' noch einen Koffer in Berlin... From talljimbo at gmail.com Tue Jan 4 00:12:43 2011 From: talljimbo at gmail.com (Jim Bosch) Date: Mon, 03 Jan 2011 15:12:43 -0800 Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: <441943.22336.qm@web111407.mail.gq1.yahoo.com> References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> Message-ID: <4D2257EB.50506@gmail.com> On 01/03/2011 01:44 PM, Ralf W. Grosse-Kunstleve wrote: > ----- Original Message ---- > >> From: Neal Becker >> To: cplusplus-sig at python.org >> Sent: Mon, January 3, 2011 12:31:36 PM >> Subject: [C++-sig] Status of Numpy support in boost python - II >> >> I was just re-reading these posts: Status of Numpy support in boost python >> http://web.archiveorange.com/archive/v/YJlerznoeRShLhlurQzC >> >> I wonder if there are any updates? My low-level numpy library (basically a Boost.Python wrapper for the Numpy C-API) has received slow but steady updates. This is similar to the built-in Boost.Python numpy support, with a few extra features: - Raw-pointer access to Numpy arrays. - Constructing Numpy arrays from raw C++ pointers. - A C++ dtype object. - Tools to wrap C++ functors to behave like broadcasting numpy universal functions. This can be found in the boost sandbox: https://svn.boost.org/svn/boost/sandbox/numpy I have also built Boost.Python converters for my own ndarray library (see below) and Eigen 2. Right now the build system (scons-based) is tailored to my own needs, but it should be pretty easy for others to tweak as needed. Feel free to email me if you need help. I've included all of those in the ndarray package, though it should be possible to separate out the eigen bits and not use ndarray if you'd like. All that's here: http://code.google.com/p/ndarray/ >> >> Is ndarray working with current (1.45.0) boost? Probably. I haven't tested 1.45, I compiled recently on 1.44. I have been doing most of my work using 1.40, but I don't think anything has changed in Boost.Python since then that would affect ndarray. > > Boost.Python's existing numpy array support has been working all the time. It > sounds like you are expecting new functionality. Could you be more specific? > I think the low-level API I have in the sandbox fits this need, or at least provides some of the basics. I'd love to see that morph into something that we can all use to connect numpy to the C++ template library of our choice, since we'll all never agreed on those. Jim From rwgk at yahoo.com Tue Jan 4 01:05:39 2011 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 3 Jan 2011 16:05:39 -0800 (PST) Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: <4D2257EB.50506@gmail.com> References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> <4D2257EB.50506@gmail.com> Message-ID: <222414.34639.qm@web111412.mail.gq1.yahoo.com> > > > I think the low-level API I have in the sandbox fits this need, or at least > provides some of the basics. I'd love to see that morph into something that > we can all use to connect numpy to the C++ template library of our choice, Yes, that would be wonderful, indeed. Maybe a GSoC project? Ralf From seefeld at sympatico.ca Tue Jan 4 01:46:26 2011 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 03 Jan 2011 19:46:26 -0500 Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: <222414.34639.qm@web111412.mail.gq1.yahoo.com> References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> <4D2257EB.50506@gmail.com> <222414.34639.qm@web111412.mail.gq1.yahoo.com> Message-ID: <4D226DE2.4050401@sympatico.ca> On 01/03/2011 07:05 PM, Ralf W. Grosse-Kunstleve wrote: >> >> I think the low-level API I have in the sandbox fits this need, or at least >> provides some of the basics. I'd love to see that morph into something that >> we can all use to connect numpy to the C++ template library of our choice, > Yes, that would be wonderful, indeed. Maybe a GSoC project? Excellent idea. Let's see whether anyone is interested to pick it up (I'd be happy to mentor it). If not, perhaps we can make progress nonetheless. Stefan -- ...ich hab' noch einen Koffer in Berlin... From micdestefano at gmail.com Tue Jan 4 09:07:49 2011 From: micdestefano at gmail.com (Michele De Stefano) Date: Tue, 4 Jan 2011 09:07:49 +0100 Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: <4D226DE2.4050401@sympatico.ca> References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> <4D2257EB.50506@gmail.com> <222414.34639.qm@web111412.mail.gq1.yahoo.com> <4D226DE2.4050401@sympatico.ca> Message-ID: I also have made a small library that supports numpy. http://code.google.com/p/mds-utils/ Try it and look if it fits your needs. 2011/1/4 Stefan Seefeld > On 01/03/2011 07:05 PM, Ralf W. Grosse-Kunstleve wrote: > >> >>> I think the low-level API I have in the sandbox fits this need, or at >>> least >>> provides some of the basics. I'd love to see that morph into something >>> that >>> we can all use to connect numpy to the C++ template library of our >>> choice, >>> >> Yes, that would be wonderful, indeed. Maybe a GSoC project? >> > > Excellent idea. Let's see whether anyone is interested to pick it up (I'd > be happy to mentor it). If not, perhaps we can make progress nonetheless. > > > Stefan > > -- > > ...ich hab' noch einen Koffer in Berlin... > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Michele De Stefano http://www.micheledestefano.joomlafree.it http://code.google.com/p/mds-utils http://it.linkedin.com/in/micheledestefano -------------- next part -------------- An HTML attachment was scrubbed... URL: From ndbecker2 at gmail.com Tue Jan 4 14:34:01 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Tue, 04 Jan 2011 08:34:01 -0500 Subject: [C++-sig] Status of Numpy support in boost python - II References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> <4D2257EB.50506@gmail.com> Message-ID: I'm interested in the ndarray http://code.google.com/p/ndarray package. I still need to figure out how to get it to compile against my customized boost installation in a non-standard location. Unfortunately, trying to build the 'test' results in ICE in gcc-4.5.1-4.fc14.x86_64 (reported upstream) g++ -o boost.python/ndarray/libs/python/ndarray/test/ndarray_mod.os -c -O2 -fno- strict-aliasing -g -pipe -Wall -fexceptions -fstack-protector --param=ssp- buffer-size=4 -m64 -mtune=generic -fPIC -fwrapv -fPIC -Wp,-D_FORTIFY_SOURCE=2 - DNDEBUG -D_GNU_SOURCE -Iinclude -I/usr/include/python2.7 - I/usr/lib64/python2.7/site-packages/numpy/core/include -I/usr/include/eigen2 boost.python/ndarray/libs/python/ndarray/test/ndarray_mod.cpp -I include In file included from include/ndarray/ArrayTraits.hpp:11:0, from include/ndarray/Array.hpp:11, from include/ndarray.hpp:9, from include/boost/python/ndarray/Array.hpp:5, from include/boost/python/ndarray/ndarray.hpp:4, from boost.python/ndarray/libs/python/ndarray/test/ndarray_mod.cpp:1: include/ndarray/ExpressionTraits.hpp: In instantiation of ?ndarray::ExpressionTraits, ndarray::Array, ndarray::detail::EqualToTag::ScalarFunction, 1> >?: include/ndarray/ExpressionBase.hpp:33:57: instantiated from ?ndarray::ExpressionBase, ndarray::Array, ndarray::detail::EqualToTag::ScalarFunction, 1> >? include/ndarray/detail/BinaryOp.hpp:81:108: instantiated from ?ndarray::detail::BinaryOpExpression, ndarray::Array, ndarray::detail::EqualToTag::ScalarFunction, 1>? boost.python/ndarray/libs/python/ndarray/test/ndarray_mod.cpp:44:36: instantiated from ?bool acceptArray(const ndarray::Array&) [with T = double, int N = 1, int C = 1]? boost.python/ndarray/libs/python/ndarray/test/ndarray_mod.cpp:125:66: instantiated from here include/ndarray/ExpressionTraits.hpp:88:165: internal compiler error: Segmentation fault Jim Bosch wrote: > On 01/03/2011 01:44 PM, Ralf W. Grosse-Kunstleve wrote: >> ----- Original Message ---- >> >>> From: Neal Becker >>> To: cplusplus-sig at python.org >>> Sent: Mon, January 3, 2011 12:31:36 PM >>> Subject: [C++-sig] Status of Numpy support in boost python - II >>> >>> I was just re-reading these posts: Status of Numpy support in boost python >>> http://web.archiveorange.com/archive/v/YJlerznoeRShLhlurQzC >>> >>> I wonder if there are any updates? > > My low-level numpy library (basically a Boost.Python wrapper for the > Numpy C-API) has received slow but steady updates. This is similar to > the built-in Boost.Python numpy support, with a few extra features: > - Raw-pointer access to Numpy arrays. > - Constructing Numpy arrays from raw C++ pointers. > - A C++ dtype object. > - Tools to wrap C++ functors to behave like broadcasting numpy > universal functions. > > This can be found in the boost sandbox: > > https://svn.boost.org/svn/boost/sandbox/numpy > > > > I have also built Boost.Python converters for my own ndarray library > (see below) and Eigen 2. Right now the build system (scons-based) is > tailored to my own needs, but it should be pretty easy for others to > tweak as needed. Feel free to email me if you need help. > > I've included all of those in the ndarray package, though it should be > possible to separate out the eigen bits and not use ndarray if you'd > like. All that's here: > > http://code.google.com/p/ndarray/ > >>> >>> Is ndarray working with current (1.45.0) boost? > > Probably. I haven't tested 1.45, I compiled recently on 1.44. I have > been doing most of my work using 1.40, but I don't think anything has > changed in Boost.Python since then that would affect ndarray. > >> >> Boost.Python's existing numpy array support has been working all the time. It >> sounds like you are expecting new functionality. Could you be more specific? >> > > > > I think the low-level API I have in the sandbox fits this need, or at > least provides some of the basics. I'd love to see that morph into > something that we can all use to connect numpy to the C++ template > library of our choice, since we'll all never agreed on those. > > Jim From talljimbo at gmail.com Tue Jan 4 20:35:11 2011 From: talljimbo at gmail.com (Jim Bosch) Date: Tue, 04 Jan 2011 11:35:11 -0800 Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> <4D2257EB.50506@gmail.com> Message-ID: <4D23766F.2030901@gmail.com> On 01/04/2011 05:34 AM, Neal Becker wrote: > I'm interested in the ndarray http://code.google.com/p/ndarray package. > > I still need to figure out how to get it to compile against my customized boost > installation in a non-standard location. > > Unfortunately, trying to build the 'test' results in ICE in > gcc-4.5.1-4.fc14.x86_64 (reported upstream) > Hmm. I haven't seen that, but I also haven't tried gcc 4.5 yet. Unfortunately, you've caught me in a busy week, but I'll make an effort to try gcc 4.5 (and boost 1.45) as soon as I can. Hopefully I can at least come up with a simple test case to help the gcc people out. In the meantime, if you just do "scons ndarray-test", do you still get the crash? That should exercise the same code that seems to be giving gcc trouble without any Python involvement. Jim From talljimbo at gmail.com Tue Jan 4 21:23:38 2011 From: talljimbo at gmail.com (Jim Bosch) Date: Tue, 04 Jan 2011 12:23:38 -0800 Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: <4D226DE2.4050401@sympatico.ca> References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> <4D2257EB.50506@gmail.com> <222414.34639.qm@web111412.mail.gq1.yahoo.com> <4D226DE2.4050401@sympatico.ca> Message-ID: <4D2381CA.9010703@gmail.com> On 01/03/2011 04:46 PM, Stefan Seefeld wrote: > On 01/03/2011 07:05 PM, Ralf W. Grosse-Kunstleve wrote: >>> >>> I think the low-level API I have in the sandbox fits this need, or at >>> least >>> provides some of the basics. I'd love to see that morph into >>> something that >>> we can all use to connect numpy to the C++ template library of our >>> choice, >> Yes, that would be wonderful, indeed. Maybe a GSoC project? > > Excellent idea. Let's see whether anyone is interested to pick it up > (I'd be happy to mentor it). If not, perhaps we can make progress > nonetheless. I'd be happy to help as well, though I don't really have the experience with boost development necessary to qualify as a mentor. I'll start by putting together a README file for the sandbox library over the next few days so others can get a better idea of where we might be starting from. Aside from direct data access (and the other things I have implemented already), what else should such a library do? One thing I can think of off the top of my head is automatic conversion for Numpy scalar types - I'm pretty sure someone else on this list has implemented that somewhere, but it's not present in the sandbox library. Jim From David.Aldrich at EU.NEC.COM Thu Jan 6 15:58:59 2011 From: David.Aldrich at EU.NEC.COM (David Aldrich) Date: Thu, 6 Jan 2011 14:58:59 +0000 Subject: [C++-sig] Boost / python-devel / python dependency problem Message-ID: <0CCCA5445D7FCB4298BBF0739D4FDD52028642454E95@EUEXCLU01.EU.NEC.COM> Hi I wonder if someone could help me out with a yum dependency problem please? I am running Centos 5.5. I installed Boost 1.40 from the Black Op Software repo. I now need to install python-devel, but when I do so I get: ================================================================= $ sudo yum install python-devel Loaded plugins: fastestmirror, priorities Loading mirror speeds from cached hostfile * addons: anorien.csc.warwick.ac.uk * base: anorien.csc.warwick.ac.uk * extras: mirror.as29550.net * updates: anorien.csc.warwick.ac.uk 943 packages excluded due to repository priority protections Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package python-devel.i386 0:2.4.3-27.el5 set to be updated --> Processing Dependency: python = 2.4.3-27.el5 for package: python-devel --> Finished Dependency Resolution python-devel-2.4.3-27.el5.i386 from base has depsolving problems ? --> Missing Dependency: python = 2.4.3-27.el5 is needed by package python-devel-2.4.3-27.el5.i386 (base) Error: Missing Dependency: python = 2.4.3-27.el5 is needed by package python-devel-2.4.3-27.el5.i386 (base) You could try using --skip-broken to work around the problem You could try running: package-cleanup --problems ??????????????????????? package-cleanup --dupes ??????????? ????????????rpm -Va --nofiles --nodigest The program package-cleanup is found in the yum-utils package. ================================================================= However, I do have python 2.4 installed: ================================================================= $ sudo yum install python Loaded plugins: fastestmirror, priorities Loading mirror speeds from cached hostfile * addons: anorien.csc.warwick.ac.uk * base: anorien.csc.warwick.ac.uk * extras: mirror.as29550.net * updates: anorien.csc.warwick.ac.uk 943 packages excluded due to repository priority protections Setting up Install Process Package matching python-2.4.3-27.el5.i386 already installed. Checking for update. Nothing to do ================================================================= Finally, this may be useful information: ================================================================= [daldrich at localhost Zodiac]$ yum list python Loaded plugins: fastestmirror, priorities blackop 474/474 943 packages excluded due to repository priority protections Installed Packages python.i386 2.4.3-27.el5_5.3 installed [daldrich at localhost Zodiac]$ ================================================================= So python-devel seems to want python 2.4.3-27.el5 but I have 2.4.3-27.el5_5.3 Any suggestions how I can fix this please? David From seefeld at sympatico.ca Thu Jan 6 16:52:00 2011 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 06 Jan 2011 10:52:00 -0500 Subject: [C++-sig] Boost / python-devel / python dependency problem In-Reply-To: <0CCCA5445D7FCB4298BBF0739D4FDD52028642454E95@EUEXCLU01.EU.NEC.COM> References: <0CCCA5445D7FCB4298BBF0739D4FDD52028642454E95@EUEXCLU01.EU.NEC.COM> Message-ID: <4D25E520.3070507@sympatico.ca> On 01/06/2011 09:58 AM, David Aldrich wrote: > Hi > > I wonder if someone could help me out with a yum dependency problem please? > > I am running Centos 5.5. I installed Boost 1.40 from the Black Op Software repo. I now need to install python-devel, but when I do so I get: [...] This looks like a packaging bug to me, or a conflict between different package repositories. You may have better luck contacting distribution-specific lists, or submitting bug reports there. Stefan -- ...ich hab' noch einen Koffer in Berlin... From talljimbo at gmail.com Sat Jan 8 00:51:49 2011 From: talljimbo at gmail.com (Jim Bosch) Date: Fri, 07 Jan 2011 15:51:49 -0800 Subject: [C++-sig] Status of Numpy support in boost python - II In-Reply-To: <4D23766F.2030901@gmail.com> References: <441943.22336.qm@web111407.mail.gq1.yahoo.com> <4D2257EB.50506@gmail.com> <4D23766F.2030901@gmail.com> Message-ID: <4D27A715.1020600@gmail.com> On 01/04/2011 11:35 AM, Jim Bosch wrote: > On 01/04/2011 05:34 AM, Neal Becker wrote: >> I'm interested in the ndarray http://code.google.com/p/ndarray package. >> >> I still need to figure out how to get it to compile against my >> customized boost >> installation in a non-standard location. >> >> Unfortunately, trying to build the 'test' results in ICE in >> gcc-4.5.1-4.fc14.x86_64 (reported upstream) >> Update: this is a known regression in gcc 4.5, and it looks like there's nothing I can do about it except work around it and wait for a fix to gcc. With that in mind, ndarray now disables some functionality (mathematical and comparison operators, but not including assignment and augmented assignment) when compiled with gcc 4.5. I have also updated the build system for both ndarray and Boost.Python.Numpy so one can specify additional include and library directories on the command line (see "scons --help" for more info). Neal, this should now be preferred to the SConstruct edit I suggested earlier. Jim From matt_boost at perceptiveautomation.com Wed Jan 12 18:03:44 2011 From: matt_boost at perceptiveautomation.com (Matt Bendiksen) Date: Wed, 12 Jan 2011 11:03:44 -0600 Subject: [C++-sig] problem returning polymorphic shared_ptr instances Message-ID: <143B6B90-1BC0-4DCB-8AAA-E2A2C1BA4B39@perceptiveautomation.com> I'm stumped on a problem encountered when trying to return a derived instance via a shared_ptr to python. The python instance is always shown as a base class and not the subclass that was created, as if the instance was sliced. struct A { }; struct B : A { }; shared_ptr get_a_or_b_instance(bool returnB) { return shared_ptr(returnB ? new B() : new A()); } ? def("get_a_or_b_instance", get_a_or_b_instance); class_ >("A", init<>()) ; class_, boost::noncopyable>("B", init<>()) ; And the Python result is always a type A object: >>> type(app.get_a_or_b_instance(False)) >>> type(app.get_a_or_b_instance(True)) Where I would expect the derived instance type B returned from the second call. I'm using Boost 1.42.0. Thanks in advance for any help. Matt Bendiksen From talljimbo at gmail.com Wed Jan 12 18:54:24 2011 From: talljimbo at gmail.com (Jim Bosch) Date: Wed, 12 Jan 2011 09:54:24 -0800 Subject: [C++-sig] problem returning polymorphic shared_ptr instances In-Reply-To: <143B6B90-1BC0-4DCB-8AAA-E2A2C1BA4B39@perceptiveautomation.com> References: <143B6B90-1BC0-4DCB-8AAA-E2A2C1BA4B39@perceptiveautomation.com> Message-ID: <4D2DEAD0.1060300@gmail.com> On 01/12/2011 09:03 AM, Matt Bendiksen wrote: > I'm stumped on a problem encountered when trying to return a derived instance via a shared_ptr to python. The python instance is always shown as a base class and not the subclass that was created, as if the instance was sliced. > > struct A { > }; > struct B : A { > }; > shared_ptr get_a_or_b_instance(bool returnB) { > return shared_ptr(returnB ? new B() : new A()); > } > ? > def("get_a_or_b_instance", get_a_or_b_instance); > class_ >("A", init<>()) > ; > class_, boost::noncopyable>("B", init<>()) > ; > > And the Python result is always a type A object: > > >>> type(app.get_a_or_b_instance(False)) > > > >>> type(app.get_a_or_b_instance(True)) > > > Where I would expect the derived instance type B returned from the second call. > > I'm using Boost 1.42.0. > > Thanks in advance for any help. You need to either declare B with "class_, bases" or do "register_ptr_to_python< boost::shared_ptr >()" somewhere in your Python module. The latter will not cause B instances to be stored in a shared_ptr when a Python constructor is called, but it will still enable shared_ptr downcasting on return. Jim > > Matt Bendiksen > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From matt_boost at perceptiveautomation.com Wed Jan 12 21:06:09 2011 From: matt_boost at perceptiveautomation.com (Matt Bendiksen) Date: Wed, 12 Jan 2011 14:06:09 -0600 Subject: [C++-sig] problem returning polymorphic shared_ptr instances In-Reply-To: <4D2DEAD0.1060300@gmail.com> References: <143B6B90-1BC0-4DCB-8AAA-E2A2C1BA4B39@perceptiveautomation.com> <4D2DEAD0.1060300@gmail.com> Message-ID: <4122E704-EB93-4D4C-8959-5C586EF65B6C@perceptiveautomation.com> Thanks Jim, but I still cannot get it to work. I now have it defined as: def("get_a_or_b_instance", get_a_or_b_instance); class_ >("A", init<>()) ; class_, bases >("B", init<>()) ; (note I change your example line from "bases" to "bases") I tried it with and without: register_ptr_to_python< boost::shared_ptr >(); But from Python whenever I call: type(app.get_a_or_b_instance(True)) I always get back: Any other ideas? Matt From talljimbo at gmail.com Wed Jan 12 22:30:03 2011 From: talljimbo at gmail.com (Jim Bosch) Date: Wed, 12 Jan 2011 13:30:03 -0800 Subject: [C++-sig] problem returning polymorphic shared_ptr instances In-Reply-To: <4122E704-EB93-4D4C-8959-5C586EF65B6C@perceptiveautomation.com> References: <143B6B90-1BC0-4DCB-8AAA-E2A2C1BA4B39@perceptiveautomation.com> <4D2DEAD0.1060300@gmail.com> <4122E704-EB93-4D4C-8959-5C586EF65B6C@perceptiveautomation.com> Message-ID: <4D2E1D5B.6010408@gmail.com> On 01/12/2011 12:06 PM, Matt Bendiksen wrote: > Thanks Jim, but I still cannot get it to work. I now have it defined as: > > def("get_a_or_b_instance", get_a_or_b_instance); > class_ >("A", init<>()) > ; > class_, bases >("B", init<>()) > ; > > (note I change your example line from "bases" to "bases") > Yeah, that's correct. Error on my part. > I tried it with and without: > > register_ptr_to_python< boost::shared_ptr >(); > > But from Python whenever I call: > > type(app.get_a_or_b_instance(True)) > > I always get back: > > > > Any other ideas? > Yes. I just remembered that the downcasting only works if your classes are polymorphic; otherwise dynamic_cast (which is the underlying mechanism) doesn't work. Adding a virtual destructor to the base class would be sufficient. Jim > Matt > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From matt_boost at perceptiveautomation.com Thu Jan 13 03:52:11 2011 From: matt_boost at perceptiveautomation.com (Matt Bendiksen) Date: Wed, 12 Jan 2011 20:52:11 -0600 Subject: [C++-sig] problem returning polymorphic shared_ptr instances In-Reply-To: <4D2E1D5B.6010408@gmail.com> References: <143B6B90-1BC0-4DCB-8AAA-E2A2C1BA4B39@perceptiveautomation.com> <4D2DEAD0.1060300@gmail.com> <4122E704-EB93-4D4C-8959-5C586EF65B6C@perceptiveautomation.com> <4D2E1D5B.6010408@gmail.com> Message-ID: <0D5E0EAC-9E53-4EB8-A532-329C63BA2FDD@perceptiveautomation.com> You are indeed correct -- I just needed to add a virtual method to the base class. As it turns out this wasn't the problem I was originally having in our application, so my attempt at creating a small sample of the problem wasn't complete. My project's code was failing for what turned out to be a different, albeit somewhat related reason. It already had many virtual methods including the destructor. The problem with my app was I had 2 levels deep of subclasses and I didn't realize the instance I was returning was the second descendant class and not the first, which was a problem because I hadn't yet defined the python bindings for the second descendant yet. So in python it ended up giving me the base class instead. Anyhow, everything is working now and I appreciate the prompt help. Regards, Matt > Yes. I just remembered that the downcasting only works if your classes are polymorphic; otherwise dynamic_cast (which is the underlying mechanism) doesn't work. > > Adding a virtual destructor to the base class would be sufficient. > > Jim From michael.wand at kit.edu Sat Jan 15 15:01:24 2011 From: michael.wand at kit.edu (Michael Wand) Date: Sat, 15 Jan 2011 15:01:24 +0100 Subject: [C++-sig] Somewhat beginners error in using boost::python::list from C++? Message-ID: <201101151501.24596.michael.wand@kit.edu> Hello, sorry if this question is too trivial, but I've got some rather strange problem with using the boost::python type wrapper for the python list. It goes as follows: I've got a very small demo program (say main.cpp). Running it causes a segfault. (The actual project is much larger, of course.) The system I use is a Linux OpenSuse 11.1, with the distribution package python-2.6.0-2.23.1 installed. I manually installed boost 1.44 (because the newer versions didn't quite work with the boost-log package, which is needed for the actual project). Which other information might be helpful? This is the program: ******************************************* #include #include using namespace std; int main() { boost::python::list myList; myList.append("foo"); myList.append("fork"); cerr << "Printing length of list." << endl; cerr << "Length of myList: " << boost::python::len(myList) << endl; // the above line causes a segfault! cerr << "TheList: " << boost::python::extract(myList[0])() << "," << boost::python::extract(myList[1])() << endl; cerr << "Finished" << endl; } ******************************************* which I compile by the two commands: /usr/bin/c++ -O0 -Wall -g -I. -I/usr/include/python2.6 -I/usr/local/include -o main.o -c main.cpp /usr/bin/c++ -O0 -Wall -g main.o -o boost_list_test -rdynamic -L/usr/local/lib /usr/local/lib/libboost_python.so -lpython2.6 -Wl,-rpath,/usr/local/lib The segfault occurs at the call to boost::python::len(myList). I debugged the program with gdb, but the backtrace #0 0xb7ec6027 in PyErr_Occurred () from /usr/lib/libpython2.6.so.1.0 #1 0x0804b0b1 in main () at main.cpp:15 doesn't really tell me anything. Interestingly, removing the offending line causes a segfault at the *next* line, with a more verbose backtrace: #0 0xb7e529d7 in PyInt_FromLong () from /usr/lib/libpython2.6.so.1.0 #1 0x0804ba33 in arg_to_python (this=0xbfffee70, x=@0xbfffef48) at /usr/local/include/boost/python/converter/builtin_converters.hpp:122 #2 0x0804ba5d in boost::python::api::object_initializer_impl::get (x=@0xbfffef48) at /usr/local/include/boost/python/object_core.hpp:393 #3 0x0804ba9e in boost::python::api::object_base_initializer (x=@0xbfffef48) at /usr/local/include/boost/python/object_core.hpp:315 #4 0x0804bab1 in object (this=0xbfffeedc, x=@0xbfffef48) at /usr/local/include/boost/python/object_core.hpp:334 #5 0x0804bae1 in boost::python::api::object_operators::operator[] (this=0xbfffef28, key=@0xbfffef48) at /usr/local/include/boost/python/object_items.hpp:61 #6 0x0804af46 in main () at main.cpp:17 I really don't understand what I'm doing wrong. Do I need to resize the list before using append? The documentation (http://www.boost.org/doc/libs/1_44_0/libs/python/doc/v2/list.html) doesn't say anything about this. Any tips or hints are greatly appreciated! Thankyou, - Michael Wand From seefeld at sympatico.ca Sat Jan 15 15:35:40 2011 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Sat, 15 Jan 2011 09:35:40 -0500 Subject: [C++-sig] Somewhat beginners error in using boost::python::list from C++? In-Reply-To: <201101151501.24596.michael.wand@kit.edu> References: <201101151501.24596.michael.wand@kit.edu> Message-ID: <4D31B0BC.9020402@sympatico.ca> On 2011-01-15 09:01, Michael Wand wrote: > int main() { > boost::python::list myList; > myList.append("foo"); > myList.append("fork"); > cerr<< "Printing length of list."<< endl; > cerr<< "Length of myList: "<< boost::python::len(myList)<< endl; > // the above line causes a segfault! > cerr<< "TheList: "<< boost::python::extract(myList[0])()<< "," > << boost::python::extract(myList[1])()<< endl; > cerr<< "Finished"<< endl; > } You haven't initialized the Python runtime with "Py_Initialize()". Stefan -- ...ich hab' noch einen Koffer in Berlin... From michael.wand at kit.edu Mon Jan 17 20:15:45 2011 From: michael.wand at kit.edu (Michael Wand) Date: Mon, 17 Jan 2011 20:15:45 +0100 Subject: [C++-sig] Somewhat beginners error in using boost::python::list from C++? In-Reply-To: <4D31B0BC.9020402@sympatico.ca> References: <201101151501.24596.michael.wand@kit.edu> <4D31B0BC.9020402@sympatico.ca> Message-ID: <201101172015.46083.michael.wand@kit.edu> Hello, yes, that's it. Thankyou for the quick response. (The story: The original error occurred in a unittest file, which previously hadn't used python at all. The main program was running flawlessly all the time.) That's it, have a nice evening - Michael On Saturday 15 January 2011 15:35:40 Stefan Seefeld wrote: > On 2011-01-15 09:01, Michael Wand wrote: > > int main() { > > > > boost::python::list myList; > > myList.append("foo"); > > myList.append("fork"); > > cerr<< "Printing length of list."<< endl; > > cerr<< "Length of myList: "<< boost::python::len(myList)<< endl; > > > > // the above line causes a segfault! > > > > cerr<< "TheList: "<< boost::python::extract(myList[0])()<< > > "," > > > > << boost::python::extract(myList[1])()<< > > endl; > > > > cerr<< "Finished"<< endl; > > > > } > > You haven't initialized the Python runtime with "Py_Initialize()". > > Stefan -- Dipl. Math. Michael Wand Cognitive Systems Lab, Institute for Anthropomatics Karlsruhe Institute of Technology http://csl.anthropomatik.kit.edu/~mwand