From alexander.belopolsky at gmail.com Fri Jan 2 19:16:49 2009 From: alexander.belopolsky at gmail.com (Alexander Belopolsky) Date: Fri, 2 Jan 2009 13:16:49 -0500 Subject: [C++-sig] #ifdef __cplusplus? In-Reply-To: <5b8d13220901020931h70d9deb9n85bb24dd568765d7@mail.gmail.com> References: <200901011630.38196.doomster@knuut.de> <5b8d13220901020141t5160eaaata5e7bb6eda4f1254@mail.gmail.com> <495E4267.30307@cheimes.de> <5b8d13220901020931h70d9deb9n85bb24dd568765d7@mail.gmail.com> Message-ID: First, by copying c++-sig, let me invite C++ experts to comment on this thread and the tracker issue: http://mail.python.org/pipermail/python-dev/2009-January/084685.html http://bugs.python.org/issue4805 My patch highlights several issues: 1. (OP's issue.) Do we need #ifdef __cplusplus wrappers in .c files? 2. Should malloc() calls be prefixed with explicit type casts? 3. Should use of void* pointers be discouraged when typed pointers will work? 4. Should exported symbols be always declared in headers or is it ok to just declare them as extern in .c files where they are used? 5. Should the use of C++ keywords such as "new" or "class" be discouraged? On #1, I find it silly to have #ifdef __cplusplus in the files that cannot be compiled with C++ in the first place. Even if the files are fixed to compile with C++, I have arguments that wrapping the entire file in extern "C" is an overkill and hides design flaws. On #4, Marc-Andre Lemburg commented on the tracker: """ Moving declarations into header files is not really in line with the way Python developers use header files: We usually only put code into header files that is meant for public use. Buy putting declarations into the header files without additional warning, you implicitly document them and make them usable in non-interpreter code. """ I disagree. Declaring functions in .c files breaks dependency analysis and may lead to subtle bugs. AFAIK, Python's convention for global functions that are not meant to be used outside of the interpreter is the _Py prefix. If an extra protection is deemed necessary, non-public global symbols can be declared in separate header files that are not included by Python.h. On #3 and #5 arguments can be made that C++ compliant code is better irrespective of C++. #2 seems to be the most controversial, but explicit casts seem to be the current norm and if perceived ugliness will discourage use of mallocs in favor of higher level APIs, it is probably a good thing. I believe restricting python core code to an intersection of c89 and C++ will improve the overall self-consistency and portability of the code and will allow python to be used in more systems, but since I don't use C++ myself, I will only argue for changes such as #4 that are independent of C++. On Fri, Jan 2, 2009 at 12:31 PM, David Cournapeau wrote: > On Sat, Jan 3, 2009 at 1:35 AM, Christian Heimes wrote: >> David Cournapeau schrieb: >>> Can't those errors be found simply using appropriate warning flags in >>> the C compiler ? C has stopped being a subset of C++ a long time ago >> >> Python's C code still follow the ANSI C89 standard. The fact puts 'long >> time ago' in a different perspective. :) > > In my mind, C++ is largely responsible for C not being a subset of > C++, so the type of C used by the codebase is not that important in > that context. > > David > From matthieu.brucher at gmail.com Fri Jan 2 19:31:40 2009 From: matthieu.brucher at gmail.com (Matthieu Brucher) Date: Fri, 2 Jan 2009 19:31:40 +0100 Subject: [C++-sig] [Python-Dev] #ifdef __cplusplus? In-Reply-To: References: <200901011630.38196.doomster@knuut.de> <5b8d13220901020141t5160eaaata5e7bb6eda4f1254@mail.gmail.com> <495E4267.30307@cheimes.de> <5b8d13220901020931h70d9deb9n85bb24dd568765d7@mail.gmail.com> Message-ID: Hi, 2009/1/2 Alexander Belopolsky : > First, by copying c++-sig, let me invite C++ experts to comment on > this thread and the tracker issue: > > http://mail.python.org/pipermail/python-dev/2009-January/084685.html > http://bugs.python.org/issue4805 > > My patch highlights several issues: > > 1. (OP's issue.) Do we need #ifdef __cplusplus wrappers in .c files? It seems that it is a reminiscence of old compilers, perhaps not even supported anymore, thus it shouldn't be an issue anymore. > 2. Should malloc() calls be prefixed with explicit type casts? When I learnt C, I was always told to explicitely cast. > 3. Should use of void* pointers be discouraged when typed pointers will work? I think type checking is good in C. > 4. Should exported symbols be always declared in headers or is it ok > to just declare them as extern in .c files where they are used? I agree with you, declaration should be put in headers, to prevent a signature change for instance. One place is always better than several places (you have to define them somewhere if you want to call them without a warning). If we don't do this, we are back to the Fortran 77 days. Besides, if those functions are not to be advertized, nothing prevents them for inclusion inside "private" headers that are not included my Python.h. This way, you can benefit from C checks, without the advertizement (letting them in C files does not prevent their use, putting them in headers prevents crashes). With Visual Studio, you can say which function you want to export in your library. This can be done with gcc as well, even if it is not the default behavior. > 5. Should the use of C++ keywords such as "new" or "class" be discouraged? As CPython will not be ported to C++, I don't see the point of not using "new" and "class" for variables names inside a function. Matthieu -- Information System Engineer, Ph.D. Website: http://matthieu-brucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher From cournape at gmail.com Fri Jan 2 20:06:38 2009 From: cournape at gmail.com (David Cournapeau) Date: Sat, 3 Jan 2009 04:06:38 +0900 Subject: [C++-sig] [Python-Dev] #ifdef __cplusplus? In-Reply-To: References: <200901011630.38196.doomster@knuut.de> <5b8d13220901020141t5160eaaata5e7bb6eda4f1254@mail.gmail.com> <495E4267.30307@cheimes.de> <5b8d13220901020931h70d9deb9n85bb24dd568765d7@mail.gmail.com> Message-ID: <5b8d13220901021106n4548c4c5ja46c3759d1301f13@mail.gmail.com> Hi Matthis, On Sat, Jan 3, 2009 at 3:31 AM, Matthieu Brucher wrote: > > When I learnt C, I was always told to explicitely cast. Maybe your professor was used to old C :) It is discouraged practice to cast malloc - the only rationale I can think of nowadays is when you have to compile the source with both a C and C++ compiler. Otherwise, it is redundant at best (it was useful when malloc was defined as returning char*, and C did not allow for automatic void* to other pointer cast). David From matthieu.brucher at gmail.com Fri Jan 2 20:21:16 2009 From: matthieu.brucher at gmail.com (Matthieu Brucher) Date: Fri, 2 Jan 2009 20:21:16 +0100 Subject: [C++-sig] [Python-Dev] #ifdef __cplusplus? In-Reply-To: <5b8d13220901021106n4548c4c5ja46c3759d1301f13@mail.gmail.com> References: <200901011630.38196.doomster@knuut.de> <5b8d13220901020141t5160eaaata5e7bb6eda4f1254@mail.gmail.com> <495E4267.30307@cheimes.de> <5b8d13220901020931h70d9deb9n85bb24dd568765d7@mail.gmail.com> <5b8d13220901021106n4548c4c5ja46c3759d1301f13@mail.gmail.com> Message-ID: >> When I learnt C, I was always told to explicitely cast. > > Maybe your professor was used to old C :) That's more than likely :D Matthieu -- Information System Engineer, Ph.D. Website: http://matthieu-brucher.developpez.com/ Blogs: http://matt.eifelle.com and http://blog.developpez.com/?blog=92 LinkedIn: http://www.linkedin.com/in/matthieubrucher From meine at informatik.uni-hamburg.de Sun Jan 4 21:21:20 2009 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Sun, 4 Jan 2009 21:21:20 +0100 Subject: [C++-sig] Rational for 'Zero-length slice' RuntimeError? Message-ID: <200901042121.29221.meine@informatik.uni-hamburg.de> Hi, I have exported an array-like class using the __getitem__ implementation given below among its overloads. It uses boost::python::slice.get_indices(), and I am getting a RuntimeError when I try to acces e.g. [1:-1] on a 2-element array. What is the rationale for this "Zero-length slice" error? I would expect an empty iterator range. OK, if end < begin, that could make problems, but what am I expected to do to prevent this RuntimeError? (Yes, I did RTFM, but maybe I am overlooking something?) template std::auto_ptr Array__getitem_slice__(Array const & a, boost::python::slice sl) { boost::python::slice::range bounds = sl.template get_indicies<>(a.begin(), a.end()); if(bounds.step != 1) { PyErr_SetString(PyExc_IndexError, "No extended slicing supported yet."); boost::python::throw_error_already_set(); } return std::auto_ptr(new Array(bounds.start, bounds.stop+1)); } 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 meine at informatik.uni-hamburg.de Mon Jan 5 15:43:06 2009 From: meine at informatik.uni-hamburg.de (Hans Meine) Date: Mon, 5 Jan 2009 15:43:06 +0100 Subject: [C++-sig] Rational for 'Zero-length slice' RuntimeError? In-Reply-To: <200901042121.29221.meine@informatik.uni-hamburg.de> References: <200901042121.29221.meine@informatik.uni-hamburg.de> Message-ID: <200901051543.13802.meine@informatik.uni-hamburg.de> On Sonntag 04 Januar 2009, Hans Meine wrote: > I have exported an array-like class using the __getitem__ implementation > given below among its overloads. It uses > boost::python::slice.get_indices(), and I am getting a RuntimeError when I > try to acces e.g. [1:-1] on a 2-element array. What is the rationale for > this "Zero-length slice" error? I would expect an empty iterator range. > OK, if end < begin, that could make problems, but what am I expected to do > to prevent this RuntimeError? (Yes, I did RTFM, but maybe I am overlooking > something?) Yes, I was overlooking quite some things. Quoting the docs: > Rationale: [...] > exceptions on empty slice: It is impossible to define a closed interval > over an empty range, so some other form of error checking would have to be > used to prevent undefined behavior. In the case where the exception is not > caught, it will simply be translated to Python by the default exception > handling mechanisms. I was tired and the automatic translation of std::invalid_argument into a RuntimeError obviously outsmarted me. So obviously, the answer is that I have to copy the boilerplate-try..catch from the docs (and handle the empty interval case). 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 correo.comercial at telefonica.net Mon Jan 5 19:37:51 2009 From: correo.comercial at telefonica.net (Correo Comercial) Date: Mon, 5 Jan 2009 19:37:51 +0100 Subject: [C++-sig] =?iso-8859-1?q?Reg_=E1_late_un_caprichito=2E=2E=2E?= Message-ID: <495FDCCF000CACF0@ctsmtpout2.frontal.correo> (added by postmaster@telefonica.net) PUBLI Publicidad Adistech Europe, S.L. *Por la compra de dos unidades o m?s, precios especiales!!! Cons?ltanos al 93 481 4162. Adistech Europe, S.L. adistech.europesl at gmail.com PD: Para cualquier consulta, puedes ponerte en contacto con nuestro equipo al tel. (+34) 93 481 4162 Si deseas darte de baja de nuestras listas de distribuciones, por favor pulsa aqu? (poniendo en el asunto la palabra "baja"). -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/jpeg Size: 84537 bytes Desc: not available URL: From ndbecker2 at gmail.com Wed Jan 7 19:34:37 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 07 Jan 2009 13:34:37 -0500 Subject: [C++-sig] can boost::python class imlement buffer protocol? Message-ID: This would require filling in tp_as_buffer field in the PyTypeObject structure for the class. Is this possible? Any clue how/where this could be done? From dave at boostpro.com Thu Jan 8 02:13:00 2009 From: dave at boostpro.com (David Abrahams) Date: Wed, 07 Jan 2009 20:13:00 -0500 Subject: [C++-sig] can boost::python class imlement buffer protocol? In-Reply-To: (Neal Becker's message of "Wed, 07 Jan 2009 13:34:37 -0500") References: Message-ID: <87hc4ad2hv.fsf@mcbain.luannocracy.com> on Wed Jan 07 2009, Neal Becker wrote: > This would require filling in tp_as_buffer field in the PyTypeObject structure for the > class. Is this possible? Any clue how/where this could be done? I honestly don't know the answers to these questions, sorry. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From ndbecker2 at gmail.com Thu Jan 8 04:05:24 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Wed, 07 Jan 2009 22:05:24 -0500 Subject: [C++-sig] can boost::python class imlement buffer protocol? References: <87hc4ad2hv.fsf@mcbain.luannocracy.com> Message-ID: David Abrahams wrote: > > on Wed Jan 07 2009, Neal Becker wrote: > >> This would require filling in tp_as_buffer field in the PyTypeObject >> structure for the >> class. Is this possible? Any clue how/where this could be done? > > I honestly don't know the answers to these questions, sorry. > As a workaround, I currently implemented an 'as_buffer' member function. For example: template inline object as_buffer (ublas::vector & m) { return object (handle<> (PyBuffer_FromReadWriteMemory ((void*)(&m.data()[0]), m.size()*sizeof(T)))); } From Del_Robins at symantec.com Thu Jan 8 18:05:45 2009 From: Del_Robins at symantec.com (Del Robins) Date: Thu, 8 Jan 2009 10:05:45 -0700 Subject: [C++-sig] Full Cross-Module Support for Boost Python on Windows Message-ID: Hi all, I am new to Python and just recently started using boost python to create some wrappers for some C++ static libraries. I have been able to get most everything to work from the documentation and community examples. Thank you for your efforts. The one issue that I am still hitting is full cross module support for boost wrapped modules running on windows. I've looked through the past discussions http://mail.python.org/pipermail/cplusplus-sig/2005-August/009135.html and have seen conclusions and solutions for platforms other than windows but have not been able to find a message summarizing a solution on windows. The last discussion that I was able to find was from January of last year http://mail.python.org/pipermail/cplusplus-sig/2008-January/012874.html http://mail.python.org/pipermail/cplusplus-sig/2008-January/012875.html http://mail.python.org/pipermail/cplusplus-sig/2008-January/012876.html Unfortunately, our build system is using an older version of the boost libraries; 1.34. I am trying to convince our CM team to upgrade them to the latest, but I am getting some push back because of a compatibility issue with the latest regex boost library. Our standard for Python (2.4.1.) is also a little out of date because of reluctance to move by our QA folks. Does anyone know the current state for the full cross-module support on the windows platform? Has this been solved by anyone? Am I still having problems because of older libraries? Thanks, Del Robins -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boostpro.com Thu Jan 8 20:49:02 2009 From: dave at boostpro.com (David Abrahams) Date: Thu, 08 Jan 2009 14:49:02 -0500 Subject: [C++-sig] Full Cross-Module Support for Boost Python on Windows In-Reply-To: (Del Robins's message of "Thu, 8 Jan 2009 10:05:45 -0700") References: Message-ID: <873aftbmtt.fsf@mcbain.luannocracy.com> on Thu Jan 08 2009, "Del Robins" wrote: > Hi all, > > > > I am new to Python and just recently started using boost python to > create some wrappers for some C++ static libraries. I have been able > to get most everything to work from the documentation and community > examples. Thank you for your efforts. > > > > The one issue that I am still hitting is full cross module support for > boost wrapped modules running on windows. I've looked through the past > discussions What do you mean by "full cross module support?" As far as I know, we do have it, provided you use the DLL version of the Boost.Python library. > Does anyone know the current state for the full cross-module support on > the windows platform? Has this been solved by anyone? Am I still having > problems because of older libraries? You haven't really said what the problem is yet. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From Del_Robins at symantec.com Fri Jan 9 00:34:43 2009 From: Del_Robins at symantec.com (Del Robins) Date: Thu, 8 Jan 2009 16:34:43 -0700 Subject: [C++-sig] Full Cross-Module Support for Boost Python on Windows In-Reply-To: <873aftbmtt.fsf@mcbain.luannocracy.com> References: <873aftbmtt.fsf@mcbain.luannocracy.com> Message-ID: Sorry, the problem is being able to find classes/types wrapped and exported from module A that are used as a parameter/argument type to a function in module B. It all works fine when they are in the same module. You may have hit the nail on the head though, I'm using the static version of the boost libs because of some other conflicts with the runtime dll and another one of our libraries. I will write a test to verify/clarify the situation. Thanks, Del Robins --------------Original thread below this line---------------------------- > Hi all, > > > > I am new to Python and just recently started using boost python to > create some wrappers for some C++ static libraries. I have been able > to get most everything to work from the documentation and community > examples. Thank you for your efforts. > > > > The one issue that I am still hitting is full cross module support for > boost wrapped modules running on windows. I've looked through the past > discussions What do you mean by "full cross module support?" As far as I know, we do have it, provided you use the DLL version of the Boost.Python library. > Does anyone know the current state for the full cross-module support on > the windows platform? Has this been solved by anyone? Am I still having > problems because of older libraries? You haven't really said what the problem is yet. -- Dave Abrahams BoostPro Computing http://www.boostpro.com _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From dave at boostpro.com Fri Jan 9 03:22:40 2009 From: dave at boostpro.com (David Abrahams) Date: Thu, 08 Jan 2009 21:22:40 -0500 Subject: [C++-sig] Full Cross-Module Support for Boost Python on Windows In-Reply-To: (Del Robins's message of "Thu, 8 Jan 2009 16:34:43 -0700") References: <873aftbmtt.fsf@mcbain.luannocracy.com> Message-ID: <87ljtl8bgv.fsf@mcbain.luannocracy.com> on Thu Jan 08 2009, "Del Robins" wrote: > Sorry, the problem is being able to find classes/types wrapped and > exported from module A that are used as a parameter/argument type to a > function in module B. It all works fine when they are in the same > module. > > You may have hit the nail on the head though, I'm using the static > version of the boost libs because of some other conflicts with the > runtime dll and another one of our libraries. I will write a test to > verify/clarify the situation. http://www.boost.org/doc/libs/1_37_0/libs/python/doc/building.html#choosing-a-boost-python-library-binary HTH, -- Dave Abrahams BoostPro Computing http://www.boostpro.com From benkloster at googlemail.com Fri Jan 9 20:04:02 2009 From: benkloster at googlemail.com (Benjamin Kloster) Date: Fri, 9 Jan 2009 20:04:02 +0100 Subject: [C++-sig] Vector of abstract class pointers with boost::python Message-ID: <2adf5e120901091104t3f2d7969s6c4a9258765a6749@mail.gmail.com> Hi everyone, I have the following problem: an abstract base class A that is exported to python and another class, let's call it L, that is little more than a glorified vector of A's with a few convenience functions. The constructor of L is passed a std::vector. Now I would like to export L to Python with the following conditions: 1. The __init__ function of L should take a Python list containing sub classes of A that is automatically converted to a vector of A pointers 2. The L instance takes ownership of the A's (they are deleted in the destructor) Since I have to write a wrapper for L anyway (for virtual functions with default implementation), I tried giving the wrapper class a constructor like so: LWrapper(boost::python::object o) { boost::python::list listOfAs = extract(o); try { bool is_ok = true; while(is_ok) { extract a(listOfAs.pop(0)) if(a.check()) { vectorOfAs.push_back(a()) // vectorOfAs is a protected class member of L } else { is_ok = false; } } } catch(exception &e) { std::cerr << "Oops!"; } } But this crashes with "glibc dected [...] free(): invalid pointer [...]". So. How would I actually go about doing this? Thanks in advance, Benjamin Kloster From ndbecker2 at gmail.com Tue Jan 13 13:15:01 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Tue, 13 Jan 2009 07:15:01 -0500 Subject: [C++-sig] Info on porting to python 3.0 Message-ID: http://wiki.python.org/moin/PortingExtensionModulesToPy3k From beckmann.maik at googlemail.com Tue Jan 13 18:11:17 2009 From: beckmann.maik at googlemail.com (Maik Beckmann) Date: Tue, 13 Jan 2009 18:11:17 +0100 Subject: [C++-sig] fancy indexing Message-ID: <200901131811.17679.Beckmann.Maik@googlemail.com> Hello, Question: Is it possible do implement numpy like fancy indexing with boost.python? Motivation: I'm playing with boost.python (and py++) to generate python bindings for some C++ code. N-dimensional arrays are involved with an api like this {{{ template class Array { ... public: Array(tr1::array const& dims) ... // arity actually done with BOOST_PP reference operator()(size_type d0, ..., size_type dN) { ... } value_type operator()(size_type d0, ..., size_type dN) const { ... } }}} Now I like to do something like numpy is able to dims = (10,20,30) a = Array3(dims) a[0,0,0] = 1.0 print a[0,0,0] Observations: A quick view in numpy/src/core/arrayobject.c gives me the impression that they make an ndarray behave like a dict when its used like a[0,0...]. This for the tp_as_mapping slot in PyTypeObject seems to be set. However, I'm not an expert at this matters. Thanks in advance, -- Maik From evert.rol at gmail.com Wed Jan 14 16:30:27 2009 From: evert.rol at gmail.com (Evert) Date: Wed, 14 Jan 2009 15:30:27 +0000 (UTC) Subject: [C++-sig] installing boost python on osx fails with non-system python Message-ID: Hi all, I'm trying to install boost python on my mac. I initially tried installing through macports, but that failed (see also http://lists.macosforge.org/pipermail/macports-users/2009-January/013226.html) Thinking that it might be something in boost (+python) itself, I tried to manually install boost, using the macports python (version 2.5.4). But that fails as well: the libboost_python-mt.dylib library still has /System/Library/Frameworks/Python.framework/Versions/2.5/Python as dependency (which is also the result when installing through macports, as mentioned in the above link). Using "brute force", I moved /System/Library/Frameworks/Python.framework, so that it cannot be picked up. Unfortunately, the macports version (living in /opt/local/Library/) is still not picked up; as a result, the dynamic library is simply not built with this "hack". I have specified --with-python and even --with-python-root pointing to the macports installation, without any luck. The last thing I could think of was something hardcoded, and the best I could find was in ./tools/build/v2/tools/python.jam, where it somewhere reads local prefix = [ GLOB /System/Library/Frameworks /Library/Frameworks : Python.framework ] ; But changing that to include /opt/local/Library/Frameworks (as a wild guess) didn't work either. Perhaps this issue is more related to the macports Python installation than to boost python (some search results on this results hint in that direction), but at the moment, I'm running out of ideas. If anyone has a suggestion, or familiar situations (or, eg, a positive build result with yet another Python version), that's most welcome. Ah yes, and the system information: mac os 10.5, G5 PPC (64 bit). Hopefully that isn't relevant here though. Evert From gumbeto at gmail.com Thu Jan 15 19:14:31 2009 From: gumbeto at gmail.com (Ricardo Abreu) Date: Thu, 15 Jan 2009 19:14:31 +0100 Subject: [C++-sig] How to get items from a tuple Message-ID: Hello, How do I extract elements from a tuple that I receive from python? boost::python::tuple doesn't seem to have anything for that in its interface... Many thanks, Ricardo Abreu PS: I hope the message is not duplicate. I'm sorry if it is, I am having problems with my client From dave at boostpro.com Thu Jan 15 19:23:22 2009 From: dave at boostpro.com (David Abrahams) Date: Thu, 15 Jan 2009 13:23:22 -0500 Subject: [C++-sig] How to get items from a tuple In-Reply-To: (Ricardo Abreu's message of "Thu, 15 Jan 2009 19:14:31 +0100") References: Message-ID: <87hc408m3p.fsf@mcbain.luannocracy.com> on Thu Jan 15 2009, Ricardo Abreu wrote: > Hello, > > How do I extract elements from a tuple that I receive from python? > boost::python::tuple doesn't seem to have anything for that in its interface... t[n] is the nth element of tuple t. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From timprepscius at gmail.com Thu Jan 15 20:24:58 2009 From: timprepscius at gmail.com (Tim Prepscius) Date: Thu, 15 Jan 2009 14:24:58 -0500 Subject: [C++-sig] built-in submodule creation Message-ID: <6fcb94430901151124l40861795t691d7320f8e477ff@mail.gmail.com> Greetings, I am trying to create built-in sub modules.. I have read everything I can on this subject.. And I've tried many many possible solutions.. And lost many hours actually (blech). Some of the e-mails in these newsgroups from long ago are quite misleading.. Other e-mails on this topic seem to be inaccessible. Much of "piper-mail" is gone it seems, or relocated beyond the reach of google. (didn't know that was still possible!) The only solution that works so far: given that I've created dD_script and dD_object and dD* modules previously.. object dD = object(handle<>(PyImport_AddModule ("dD"))); char *statement = // import what I need, I guess I don't need types actually "import sys, types\n"\ // this is importing all of the modules that I've already created.. "import dD, dD_script, dD_object, dD_types, dD_ui, dD_device, dD_render, dD_internal\n"\ // this is assigning them to the parent "package" "dD.Script = sys.modules['dD.Script'] = dD_script\n"\ "dD.Object = sys.modules['dD.Object'] = dD_object\n"\ "dD.Types = sys.modules['dD.Types'] = dD_types\n"\ "dD.UI = sys.modules['dD.UI'] = dD_ui\n"\ "dD.Device = sys.modules['dD.Device'] = dD_device\n"\ "dD.Render = sys.modules['dD.Render'] = dD_render\n"\ "dD.Internal = sys.modules['dD.Internal'] = dD_internal"\ ; exec(statement, mainDict, mainDict); The non-solutions that don't work: object dD = object(handle<>(PyImport_AddModule ("dD"))); dD.attr("Script") = handle<>(PyImport_ImportModule("dD_script")); dD.attr("Object") = handle<>(PyImport_ImportModule("dD_object")); dD.attr("Types") = handle<>(PyImport_ImportModule("dD_types")); dD.attr("UI") = handle<>(PyImport_ImportModule("dD_ui")); dD.attr("Device") = handle<>(PyImport_ImportModule("dD_device")); dD.attr("Render") = handle<>(PyImport_ImportModule("dD_render")); dD.attr("Internal") = handle<>(PyImport_ImportModule("dD_internal")); I also tried manipulating the dictionary of the dD module instead of attributes above object sys = import("sys"); dict sysDict (sys.attr("__dict__")); sysDict["dD.Script"] = dD["Script"]; sysDict["dD.Object"] = dD["Object"]; sysDict["dD.Types"] = dD["Types"]; sysDict["dD.UI"] = dD["UI"]; sysDict["dD.Device"] = dD["Device"]; sysDict["dD.Render"] = dD["Render"]; sysDict["dD.Internal"] = dD["Internal"]; well if dD is the object these direct [] indexes fail, if it is a dict, the solution still doesn't work dict sysModules (sys.attr("modules")); sysModules["dD.Script"] = dD.attr("Script"); sysModules["dD.Object"] = dD.attr("Object"); sysModules["dD.Types"] = dD.attr("Types"); sysModules["dD.UI"] = dD.attr("UI"); sysModules["dD.Device"] = dD.attr("Device"); sysModules["dD.Render"] = dD.attr("Render"); sysModules["dD.Internal"] = dD.attr("Internal"); if I do dict stuff instead of attribute, still doesn't work.. =================== Is there a better way for me to do this, than executing the python code segment? Does anyone know what is going on behind the scenes in the python code execution? Thanks, -tim -------------- next part -------------- An HTML attachment was scrubbed... URL: From gumbeto at gmail.com Fri Jan 16 11:49:34 2009 From: gumbeto at gmail.com (Ricardo Abreu) Date: Fri, 16 Jan 2009 11:49:34 +0100 Subject: [C++-sig] How to get items from a tuple In-Reply-To: <87hc408m3p.fsf@mcbain.luannocracy.com> References: <87hc408m3p.fsf@mcbain.luannocracy.com> Message-ID: Thank you, but I had tried that and it doesn't seem to work... look: http://codepad.org/kA61P8r0 What am I doing wrong? Thanks, Ricardo Abreu David Abrahams wrote: > on Thu Jan 15 2009, Ricardo Abreu wrote: > >> Hello, >> >> How do I extract elements from a tuple that I receive from python? >> boost::python::tuple doesn't seem to have anything for that in its interface... > > t[n] > > is the nth element of tuple t. > From seefeld at sympatico.ca Fri Jan 16 13:13:25 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 16 Jan 2009 07:13:25 -0500 Subject: [C++-sig] How to get items from a tuple In-Reply-To: References: <87hc408m3p.fsf@mcbain.luannocracy.com> Message-ID: <497079E5.1000707@sympatico.ca> Ricardo Abreu wrote: > Thank you, but I had tried that and it doesn't seem to work... look: > http://codepad.org/kA61P8r0 > > What am I doing wrong? In the sake of understanding the code a little better you may write it a bit less compact. Then you will realize that the item extraction you were asking about is in fact working fine. However, as it returns an object, not an int, you still need to extract the C++ type properly. Just using C casts doesn't work. (Read the docs.) Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From gumbeto at gmail.com Fri Jan 16 14:16:16 2009 From: gumbeto at gmail.com (Ricardo Abreu) Date: Fri, 16 Jan 2009 14:16:16 +0100 Subject: [C++-sig] How to get items from a tuple In-Reply-To: <497079E5.1000707@sympatico.ca> References: <87hc408m3p.fsf@mcbain.luannocracy.com> <497079E5.1000707@sympatico.ca> Message-ID: Well, thank you. The problem was really that I didn't notice I had to extract the int explicitly. About the code being too compact, I'm not sure I got what you mean, unless you would want me to have a variable in between the tuple and the print just to hold the result of the cast, in which case I don't agree you would gain on readability - you would see a cleaner print but you would have to track another variable, and I really don't think that (int)t[0] is that difficult to understand. Anyway, thanks again, Ricardo From seefeld at sympatico.ca Fri Jan 16 15:16:05 2009 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Fri, 16 Jan 2009 09:16:05 -0500 Subject: [C++-sig] How to get items from a tuple In-Reply-To: References: <87hc408m3p.fsf@mcbain.luannocracy.com> <497079E5.1000707@sympatico.ca> Message-ID: <497096A5.2020507@sympatico.ca> Ricardo Abreu wrote: > Well, thank you. The problem was really that I didn't notice I had to > extract the int explicitly. > > About the code being too compact, I'm not sure I got what you mean, > unless you would want me to have a variable in between the tuple and > the print just to hold the result of the cast, in which case I don't > agree you would gain on readability - you would see a cleaner print > but you would have to track another variable, and I really don't think > that (int)t[0] is that difficult to understand. I'm not saying the code itself is too compact. My point was that often there is a lot going on in the evaluation of a single expression. As long as all works fine, that's good, but when something fails it might be a good idea to dissect the statement to see what is actually happening, step by step. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin... From Beckmann.Maik at googlemail.com Fri Jan 16 18:33:52 2009 From: Beckmann.Maik at googlemail.com (Maik Beckmann) Date: Fri, 16 Jan 2009 17:33:52 +0000 (UTC) Subject: [C++-sig] py++ with template member function of template class References: <7465b6170806171221o49847dd1s447b84b64759a169@mail.gmail.com> Message-ID: Roman Yakovenko gmail.com> writes: > > ///////////////////////////////////////////////////////////////////////// > > // test template member function in template class > > // THIS WON'T WORK!!!!!!!!!!!!! > > template > > class BFunc > > { > > public: > > template > > T foo(const T& t) const; > > }; > > > > template template > > T BFunc::foo(const T& t) const { return t + 1; }; > > > > Unfortunately GCC-XML doesn't dump the information. I am also not > aware of any workaround . > > You will have to create the code by yourself. Py++ allows you to > integrate your code easily( > http://language-binding.net/pyplusplus/documentation/inserting_code.html > ) In this mail http://www.mail-archive.com/gccxml at gccxml.org/msg00316.html Brad King, author of gccxml, confirms that this isn't implemented. I hope as gccxml gets more and more popular (i.e. the CERN started using it to get C++ reflection) someone will continue Brads good work. He stated in the mail that he is willing to help get people started. -- Maik From ndbecker2 at gmail.com Fri Jan 16 20:23:20 2009 From: ndbecker2 at gmail.com (Neal Becker) Date: Fri, 16 Jan 2009 14:23:20 -0500 Subject: [C++-sig] can boost::python class imlement buffer protocol? References: <87hc4ad2hv.fsf@mcbain.luannocracy.com> Message-ID: Neal Becker wrote: > David Abrahams wrote: > >> >> on Wed Jan 07 2009, Neal Becker wrote: >> >>> This would require filling in tp_as_buffer field in the PyTypeObject >>> structure for the >>> class. Is this possible? Any clue how/where this could be done? >> >> I honestly don't know the answers to these questions, sorry. >> > > As a workaround, I currently implemented an 'as_buffer' member function. > For example: > > template > inline object as_buffer (ublas::vector & m) { > return object (handle<> (PyBuffer_FromReadWriteMemory > ((void*)(&m.data()[0]), m.size()*sizeof(T)))); > } Actually, this isn't all that useful. The problem is, you want to use with_custodian_and_ward_postcall<0,1> on this. But you can't because buffer object doesn't support weak references. So while the above can provide a buffer interface, it has no idea of lifetime management. I wonder whether python buffer object could be made to support weak refs? Of course, if there was really a way to add a buffer interface to my boost::python object that would be even better. From walter at mathematik.hu-berlin.de Sat Jan 17 12:04:57 2009 From: walter at mathematik.hu-berlin.de (Sebastian Walter) Date: Sat, 17 Jan 2009 12:04:57 +0100 (CET) Subject: [C++-sig] call Python function from C++ without copying the arguments Message-ID: Hello, I want to call a Python function from C++ with an object of a C++ class as argument. The Python-function should change the argument, so changes are visible on the C++ side. Boost::Python automatically makes a copy of all the arguments that are passed to a callable Python object. Is there a way NOT to copy all arguments?? Below sample code: The desired output of $python test.py would be - ----------- desired output --------------- a.x=0 a.x=12345 - ----------- test.cpp --------------- #include #include using namespace std; using namespace boost; using namespace boost::python; class A{ public: int x; A(){x=0;} }; void my_test_function(object fun_obj){ A a; cout<<"a.x="<("A", init<>()) .def_readwrite("x", &A::x); } - ----------- test.py --------------- from _test import * def f(a): a.x = 12345 Thanks in advance, Sebastian Walter -- Sebastian Walter Institut fuer Mathematik, Humboldt-Universitaet Tel: +49 (30) 2093-5869 Rudower Chaussee 25, Adlershof, Berlin Fax: +49 (30) 2093-5859 Post: Unter den Linden 6, D-10099 Berlin Email: walter at mathematik.hu-berlin.de WWW: http://www.mathematik.hu-berlin.de/~walter From dave at boostpro.com Sat Jan 17 13:38:19 2009 From: dave at boostpro.com (David Abrahams) Date: Sat, 17 Jan 2009 07:38:19 -0500 Subject: [C++-sig] call Python function from C++ without copying the arguments In-Reply-To: (Sebastian Walter's message of "Sat, 17 Jan 2009 12:04:57 +0100 (CET)") References: Message-ID: <877i4uf6pw.fsf@mcbain.luannocracy.com> on Sat Jan 17 2009, "Sebastian Walter" wrote: > Hello, > > I want to call a Python function from C++ with an object of a C++ class > as argument. > The Python-function should change the argument, so changes are visible > on the C++ side. > Boost::Python automatically makes a copy of all the arguments that are > passed to a callable Python object. > > Is there a way NOT to copy all arguments?? > > void my_test_function(object fun_obj){ > A a; > cout<<"a.x="< fun_obj(a); fun_obj(boost::ref(a)) ^^^^^^^^^^^ ^ > cout<<"a.x="< } HTH, -- Dave Abrahams BoostPro Computing http://www.boostpro.com From pyplusplus at assumetheposition.nl Sat Jan 17 16:11:05 2009 From: pyplusplus at assumetheposition.nl (Paul Melis) Date: Sat, 17 Jan 2009 16:11:05 +0100 Subject: [C++-sig] Custom smart pointer and bp::wrapper Message-ID: <4971F509.5080309@assumetheposition.nl> Given two reference-counted classes Thing and Callback, and using a custom smart pointer ref_ptr, with all the needed pointee stuff added and using ref_ptr<..> as held type I'm getting into trouble when trying to use a boost::python::wrapper<> derived class. See the attached files for the complete code. Summarized, I have: class Referenced { /* methods ref() and unref() */ } class Thing : public Referenced { ...} class Callback : public Referenced { ...} template class ref_ptr { ... } You can set a Callback instance on Thing, and the Callback's execute() method gets called at some point in time. The Callback class is to be implemented in Python (although it has a default empty implementation for execute()), so I defined: struct CallbackWrap : Callback, bp::wrapper { void execute(Thing* t) { if (bp::override ovr = this->get_override("execute")) ovr(t); else Callback::execute(t); } void default_execute(Thing* t) { this->Callback::execute(t); } }; The whole shebang is packaged up in a BP module: BOOST_PYTHON_MODULE(doh) { bp::class_, boost::noncopyable>("Thing") .def("update", &Thing::update) .def("setCallback", &Thing::setCallback) ; bp::class_, boost::noncopyable>("Callback") // Don't add bp::no_init, as we won't be able to derive a class from C in that case .def("execute", &Callback::execute, &CallbackWrap::default_execute) ; } But when trying to run import doh class MyCallback(doh.Callback): def execute(self, t): print t return 1 m = MyCallback() t = doh.Thing() t.setCallback(m) t.update() i'm getting Traceback (most recent call last): File "no_to_python_converter.py", line 12, in t.update() TypeError: No to_python (by-value) converter found for C++ type: Thing". And I fail to see why... I'm also not sure if the message really means that there is an instance of Thing that needs to be converted to its Python equivalent, or if there is a _pointer_ to a Thing instance that needs to be converted to Python. If the former, then I'm surprised these instances even show up, as the code being wrapped doesn't pass a Thing anywhere, only a Thing*. I've also tagged Thing as noncopyable, so I wouldn't expect Thing instances to be passed around internally by BP by value. It kind of feels like BP knows how to convert an ref_ptr to a Python object, but not how to convert a Thing. Any help is appreciated, Regards, Paul -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: no_to_python_converter.cpp URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: no_to_python_converter.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: ref_ptr.h URL: From roman.yakovenko at gmail.com Sat Jan 17 20:15:12 2009 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Sat, 17 Jan 2009 21:15:12 +0200 Subject: [C++-sig] Custom smart pointer and bp::wrapper In-Reply-To: <4971F509.5080309@assumetheposition.nl> References: <4971F509.5080309@assumetheposition.nl> Message-ID: <7465b6170901171115ja063ec2l27daf4d7b137328a@mail.gmail.com> 2009/1/17 Paul Melis : > But when trying to run > > import doh > > class MyCallback(doh.Callback): > def execute(self, t): > print t > return 1 > > m = MyCallback() > > t = doh.Thing() > t.setCallback(m) > t.update() > > i'm getting > > Traceback (most recent call last): > File "no_to_python_converter.py", line 12, in > t.update() > TypeError: No to_python (by-value) converter found for C++ type: Thing". > > And I fail to see why... Mu guess: you need to use boost::python::register_ptr_to_python< Thin >(); ( http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/register_ptr_to_python.html ) I have more or less complete example, which exports custom pointer class and its usage to Python: http://www.language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From pyplusplus at assumetheposition.nl Sun Jan 18 11:58:46 2009 From: pyplusplus at assumetheposition.nl (Paul Melis) Date: Sun, 18 Jan 2009 11:58:46 +0100 Subject: [C++-sig] Custom smart pointer and bp::wrapper In-Reply-To: <7465b6170901171115ja063ec2l27daf4d7b137328a@mail.gmail.com> References: <4971F509.5080309@assumetheposition.nl> <7465b6170901171115ja063ec2l27daf4d7b137328a@mail.gmail.com> Message-ID: <49730B66.2010303@assumetheposition.nl> Roman Yakovenko wrote: > 2009/1/17 Paul Melis : > >> But when trying to run >> >> import doh >> >> class MyCallback(doh.Callback): >> def execute(self, t): >> print t >> return 1 >> >> m = MyCallback() >> >> t = doh.Thing() >> t.setCallback(m) >> t.update() >> >> i'm getting >> >> Traceback (most recent call last): >> File "no_to_python_converter.py", line 12, in >> t.update() >> TypeError: No to_python (by-value) converter found for C++ type: Thing". >> >> And I fail to see why... >> > > Mu guess: you need to use boost::python::register_ptr_to_python< Thin >(); > ( http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/register_ptr_to_python.html > ) > Well, that only seems to work on smart pointers, i.e. ref_ptr. >From the docs: "|| supplies |register_ptr_to_python|, a function template which registers a conversion for smart pointers to Python" > I have more or less complete example, which exports custom pointer > class and its usage to Python: > http://www.language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html > Right, I did find it while trying to solve the above. But where our code differs is that my wrapper class (CallbackWrap) needs to pass a Thing* to Python and that's where it goes wrong. Your wrapper only returns an integer and doesn't pass any parameters to Python. I think that is the crucial difference here. Regards, Paul From walter at mathematik.hu-berlin.de Mon Jan 19 13:16:10 2009 From: walter at mathematik.hu-berlin.de (Sebastian Walter) Date: Mon, 19 Jan 2009 13:16:10 +0100 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? Message-ID: <49746F0A.40402@mathematik.hu-berlin.de> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, On the C++ side I have a raw pointer: double *x; I'd like to change its values from within Python by using Numpy. Example code can be found below. My approach is 1) make PyArray from existing data *x with PyArray_SimpleNewFromData 2) create a boost array from the PyArray. My question is: How can I create a boost::python::numeric::array from a Numpy Pyarray without copying data? Here my best shot... (compiles and runs but copies the data) - ----------------- test.cpp ------------------ #define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle #include #include #include using namespace std; using namespace boost; using namespace boost::python; using namespace boost::python::numeric; array test_function(){ int N = 3; double x[] = {1.,2.,3.}; array numpy_x(static_cast(handle<>(PyArray_SimpleNewFromData(1, &N, PyArray_DOUBLE, x)))); double *y = static_cast PyArray_DATA(numpy_x.ptr()); y[0] = 37.; for(int n = 0; n Hello, Suppose that I have a class A with a member of type std::pair, called a. If I want to expose class A to python while allowing read/write access to the object pointed to by a.first, what to I put in def_readwrite? I tried something like class("A").def_readwrite("a", &A::a::first); but it doesn't work. Also been trying with boost::bind but didn't have any luck :s I know I could do getters and setters, but there would be a lot of them because my "A" has a lot of members like "a". Thank you in advance, Ricardo From lists_ravi at lavabit.com Mon Jan 19 16:21:02 2009 From: lists_ravi at lavabit.com (Ravi) Date: Mon, 19 Jan 2009 10:21:02 -0500 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? In-Reply-To: <49746F0A.40402@mathematik.hu-berlin.de> References: <49746F0A.40402@mathematik.hu-berlin.de> Message-ID: <200901191021.02977.lists_ravi@lavabit.com> On Monday 19 January 2009 07:16:10 Sebastian Walter wrote: > My question is: > How can I create a > boost::python::numeric::array from a Numpy Pyarray without copying data? See http://mail.python.org/pipermail/cplusplus-sig/2008-October/013825.html for a full solution. In particular, see the following class: template struct ublas_vector_from_numpy Regards, Ravi From rwgk at yahoo.com Tue Jan 20 00:13:28 2009 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 19 Jan 2009 15:13:28 -0800 (PST) Subject: [C++-sig] Exposing members of members References: Message-ID: <518385.38055.qm@web111409.mail.gq1.yahoo.com> I'd define to/from Python conversions for the std::pair, mapping to Python tuples, then use .add_property(). Alternatively, wrap std::pair the normal way via class_, then you can use .def_readwrite(). You need to handle each std::pair combination of A,B separately, like any other template class you want to wrap or convert to/from a Python object. See also: http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/faq.html#custom_string This could be a starting point for std::pair conversions. Probably you want to make it into a converter template if you have more than one combination of A,B. Here is code we use for the to_python conversions: http://cci.lbl.gov/cctbx_sources/boost_adaptbx/ std_pair_conversion.h std_pair_ext.cpp We don't have corresponding from_python conversions. If you add them, let me know! The next item in the FAQ is also relevant. ----- Original Message ---- From: Ricardo Abreu To: cplusplus-sig at python.org Sent: Monday, January 19, 2009 7:27:46 AM Subject: [C++-sig] Exposing members of members Hello, Suppose that I have a class A with a member of type std::pair, called a. If I want to expose class A to python while allowing read/write access to the object pointed to by a.first, what to I put in def_readwrite? I tried something like class("A").def_readwrite("a", &A::a::first); but it doesn't work. Also been trying with boost::bind but didn't have any luck :s I know I could do getters and setters, but there would be a lot of them because my "A" has a lot of members like "a". Thank you in advance, Ricardo From walter at mathematik.hu-berlin.de Tue Jan 20 10:27:02 2009 From: walter at mathematik.hu-berlin.de (Sebastian Walter) Date: Tue, 20 Jan 2009 10:27:02 +0100 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? In-Reply-To: <200901191021.02977.lists_ravi@lavabit.com> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901191021.02977.lists_ravi@lavabit.com> Message-ID: <497598E6.3060005@mathematik.hu-berlin.de> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hey, Thanks for the hint :), Your code is a little to heavyweight for my purposes, but I learned quite a lot how boost::python converters work. And it also gave me the idea that finally solved my problem! I don't have to convert the PyArray to boost::python::numeric::array since the indirection over the boost array is presumably also implemented as a converter (which basically undoes my wrapping, i guess). So I figured, if that is the case, it should be possible to return the PyArray directly. And it does. At least my test example works as wished: - -------------------- test.cpp ---------------------------- #define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle #include #include #include using namespace std; using namespace boost; using namespace boost::python; using namespace boost::python::numeric; // global to guarantee that the data is not freed after test_function_that_doesnt_copy returns double x[] = {1.,2.,3.}; // this function copies PyArray when the boost array constructor is called array test_function_that_copies(){ int N = 3; double x[] = {1.,2.,3.}; array numpy_x(static_cast(handle<>(PyArray_SimpleNewFromData(1, &N, PyArray_DOUBLE, x)))); double *y = static_cast PyArray_DATA(numpy_x.ptr()); y[0] = 37.; for(int n = 0; n On Monday 19 January 2009 07:16:10 Sebastian Walter wrote: >> My question is: >> How can I create a >> boost::python::numeric::array from a Numpy Pyarray without copying data? > > See http://mail.python.org/pipermail/cplusplus-sig/2008-October/013825.html > for a full solution. In particular, see the following class: > > template struct ublas_vector_from_numpy > > Regards, > Ravi > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.4-svn0 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFJdZjm9PBA5IG0h0ARAnEPAJ9yWG1AUu94qMGij3w9wvwboXYpEACeKA0N fDFO2ev8dvaBmjbyQdvlSvk= =SWB+ -----END PGP SIGNATURE----- From gumbeto at gmail.com Tue Jan 20 11:30:30 2009 From: gumbeto at gmail.com (Ricardo Abreu) Date: Tue, 20 Jan 2009 11:30:30 +0100 Subject: [C++-sig] Exposing members of members In-Reply-To: <518385.38055.qm@web111409.mail.gq1.yahoo.com> References: <518385.38055.qm@web111409.mail.gq1.yahoo.com> Message-ID: Hello, Thank you very much for your answer, but what I really wanted was to expose to Python just the first element of the pair and not the whole pair, so that the fact that it is a pair in c++ would be transparent to python. For instance, if I had struct A{std::pair a;};, I just wanted to expose a.first, as a simple int... Any ideas for this? Thanks, Ricardo Ralf W. Grosse-Kunstleve wrote: > I'd define to/from Python conversions for the std::pair, mapping to > Python tuples, then use .add_property(). > > Alternatively, wrap std::pair the normal way via class_, then you > can use .def_readwrite(). > > You need to handle each std::pair combination of A,B separately, > like any other template class you want to wrap or convert to/from a > Python object. > > See also: > > http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/faq.html#custom_string > > This could be a starting point for std::pair conversions. Probably you want > to make it into a converter template if you have more than one combination of A,B. > Here is code we use for the to_python conversions: > > http://cci.lbl.gov/cctbx_sources/boost_adaptbx/ > std_pair_conversion.h > std_pair_ext.cpp > > We don't have corresponding from_python conversions. > If you add them, let me know! > > The next item in the FAQ is also relevant. > > > > ----- Original Message ---- > From: Ricardo Abreu > To: cplusplus-sig at python.org > Sent: Monday, January 19, 2009 7:27:46 AM > Subject: [C++-sig] Exposing members of members > > Hello, > > Suppose that I have a class A with a member of type std::pair, called a. If I want to expose class A to python while allowing read/write access to the object pointed to by a.first, what to I put in def_readwrite? > > I tried something like class("A").def_readwrite("a", &A::a::first); but it doesn't work. Also been trying with boost::bind but didn't have any luck :s > > I know I could do getters and setters, but there would be a lot of them because my "A" has a lot of members like "a". > > Thank you in advance, > Ricardo From lists_ravi at lavabit.com Tue Jan 20 15:11:00 2009 From: lists_ravi at lavabit.com (Ravi) Date: Tue, 20 Jan 2009 09:11:00 -0500 Subject: [C++-sig] =?iso-8859-1?q?create_boost_array_from_a_Numpy_PyArray_?= =?iso-8859-1?q?without=09copying_data=3F?= In-Reply-To: <497598E6.3060005@mathematik.hu-berlin.de> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901191021.02977.lists_ravi@lavabit.com> <497598E6.3060005@mathematik.hu-berlin.de> Message-ID: <200901200911.01286.lists_ravi@lavabit.com> On Tuesday 20 January 2009 04:27:02 Sebastian Walter wrote: > Thanks for the hint :), > Your code is a little to heavyweight for my purposes, > but I learned quite a lot how boost::python converters work. > > And it also gave me the idea that finally solved my problem! Good. > // global to guarantee that the data is not freed after > test_function_that_doesnt_copy returns > double x[] = {1.,2.,3.}; In this case, you run into the problem of lifetime management. Unless you use globals, how do you guarantee that once all users of the array are done with it, the array is deleted? The array must be deleted from either the python side (via numpy) or from the C++ side (by yourself). Handling this problem is the cause for much of my code (which may still be too heavyweight for you). Regards, Ravi From rwgk at yahoo.com Tue Jan 20 15:30:02 2009 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Tue, 20 Jan 2009 06:30:02 -0800 (PST) Subject: [C++-sig] Exposing members of members References: <518385.38055.qm@web111409.mail.gq1.yahoo.com> Message-ID: <110391.46405.qm@web111409.mail.gq1.yahoo.com> > Thank you very much for your answer, but what I really wanted was to> expose to Python just the first element of the pair and not the whole> pair, so that the fact that it is a pair in c++ would be transparent> to python. For instance, if I had struct A{std::pair a;};, > I just wanted to expose a.first, as a simple int... Do you want a_obj = A() a_obj.a.first to give you the int in Python or a_obj.a ? From roman.yakovenko at gmail.com Tue Jan 20 20:16:02 2009 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 20 Jan 2009 21:16:02 +0200 Subject: [C++-sig] [ANN] Py++ based ctypes code generator In-Reply-To: <7465b6170901201115j7170df39m8a4f022788f6a028@mail.gmail.com> References: <7465b6170901201110m15271905t8a393c200bca357b@mail.gmail.com> <7465b6170901201115j7170df39m8a4f022788f6a028@mail.gmail.com> Message-ID: <7465b6170901201116u15dd6fefs596d7d3eafb190e0@mail.gmail.com> Hello. I would like to announce a new code generator for ctypes package. The new code generator is integrated with Py++ package and reuse most of its infrastructure: * GCC-XML - generates an XML description of a C++ program from GCC's internal representation * pygccxml - powerful and simple Python package to work GCCXML generated file. * Py++ design, code and utilities * It was build with future ( ctypes-cpp ) functionality in mind. * It has cross-platform ( Windows XP and Linux Ubuntu 8.10 ) unit tests How it works: 1. Using pygccxml, the source files are parsed ( using pygccxml ) and in-memory description of the code is created 2. .dll, .map or .so file is parsed ( pygccxml ) and exported symbols list is created 3. Using decorated and undecorated names, mapping between declarations and exported symbols is created. 4. The exported symbols and the mapping are used to: * define for what declarations the code should be generated * find out function calling convention 5. The last step - the code is generated Supported functionality: * shared libraries, that were compiled using C or C++ compiler * free functions, including variable number of arguments * [global|member] [named|anonymous] variables * [named|anonymous|opaque|nested] structures * enumerations * typedefs Examples: Is it useful? - Yes. I created example, which generates code for GMP( http://gmplib.org/ ) library. The following link (http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/examples/gmplib_dev/) will take you to the example. * dev - directory, which contains code generator source code * pygmplib - directory, which contains generated code - Python package, which provides access to GMP lib functionality * test.py - Python script - tester. Want to try? Windows users: http://sourceforge.net/project/showfiles.php?group_id=118209 contains few packages * ctypes code generator - stand alone executable, which generates code for a .dll file * gccxml-setup - gccxml setup file Linux users: Sorry, no binaries :-( * you need to check-out( http://gccxml.org/HTML/Download.html) and build gccxml ( http://gccxml.org/HTML/Install.html ) * you need to check-out Py++ and pygccxml (http://sourceforge.net/svn/?group_id=118209 ) * wrap_library.py (http://pygccxml.svn.sourceforge.net/viewvc/pygccxml/pyplusplus_dev/scripts/) is the script, which generates code for .so file. Your feedback is welcome. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From dave at boostpro.com Tue Jan 20 20:57:39 2009 From: dave at boostpro.com (David Abrahams) Date: Tue, 20 Jan 2009 14:57:39 -0500 Subject: [C++-sig] Exposing members of members In-Reply-To: (Ricardo Abreu's message of "Tue, 20 Jan 2009 11:30:30 +0100") References: <518385.38055.qm@web111409.mail.gq1.yahoo.com> Message-ID: <87sknd92do.fsf@mcbain.luannocracy.com> on Tue Jan 20 2009, Ricardo Abreu wrote: > Thank you very much for your answer, but what I really wanted was to expose to Python > just the first element of the pair and not the whole pair, so that the fact that it is > a pair in c++ would be transparent to python. Looks like you want to use class_<...> .add_property( ... ) http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/class.html#class_-spec-modifiers -- Dave Abrahams BoostPro Computing http://www.boostpro.com From walter at mathematik.hu-berlin.de Wed Jan 21 09:40:00 2009 From: walter at mathematik.hu-berlin.de (Sebastian Walter) Date: Wed, 21 Jan 2009 09:40:00 +0100 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? In-Reply-To: <200901200911.01286.lists_ravi@lavabit.com> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901191021.02977.lists_ravi@lavabit.com> <497598E6.3060005@mathematik.hu-berlin.de> <200901200911.01286.lists_ravi@lavabit.com> Message-ID: <4976DF60.7010206@mathematik.hu-berlin.de> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Ravi schrieb: > On Tuesday 20 January 2009 04:27:02 Sebastian Walter wrote: >> Thanks for the hint :), >> Your code is a little to heavyweight for my purposes, >> but I learned quite a lot how boost::python converters work. >> >> And it also gave me the idea that finally solved my problem! > > Good. > >> // global to guarantee that the data is not freed after >> test_function_that_doesnt_copy returns >> double x[] = {1.,2.,3.}; > > In this case, you run into the problem of lifetime management. Unless you use > globals, how do you guarantee that once all users of the array are done with > it, the array is deleted? The array must be deleted from either the python > side (via numpy) or from the C++ side (by yourself). Handling this problem is > the cause for much of my code (which may still be too heavyweight for you). > Yes, doing it as I posted above caused a memory corruption. Apparently Python frees the memory of the Numpy array when it's not needed anymore in Python. Apparently I have to increase the refcount manually when I return a raw PyObject. So I wrote getter functions like that: PyObject* get_rhs(){ return incref((PyObject *) _array_my_rhs); } I'm not sure that this is the way it should be done. But it solved the memory corruption problem. With heavyweight I meant that I don't want to use ublas nor do I want to write a converter. I simply don't have the time to check out the inner workings of boost::python. And writing a converter requires a fair amount of template meta programming + knowledge of boost::python. What I'm trying to accomplish is the following: I have a DAE-solver which is implemented in C++. A DAE is basically an ODE + algebraic equations: \dot xd = f(t,xd,xa) 0 = g(t,xd,xa) where xd/xa are the differential resp. algebraic state variables. In C++ I give the integrator a function pointer to a function: void f(double *t, double *x, double *rhs); The integrator is responsible for the memory management of *t, *x, *rhs and just calls the function f. The function f should then convert the raw pointers to numpy arrays and then call the implementation of f which is in Python. void f(double *t, double *x, double *rhs){ 1) create PyArray with PyArray_FromDimsAndData(1, &Nx, PyArray_DOUBLE, (char*) t); 2) then call a callable python object with those PyArrays as argument. } > Regards, > Ravi > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.4-svn0 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFJdt9g9PBA5IG0h0ARAlY4AJ0T0M2ljLLjegLYikBeRuDeaJelUgCdEF4q sORViEHb8BmzhNSyhdmNWZA= =eSRA -----END PGP SIGNATURE----- From lists_ravi at lavabit.com Wed Jan 21 15:33:32 2009 From: lists_ravi at lavabit.com (Ravi) Date: Wed, 21 Jan 2009 09:33:32 -0500 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? In-Reply-To: <4976DF60.7010206@mathematik.hu-berlin.de> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901200911.01286.lists_ravi@lavabit.com> <4976DF60.7010206@mathematik.hu-berlin.de> Message-ID: <200901210933.33071.lists_ravi@lavabit.com> On Wednesday 21 January 2009 03:40:00 Sebastian Walter wrote: > Apparently I have to increase the refcount manually when I return a raw > PyObject. So I wrote getter functions like that: > > ????????PyObject* get_rhs(){ > ????????????????return incref((PyObject *) _array_my_rhs); > ????????} > I'm not sure that this is the way it should be done. But it solved the > memory corruption problem. In this case, you are essentially creating a memory leak. The returned array will never be deleted by python since its reference count will never go to zero. Please see http://blog.enthought.com/?p=62 for information on fixing this issue. The code I pointed out earlier encapsulates this using a custom deleter in C++ which can be used independently of ublas. Regards, Ravi From walter at mathematik.hu-berlin.de Thu Jan 22 11:51:20 2009 From: walter at mathematik.hu-berlin.de (Sebastian Walter) Date: Thu, 22 Jan 2009 11:51:20 +0100 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? In-Reply-To: <200901210933.33071.lists_ravi@lavabit.com> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901200911.01286.lists_ravi@lavabit.com> <4976DF60.7010206@mathematik.hu-berlin.de> <200901210933.33071.lists_ravi@lavabit.com> Message-ID: <49784FA8.5090100@mathematik.hu-berlin.de> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Ravi schrieb: > On Wednesday 21 January 2009 03:40:00 Sebastian Walter wrote: >> Apparently I have to increase the refcount manually when I return a raw >> PyObject. So I wrote getter functions like that: >> >> PyObject* get_rhs(){ >> return incref((PyObject *) _array_my_rhs); >> } >> I'm not sure that this is the way it should be done. But it solved the >> memory corruption problem. > > In this case, you are essentially creating a memory leak. The returned array > will never be deleted by python since its reference count will never go to > zero. I'm sorry, the above code is not really self contained and now I'm not sure that we are talking about the same thing. I do not intend to EVER free the memory of *x!! So I don't have to worry about ?make sure the memory doesn?t get deallocated before the NumPy array disappears.? because *x is global! So here is the code that I actually wanted to post: - ------------------ test.cpp ------------------- #define PY_ARRAY_UNIQUE_SYMBOL PyArrayHandle #include #include #include using namespace std; using namespace boost; using namespace boost::python; using namespace boost::python::numeric; double x[] = {1.,2.,3.}; int N = 3; class A{ public: PyArrayObject *my_array; A(){ my_array = (PyArrayObject*)PyArray_SimpleNewFromData(1, &N, PyArray_DOUBLE, x); } PyObject* get_my_array(){ return incref((PyObject*) my_array); } }; BOOST_PYTHON_MODULE(_test) { import_array(); array::set_module_and_type("numpy", "ndarray"); class_("A", init<>()) .add_property("my_array", &A::get_my_array) ; } - --------------- end test.cpp --------------------- - --------------- test.py ------------------ from _test import * def test_my_array(): a = A() print a.my_array print a.my_array - ---------------- end test.py ---------------- If I don't put the incref I get an *** glibc detected *** python: malloc(): memory corruption (fast): 0x0817b6a1 *** > Please see http://blog.enthought.com/?p=62 for information on fixing > this issue. The code I pointed out earlier encapsulates this using a custom > deleter in C++ which can be used independently of ublas. I cite from that blog: """The suggestions have always been something similar to ?make sure the memory doesn?t get deallocated before the NumPy array disappears.? This is nice advice, but not generally helpful as it basically just tells you to create a memory leak. All that NumPy does internally is to un-set a flag on the array object indicating that the array doesn?t own its memory pointer and so NumPy won?t free the memory when the last reference to the array disappears.""" So that means: In my above case I know that *x won't be deleted because it is global, right? Why exactly does the *** glibc detected *** python: malloc(): memory corruption (fast): 0x0817b6a1 *** occur then? And is there a way to get the refcount of an object within Python? I'd like to test if the refcount is correct. regards, Sebastian -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.4-svn0 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFJeE+o9PBA5IG0h0ARAjeQAKCR+HQQ2XjiqLAO01ZwWRqIC5KUcgCgl+ne b80vmGWmi6jLLuxeOyjOt0w= =mmAr -----END PGP SIGNATURE----- From lists_ravi at lavabit.com Thu Jan 22 16:27:25 2009 From: lists_ravi at lavabit.com (Ravi) Date: Thu, 22 Jan 2009 10:27:25 -0500 Subject: [C++-sig] =?utf-8?q?create_boost_array_from_a_Numpy_PyArray_witho?= =?utf-8?q?ut=09copying_data=3F?= In-Reply-To: <49784FA8.5090100@mathematik.hu-berlin.de> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901210933.33071.lists_ravi@lavabit.com> <49784FA8.5090100@mathematik.hu-berlin.de> Message-ID: <200901221027.26653.lists_ravi@lavabit.com> On Thursday 22 January 2009 05:51:20 Sebastian Walter wrote: > In my above case I know that *x won't be deleted because it is global, > right? *x is not deleted since you used PyArray_SimpleNewFromData; that *x is a global is irrelevant here. > Why exactly does the > *** glibc detected *** python: malloc(): memory corruption (fast): > 0x0817b6a1 *** > occur then? In the code you posted this time, incref() is required so that someone owns a reference to the array object. What I don't know off the top of my head is whether you need an incref in the constructor of A and a decref in the destructor of A. > And is there a way to get the refcount of an object within Python? I'd > like to test if the refcount is correct. sys.getrefcount(obj) Regards, Ravi From walter at mathematik.hu-berlin.de Fri Jan 23 10:44:43 2009 From: walter at mathematik.hu-berlin.de (Sebastian Walter) Date: Fri, 23 Jan 2009 10:44:43 +0100 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? In-Reply-To: <200901221027.26653.lists_ravi@lavabit.com> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901210933.33071.lists_ravi@lavabit.com> <49784FA8.5090100@mathematik.hu-berlin.de> <200901221027.26653.lists_ravi@lavabit.com> Message-ID: <4979918B.8030101@mathematik.hu-berlin.de> -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hey, thanks for your replies: they helped a lot. Ravi schrieb: > On Thursday 22 January 2009 05:51:20 Sebastian Walter wrote: >> In my above case I know that *x won't be deleted because it is global, >> right? > > *x is not deleted since you used PyArray_SimpleNewFromData; that *x is a > global is irrelevant here. > >> Why exactly does the >> *** glibc detected *** python: malloc(): memory corruption (fast): >> 0x0817b6a1 *** >> occur then? > > In the code you posted this time, incref() is required so that someone owns a > reference to the array object. What I don't know off the top of my head is > whether you need an incref in the constructor of A and a decref in the > destructor of A. This is also something I wondered about. I guess it's better incref/decref them to be on the save side, right? > >> And is there a way to get the refcount of an object within Python? I'd >> like to test if the refcount is correct. > > sys.getrefcount(obj) Good to know. When I run - ------------------- test.py ---------------- from _test import * import sys def test_my_array(): a = A() print 'a.my_array=',a.my_array print 'sys.getrefcount(a)=',sys.getrefcount(a) print 'sys.getrefcount(a.my_array)=',sys.getrefcount(a.my_array) if __name__ == "__main__": test_my_array() - ------------ end test.py ------------- I get the output - ----------- output ------------- walter at wronski$ python test.py a.my_array= [ 1. 2. 3.] sys.getrefcount(a)= 2 sys.getrefcount(a.my_array)= 2 - ----------- end output -------- Ermm, is that good? I expected that the refcount would be 1 and not 2. > > Regards, > Ravi > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.4-svn0 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFJeZGL9PBA5IG0h0ARAg74AJ9yVUaruKo5XmsuDoCcmnyApZCUcACfdIig R2c/WdZ08nayG/hWDkiDF8k= =GY7d -----END PGP SIGNATURE----- From lists_ravi at lavabit.com Fri Jan 23 16:36:55 2009 From: lists_ravi at lavabit.com (Ravi) Date: Fri, 23 Jan 2009 10:36:55 -0500 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? In-Reply-To: <4979918B.8030101@mathematik.hu-berlin.de> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901221027.26653.lists_ravi@lavabit.com> <4979918B.8030101@mathematik.hu-berlin.de> Message-ID: <200901231036.55414.lists_ravi@lavabit.com> On Friday 23 January 2009 04:44:43 Sebastian Walter wrote: > > In the code you posted this time, incref() is required so that someone > > owns a reference to the array object. What I don't know off the top of my > > head is whether you need an incref in the constructor of A and a decref > > in the destructor of A. > > This is also something I wondered about. > I guess it's better incref/decref them to be on the save side, right? IIRC, PyArray_SimpleNewFromData gives you a new reference. If that is correct, you will need to decref() it in the destructor of A so that python can clean up the numpy array (without touching your global data). What is the value of the flag NPY_OWNDATA after the call to PyArray_SimpleNewFromData? That should tell you whether python will attempt to free your global data structure. > ------------------- test.py ---------------- > from _test import * > import sys > > def test_my_array(): > ????????a = A() > ????????print 'a.my_array=',a.my_array > ????????print 'sys.getrefcount(a)=',sys.getrefcount(a) > ????????print 'sys.getrefcount(a.my_array)=',sys.getrefcount(a.my_array) > > > if __name__ == "__main__": > ????????test_my_array() > > ------------ end test.py ------------- > I get the output > > ----------- output ------------- > > walter at wronski$ python test.py > a.my_array= [ 1. ?2. ?3.] > sys.getrefcount(a)= 2 > sys.getrefcount(a.my_array)= 2 > > ----------- end output -------- > > Ermm, is that good? > I expected that the refcount would be 1 and not 2. I don't know. I generally avoid these issues by writing custom converters and letting boost.python handle reference counting. Perhaps someone else on the list might be able to help. Regards, Ravi From renatox at gmail.com Fri Jan 23 17:24:05 2009 From: renatox at gmail.com (Renato Araujo) Date: Fri, 23 Jan 2009 13:24:05 -0300 Subject: [C++-sig] bindings size In-Reply-To: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> Message-ID: <95291a80901230824i3e9a3c96m1140fa48b07cb362@mail.gmail.com> hi all, After some vacation I got back to my library binding. Most problems have been solved but now I got another big problem here. My current binding library is very huge compared to the wrapped C++ library (about four times). Checking the code I found that the components responsible for a great deal of the size are "class_" instantiation and ".def" functions. Without these functions my code size is about 300k and with these functions my code grows to enormous 4MB. I would like to know if there is a way to minimize the library size, some other function which I can call instead of ".def" functions, to minimize template usage, or some template call that I can transform into a function call. Anyway if there is work in progress to minimize the size of boost-python bindings I am very eager to help. BR -- Renato Araujo Oliveira Filho From dave at boostpro.com Fri Jan 23 17:31:09 2009 From: dave at boostpro.com (David Abrahams) Date: Fri, 23 Jan 2009 11:31:09 -0500 Subject: [C++-sig] bindings size In-Reply-To: <95291a80901230824i3e9a3c96m1140fa48b07cb362@mail.gmail.com> (Renato Araujo's message of "Fri, 23 Jan 2009 13:24:05 -0300") References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> <95291a80901230824i3e9a3c96m1140fa48b07cb362@mail.gmail.com> Message-ID: <87eiyuvvaq.fsf@mcbain.luannocracy.com> on Fri Jan 23 2009, Renato Araujo wrote: > hi all, > After some vacation I got back to my library binding. Most problems > have been solved > but now I got another big problem here. My current binding library is very huge > compared to the wrapped C++ library (about four times). Checking the > code I found > that the components responsible for a great deal of the size are > "class_" instantiation > and ".def" functions. Without these functions my code size is about > 300k and with > these functions my code grows to enormous 4MB. > > I would like to know if there is a way to minimize the library size, some other > function which I can call instead of ".def" functions, to minimize > template usage, > or some template call that I can transform into a function call. The usual C++ tricks apply: turn on optimizations and inlining (if using bjam add "release" to the command line), and strip symbols from the result if on *nix. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From renatox at gmail.com Fri Jan 23 17:44:35 2009 From: renatox at gmail.com (Renato Araujo) Date: Fri, 23 Jan 2009 13:44:35 -0300 Subject: [C++-sig] bindings size In-Reply-To: <87eiyuvvaq.fsf@mcbain.luannocracy.com> References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> <95291a80901230824i3e9a3c96m1140fa48b07cb362@mail.gmail.com> <87eiyuvvaq.fsf@mcbain.luannocracy.com> Message-ID: <95291a80901230844u263614y5697d098abb0bdd@mail.gmail.com> I'm current using "CXXFLAGS=-ffunction-sections -Os" and "LDFLAGS --gc-sections", and the library already striped, without strip the size is about 12MB. On Fri, Jan 23, 2009 at 1:31 PM, David Abrahams wrote: > > on Fri Jan 23 2009, Renato Araujo wrote: > >> hi all, >> After some vacation I got back to my library binding. Most problems >> have been solved >> but now I got another big problem here. My current binding library is very huge >> compared to the wrapped C++ library (about four times). Checking the >> code I found >> that the components responsible for a great deal of the size are >> "class_" instantiation >> and ".def" functions. Without these functions my code size is about >> 300k and with >> these functions my code grows to enormous 4MB. >> >> I would like to know if there is a way to minimize the library size, some other >> function which I can call instead of ".def" functions, to minimize >> template usage, >> or some template call that I can transform into a function call. > > The usual C++ tricks apply: turn on optimizations and inlining (if using > bjam add "release" to the command line), and strip symbols from the > result if on *nix. > > -- > Dave Abrahams > BoostPro Computing > http://www.boostpro.com > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Renato Araujo Oliveira Filho From AFoglia at princeton.com Fri Jan 23 18:03:38 2009 From: AFoglia at princeton.com (AFoglia at princeton.com) Date: Fri, 23 Jan 2009 12:03:38 -0500 Subject: [C++-sig] create boost array from a Numpy PyArray without copying data? In-Reply-To: <4979918B.8030101@mathematik.hu-berlin.de> References: <49746F0A.40402@mathematik.hu-berlin.de> <200901210933.33071.lists_ravi@lavabit.com> <49784FA8.5090100@mathematik.hu-berlin.de> <200901221027.26653.lists_ravi@lavabit.com> <4979918B.8030101@mathematik.hu-berlin.de> Message-ID: <4979F86A.3000204@princeton.com> Sebastian Walter wrote: >>> And is there a way to get the refcount of an object within Python? I'd >>> like to test if the refcount is correct. >> sys.getrefcount(obj) > > Good to know. When I run > - ------------------- test.py ---------------- > from _test import * > import sys > > def test_my_array(): > a = A() > print 'a.my_array=',a.my_array > print 'sys.getrefcount(a)=',sys.getrefcount(a) > print 'sys.getrefcount(a.my_array)=',sys.getrefcount(a.my_array) > > > if __name__ == "__main__": > test_my_array() > > - ------------ end test.py ------------- > I get the output > > - ----------- output ------------- > > walter at wronski$ python test.py > a.my_array= [ 1. 2. 3.] > sys.getrefcount(a)= 2 > sys.getrefcount(a.my_array)= 2 > > - ----------- end output -------- > > Ermm, is that good? > I expected that the refcount would be 1 and not 2. When you call getrefcount, the argument becomes another reference, so there are two references. http://docs.python.org/library/sys.html#sys.getrefcount sys.getrefcount(object) Return the reference count of the object. The count returned is generally one higher than you might expect, because it includes the (temporary) reference as an argument to getrefcount(). -- Anthony Foglia Princeton Consultants From pyplusplus at assumetheposition.nl Fri Jan 23 18:25:33 2009 From: pyplusplus at assumetheposition.nl (Paul Melis) Date: Fri, 23 Jan 2009 18:25:33 +0100 Subject: [C++-sig] Custom smart pointer and bp::wrapper In-Reply-To: <49730B66.2010303@assumetheposition.nl> References: <4971F509.5080309@assumetheposition.nl> <7465b6170901171115ja063ec2l27daf4d7b137328a@mail.gmail.com> <49730B66.2010303@assumetheposition.nl> Message-ID: <4979FD8D.3050707@assumetheposition.nl> Paul Melis wrote: > Roman Yakovenko wrote: > >> 2009/1/17 Paul Melis : >> >> >>> But when trying to run >>> >>> import doh >>> >>> class MyCallback(doh.Callback): >>> def execute(self, t): >>> print t >>> return 1 >>> >>> m = MyCallback() >>> >>> t = doh.Thing() >>> t.setCallback(m) >>> t.update() >>> >>> i'm getting >>> >>> Traceback (most recent call last): >>> File "no_to_python_converter.py", line 12, in >>> t.update() >>> TypeError: No to_python (by-value) converter found for C++ type: Thing". >>> >>> And I fail to see why... >>> >>> >> Mu guess: you need to use boost::python::register_ptr_to_python< Thin >(); >> ( http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/register_ptr_to_python.html >> ) >> >> > Well, that only seems to work on smart pointers, i.e. ref_ptr. > >From the docs: > "|| supplies > |register_ptr_to_python|, a function template which registers a > conversion for smart pointers to Python" > >> I have more or less complete example, which exports custom pointer >> class and its usage to Python: >> http://www.language-binding.net/pyplusplus/troubleshooting_guide/smart_ptrs/smart_ptrs.html >> >> > Right, I did find it while trying to solve the above. But where our code > differs is that my wrapper class (CallbackWrap) needs to pass a Thing* > to Python and that's where it goes wrong. Your wrapper only returns an > integer and doesn't pass any parameters to Python. I think that is the > crucial difference here. > Well, I seem to have found a solution. Instead of passing the pointer in the call to the bp::override I wrap it back up in a ref_ptr, i.e.: struct CallbackWrap : Callback, bp::wrapper { void execute(Thing* t) { if (bp::override ovr = this->get_override("execute")) { ref_ptr t_ref = t; ovr(t_ref); } else Callback::execute(t); } void default_execute(Thing* t) { this->Callback::execute(t); } }; Paul From dave at boostpro.com Fri Jan 23 18:43:01 2009 From: dave at boostpro.com (David Abrahams) Date: Fri, 23 Jan 2009 12:43:01 -0500 Subject: [C++-sig] bindings size In-Reply-To: <95291a80901230844u263614y5697d098abb0bdd@mail.gmail.com> (Renato Araujo's message of "Fri, 23 Jan 2009 13:44:35 -0300") References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> <95291a80901230824i3e9a3c96m1140fa48b07cb362@mail.gmail.com> <87eiyuvvaq.fsf@mcbain.luannocracy.com> <95291a80901230844u263614y5697d098abb0bdd@mail.gmail.com> Message-ID: <87y6x1vryy.fsf@mcbain.luannocracy.com> on Fri Jan 23 2009, Renato Araujo wrote: > I'm current using "CXXFLAGS=-ffunction-sections -Os" and "LDFLAGS > --gc-sections", and the library already striped, without strip the > size is about 12MB. There's a lot more you can do. -fomit-frame-pointer, -finline-functions, ...man gcc, man There's no reason in and of itself that templates should cause this thing to be big. You /are/ talking about the size of your binding .so and not libboost_python, right? -- Dave Abrahams BoostPro Computing http://www.boostpro.com From renatox at gmail.com Fri Jan 23 19:51:53 2009 From: renatox at gmail.com (Renato Araujo) Date: Fri, 23 Jan 2009 15:51:53 -0300 Subject: [C++-sig] bindings size In-Reply-To: <87y6x1vryy.fsf@mcbain.luannocracy.com> References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> <95291a80901230824i3e9a3c96m1140fa48b07cb362@mail.gmail.com> <87eiyuvvaq.fsf@mcbain.luannocracy.com> <95291a80901230844u263614y5697d098abb0bdd@mail.gmail.com> <87y6x1vryy.fsf@mcbain.luannocracy.com> Message-ID: <95291a80901231051j3dc6e3c6q786820367734e319@mail.gmail.com> hi David, This size is about my library, when I use "class_" and ".def()" templates from libboost_python. And about the g++ flags I use -Os which includes most of the flags for size optimizations. I tried with others flags but the difference is minimum, nothing significant. Thanks On Fri, Jan 23, 2009 at 2:43 PM, David Abrahams wrote: > > on Fri Jan 23 2009, Renato Araujo wrote: > >> I'm current using "CXXFLAGS=-ffunction-sections -Os" and "LDFLAGS >> --gc-sections", and the library already striped, without strip the >> size is about 12MB. > > There's a lot more you can do. -fomit-frame-pointer, > -finline-functions, ...man gcc, man > > There's no reason in and of itself that templates should cause this > thing to be big. You /are/ talking about the size of your binding .so > and not libboost_python, right? > > -- > Dave Abrahams > BoostPro Computing > http://www.boostpro.com > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Renato Araujo Oliveira Filho From s_sourceforge at nedprod.com Sat Jan 24 15:13:50 2009 From: s_sourceforge at nedprod.com (Niall Douglas) Date: Sat, 24 Jan 2009 14:13:50 -0000 Subject: [C++-sig] bindings size In-Reply-To: <95291a80901231051j3dc6e3c6q786820367734e319@mail.gmail.com> References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com>, <87y6x1vryy.fsf@mcbain.luannocracy.com>, <95291a80901231051j3dc6e3c6q786820367734e319@mail.gmail.com> Message-ID: <497B221E.12296.3448808E@s_sourceforge.nedprod.com> Have you tried -fvisibility=hidden on GCC? That chomps a good few megabyte off my bindings. Cheers, Niall On 23 Jan 2009 at 15:51, Renato Araujo wrote: > hi David, > > This size is about my library, when I use "class_" and ".def()" > templates from libboost_python. And about the g++ flags I use -Os > which includes most of the flags for size optimizations. I tried with > others flags but the difference is minimum, nothing significant. > > Thanks > > On Fri, Jan 23, 2009 at 2:43 PM, David Abrahams wrote: > > > > on Fri Jan 23 2009, Renato Araujo wrote: > > > >> I'm current using "CXXFLAGS=-ffunction-sections -Os" and "LDFLAGS > >> --gc-sections", and the library already striped, without strip the > >> size is about 12MB. > > > > There's a lot more you can do. -fomit-frame-pointer, > > -finline-functions, ...man gcc, man > > > > There's no reason in and of itself that templates should cause this > > thing to be big. You /are/ talking about the size of your binding .so > > and not libboost_python, right? > > > > -- > > Dave Abrahams > > BoostPro Computing > > http://www.boostpro.com > > _______________________________________________ > > Cplusplus-sig mailing list > > Cplusplus-sig at python.org > > http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > > -- > Renato Araujo Oliveira Filho > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From renatox at gmail.com Mon Jan 26 15:16:59 2009 From: renatox at gmail.com (Renato Araujo) Date: Mon, 26 Jan 2009 11:16:59 -0300 Subject: [C++-sig] bindings size In-Reply-To: <497B221E.12296.3448808E@s_sourceforge.nedprod.com> References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> <87y6x1vryy.fsf@mcbain.luannocracy.com> <95291a80901231051j3dc6e3c6q786820367734e319@mail.gmail.com> <497B221E.12296.3448808E@s_sourceforge.nedprod.com> Message-ID: <95291a80901260616x79bf29d5q70d269482b23c829@mail.gmail.com> I'm already using this. This help but I need reduce more because the python binding is about four times bigger then c++ library. Thanks On Sat, Jan 24, 2009 at 11:13 AM, Niall Douglas wrote: > Have you tried -fvisibility=hidden on GCC? That chomps a good few > megabyte off my bindings. > > Cheers, > Niall > > On 23 Jan 2009 at 15:51, Renato Araujo wrote: > >> hi David, >> >> This size is about my library, when I use "class_" and ".def()" >> templates from libboost_python. And about the g++ flags I use -Os >> which includes most of the flags for size optimizations. I tried with >> others flags but the difference is minimum, nothing significant. >> >> Thanks >> >> On Fri, Jan 23, 2009 at 2:43 PM, David Abrahams wrote: >> > >> > on Fri Jan 23 2009, Renato Araujo wrote: >> > >> >> I'm current using "CXXFLAGS=-ffunction-sections -Os" and "LDFLAGS >> >> --gc-sections", and the library already striped, without strip the >> >> size is about 12MB. >> > >> > There's a lot more you can do. -fomit-frame-pointer, >> > -finline-functions, ...man gcc, man >> > >> > There's no reason in and of itself that templates should cause this >> > thing to be big. You /are/ talking about the size of your binding .so >> > and not libboost_python, right? >> > >> > -- >> > Dave Abrahams >> > BoostPro Computing >> > http://www.boostpro.com >> > _______________________________________________ >> > Cplusplus-sig mailing list >> > Cplusplus-sig at python.org >> > http://mail.python.org/mailman/listinfo/cplusplus-sig >> > >> >> >> >> -- >> Renato Araujo Oliveira Filho >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Renato Araujo Oliveira Filho From Matthew.Scouten at tradingtechnologies.com Mon Jan 26 17:18:19 2009 From: Matthew.Scouten at tradingtechnologies.com (Matthew Scouten (TT)) Date: Mon, 26 Jan 2009 10:18:19 -0600 Subject: [C++-sig] Pickle an enum from c++ In-Reply-To: <913058.62639.qm@web111402.mail.gq1.yahoo.com> References: <32490DFF7774554A85D65D23A9F0F93809A51DA7@chiex01><7465b6170812081102m3236d3d5w5b7634abbe77a8b@mail.gmail.com><200812091340.17902.meine@informatik.uni-hamburg.de><947038.27443.qm@web111408.mail.gq1.yahoo.com><32490DFF7774554A85D65D23A9F0F93809AF0663@chiex01><714948.24769.qm@web111404.mail.gq1.yahoo.com><32490DFF7774554A85D65D23A9F0F93809AF0748@chiex01> <913058.62639.qm@web111402.mail.gq1.yahoo.com> Message-ID: <32490DFF7774554A85D65D23A9F0F93809F6A9B4@chiex01> I ran the enum unit test that came with boost. I got the same error that I got from my own example. (just on the pickle stuff. The rest passed.) I am using 1.34.1, because that is the version of boost that the library-to-be-wrapped is on. Obviously, either my version of boost is broken, or something is wrong with my environment. Has one else out there tried to run the unit tests against this version recently? (PS: Sorry to be reviving a thread after so long. Took along xmas break, and have been busy with other projects since then. To restore context, OP is here: http://mail.python.org/pipermail/cplusplus-sig/2008-December/014084.html ) -----Original Message----- From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at py thon.org] On Behalf Of Ralf W. Grosse-Kunstleve Sent: Friday, December 12, 2008 12:23 PM To: Development of Python/C++ integration Subject: Re: [C++-sig] Pickle an enum from c++ Could you try if the enum test works with your version of boost? What version do you have? I just tried it out (using the current boost svn trunk) and it works for me. I think it should work with any boost version after Dec 2006. Note that you have to use pickle.HIGHEST_PROTOCOL as in the test, otherwise it crashes. ----- Original Message ---- From: Matthew Scouten (TT) To: Development of Python/C++ integration Sent: Friday, December 12, 2008 10:01:26 AM Subject: Re: [C++-sig] Pickle an enum from c++ No, that didn't make a difference. -----Original Message----- From: cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at python.org [mailto:cplusplus-sig-bounces+matthew.scouten=tradingtechnologies.com at py thon.org] On Behalf Of Ralf W. Grosse-Kunstleve Sent: Friday, December 12, 2008 10:29 AM To: Development of Python/C++ integration Subject: Re: [C++-sig] Pickle an enum from c++ > Is there something I am supposed to do to the enum_ to make it work? Did you look at thid test? boost/libs/python/test/enum.cpp Do you have the .export_values()? (Not sure if that's important; I never use enum.) Ralf _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig From rwgk at yahoo.com Tue Jan 27 00:06:21 2009 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 26 Jan 2009 15:06:21 -0800 (PST) Subject: [C++-sig] bindings size References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> <87y6x1vryy.fsf@mcbain.luannocracy.com> <95291a80901231051j3dc6e3c6q786820367734e319@mail.gmail.com> <497B221E.12296.3448808E@s_sourceforge.nedprod.com> <95291a80901260616x79bf29d5q70d269482b23c829@mail.gmail.com> Message-ID: <645539.65562.qm@web111415.mail.gq1.yahoo.com> Are you wrapping many trivial (member) functions? ----- Original Message ---- From: Renato Araujo To: Development of Python/C++ integration Sent: Monday, January 26, 2009 6:16:59 AM Subject: Re: [C++-sig] bindings size I'm already using this. This help but I need reduce more because the python binding is about four times bigger then c++ library. Thanks From zaexage at gmail.com Tue Jan 27 06:21:19 2009 From: zaexage at gmail.com (ZaeX) Date: Tue, 27 Jan 2009 13:21:19 +0800 Subject: [C++-sig] ArgumentError when use the shared_ptr Message-ID: <2c5080560901262121v4efe2546ia049758e2d631917@mail.gmail.com> Hi, c++-sig: I got a problem involving shared_ptr here, according to some threads here I believed it should be solved by implicitly_convertible, however after many tries, I still cannot make it work. although this problem may look old, I really need your help or hint. My environment is WinXP + python 2.5.4 + boost1.36 class A { std::vector vec; public: A(boost::shared_ptr& ptr) :vec(ptr->vec) {} std::vector& GetV(){return vec;} }; -- Time is mana, we must hurry -------------- next part -------------- An HTML attachment was scrubbed... URL: From zaexage at gmail.com Tue Jan 27 06:30:16 2009 From: zaexage at gmail.com (ZaeX) Date: Tue, 27 Jan 2009 13:30:16 +0800 Subject: [C++-sig] ArgumentError when use the shared_ptr Message-ID: <2c5080560901262130v4f007f82u8e56fc9cdd084a3a@mail.gmail.com> Hi, c++-sig: I got a problem involving shared_ptr here, according to some threads here I believed it should be solved by implicitly_convertible, however after many tries, I still cannot make it work. although this problem may look old, I really need your help or hint. My environment is WinXP + python 2.5.4 + boost1.36 ///////////////////////////////////////// class A { std::vector vec; public: A(){} A(boost::shared_ptr& ptr) :vec(ptr->vec) std::vector& GetV(){return vec;} }; class D { boost::shared_ptr m_aa; public: boost::shared_ptr& GetAA() {return m_aa;} }; implicitly_convertible, A>(); /////////////////////////////////////// I exported the A and D, and also added the implicitly_convertible but when I tried to do this in python: d = D() a = d.GetAA() a.GetV() Python interpreter will complains Boost.Python.ArgumentError: Python argument types in A.GetV(A) did not match C++ signature: GetV(class A {lvalue}) --------------------------------------------------------------------- Can you give some help on this, I have been stuck here for 2 days. -- Time is mana, we must hurry -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Tue Jan 27 09:26:06 2009 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 27 Jan 2009 10:26:06 +0200 Subject: [C++-sig] ArgumentError when use the shared_ptr In-Reply-To: <2c5080560901262130v4f007f82u8e56fc9cdd084a3a@mail.gmail.com> References: <2c5080560901262130v4f007f82u8e56fc9cdd084a3a@mail.gmail.com> Message-ID: <7465b6170901270026u97d6f80ued5dba291864c1b1@mail.gmail.com> 2009/1/27 ZaeX : > Hi, c++-sig: > > I got a problem involving shared_ptr here, > according to some threads here I believed it should be solved by > implicitly_convertible, > however after many tries, I still cannot make it work. although this problem > may look old, I really need your help or hint. > My environment is WinXP + python 2.5.4 + boost1.36 > ///////////////////////////////////////// > class A > { > std::vector vec; > public: > A(){} > A(boost::shared_ptr& ptr) :vec(ptr->vec) > std::vector& GetV(){return vec;} > }; > > class D > { > boost::shared_ptr m_aa; > public: > boost::shared_ptr& GetAA() {return m_aa;} > }; > > implicitly_convertible, A>(); > /////////////////////////////////////// > I exported the A and D, and also added the implicitly_convertible > but when I tried to do this in python: > > d = D() > a = d.GetAA() > a.GetV() > > Python interpreter will complains > Boost.Python.ArgumentError: Python argument types in > A.GetV(A) > did not match C++ signature: > GetV(class A {lvalue}) > --------------------------------------------------------------------- > Can you give some help on this, I have been stuck here for 2 days. I suggest you not fight with Boost.Python - pass/return shared_ptr<...> by value. The code will start to work. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From cameron at sandboxsoftware.com.au Tue Jan 27 12:05:09 2009 From: cameron at sandboxsoftware.com.au (Cameron Royal) Date: Tue, 27 Jan 2009 03:05:09 -0800 (PST) Subject: [C++-sig] Boost Pro Python Version Message-ID: <21683370.post@talk.nabble.com> So I'm having some difficulty getting one of the boost.python tutorial examples working. In particular the class with a string constuctor. http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.constructors This exact example crashes for me in Visual Studio with an exception at line 75 in . I've read a few other forum posts and I can only assume there was something wrong with my build / config seeing as this seems like a trivial example. I am using the free boost download (http://www.boostpro.com/products/free) for VC9 and Python 2.5. I'm wondering if this is the right version of Python for that build? I couldn't find in the download any indication of what python version it's expecting, or if it evens matters. Any guidance would be much appreciated. Many thanks- Cameron -- View this message in context: http://www.nabble.com/Boost-Pro-Python-Version-tp21683370p21683370.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From zaexage at gmail.com Tue Jan 27 13:18:52 2009 From: zaexage at gmail.com (ZaeX) Date: Tue, 27 Jan 2009 20:18:52 +0800 Subject: [C++-sig] ArgumentError when use the shared_ptr In-Reply-To: <7465b6170901270026u97d6f80ued5dba291864c1b1@mail.gmail.com> References: <2c5080560901262130v4f007f82u8e56fc9cdd084a3a@mail.gmail.com> <7465b6170901270026u97d6f80ued5dba291864c1b1@mail.gmail.com> Message-ID: <2c5080560901270418t357655c5n120ee7e607670920@mail.gmail.com> Thanks, Roman. It solved the problem. Can't believe this '&' takes me two days. On Tue, Jan 27, 2009 at 4:26 PM, Roman Yakovenko wrote: > 2009/1/27 ZaeX : > > Hi, c++-sig: > > > > I got a problem involving shared_ptr here, > > according to some threads here I believed it should be solved by > > implicitly_convertible, > > however after many tries, I still cannot make it work. although this > problem > > may look old, I really need your help or hint. > > My environment is WinXP + python 2.5.4 + boost1.36 > > ///////////////////////////////////////// > > class A > > { > > std::vector vec; > > public: > > A(){} > > A(boost::shared_ptr& ptr) :vec(ptr->vec) > > std::vector& GetV(){return vec;} > > }; > > > > class D > > { > > boost::shared_ptr m_aa; > > public: > > boost::shared_ptr& GetAA() {return m_aa;} > > }; > > > > implicitly_convertible, A>(); > > /////////////////////////////////////// > > I exported the A and D, and also added the implicitly_convertible > > but when I tried to do this in python: > > > > d = D() > > a = d.GetAA() > > a.GetV() > > > > Python interpreter will complains > > Boost.Python.ArgumentError: Python argument types in > > A.GetV(A) > > did not match C++ signature: > > GetV(class A {lvalue}) > > --------------------------------------------------------------------- > > Can you give some help on this, I have been stuck here for 2 days. > > I suggest you not fight with Boost.Python - pass/return > shared_ptr<...> by value. > > The code will start to work. > > -- > Roman Yakovenko > C++ Python language binding > http://www.language-binding.net/ > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Time is mana, we must hurry -------------- next part -------------- An HTML attachment was scrubbed... URL: From dave at boostpro.com Tue Jan 27 13:34:17 2009 From: dave at boostpro.com (David Abrahams) Date: Tue, 27 Jan 2009 07:34:17 -0500 Subject: [C++-sig] Boost Pro Python Version In-Reply-To: <21683370.post@talk.nabble.com> (Cameron Royal's message of "Tue, 27 Jan 2009 03:05:09 -0800 (PST)") References: <21683370.post@talk.nabble.com> Message-ID: <87pri97ws6.fsf@mcbain.luannocracy.com> on Tue Jan 27 2009, Cameron Royal wrote: > So I'm having some difficulty getting one of the boost.python tutorial > examples working. In particular the class with a string constuctor. > > http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.constructors > > This exact example crashes for me in Visual Studio Have you tried the build and test with Boost.Build/bjam as instructed in the tutorial? > with an exception at line > 75 in . I've read a few other forum posts > and I can only assume there was something wrong with my build / config > seeing as this seems like a trivial example. > > I am using the free boost download (http://www.boostpro.com/products/free) > for VC9 and Python 2.5. I'm wondering if this is the right version of Python > for that build? I couldn't find in the download any indication of what > python version it's expecting, or if it evens matters. Well, it has to be at least Python 2.2, but otherwise, it should be fine. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From dave at boostpro.com Tue Jan 27 15:32:23 2009 From: dave at boostpro.com (David Abrahams) Date: Tue, 27 Jan 2009 09:32:23 -0500 Subject: [C++-sig] ArgumentError when use the shared_ptr In-Reply-To: <2c5080560901270418t357655c5n120ee7e607670920@mail.gmail.com> (ZaeX's message of "Tue, 27 Jan 2009 20:18:52 +0800") References: <2c5080560901262130v4f007f82u8e56fc9cdd084a3a@mail.gmail.com> <7465b6170901270026u97d6f80ued5dba291864c1b1@mail.gmail.com> <2c5080560901270418t357655c5n120ee7e607670920@mail.gmail.com> Message-ID: <871vuo95vs.fsf@mcbain.luannocracy.com> on Tue Jan 27 2009, ZaeX wrote: > Thanks, Roman. It solved the problem. > Can't believe this '&' takes me two days. Using T const& as a parameter would've worked too. The thing to remember with Boost.Python is that a T& parameter has a very special meaning: there has to be a T hanging around in memory somewhere that your code can modify. shared_ptr is conjured up on-the-fly unless you happen to have one embedded in your Python object. -- Dave Abrahams BoostPro Computing http://www.boostpro.com From timprepscius at gmail.com Tue Jan 27 18:24:09 2009 From: timprepscius at gmail.com (Tim Prepscius) Date: Tue, 27 Jan 2009 12:24:09 -0500 Subject: [C++-sig] instance_holder::uninstall Message-ID: <6fcb94430901270924i69e25ea3yf67531fcf4b00f57@mail.gmail.com> Does this look about right? Perhaps this should be added to the standard installation. Necessary for forcing disconnects from python objects on the c++ side... // Uninstall the instance data for a C++ object from a Python instance // object. void instance_holder::uninstall(PyObject* self) throw() { assert(self->ob_type->ob_type == &class_metatype_object); instance_holder **chain = &(((objects::instance<>*)self)->objects); // iterate through, looking for the pointer that points to this while ( *chain && *chain != this ) chain = &((*chain)->m_next); // set that pointer to point to the m_next if (*chain) *chain = m_next; m_next = 0; } From J.Zhao at andor.com Tue Jan 27 20:26:10 2009 From: J.Zhao at andor.com (Jizhen Zhao) Date: Tue, 27 Jan 2009 19:26:10 -0000 Subject: [C++-sig] unload python extension lib (*.pyd) Message-ID: Hi, All: I need to unload a C++ extension libraries in python (*.pyd files), but found many posts said Python do not have this ability when I search the web. I wonder if anyone knows if this is true or not. If that is true, is there any way to solve this problem? Thanks a lot, Jizhen -------------- next part -------------- An HTML attachment was scrubbed... URL: From renatox at gmail.com Tue Jan 27 20:50:23 2009 From: renatox at gmail.com (Renato Araujo) Date: Tue, 27 Jan 2009 16:50:23 -0300 Subject: [C++-sig] bindings size In-Reply-To: <645539.65562.qm@web111415.mail.gq1.yahoo.com> References: <95291a80901230822w17bd406cv51b88eeb4b6bdd69@mail.gmail.com> <87y6x1vryy.fsf@mcbain.luannocracy.com> <95291a80901231051j3dc6e3c6q786820367734e319@mail.gmail.com> <497B221E.12296.3448808E@s_sourceforge.nedprod.com> <95291a80901260616x79bf29d5q70d269482b23c829@mail.gmail.com> <645539.65562.qm@web111415.mail.gq1.yahoo.com> Message-ID: <95291a80901271150y68202422t843204fc96fa2e3d@mail.gmail.com> Hi, The main problem here is that, in my base class I have a lot of protected virtual methods, and to export this I need replicate and export again all functions for all derived class. This cause a lot of call to function ".def" from "class_" template, and this make the code bigger. Some classes I have about 20 protected virtual functions, and another 10 derived classes, then I need use 200 calls of ".def". There is some workaround to export protected functions and virtual functions to avoid call ".def" function in all derived wrapper class? Thanks. On Mon, Jan 26, 2009 at 8:06 PM, Ralf W. Grosse-Kunstleve wrote: > Are you wrapping many trivial (member) functions? > > > > ----- Original Message ---- > From: Renato Araujo > To: Development of Python/C++ integration > Sent: Monday, January 26, 2009 6:16:59 AM > Subject: Re: [C++-sig] bindings size > > I'm already using this. This help but I need reduce more because the > python binding is about four times bigger then c++ library. > > Thanks > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Renato Araujo Oliveira Filho From macke at yar.nu Wed Jan 28 10:29:47 2009 From: macke at yar.nu (Marcus Lindblom) Date: Wed, 28 Jan 2009 10:29:47 +0100 Subject: [C++-sig] Boost Pro Python Version In-Reply-To: <21683370.post@talk.nabble.com> References: <21683370.post@talk.nabble.com> Message-ID: <4980258B.2000807@yar.nu> Cameron Royal wrote: > So I'm having some difficulty getting one of the boost.python tutorial > examples working. In particular the class with a string constuctor. > > http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.constructors > > This exact example crashes for me in Visual Studio with an exception at line > 75 in . I've read a few other forum posts > and I can only assume there was something wrong with my build / config > seeing as this seems like a trivial example. > > I am using the free boost download (http://www.boostpro.com/products/free) > for VC9 and Python 2.5. I'm wondering if this is the right version of Python > for that build? I couldn't find in the download any indication of what > python version it's expecting, or if it evens matters. Depends on your python binary. IIUC, python and its extensions needs to be built with the same compiler. (I recall getting complaints on that from setuptools.) I think Python 2.5 (as released on python.org) was built with VC71. Python 2.6 uses VC90, so going there might help. HTH /Marcus P.S. I'd wish Python would use something like JNI to avoid this. Would make it much eaiser to distribute and use extension binaries. IIRC there was a PEP for PNI, but that got voted down. :-/ From macke at yar.nu Wed Jan 28 10:29:47 2009 From: macke at yar.nu (Marcus Lindblom) Date: Wed, 28 Jan 2009 10:29:47 +0100 Subject: [C++-sig] Boost Pro Python Version In-Reply-To: <21683370.post@talk.nabble.com> References: <21683370.post@talk.nabble.com> Message-ID: <4980258B.2000807@yar.nu> Cameron Royal wrote: > So I'm having some difficulty getting one of the boost.python tutorial > examples working. In particular the class with a string constuctor. > > http://www.boost.org/doc/libs/1_37_0/libs/python/doc/tutorial/doc/html/python/exposing.html#python.constructors > > This exact example crashes for me in Visual Studio with an exception at line > 75 in . I've read a few other forum posts > and I can only assume there was something wrong with my build / config > seeing as this seems like a trivial example. > > I am using the free boost download (http://www.boostpro.com/products/free) > for VC9 and Python 2.5. I'm wondering if this is the right version of Python > for that build? I couldn't find in the download any indication of what > python version it's expecting, or if it evens matters. Depends on your python binary. IIUC, python and its extensions needs to be built with the same compiler. (I recall getting complaints on that from setuptools.) I think Python 2.5 (as released on python.org) was built with VC71. Python 2.6 uses VC90, so going there might help. HTH /Marcus P.S. I'd wish Python would use something like JNI to avoid this. Would make it much eaiser to distribute and use extension binaries. IIRC there was a PEP for PNI, but that got voted down. :-/ From william.marie at gmail.com Wed Jan 28 19:16:25 2009 From: william.marie at gmail.com (=?UTF-8?Q?William_Mari=C3=A9?=) Date: Wed, 28 Jan 2009 10:16:25 -0800 (PST) Subject: [C++-sig] Calling python virtual function from C++ Message-ID: <21711534.post@talk.nabble.com> Hi, I have a little problem, i can't find a way to call a pure virtual function from C++ on a Python object taking a specific class object as argument : // Base Class for a component class BaseCmp { public: ~BaseCmp() {}; }; // Base class class Base { public: Base() {}; virtual ~Base() {}; virtual void testVirtualPure( BaseCmp* pBaseCmp ) = 0; }; struct BaseWrap : Base, bp::wrapper { void testVirtualPure( BaseCmp* pBaseCmp ) { this->get_override("testVirtualPure")( pBaseCmp ); } }; // Module TestScript BOOST_PYTHON_MODULE(test) { // BaseCmp Class exposition bp::class_("BaseCmp", bp::no_init) ; // Base Class Wrapper exposition bp::class_("Base") .def( "testVirtualPure", bp::pure_virtual( &Base::testVirtualPure ) ) ; } I need to call my testVirtualPure function from C++ side : BaseCmp* bc = BaseCmp(); Base* b = Base(); b->testVirtualPure( bc ); But this causes an error : TypeError: No to_python (by-value) converter found for C++ type: class BaseCmp Does someone know how i can do that ? Thanks a lot -- View this message in context: http://www.nabble.com/Calling-python-virtual-function-from-C%2B%2B-tp21711534p21711534.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From dave at boostpro.com Wed Jan 28 22:27:33 2009 From: dave at boostpro.com (David Abrahams) Date: Wed, 28 Jan 2009 16:27:33 -0500 Subject: [C++-sig] Calling python virtual function from C++ In-Reply-To: <21711534.post@talk.nabble.com> ("William =?utf-8?Q?Mari?= =?utf-8?Q?=C3=A9=22's?= message of "Wed, 28 Jan 2009 10:16:25 -0800 (PST)") References: <21711534.post@talk.nabble.com> Message-ID: <873af3nmt6.fsf@mcbain.luannocracy.com> on Wed Jan 28 2009, William Mari? wrote: > I need to call my testVirtualPure function from C++ side : > BaseCmp* bc = BaseCmp(); > Base* b = Base(); > b->testVirtualPure( bc ); > > But this causes an error : > TypeError: No to_python (by-value) converter found for C++ type: class > BaseCmp The above doesn't compile, so you can't possibly be getting that TypeError from it. Please post the exact complete, reproducible example that causes youi problems, after reducing it to a minimal test case. Thanks, -- Dave Abrahams BoostPro Computing http://www.boostpro.com From cameron at sandboxsoftware.com.au Thu Jan 29 01:33:08 2009 From: cameron at sandboxsoftware.com.au (Cameron Royal) Date: Wed, 28 Jan 2009 16:33:08 -0800 (PST) Subject: [C++-sig] Boost Pro Python Version In-Reply-To: <87pri97ws6.fsf@mcbain.luannocracy.com> References: <21683370.post@talk.nabble.com> <87pri97ws6.fsf@mcbain.luannocracy.com> Message-ID: <21718547.post@talk.nabble.com> David Abrahams-3 wrote: > > Have you tried the build and test with Boost.Build/bjam as instructed in > the tutorial? > I had hoped the precompiled binaries would be fine, but it seems not. After building the latest version of boost myself with python 2.5 it seems to be working now. Thanks for the tip. - Cameron -- View this message in context: http://www.nabble.com/Boost-Pro-Python-Version-tp21683370p21718547.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From dave at boostpro.com Thu Jan 29 02:46:44 2009 From: dave at boostpro.com (David Abrahams) Date: Wed, 28 Jan 2009 20:46:44 -0500 Subject: [C++-sig] Boost Pro Python Version In-Reply-To: <21718547.post@talk.nabble.com> (Cameron Royal's message of "Wed, 28 Jan 2009 16:33:08 -0800 (PST)") References: <21683370.post@talk.nabble.com> <87pri97ws6.fsf@mcbain.luannocracy.com> <21718547.post@talk.nabble.com> Message-ID: <87k58esx2z.fsf@mcbain.luannocracy.com> on Wed Jan 28 2009, Cameron Royal wrote: > David Abrahams-3 wrote: >> >> Have you tried the build and test with Boost.Build/bjam as instructed in >> the tutorial? >> > > I had hoped the precompiled binaries would be fine, but it seems not. After > building the latest version of boost myself with python 2.5 it seems to be > working now. Glad it worked out in the end. Still, could I trouble you to produce a minimal reproducible example of something that should, but doesn't, work with the precompiled binaries? I'd really like to get that fixed. Thanks, -- Dave Abrahams BoostPro Computing http://www.boostpro.com From william.marie at gmail.com Thu Jan 29 12:27:38 2009 From: william.marie at gmail.com (=?UTF-8?B?V2lsbGlhbSBNYXJpw6k=?=) Date: Thu, 29 Jan 2009 11:27:38 +0000 Subject: [C++-sig] Calling python virtual function from C++ In-Reply-To: <873af3nmt6.fsf@mcbain.luannocracy.com> References: <21711534.post@talk.nabble.com> <873af3nmt6.fsf@mcbain.luannocracy.com> Message-ID: <498192AA.9040605@gmail.com> Hi David, Here is the reproducible test ( just change the boost.strap and the python paths in the jamroot ) !! Thanks for your time David Abrahams a ?crit : > on Wed Jan 28 2009, William Mari? wrote: > > >> I need to call my testVirtualPure function from C++ side : >> BaseCmp* bc = BaseCmp(); >> Base* b = Base(); >> b->testVirtualPure( bc ); >> >> But this causes an error : >> TypeError: No to_python (by-value) converter found for C++ type: class >> BaseCmp >> > > The above doesn't compile, so you can't possibly be getting that > TypeError from it. Please post the exact complete, reproducible example > that causes youi problems, after reducing it to a minimal test case. > > Thanks, > -------------- next part -------------- A non-text attachment was scrubbed... Name: testboost.rar Type: application/octet-stream Size: 1761 bytes Desc: not available URL: From dave at boostpro.com Thu Jan 29 16:38:32 2009 From: dave at boostpro.com (David Abrahams) Date: Thu, 29 Jan 2009 10:38:32 -0500 Subject: [C++-sig] Calling python virtual function from C++ In-Reply-To: <498192AA.9040605@gmail.com> ("William =?utf-8?Q?Mari=C3=A9?= =?utf-8?Q?=22's?= message of "Thu, 29 Jan 2009 11:27:38 +0000") References: <21711534.post@talk.nabble.com> <873af3nmt6.fsf@mcbain.luannocracy.com> <498192AA.9040605@gmail.com> Message-ID: <874oziqg07.fsf@mcbain.luannocracy.com> on Thu Jan 29 2009, William Mari? wrote: > Hi David, > > Here is the reproducible test ( just change the boost.strap and the python paths in > the jamroot ) !! > Thanks for your time void testVirtualPure( BaseCmp* pBaseCmp ) { this->get_override("testVirtualPure")( ptr( pBaseCmp ) ); } // ^^^^ ^ Creating Python objects that reference C++ objects by pointer is dangerous; if you really want to do that, wrap the C++ pointer in ptr(...). Otherwise Boost.Python will try to copy it. You declared BaseCmp to be noncopyable, so no converter was registered that would copy a BaseCmp object into a new Python object. > David Abrahams a ?crit : >> on Wed Jan 28 2009, William Mari? wrote: >> >> >>> I need to call my testVirtualPure function from C++ side : BaseCmp* bc = BaseCmp(); >>> Base* b = Base(); >>> b->testVirtualPure( bc ); >>> >>> But this causes an error : TypeError: No to_python (by-value) converter found for >>> C++ type: class >>> BaseCmp >>> >> >> The above doesn't compile, so you can't possibly be getting that >> TypeError from it. Please post the exact complete, reproducible example >> that causes youi problems, after reducing it to a minimal test case. >> >> Thanks, >> > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig -- Dave Abrahams BoostPro Computing http://www.boostpro.com From renatox at gmail.com Fri Jan 30 21:52:40 2009 From: renatox at gmail.com (Renato Araujo) Date: Fri, 30 Jan 2009 17:52:40 -0300 Subject: [C++-sig] virtual functions and bind size reduction Message-ID: <95291a80901301252p1525ef0paf631578d7f70e1f@mail.gmail.com> Hi guys, Now I'm trying to reduce my python module. To cut the size I've reduced the number of times that I have to expose a function. I had some success while working in the same module, but when dealing with more than one things get complicated. To do this I created a "workaround", described below (very ugly I know but with this I save 6MB, 30%). The problem is that between modules this does not work, there is something wrong during dynamic_cast calls. I will show the basic idea of my solution. (you can check the full code attached) The main idea is: I have my base and derived class like this: =========================================== class Base { protected: virtual void virtual_func() {/*...*/}. }; class Derived : public Base { }; I created a proxy class: ============================================== class BaseProxy { public: virtual void virtual_func_proxy() = 0; }; Now the wrapper class which inherits from my proxy class. ========================================================= Class BaseWrapper : public Base, public wrapper, public BaseProxy { public: void virtual_func() { } void virtual_func_proxy() { Base::virtual_func_proxy(); } }; Class DerivedWrapper : public Derived, public wrapper, public BaseProxy { public: void virtual_func_proxy() { Base::virtual_func_proxy(); } }; Then I create a function which accept my Base class. ==================================================== static void base_virtual_func(Base &self) { //here is the problem this dynamic_cast fails when called from another module BaseProxy *proxy = dynamic_cast(&self); assert(proxy); proxy->virtual_func_proxy(); } Now I export my module ======================= //... class_("Base") //with this not is necessary export this function again in derived class .def("virtual_func", base_virtual_func); class_("Derived"); //... This apparently works fine when running in same module but if I create a class derived from Base in another module (another library), this stop working because the dynamic_cast fails. I'm not sure if this will works in all cases, but this is working fine in my tests when in the same module. I'll appreciate if someone give some idea how solve this, or a another way to save some calls of ".def" functions. -- Renato Araujo Oliveira Filho -------------- next part -------------- A non-text attachment was scrubbed... Name: testboost.tar Type: application/x-tar Size: 10240 bytes Desc: not available URL: From rwgk at yahoo.com Sat Jan 31 02:13:55 2009 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Fri, 30 Jan 2009 17:13:55 -0800 (PST) Subject: [C++-sig] virtual functions and bind size reduction References: <95291a80901301252p1525ef0paf631578d7f70e1f@mail.gmail.com> Message-ID: <587097.45029.qm@web111404.mail.gq1.yahoo.com> > This apparently works fine when running in same module but if I create a class > derived from Base in another module (another library), this stop > working because the dynamic_cast fails. Simple idea: is the other module imported before the dynamic_cast is executed? That's probably important. (Not sure that's it; I hardly ever work with virtual functions.) Ralf