From vishal.bayskar at nechclst.in Wed May 5 12:19:22 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Wed, 5 May 2010 03:19:22 -0700 (PDT) Subject: [C++-sig] How to use functionality of list. Message-ID: <28459066.post@talk.nabble.com> Actually I am able create wrapper code for list . All the functions of list are exposed. But when I try to compile it (try to create shared library) it showing below error ----------- testModule.cpp:11: error: _Alloc was not declared in this scope testModule.cpp:18: error: _Tp was not declared in this scope testModule.cpp:18: error: _Alloc was not declared in this scope testModule.cpp: In function void init_module_testModule(): testModule.cpp:110: error: _Alloc was not declared in this scope testModule.cpp:113: error: _Tp was not declared in this scope /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/list.tcc: In member function ?void std::list<_Tp, _Alloc>::merge(std::list<_Tp, _Alloc>&) [with _Tp = A, _Alloc = std::allocator ]?: testModule.cpp:368: instantiated from here /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/list.tcc:222: error: no match for ?operator::operator* [with _Tp = A]() < __first1.std::_List_iterator<_Tp>::operator* [with _Tp = A]()? /usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/list.tcc: In member function ?void std::list<_Tp, _Alloc>::remove(const _Tp&) [with _Tp = A, _Alloc = std::allocator ]?: ----------- I think if I can somewhere declare _Alloc and _Tp I will be able to use it (list) correctly. Please let me know the possible solution Below are the code (and one file is attahced) that is used. cpp code : listP.cpp =============== #include #include class A { public: A(int num):num(num) { } private: int num; }; class Alist : public std::list { public: static void a() { std::cout << "check" << std::endl; } }; Alist getDummyAlist() { Alist aList; A a1(5); A a2(6); aList.push_back(a1); aList.push_back(a2); return aList; }; python file for generating wrapper code : afile.py ==================================== import os from pyplusplus import module_builder from pyplusplus.module_builder import call_policies from pyplusplus import code_creators mb=module_builder.module_builder_t(["./listP.cpp"], ) classA = mb.class_('list') classA.include() for className in mb.classes(): print "className : %s"%className http://old.nabble.com/file/p28459066/testModule.cpp testModule.cpp mb.build_code_creator( module_name = 'testModule') mb.write_module('./testModule.cpp') Generated wrapper code : testModule.cpp =============================== file testModule.cpp is uploaded (attached) on this page only. -- View this message in context: http://old.nabble.com/How-to-use-functionality-of-list.-tp28459066p28459066.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Wed May 5 12:42:31 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 5 May 2010 13:42:31 +0300 Subject: [C++-sig] How to use functionality of list. In-Reply-To: <28459066.post@talk.nabble.com> References: <28459066.post@talk.nabble.com> Message-ID: On Wed, May 5, 2010 at 1:19 PM, vishal bayskar wrote: > > Actually I am able create wrapper code for list . All the functions of list > are exposed. > But when I try to compile it (try to create shared library) it showing below > error > ... > mb=module_builder.module_builder_t(["./listP.cpp"], > ) > > classA = ?mb.class_('list') > classA.include() There is no need to expose std containers directly. Instead, expose declarations that are using them and Py++ will do the rest. mb.free_function( 'getDummyAlist' ).include() This topic is covered in the following document: http://language-binding.net/pyplusplus/documentation/containers.html HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From vishal.bayskar at nechclst.in Wed May 5 13:20:28 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Wed, 5 May 2010 04:20:28 -0700 (PDT) Subject: [C++-sig] How to use functionality of list. In-Reply-To: References: <28459066.post@talk.nabble.com> Message-ID: <28459555.post@talk.nabble.com> >There is no need to expose std containers directly. Instead, expose >declarations that are using them and Py++ will do the rest. >mb.free_function( 'getDummyAlist' ).include() >This topic is covered in the following document: >http://language-binding.net/pyplusplus/documentation/containers.html Yes I tried like as you suggested. but I am not able to use the functionality of list from python. Like suppose I need to use push_back etc from python. >>> import testModule >>> alistobj = testModule.Alist() >>> aobj=testModule.A(5) >>> alistobj.push_back(aobj) Traceback (most recent call last): File "", line 1, in ? AttributeError: 'Alist' object has no attribute 'push_back' >>> >>> dummyList =testModule.getDummyAlist() >>> dummyList.push_back(aobj) Traceback (most recent call last): File "", line 1, in ? AttributeError: 'Alist' object has no attribute 'push_back' Generated wrapper code : testModule.cpp =============================== // This file has been generated by Py++. #include "boost/python.hpp" #include "listP.cpp" namespace bp = boost::python; BOOST_PYTHON_MODULE(testModule){ { //::A typedef bp::class_< A > A_exposer_t; A_exposer_t A_exposer = A_exposer_t( "A", bp::init< int >(( bp::arg("num") )) ); bp::scope A_scope( A_exposer ); bp::implicitly_convertible< int, A >(); } bp::class_< Alist >( "Alist" ) .def( "a" , (void (*)( ))( &::Alist::a ) ) .staticmethod( "a" ); { //::getDummyAlist typedef ::Alist ( *getDummyAlist_function_type )( ); bp::def( "getDummyAlist" , getDummyAlist_function_type( &::getDummyAlist ) ); } { //::main typedef int ( *main_function_type )( ); bp::def( "main" , main_function_type( &::main ) ); } } -- View this message in context: http://old.nabble.com/How-to-use-functionality-of-list.-tp28459066p28459555.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Wed May 5 13:40:05 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Wed, 5 May 2010 14:40:05 +0300 Subject: [C++-sig] How to use functionality of list. In-Reply-To: <28459555.post@talk.nabble.com> References: <28459066.post@talk.nabble.com> <28459555.post@talk.nabble.com> Message-ID: On Wed, May 5, 2010 at 2:20 PM, vishal bayskar wrote: > >>There is no need to expose std containers directly. Instead, expose >>declarations that are using them and Py++ will do the rest. > >>mb.free_function( 'getDummyAlist' ).include() > >>This topic is covered in the following document: >>http://language-binding.net/pyplusplus/documentation/containers.html > > > Yes I tried like as you suggested. > but I am not able to use the functionality of list from python. > Like suppose I need to use push_back etc from python. Both indexing suites provides interface very similar to Python containers. So the user will not have to learn new API, but if you insists, you can rename/introduce new method from Python AList.push_back = AList.append -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From vishal.bayskar at nechclst.in Wed May 5 14:40:52 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Wed, 5 May 2010 05:40:52 -0700 (PDT) Subject: [C++-sig] How to use functionality of list. In-Reply-To: References: <28459066.post@talk.nabble.com> <28459555.post@talk.nabble.com> Message-ID: <28460323.post@talk.nabble.com> >Both indexing suites provides interface very similar to Python >containers. So the user will not have to learn new API, but if you >insists, you can rename/introduce new method from Python >AList.push_back = AList.append But object Alist is not treated as a list and giving the below error. >>> dummyList =testModule.getDummyAlist() >>> >>> print dummyList >>> >>> aobj=testModule.A(5) >>> >>> print aobj >>> >>> dummyList.append(aobj) Traceback (most recent call last): File "", line 1, in ? AttributeError: 'Alist' object has no attribute 'append' -- View this message in context: http://old.nabble.com/How-to-use-functionality-of-list.-tp28459066p28460323.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From vishal.bayskar at nechclst.in Thu May 6 09:24:05 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Thu, 6 May 2010 00:24:05 -0700 (PDT) Subject: [C++-sig] How to use functionality of list. In-Reply-To: <28460323.post@talk.nabble.com> References: <28459066.post@talk.nabble.com> <28459555.post@talk.nabble.com> <28460323.post@talk.nabble.com> Message-ID: <28470164.post@talk.nabble.com> Actually I need to extract the item present in the list, so I am trying to use methonds of list (just for an example I tried push_back in the below example). vishal bayskar wrote: > >>Both indexing suites provides interface very similar to Python >>containers. So the user will not have to learn new API, but if you >>insists, you can rename/introduce new method from Python > >>AList.push_back = AList.append > > But object Alist is not treated as a list and giving the below error. > >>>> dummyList =testModule.getDummyAlist() >>>> >>>> print dummyList > >>>> >>>> aobj=testModule.A(5) >>>> >>>> print aobj > >>>> >>>> dummyList.append(aobj) > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: 'Alist' object has no attribute 'append' > > > > -- View this message in context: http://old.nabble.com/How-to-use-functionality-of-list.-tp28459066p28470164.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Thu May 6 09:53:54 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 6 May 2010 10:53:54 +0300 Subject: [C++-sig] How to use functionality of list. In-Reply-To: <28470164.post@talk.nabble.com> References: <28459066.post@talk.nabble.com> <28459555.post@talk.nabble.com> <28460323.post@talk.nabble.com> <28470164.post@talk.nabble.com> Message-ID: On Thu, May 6, 2010 at 10:24 AM, vishal bayskar wrote: > > Actually I need to extract the item present in the list, so I am trying to > use methonds of list (just for an example I tried push_back in the below > example). I don't understand why do you struggle. The "exposed via an indexing suite" method allows you to modify and iterate over the list using the well defined and documented protocol. If you need to use: * the original function names - you can expose it via indexing suite and then add aliases to the functions * the original function functionality or some unexposed functions - you will have to extend the exposed class. Boost.Python interface allows you to achieve this. void mylist_push_back( AList& alist, const MyItem& item ){ alist.push_back( item ); } ... class_< AList > < register indexing suite functionality > .def( "push_back", &mylist_push_back ) ... >From Python, your user will not see any difference: alist = AList() alist.push_back|( .... ) HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From nitroamos at gmail.com Fri May 7 06:03:38 2010 From: nitroamos at gmail.com (Amos Anderson) Date: Thu, 6 May 2010 21:03:38 -0700 Subject: [C++-sig] "version mismatch?" because of libpython link problem? Message-ID: Summary: I have a setup which works on Linux, but not OSX 10.6.3. I want my build system to build its own python, build boost linking to that python, build my extensions linking to boost, and then run my python application with my c++ extensions. I'm getting the "version mismatch" error, and I can't figure out why. Any ideas on things I could try to get it to work? I want to bundle python with my source code because I need to control the version. I'm compiling my c++ extensions with boost. But there's something wrong because at the end, I'm rewarded with: Fatal Python error: Interpreter not initialized (version mismatch?) ../triad.sh: line 19: 83478 Abort trap $trunk/src/python/bin/python $@ This process does work on Linux, so there must be something peculiar about doing this on OSX 10.6.3. 1) I build python: ./configure --prefix=/Users/amos/test_fresh_compile/trunk/src/python I can see that libpython2.6.a was compiled like this: ar cr libpython2.6.a Modules/getbuildinfo.o^M ar cr libpython2.6.a Parser/acceler.o Parser/grammar1.o Parser/listnode.o Parser/node.o Parser/parser.o Parser/parsetok.o Parser/bitset.o Parser/metagrammar.o Parser/firstsets.o Parser/grammar.o Parser/pgen.o Parser/myreadline.o Parser/tokenizer.o^M ar cr libpython2.6.a Objects/abstract.o Objects/boolobject.o Objects/bufferobject.o Objects/bytes_methods.o Objects/bytearrayobject.o Objects/cellobject.o Objects/classobject.o Objects/cobject.o Objects/codeobject.o Objects/complexobject.o Objects/descrobject.o Objects/enumobject.o Objects/exceptions.o Objects/gen\ object.o Objects/fileobject.o Objects/floatobject.o Objects/frameobject.o Objects/funcobject.o Objects/intobject.o Objects/iterobject.o Objects/listobject.o Objects/longobject.o Objects/dictobject.o Objects/methodobject.o Objects/moduleobject.o Objects/object.o Objects/obmalloc.o Objects/rangeobject.o Objects/seto\ bject.o Objects/sliceobject.o Objects/stringobject.o Objects/structseq.o Objects/tupleobject.o Objects/typeobject.o Objects/weakrefobject.o Objects/unicodeobject.o Objects/unicodectype.o^M ar cr libpython2.6.a Python/_warnings.o Python/Python-ast.o Python/asdl.o Python/ast.o Python/bltinmodule.o Python/ceval.o Python/compile.o Python/codecs.o Python/errors.o Python/frozen.o Python/frozenmain.o Python/future.o Python/getargs.o Python/getcompiler.o Python/getcopyright.o Python/getmtime.o Python/getpla\ tform.o Python/getversion.o Python/graminit.o Python/import.o Python/importdl.o Python/marshal.o Python/modsupport.o Python/mystrtoul.o Python/mysnprintf.o Python/peephole.o Python/pyarena.o Python/pyfpe.o Python/pymath.o Python/pystate.o Python/pythonrun.o Python/structmember.o Python/symtable.o Python/sysmodule.\ o Python/traceback.o Python/getopt.o Python/pystrcmp.o Python/pystrtod.o Python/formatter_unicode.o Python/formatter_string.o Python/dynload_shlib.o Python/mactoolboxglue.o Python/thread.o^M /usr/bin/ranlib: file: libpython2.6.a(pymath.o) has no symbols^M ar cr libpython2.6.a Modules/config.o Modules/getpath.o Modules/main.o Modules/gcmodule.o ^M /usr/bin/ranlib: file: libpython2.6.a(pymath.o) has no symbols^M ar cr libpython2.6.a Modules/threadmodule.o Modules/signalmodule.o Modules/posixmodule.o Modules/errnomodule.o Modules/pwdmodule.o Modules/_sre.o Modules/_codecsmodule.o Modules/zipimport.o Modules/symtablemodule.o Modules/xxsubtype.o^M /usr/bin/ranlib: file: libpython2.6.a(pymath.o) has no symbols^M ranlib libpython2.6.a^M ranlib: file: libpython2.6.a(pymath.o) has no symbols^M 2) Build boost. In bjam I have: using python : 2.6 : python/bin/python2.6 : python/include/python2.6 : python/lib/python2.6/config : ; which produces lines like: darwin.compile.c++ boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/list.o "g++" -ftemplate-depth-128 -O3 -finline-functions -Wno-inline -Wall -g -dynamic -no-cpp-precomp -gdwarf-2 -fPIC -DBOOST_ALL_NO_LIB=1 -DBOOST_PYTHON_SOURCE -I"boost/boost_1_42_0" -I"python/include/python2.6" -c -o "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/list.o" "boost/boost_1_42_0/libs/python/src/list.cpp" so it appears to be linking to the version of python I just built. 3) Build libboost_python: darwin.link.dll boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/libboost_python.dylib "g++" -dynamiclib -Wl,-single_module -install_name "libboost_python.dylib" -L"python/lib/python2.6/config" -o "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/libboost_python.dylib" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/numeric.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/list.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/long.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/dict.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/tuple.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/str.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/slice.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/converter/from_python.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/converter/registry.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/converter/type_id.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/enum.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/class.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/function.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/inheritance.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/life_support.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/pickle_support.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/errors.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/module.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/converter/builtin_converters.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/converter/arg_to_python_base.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/iterator.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/stl_iterator.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object_protocol.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object_operators.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/wrapper.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/import.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/exec.o" "boost/boost_1_42_0/bin.v2/libs/python/build/darwin-4.2.1/release-triad/object/function_doc_signature.o" -lpython2.6 -headerpad_max_install_names -g -Wl,-dead_strip -no_dead_strip_inits_and_terms 4) Investigate libraries: > otool -L libboost_python.dylib libboost_python.dylib: libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0) /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1) now this is peculiar, because if I build with my /Library installed framework version of python instead, I get: > otool -L libboost_python.dylib libboost_python.dylib: libboost_python.dylib (compatibility version 0.0.0, current version 0.0.0) /Library/Frameworks/Python.framework/Versions/2.6/Python (compatibility version 2.6.0, current version 2.6.0) /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.1) so this is my first clue that something went wrong... however, I can't figure out any way to get the link stage for libboost_python.dylib to include the python library I want it to. I can even change the name -lpython2.6 to -lpythonamos, and it gives an error (so I know it's looking where I think it is), and then I copy libpython2.6.dylib to libpythonamos.dylib and i don't get an error. This suggests it's finding the library but it doesn't like it. the problem isn't 32 vs 64 bits: > lipo -info libboost_python.dylib Non-fat file: libboost_python.dylib is architecture: x86_64 > lipo -info libpython2.6.dylib input file libpython2.6.dylib is not a fat file Non-fat file: libpython2.6.dylib is architecture: x86_64 It's like the library requires some special OSX blessing that's normally only given when a framework is built. From talljimbo at gmail.com Sun May 9 21:52:01 2010 From: talljimbo at gmail.com (Jim Bosch) Date: Sun, 09 May 2010 12:52:01 -0700 Subject: [C++-sig] single-declaration code generation in Py++ Message-ID: <4BE71261.70103@gmail.com> I'm a relatively experienced boost.python user trying to take my first steps with Py++, and I find that what I'd often like to do is just use Py++ to declare a single class here and there, or perhaps just some members of a class, and then be able to insert those bits of code into C++ source files I mostly write manually. This is of course the opposite of the main Py++ paradigm, in which one inserts snippets of custom code into a automatically-generated module, but seems like all the functionality to do this must already present in Py++ - I just need to extract pieces of the declaration tree by name, and generate declaration and registration boost.python code for those individually (preferably as Python strings rather than separate files). I'm not afraid to dive into the source to figure out how to do this, but I'd appreciate it if anyone has any tips or suggestions on where to get started. Jim Bosch From roman.yakovenko at gmail.com Mon May 10 06:25:08 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Mon, 10 May 2010 07:25:08 +0300 Subject: [C++-sig] single-declaration code generation in Py++ In-Reply-To: <4BE71261.70103@gmail.com> References: <4BE71261.70103@gmail.com> Message-ID: On Sun, May 9, 2010 at 10:52 PM, Jim Bosch wrote: > I'm a relatively experienced boost.python user trying to take my first steps > with Py++, and I find that what I'd often like to do is just use Py++ to > declare a single class here and there, or perhaps just some members of a > class, and then be able to insert those bits of code into C++ source files I > mostly write manually. > > This is of course the opposite of the main Py++ paradigm, in which one > inserts snippets of custom code into a automatically-generated module, but > seems like all the functionality to do this must already present in Py++ - I > just need to extract pieces of the declaration tree by name, and generate > declaration and registration boost.python code for those individually > (preferably as Python strings rather than separate files). > > I'm not afraid to dive into the source to figure out how to do this, but I'd > appreciate it if anyone has any tips or suggestions on where to get started. It should not be too difficult. I suggest you to read this document ( http://language-binding.net/pyplusplus/documentation/architecture.html#code-generation-engine ), especially "code creators" sections. Then take a look on 'pyplusplus/file_writers/( single_file.py and multiple_files.py ) modules. pyplusplus/code_creators/algorithm.py has some code, that helps to work with "code creators tree". HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From vishal.bayskar at nechclst.in Mon May 10 13:02:56 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Mon, 10 May 2010 04:02:56 -0700 (PDT) Subject: [C++-sig] How to use functionality of list. In-Reply-To: References: <28459066.post@talk.nabble.com> <28459555.post@talk.nabble.com> <28460323.post@talk.nabble.com> <28470164.post@talk.nabble.com> Message-ID: <28510154.post@talk.nabble.com> I don't understand why do you struggle. The "exposed via an indexing suite" method allows you to modify and iterate over the list using the well defined and documented protocol. If you need to use: Thanks Roman, Now I am able to use the functionality of list (although not all like pop() but I use some other alternatives of pop). Roman can you tell me how could I get pyplusplus version 1.1. I have tried using "svn co" but the latest version that is checking out it only 1.0.0 I tried with below cvs command svn co https://pygccxml.svn.sourceforge.net/svnroot/pygccxml pygccxml and svn co http://pygccxml.svn.sourceforge.net/svnroot/pygccxml/pyplusplus_dev pyplusplus But both are showing version 1.0.0 (I haved checked the setup.py file ). -- View this message in context: http://old.nabble.com/How-to-use-functionality-of-list.-tp28459066p28510154.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From vishal.bayskar at nechclst.in Mon May 10 15:39:45 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Mon, 10 May 2010 06:39:45 -0700 (PDT) Subject: [C++-sig] How to use functionality of list. In-Reply-To: <28510154.post@talk.nabble.com> References: <28459066.post@talk.nabble.com> <28459555.post@talk.nabble.com> <28460323.post@talk.nabble.com> <28470164.post@talk.nabble.com> <28510154.post@talk.nabble.com> Message-ID: <28511646.post@talk.nabble.com> >Roman can you tell me how could I get pyplusplus version 1.1. I have tried using "svn co" but the >latest version that is checking out it only 1.0.0 >I tried with below cvs command >svn co https://pygccxml.svn.sourceforge.net/svnroot/pygccxml pygccxml >and >svn co http://pygccxml.svn.sourceforge.net/svnroot/pygccxml/pyplusplus_dev pyplusplus >But both are showing version 1.0.0 (I haved checked the setup.py file ). OK I think version was not changing (it was only 1.0.0) but the code is getting updated. -- View this message in context: http://old.nabble.com/How-to-use-functionality-of-list.-tp28459066p28511646.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From jean-charles.quillet at alyotech.fr Tue May 11 11:32:08 2010 From: jean-charles.quillet at alyotech.fr (QUILLET Jean-Charles) Date: Tue, 11 May 2010 11:32:08 +0200 Subject: [C++-sig] Trouble converting data from numpy.array to c++ Message-ID: <06A7AD4E92172446ADDEB44F8A5D5C101CAF8F453A@ARE01.alyotech.fr> Hi, I've got this problem I cannot solve. I've a c++ application from which I create an array sending to python this string: anArray = numpy.array((1, 2, 3)) After extracting the symbol "anArray" from the directory in a boost::python::object, I'm trying to extract the values in C++: int val = bp::extract(anArray[0]); It doesn't work and raise the error: "TypeError: No registered converter was able to produce a C++ rvalue of type int from this Python object of type numpy.int32" What is going on ? I'm using python 2.6, last version of Numpy 1.4.1 with boost 1.38 on Linux. Looking on the list, it seems that someone had this very problem but he didn't get any answer. http://article.gmane.org/gmane.comp.python.c++/11279/match=typeerror+no+registered+converter+able+produce+c%2b%2b+rvalue+type+int+python+object+numpy+int32 Hopefully I'll have more chance ! Any idea greatly appreciated, Jean-Charles -------------- next part -------------- An HTML attachment was scrubbed... URL: From diepen at astron.nl Tue May 11 12:24:42 2010 From: diepen at astron.nl (Ger van Diepen) Date: Tue, 11 May 2010 12:24:42 +0200 Subject: [C++-sig] Trouble converting data from numpy.array to c++ Message-ID: <4BE94C91020000A90000BDFB@smtp.nfra.nl> Hi Jean-Charles, AFAIK there are no standard converters for the numpy scalar types to C++ types. In pyrap it is solved by having explicit converters for numpy scalar types to C++ types (see pyrap.googlecode.com). Cheers, Ger >>> QUILLET Jean-Charles 05/11/10 11:38 AM >>> Hi, I've got this problem I cannot solve. I've a c++ applicationfrom which I create an array sending to python this string: anArray = numpy.array((1, 2, 3)) After extracting the symbol "anArray" from thedirectory in a boost::python::object, I'm trying to extract the values in C++: int val = bp::extract(anArray[0]); It doesn't work and raise the error: "TypeError: No registered converter was able toproduce a C++ rvalue of type int from this Python object of type numpy.int32" What is going on ? I'm using python 2.6, last version ofNumpy 1.4.1 with boost 1.38 on Linux. Looking on the list, it seems thatsomeone had this very problem but he didn't get any answer. http://article.gmane.org/gmane.comp.python.c++/11279/match=typeerror+no+registered+converter+able+produce+c%2b%2b+rvalue+type+int+python+object+numpy+int32 Hopefully I'll have more chance ! Any idea greatly appreciated, Jean-Charles -------------- next part -------------- An HTML attachment was scrubbed... URL: From jean-charles.quillet at alyotech.fr Tue May 11 14:32:34 2010 From: jean-charles.quillet at alyotech.fr (QUILLET Jean-Charles) Date: Tue, 11 May 2010 14:32:34 +0200 Subject: [C++-sig] Trouble converting data from numpy.array to c++ In-Reply-To: <4BE94C91020000A90000BDFB@smtp.nfra.nl> References: <4BE94C91020000A90000BDFB@smtp.nfra.nl> Message-ID: <06A7AD4E92172446ADDEB44F8A5D5C101CAF8F453B@ARE01.alyotech.fr> Thanks Ger, I'll check out this, it seems very interesting. But I'm confused because what I'm describing is working without any problem on windows using Visual Studio 2005 python 2.5 and boost 1.34 But for some reason I cant use these versions on Linux because of an incompatibility between Qt and Python 25 http://bugs.python.org/issue1086854 This is library hell ! Despite this, I'll check out the converters, but it seems a bit overkill for me. Whould you know an easy way to retrieve my values from the array ? Thanks in advance, Jean-Charles De : Ger van Diepen [mailto:diepen at astron.nl] Envoy? : mardi 11 mai 2010 12:25 ? : QUILLET Jean-Charles; cplusplus-sig at python.org Objet : [Spam Probable] Re: [C++-sig] Trouble converting data from numpy.array to c++ Hi Jean-Charles, AFAIK there are no standard converters for the numpy scalar types to C++ types. In pyrap it is solved by having explicit converters for numpy scalar types to C++ types (see pyrap.googlecode.com). Cheers, Ger >>> QUILLET Jean-Charles 05/11/10 11:38 AM >>> Hi, I've got this problem I cannot solve. I've a c++ application from which I create an array sending to python this string: anArray = numpy.array((1, 2, 3)) After extracting the symbol "anArray" from the directory in a boost::python::object, I'm trying to extract the values in C++: int val = bp::extract(anArray[0]); It doesn't work and raise the error: "TypeError: No registered converter was able to produce a C++ rvalue of type int from this Python object of type numpy.int32" What is going on ? I'm using python 2.6, last version of Numpy 1.4.1 with boost 1.38 on Linux. Looking on the list, it seems that someone had this very problem but he didn't get any answer. http://article.gmane.org/gmane.comp.python.c++/11279/match=typeerror+no+registered+converter+able+produce+c%2b%2b+rvalue+type+int+python+object+numpy+int32 Hopefully I'll have more chance ! Any idea greatly appreciated, Jean-Charles -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at informa.tiker.net Tue May 11 17:46:28 2010 From: lists at informa.tiker.net (Andreas Kloeckner) Date: Tue, 11 May 2010 11:46:28 -0400 Subject: [C++-sig] Trouble converting data from numpy.array to c++ In-Reply-To: <06A7AD4E92172446ADDEB44F8A5D5C101CAF8F453A@ARE01.alyotech.fr> References: <06A7AD4E92172446ADDEB44F8A5D5C101CAF8F453A@ARE01.alyotech.fr> Message-ID: <87r5liqw4r.fsf@grizzly.selfip.net> Hi Jean-Charles, On Tue, 11 May 2010 11:32:08 +0200, QUILLET Jean-Charles wrote: > I've got this problem I cannot solve. I've a c++ application from which I create an array sending to python this string: > > anArray = numpy.array((1, 2, 3)) > > After extracting the symbol "anArray" from the directory in a boost::python::object, I'm trying to extract the values in C++: > > int val = bp::extract(anArray[0]); > > It doesn't work and raise the error: This is probably not what you wanted to hear, but my PyUblas package solves your problem. :) http://mathema.tician.de/software/pyublas HTH, Andreas From kun.hong at uqconnect.edu.au Wed May 12 04:05:30 2010 From: kun.hong at uqconnect.edu.au (Mr Kun Hong) Date: Wed, 12 May 2010 02:05:30 +0000 Subject: [C++-sig] When not to use static boost python library Message-ID: <4D860A08F2325E4CB3D482A3F2EB99048937911F@BL2PRD0102MB009.prod.exchangelabs.com> Hi, I am new to boost python. I have some experience making python extension using pure C. Now I have a question: when should I not use static boost python library? I tried to search the archive a bit. But haven't found a good answer. I read the boost python library documentation, where the section 6.2 mentioned when should should static binary. But I still not very clear about it. The lines in the documentation is: ''' It might be appropriate to use the static Boost.Python library in any of the following cases: * You are extending python and the types exposed in your dynamically-loaded extension module don't need to be used by any other Boost.Python extension modules, and you don't care if the core library code is duplicated among them. * You are embedding python in your application and either: * You are targeting a Unix variant OS other than MacOS or AIX, where the dynamically-loaded extension modules can ?see? the Boost.Python library symbols that are part of the executable. * Or, you have statically linked some Boost.Python extension modules into your application and you don't care if any dynamically-loaded Boost.Python extension modules are able to use the types exposed by your statically-linked extension modules (and vice-versa). ''' First of all, I am not embedding python interpreter in my app. I want to write an extention that can by loaded dynamicaly. So I fall into the first case, and I don't care my core library code is duplicated (i think here it means the boost python code is duplicated). But I do care the types and functions in my produced extension can be used. I have built my extension using static boost python lib and test it by writing some python code access the typs and functions exposed. It all works fine. So I guess what the problem is should be that: another python extension, written in boost python, cannot access the types and functions exposed in my current extension. I am not clear why is it so, becaused it is exposed to the python interpreter already. If there are two python extensions, both written in pure C, can't they access each other's type and functions? Thanks in advance for any help. Kun From jean-charles.quillet at alyotech.fr Wed May 12 10:11:40 2010 From: jean-charles.quillet at alyotech.fr (QUILLET Jean-Charles) Date: Wed, 12 May 2010 10:11:40 +0200 Subject: [C++-sig] Trouble converting data from numpy.array to c++ In-Reply-To: <87r5liqw4r.fsf@grizzly.selfip.net> References: <06A7AD4E92172446ADDEB44F8A5D5C101CAF8F453A@ARE01.alyotech.fr> <87r5liqw4r.fsf@grizzly.selfip.net> Message-ID: <06A7AD4E92172446ADDEB44F8A5D5C101CAF8F453D@ARE01.alyotech.fr> Thanks Andreas, I've found out my solution in pyrap source code (pyrap.googlecode.com). I post it here as it can be usefull for others: #include int type = PyArray_TYPE(anArray.ptr()); for (unsigned i = 0; i < 3; ++ i) { void * buffer = PyArray_GETPTR1(anArray.ptr(), i); double val = 0; switch (type) { case NPY_BOOL: val = (*(::npy_bool*)buffer != 0); break; case NPY_INT8: val = int(*(::npy_int8*)buffer); break; case NPY_UINT8: val = uint(*(::npy_uint8*)buffer); break; case NPY_INT16: val = int(*(::npy_int16*)buffer); break; case NPY_UINT16: val = uint(*(::npy_int16*)buffer); break; case NPY_INT32: val = int(*(::npy_int32*)buffer); break; case NPY_UINT32: val = uint(*(::npy_int32*)buffer); break; case NPY_INT64: val = uint(*(::npy_int64*)buffer); break; case NPY_UINT64: val = uint(*(::npy_int64*)buffer); break; case NPY_FLOAT32: val = float(*(::npy_float32*)buffer); break; case NPY_FLOAT64: val = double(*(::npy_float64*)buffer); break; } } Jean-Charles -----Message d'origine----- De?: Andreas Kloeckner [mailto:lists at informa.tiker.net] Envoy??: mardi 11 mai 2010 17:46 ??: QUILLET Jean-Charles; 'cplusplus-sig at python.org' Objet?: Re: [C++-sig] Trouble converting data from numpy.array to c++ Hi Jean-Charles, On Tue, 11 May 2010 11:32:08 +0200, QUILLET Jean-Charles wrote: > I've got this problem I cannot solve. I've a c++ application from which I create an array sending to python this string: > > anArray = numpy.array((1, 2, 3)) > > After extracting the symbol "anArray" from the directory in a boost::python::object, I'm trying to extract the values in C++: > > int val = bp::extract(anArray[0]); > > It doesn't work and raise the error: This is probably not what you wanted to hear, but my PyUblas package solves your problem. :) http://mathema.tician.de/software/pyublas HTH, Andreas From nat at lindenlab.com Wed May 12 14:16:54 2010 From: nat at lindenlab.com (Nat Goodspeed) Date: Wed, 12 May 2010 08:16:54 -0400 Subject: [C++-sig] When not to use static boost python library In-Reply-To: <4D860A08F2325E4CB3D482A3F2EB99048937911F@BL2PRD0102MB009.prod.exchangelabs.com> References: <4D860A08F2325E4CB3D482A3F2EB99048937911F@BL2PRD0102MB009.prod.exchangelabs.com> Message-ID: <4BEA9C36.40900@lindenlab.com> Mr Kun Hong wrote: > The lines in the documentation is: > > ''' > It might be appropriate to use the static Boost.Python library in any of the following cases: > > * You are extending python and the types exposed in your dynamically-loaded extension module > don't need to be used by any other Boost.Python extension modules, and you don't care if the > core library code is duplicated among them. > ... > ''' > > I guess > what the problem is should be that: another python extension, written in boost python, > cannot access the types and functions exposed in my current extension. I am not > clear why is it so, becaused it is exposed to the python interpreter already. If there are > two python extensions, both written in pure C, can't they access each other's type and > functions? Suppose you develop two different Python extensions, each statically linked with Boost.Python. The situation looks like this: Ext1 | Boost.Python / Python interpreter \ Ext2 | Boost.Python You have two different copies of Boost.Python in memory at the same time. Presumably Boost.Python contains certain static data used to manage registering types to the Python interpreter. I guess the documentation is saying that if Ext2 tries to reference types defined in Ext1, it might confuse Boost.Python's static data. Contrast with dynamic linking: Ext1 / \ Python Boost.Python interpreter / \ / Ext2 Having a single copy of the Boost.Python static data eliminates the potential for confusion. From kun.hong at uqconnect.edu.au Thu May 13 16:22:52 2010 From: kun.hong at uqconnect.edu.au (Kun Hong) Date: Fri, 14 May 2010 00:22:52 +1000 Subject: [C++-sig] When not to use static boost python library In-Reply-To: <4BEA9C36.40900@lindenlab.com> References: <4D860A08F2325E4CB3D482A3F2EB99048937911F@BL2PRD0102MB009.prod.exchangelabs.com> <4BEA9C36.40900@lindenlab.com> Message-ID: <4BEC0B3C.5080001@uqconnect.edu.au> Nat Goodspeed wrote: >> >> I guess >> what the problem is should be that: another python extension, written >> in boost python, >> cannot access the types and functions exposed in my current >> extension. I am not >> clear why is it so, becaused it is exposed to the python interpreter >> already. If there are >> two python extensions, both written in pure C, can't they access each >> other's type and >> functions? > > Suppose you develop two different Python extensions, each statically > linked with Boost.Python. The situation looks like this: > > Ext1 | Boost.Python > / > Python > interpreter > \ > Ext2 | Boost.Python > > You have two different copies of Boost.Python in memory at the same > time. Presumably Boost.Python contains certain static data used to > manage registering types to the Python interpreter. I guess the > documentation is saying that if Ext2 tries to reference types defined in > Ext1, it might confuse Boost.Python's static data. > > Contrast with dynamic linking: > > Ext1 > / \ > Python Boost.Python > interpreter / > \ / > Ext2 > > Having a single copy of the Boost.Python static data eliminates the > potential for confusion. > Thanks for the explantion. I think I understand better now. So I think as long as I don't need to share this static registation information between two boost python modules (in a sense, that these modules are self-contained and just use python types), it is ok to use static boost python lib. From talljimbo at gmail.com Fri May 14 02:53:45 2010 From: talljimbo at gmail.com (Jim Bosch) Date: Thu, 13 May 2010 17:53:45 -0700 Subject: [C++-sig] Miscellaneous Boost.Python Utilities Message-ID: <4BEC9F19.9020300@gmail.com> In the hope that others might find them useful, and perhaps contribute some of their own, I've posted a collection of miscellaneous Boost.Python extensions I've been using at the Boost sandbox: http://svn.boost.org/svn/boost/sandbox/python_extensions Features include: - Conversions between STL containers and Boost.Range iterator ranges and Python builtins like list, tuple, and dict. - Conversions between Boost.Fusion sequences and Python tuples. - A third solution to the const shared_ptr problem (see http://language-binding.net/pyplusplus/troubleshooting_guide/shared_ptr/shared_ptr.html) that isn't evil (I don't think) and doesn't involve changing the Boost.Python library itself - it simply doesn't use register_ptr_to_python to register the conversion. More info is available in the README file. Also, I'm sure many of the regulars on this list have their own similar extensions, and I'd love to see them collected in one place. If you'd like to add your collection to mine, or have a suggestion for some other place I should put my collection, please let me know. I would like to limit this collection to simple things that don't depend on non-Boost libraries, however. Jim Bosch From wangx0802 at yahoo.com Sun May 16 22:17:44 2010 From: wangx0802 at yahoo.com (X Wang) Date: Sun, 16 May 2010 13:17:44 -0700 (PDT) Subject: [C++-sig] How to expose to a python user defined type with to/from_python convertors? In-Reply-To: <351566.3139.qm@web112604.mail.gq1.yahoo.com> Message-ID: <405902.42251.qm@web112603.mail.gq1.yahoo.com> How do I expose my C++ type to a user defined python type? I think I could use boost python class_<> wrapper to wrap around, but if I want to use to_python/from_python convertors? Say I want to expose my C++ class(CA) to a python type(PA): C++: class CA { public: //ctors, accessors... private: std::string name_; int value_; }; Python: class PA(object): def __init__(self, name, value): self.name=name self.value=value If I use, void initializeConverters() { using namespace boost::python; to_python_converter(); } with struct CA_to_PA { static PyObject* convert(CA const& s) { printf("In C++ -> python converter?\n"); std::string name=s.name(); int value=s.value(); #if 0 static boost::python::object obj; PyObject* obj_ptr=obj.ptr(); PyObject_SetAttrString(obj_ptr, "name", PyString_FromString(name.c_str())); PyObject_SetAttrString(obj_ptr, "value", PyInt_FromLong(value)); return boost::python::incref(obj_ptr); #else return boost::python::incref( //PyInt_FromLong(1999) Py_BuildValue("s", "native string works") //Py_BuildValue("{s:i,s:i}", "abc", 123, "def", 456) ); #endif } }; How do I create a boost::python::object (or PyObject*) with "name" and "value" attributes? I could get creation and return of python native types to work, but can not get user defined types returned to the python side -- always get NoneType returned. boost::python::object C2Py() { printf("Pre assignment\n"); #if 0 boost::python::object obj(new CA("From C++ to python", 987654321)); PyObject* o=obj.ptr(); #else CA a("From C++ to python", 987654321); PyObject *o=CA_to_PA::convert(a); #endif printf("Post assignment\n"); PyObject *tmp=NULL; std::string name="No Name"; int value=-1; tmp=PyObject_GetAttrString(o, "name"); if (tmp!=NULL) name=PyString_AsString(tmp); tmp=PyObject_GetAttrString(o, "value"); if (tmp!=NULL) value=PyInt_AsLong(tmp); printf("(%s, %d)\n", name.c_str(), value); return obj; } $ python Python 2.5.1 (r251:54863, Oct 5 2007, 11:16:46) [GCC 4.1.1 20070105 (Red Hat 4.1.1-53)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import mytypes >>> pa=mytypes.C2Py() ("No Name", -1) >>>type(pa) NoneType Any help is appreciated, thanks! From diepen at astron.nl Mon May 17 09:34:00 2010 From: diepen at astron.nl (Ger van Diepen) Date: Mon, 17 May 2010 09:34:00 +0200 Subject: [C++-sig] Trouble converting data from numpy.array to c++ In-Reply-To: <06A7AD4E92172446ADDEB44F8A5D5C101CAF8F453B@ARE01.alyotech.fr> References: <4BE94C91020000A90000BDFB@smtp.nfra.nl> <06A7AD4E92172446ADDEB44F8A5D5C101CAF8F453B@ARE01.alyotech.fr> Message-ID: <4BF10D88020000A90000C0DC@smtp.nfra.nl> Hi Jean-Charles, Sorry for answering a bit late. I was on vacation. I don't know an easier way. You can take from the code what you need and adapt it as needed. You could also look at Jim Bosch's tools he mentioned in a very recent posting. Cheers, Ger >>> QUILLET Jean-Charles 5/11/2010 2:32 PM >>> Thanks Ger, I'll check out this, it seems very interesting. But I'm confused because what I'm describing is working without any problem on windows using Visual Studio 2005 python 2.5 and boost 1.34 But for some reason I cant use these versions on Linux because of an incompatibility between Qt and Python 25 http://bugs.python.org/issue1086854 This is library hell ! Despite this, I'll check out the converters, but it seems a bit overkill for me. Whould you know an easy way to retrieve my values from the array ? Thanks in advance, Jean-Charles De : Ger van Diepen [mailto:diepen at astron.nl] Envoy? : mardi 11 mai 2010 12:25 ? : QUILLET Jean-Charles; cplusplus-sig at python.org Objet : [Spam Probable] Re: [C++-sig] Trouble converting data from numpy.array to c++ Hi Jean-Charles, AFAIK there are no standard converters for the numpy scalar types to C++ types. In pyrap it is solved by having explicit converters for numpy scalar types to C++ types (see pyrap.googlecode.com). Cheers, Ger >>> QUILLET Jean-Charles 05/11/10 11:38 AM >>> Hi, I've got this problem I cannot solve. I've a c++ application from which I create an array sending to python this string: anArray = numpy.array((1, 2, 3)) After extracting the symbol "anArray" from the directory in a boost::python::object, I'm trying to extract the values in C++: int val = bp::extract(anArray[0]); It doesn't work and raise the error: "TypeError: No registered converter was able to produce a C++ rvalue of type int from this Python object of type numpy.int32" What is going on ? I'm using python 2.6, last version of Numpy 1.4.1 with boost 1.38 on Linux. Looking on the list, it seems that someone had this very problem but he didn't get any answer. http://article.gmane.org/gmane.comp.python.c++/11279/match=typeerror+no+registered+converter+able+produce+c%2b%2b+rvalue+type+int+python+object+numpy+int32 Hopefully I'll have more chance ! Any idea greatly appreciated, Jean-Charles -------------- next part -------------- An HTML attachment was scrubbed... URL: From kun.hong at uqconnect.edu.au Mon May 17 15:41:14 2010 From: kun.hong at uqconnect.edu.au (Mr Kun Hong) Date: Mon, 17 May 2010 13:41:14 +0000 Subject: [C++-sig] RuntimeError(Pure virtual function called) when using iterator with abstract class Message-ID: <4D860A08F2325E4CB3D482A3F2EB990489379985@BL2PRD0102MB009.prod.exchangelabs.com> Hi all, I am new to Booth Python. I am trying to make a python wrapper for one of my job's C++ library. But after reading the docs and related mailing list archive, I am still stucked. :( If any one can shed some light on me, it is greatly appreciated. Below is a simplified version of the library I am trying to wrap. The design is basically an abstract class A (with factory method to return implementation instance) which provides an iterator interface. The iterator also returns an abstract class Position instances, which is supposed to be implemented by subclasses of A. Please see be code (it is a bit long, thanks for you patience). === test.h: #include #include #include class A { public: class Position { public: virtual ~Position() {}; virtual bool exists() const = 0; virtual int getId() const = 0; }; // forward declaration class AIterator; class AIteratorBase : public std::iterator { public: virtual ~AIteratorBase() {} virtual boost::shared_ptr clone() const = 0; virtual reference operator*() const = 0; virtual pointer operator->() const = 0; virtual void increment() = 0; virtual void decrement() = 0; virtual bool operator==(const AIterator& other) const = 0; }; class AIterator : public std::iterator { public: AIterator(boost::shared_ptr& priv) : mPriv(priv) { } AIterator(const AIterator& other) : mPriv(other.mPriv->clone()) { } AIterator& operator=(const AIterator& other) { boost::shared_ptr clone = other.mPriv->clone(); mPriv = clone; return *this; } reference operator*() const { return mPriv->operator*(); } pointer operator->() const { return mPriv->operator->(); } AIterator& operator++() { mPriv->increment(); return *this; } AIterator operator++(int) { boost::shared_ptr privClone = mPriv->clone(); AIterator toReturn(privClone); mPriv->increment(); return toReturn; } AIterator& operator--() { mPriv->decrement(); return *this; } AIterator operator--(int) { boost::shared_ptr privClone = mPriv->clone(); AIterator toReturn(privClone); mPriv->decrement(); return toReturn; } bool operator==(const AIterator& other) const { return mPriv->operator==(other); } bool operator!=(const AIterator& other) const { return !operator==(other); } private: boost::shared_ptr mPriv; }; static boost::shared_ptr getAInstance(int type); virtual AIterator begin(int row) const = 0; virtual AIterator end(int row) const = 0; }; class AOdd : public A { public: class Position : public A::Position { public: Position(int id) : mId(id) {} virtual ~Position() {} virtual bool exists() const { return (mId % 2 != 0); } virtual int getId() const { return mId; } int mId; }; class AOddIterator : public A::AIteratorBase { public: AOddIterator(int pos) : mPos(pos) {} virtual ~AOddIterator() {} virtual boost::shared_ptr clone() const { return boost::shared_ptr( new AOddIterator(*this)); } virtual reference operator*() const { return mPos; } virtual pointer operator->() const { return &mPos; } virtual void increment() { mPos.mId++; } virtual void decrement() { mPos.mId--; } virtual bool operator==(const AIterator& other) const { return mPos.getId() == (*other).getId(); } private: Position mPos; }; virtual AIterator begin(int row) const { boost::shared_ptr iterPriv (new AOdd::AOddIterator(row * 1024)); return A::AIterator(iterPriv); } virtual AIterator end(int row) const { boost::shared_ptr iterPriv (new AOdd::AOddIterator((row + 1) * 1024)); return A::AIterator(iterPriv); } }; boost::shared_ptr A::getAInstance(int type) { // return an instance base on type value, but only return ADerived here. static boost::shared_ptr aInstance(new AOdd()); return aInstance; } === test.cpp (wrapping code) #include "test.h" #include using namespace boost::python; boost::python::object aIterator(A const &a, int row) { return //range, A>( range, A>( boost::bind(&A::begin, _1, row), boost::bind(&A::end, _1, row) )(boost::ref(a)); } class PositionWrap : public A::Position, public wrapper { public: virtual int getId() { return this->get_override("getId")(); } virtual bool exists() { return this->get_override("exists")(); } }; BOOST_PYTHON_MODULE(test) { scope as = class_, boost::noncopyable> ("A", no_init) .def("getAInstance", &A::getAInstance) .staticmethod("getAInstance") .def("getIterator", &aIterator) ; class_ ("Position", no_init) .def("exists", pure_virtual(&A::Position::exists)) .def("getId", pure_virtual(&A::Position::getId)) ; } === main.cpp (code for demonstrating the use of the C++ library) #include "test.h" #include using namespace std; int main() { A *a = (A::getAInstance(0)).get(); for(A::AIterator begin = a->begin(0); begin != a->end(0); begin++) { cout << begin->exists() << endl; } return 0; } === test.py (Code for tesing the python extension) from test import * a = A.getAInstance(0) for x in a.getIterator(0): #print type(x) print x.exists() === end Thanks a lot for you patience, since you read this far... So now, I run the test.py code, which is fine if I just "print type(x)" which did print correctly as "". But when I tried to do "print x.exists()", it gave this error: Traceback (most recent call last): File "test.py", line 6, in print x.exists() RuntimeError: Pure virtual function called It is supposed to call the solid subclass AOdd::Position code, just like the C++ main.cpp code does. Why it doesn't work? It could be something very simple and stupid, that I did wrong. If you spot it, please help me. Thanks a lot! Kun From kun.hong at uqconnect.edu.au Mon May 17 15:58:48 2010 From: kun.hong at uqconnect.edu.au (Mr Kun Hong) Date: Mon, 17 May 2010 13:58:48 +0000 Subject: [C++-sig] RuntimeError(Pure virtual function called) when using iterator with abstract class Message-ID: <4D860A08F2325E4CB3D482A3F2EB9904893799C1@BL2PRD0102MB009.prod.exchangelabs.com> Hi all, I am new to Booth Python. I am trying to make a python wrapper for one of my job's C++ library. But after reading the docs and related mailing list archive, I am still stucked. :( If any one can shed some light on me, it is greatly appreciated. Below is a simplified version of the library I am trying to wrap. The design is basically an abstract class A (with factory method to return implementation instance) which provides an iterator interface. The iterator also returns an abstract class Position instances, which is supposed to be implemented by subclasses of A. Please see be code (it is a bit long, thanks for you patience). === test.h: #include #include #include class A { public: class Position { public: virtual ~Position() {}; virtual bool exists() const = 0; virtual int getId() const = 0; }; // forward declaration class AIterator; class AIteratorBase : public std::iterator { public: virtual ~AIteratorBase() {} virtual boost::shared_ptr clone() const = 0; virtual reference operator*() const = 0; virtual pointer operator->() const = 0; virtual void increment() = 0; virtual void decrement() = 0; virtual bool operator==(const AIterator& other) const = 0; }; class AIterator : public std::iterator { public: AIterator(boost::shared_ptr& priv) : mPriv(priv) { } AIterator(const AIterator& other) : mPriv(other.mPriv->clone()) { } AIterator& operator=(const AIterator& other) { boost::shared_ptr clone = other.mPriv->clone(); mPriv = clone; return *this; } reference operator*() const { return mPriv->operator*(); } pointer operator->() const { return mPriv->operator->(); } AIterator& operator++() { mPriv->increment(); return *this; } AIterator operator++(int) { boost::shared_ptr privClone = mPriv->clone(); AIterator toReturn(privClone); mPriv->increment(); return toReturn; } AIterator& operator--() { mPriv->decrement(); return *this; } AIterator operator--(int) { boost::shared_ptr privClone = mPriv->clone(); AIterator toReturn(privClone); mPriv->decrement(); return toReturn; } bool operator==(const AIterator& other) const { return mPriv->operator==(other); } bool operator!=(const AIterator& other) const { return !operator==(other); } private: boost::shared_ptr mPriv; }; static boost::shared_ptr getAInstance(int type); virtual AIterator begin(int row) const = 0; virtual AIterator end(int row) const = 0; }; class AOdd : public A { public: class Position : public A::Position { public: Position(int id) : mId(id) {} virtual ~Position() {} virtual bool exists() const { return (mId % 2 != 0); } virtual int getId() const { return mId; } int mId; }; class AOddIterator : public A::AIteratorBase { public: AOddIterator(int pos) : mPos(pos) {} virtual ~AOddIterator() {} virtual boost::shared_ptr clone() const { return boost::shared_ptr( new AOddIterator(*this)); } virtual reference operator*() const { return mPos; } virtual pointer operator->() const { return &mPos; } virtual void increment() { mPos.mId++; } virtual void decrement() { mPos.mId--; } virtual bool operator==(const AIterator& other) const { return mPos.getId() == (*other).getId(); } private: Position mPos; }; virtual AIterator begin(int row) const { boost::shared_ptr iterPriv (new AOdd::AOddIterator(row * 1024)); return A::AIterator(iterPriv); } virtual AIterator end(int row) const { boost::shared_ptr iterPriv (new AOdd::AOddIterator((row + 1) * 1024)); return A::AIterator(iterPriv); } }; boost::shared_ptr A::getAInstance(int type) { // return an instance base on type value, but only return ADerived here. static boost::shared_ptr aInstance(new AOdd()); return aInstance; } === test.cpp (wrapping code) #include "test.h" #include using namespace boost::python; boost::python::object aIterator(A const &a, int row) { return range, A>( boost::bind(&A::begin, _1, row), boost::bind(&A::end, _1, row) )(boost::ref(a)); } class PositionWrap : public A::Position, public wrapper { public: virtual int getId() { return this->get_override("getId")(); } virtual bool exists() { return this->get_override("exists")(); } }; BOOST_PYTHON_MODULE(test) { scope as = class_, boost::noncopyable> ("A", no_init) .def("getAInstance", &A::getAInstance) .staticmethod("getAInstance") .def("getIterator", &aIterator) ; class_ ("Position", no_init) .def("exists", pure_virtual(&A::Position::exists)) .def("getId", pure_virtual(&A::Position::getId)) ; } === main.cpp (code for demonstrating the use of the C++ library) #include "test.h" #include using namespace std; int main() { A *a = (A::getAInstance(0)).get(); for(A::AIterator begin = a->begin(0); begin != a->end(0); begin++) { cout << begin->exists() << endl; } return 0; } === test.py (Code for tesing the python extension) from test import * a = A.getAInstance(0) for x in a.getIterator(0): #print type(x) print x.exists() === end Thanks a lot for you patience, since you read this far... So now, I run the test.py code, which is fine if I just "print type(x)" which did print correctly as "". But when I tried to do "print x.exists()", it gave this error: Traceback (most recent call last): File "test.py", line 6, in print x.exists() RuntimeError: Pure virtual function called It is supposed to call the solid subclass AOdd::Position code, just like the C++ main.cpp code does. Why it doesn't work? It could be something very simple and stupid, that I did wrong. If you spot it, please help me. Thanks a lot! Kun From seefeld at sympatico.ca Mon May 17 16:19:43 2010 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Mon, 17 May 2010 10:19:43 -0400 Subject: [C++-sig] RuntimeError(Pure virtual function called) when using iterator with abstract class In-Reply-To: <4D860A08F2325E4CB3D482A3F2EB990489379985@BL2PRD0102MB009.prod.exchangelabs.com> References: <4D860A08F2325E4CB3D482A3F2EB990489379985@BL2PRD0102MB009.prod.exchangelabs.com> Message-ID: <4BF1507F.50809@sympatico.ca> On 05/17/2010 09:41 AM, Mr Kun Hong wrote: > class A > { > public: > > class Position > { > public: > virtual ~Position() {}; > virtual bool exists() const = 0; > virtual int getId() const = 0; > }; > A::Position is thus an abstract base class. > class PositionWrap : public A::Position, public wrapper > { > public: > virtual int getId() { return this->get_override("getId")(); } > virtual bool exists() { return this->get_override("exists")(); } > }; > You derive PositionWrap from the above, but allow for the base class' "exists()" to be called (if it wasn't provided by a Python derived class). I think what you may do instead is this: class PositionWrap : public A::Position, public wrapper { public: bool default_exists(); virtual bool exists() { override exists = this->get_override("exists"); if (exists) return exists(); else return default_exists(); } ... }; This makes sure you have a valid fallback, so the base class' "exists()" is never called. Stefan -- ...ich hab' noch einen Koffer in Berlin... From cameron.royal at gmail.com Tue May 18 15:23:32 2010 From: cameron.royal at gmail.com (cammm) Date: Tue, 18 May 2010 06:23:32 -0700 (PDT) Subject: [C++-sig] Callbacks in PyBindGen Message-ID: <28595532.post@talk.nabble.com> So I've just started playing with pybindgen and having used pyobject plus and boost::python before I have to say, it is a breathe of fresh air. Simple, explicit, fast and written in python. I'm not a fan of automatic coverage / source code parsing, explicit description of the python interface doesn't take that long and validates what you are doing. It is also nice not having thousands of files and a large build process as you get with boost, when all you want is a nice way to do python bindings. Many thanks to you Gustavo for a brilliant project and I encourage everyone to try it out and get behind him to help develop it. One thing I would really like to see go in is callback methods. Was there anything technical blocking this or just a case of haven't gotten to it yet? You mention in the docs it is easy to working around with some extra code, do you have a simple example of that working? Again, kudos and thanks :-) - Cameron -- View this message in context: http://old.nabble.com/Callbacks-in-PyBindGen-tp28595532p28595532.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From amont65 at hotmail.fr Tue May 18 21:44:52 2010 From: amont65 at hotmail.fr (alexandre monti) Date: Tue, 18 May 2010 21:44:52 +0200 Subject: [C++-sig] Boost.Python Problem Message-ID: Hello ! I've just installed Boost.Python on my iMac intel, but i've a bug. This is the code : object main_module((handle<>(borrowed(PyImport_AddModule("__main__"))))); object main_namespace = main_module.attr("__dict__"); As you can see it is very, very, very minimal and simple, and i've an exception from the second line : "TypeError: Argument name must be string, not 'str'" (please forgive my bad English, i'm French) Thanks ! _________________________________________________________________ La bo?te mail NOW G?n?ration vous permet de r?unir toutes vos bo?tes mail dans Hotmail ! http://www.windowslive.fr/hotmail/nowgeneration/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From Paul_Kunz at slac.stanford.edu Wed May 19 01:00:53 2010 From: Paul_Kunz at slac.stanford.edu (Paul F. Kunz) Date: Tue, 18 May 2010 16:00:53 -0700 Subject: [C++-sig] Trouble converting data from numpy.array to c++ In-Reply-To: <4BE94C91020000A90000BDFB@smtp.nfra.nl> (diepen@astron.nl) References: <4BE94C91020000A90000BDFB@smtp.nfra.nl> Message-ID: <201005182300.o4IN0r6v011781@ki-ls01.slac.stanford.edu> >>>>> On Tue, 11 May 2010 12:24:42 +0200, "Ger van Diepen" said: > AFAIK there are no standard converters for the numpy scalar types to > C++ types. In pyrap it is solved by having explicit converters for > numpy scalar types to C++ types (see pyrap.googlecode.com). > Cheers, Ger Could you narrow this down a bit, i.e. which file(s) to look at. From gjcarneiro at gmail.com Wed May 19 11:36:07 2010 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Wed, 19 May 2010 10:36:07 +0100 Subject: [C++-sig] Callbacks in PyBindGen In-Reply-To: <28595532.post@talk.nabble.com> References: <28595532.post@talk.nabble.com> Message-ID: On Tue, May 18, 2010 at 14:23, cammm wrote: > > So I've just started playing with pybindgen and having used pyobject plus > and > boost::python before I have to say, it is a breathe of fresh air. > > Simple, explicit, fast and written in python. I'm not a fan of automatic > coverage / source code parsing, explicit description of the python > interface > doesn't take that long and validates what you are doing. It is also nice > not > having thousands of files and a large build process as you get with boost, > when all you want is a nice way to do python bindings. > > Many thanks to you Gustavo for a brilliant project and I encourage everyone > to try it out and get behind him to help develop it. > Thanks, glad you like it :-) > > One thing I would really like to see go in is callback methods. Was there > anything technical blocking this or just a case of haven't gotten to it > yet? > You mention in the docs it is easy to working around with some extra code, > do you have a simple example of that working? > I have not enough time and motivation. And I'm not sure how much effort it would take. There is some callback support code in NS-3 (www.nsnam.org), but in NS-3 "callbacks" are actually objects. It's not a very simple example to learn from. I will try to come up with a simple example soon. > > Again, kudos and thanks :-) > - > Cameron > -- > View this message in context: > http://old.nabble.com/Callbacks-in-PyBindGen-tp28595532p28595532.html > Sent from the Python - c++-sig mailing list archive at Nabble.com. > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Gustavo J. A. M. Carneiro INESC Porto, UTM, WiN, http://win.inescporto.pt/gjc "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From gjcarneiro at gmail.com Wed May 19 13:02:18 2010 From: gjcarneiro at gmail.com (Gustavo Carneiro) Date: Wed, 19 May 2010 12:02:18 +0100 Subject: [C++-sig] Callbacks in PyBindGen In-Reply-To: References: <28595532.post@talk.nabble.com> Message-ID: Example: http://bazaar.launchpad.net/~gjc/pybindgen/trunk/revision/766 (don't forget the previous revision 765 for a bug fix) On Wed, May 19, 2010 at 10:36, Gustavo Carneiro wrote: > > > On Tue, May 18, 2010 at 14:23, cammm wrote: > >> >> So I've just started playing with pybindgen and having used pyobject plus >> and >> boost::python before I have to say, it is a breathe of fresh air. >> >> Simple, explicit, fast and written in python. I'm not a fan of automatic >> coverage / source code parsing, explicit description of the python >> interface >> doesn't take that long and validates what you are doing. It is also nice >> not >> having thousands of files and a large build process as you get with boost, >> when all you want is a nice way to do python bindings. >> >> Many thanks to you Gustavo for a brilliant project and I encourage >> everyone >> to try it out and get behind him to help develop it. >> > > Thanks, glad you like it :-) > > >> >> One thing I would really like to see go in is callback methods. Was there >> anything technical blocking this or just a case of haven't gotten to it >> yet? >> You mention in the docs it is easy to working around with some extra code, >> do you have a simple example of that working? >> > > I have not enough time and motivation. And I'm not sure how much effort it > would take. > > There is some callback support code in NS-3 (www.nsnam.org), but in NS-3 > "callbacks" are actually objects. It's not a very simple example to learn > from. > > I will try to come up with a simple example soon. > > >> >> Again, kudos and thanks :-) >> - >> Cameron >> -- >> View this message in context: >> http://old.nabble.com/Callbacks-in-PyBindGen-tp28595532p28595532.html >> Sent from the Python - c++-sig mailing list archive at Nabble.com. >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> > > > > -- > Gustavo J. A. M. Carneiro > INESC Porto, UTM, WiN, http://win.inescporto.pt/gjc > "The universe is always one step beyond logic." -- Frank Herbert > -- Gustavo J. A. M. Carneiro INESC Porto, UTM, WiN, http://win.inescporto.pt/gjc "The universe is always one step beyond logic." -- Frank Herbert -------------- next part -------------- An HTML attachment was scrubbed... URL: From thomasd57 at yahoo.com Thu May 20 03:23:46 2010 From: thomasd57 at yahoo.com (Thomas Daniel) Date: Wed, 19 May 2010 18:23:46 -0700 Subject: [C++-sig] Problem with overloading between int and double Message-ID: <4BF48F22.9090502@yahoo.com> I have a class with "create" methods for different types of arguments: #include class Test { public: Test() {} Test(int x) : _type(x) {} static Test create(int) { return Test(0); } static Test create(double) { return Test(1); } static Test create(const char*) { return Test(2); } int get_type() const { return _type; } private: int _type; }; using namespace boost::python; BOOST_PYTHON_MODULE(Test) { class_("Test") .def("create", (Test (*)(int)) &Test::create) .def("create", (Test (*)(double)) &Test::create) .def("create", (Test (*)(const char*)) &Test::create) .def("get_type", &Test::get_type) .staticmethod("create") ; } The python wrappers created by boost don't seem to distinguish between create(int) and create(double), although they do notice create(const char*): >>> from Test import * >>> x = Test.create(0) >>> print x.get_type() 1 >>> x = Test.create(0.0) >>> print x.get_type() 1 >>> y = Test.create("hi") >>> print y.get_type() 2 Any ideas what I do wrong? From talljimbo at gmail.com Thu May 20 03:41:52 2010 From: talljimbo at gmail.com (Jim Bosch) Date: Wed, 19 May 2010 18:41:52 -0700 Subject: [C++-sig] Problem with overloading between int and double In-Reply-To: <4BF48F22.9090502@yahoo.com> References: <4BF48F22.9090502@yahoo.com> Message-ID: <4BF49360.80707@gmail.com> On 05/19/2010 06:23 PM, Thomas Daniel wrote: > The python wrappers created by boost don't seem to distinguish between > create(int) and create(double), although they do notice create(const char*): > > > Any ideas what I do wrong? > > You aren't doing anything wrong. The problem is that the boost.python overload matcher for double also matches integers (I think it probably should, generally), and that's getting tried first; only when the first overload fails does Boost.Python try another. A workaround is to change the order to expose them (the last one exposed gets tried first): BOOST_PYTHON_MODULE(Test) { class_("Test") .def("create", (Test (*)(double))&Test::create) .def("create", (Test (*)(int))&Test::create) .def("create", (Test (*)(const char*))&Test::create) .def("get_type",&Test::get_type) .staticmethod("create") ; } I admit it's not a very satisfying solution. Maybe someone else has a better idea, but I think fixing this in Boost.Python would require a lot of work. Jim Bosch From rwgk at yahoo.com Thu May 20 04:56:42 2010 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Wed, 19 May 2010 19:56:42 -0700 (PDT) Subject: [C++-sig] Problem with overloading between int and double In-Reply-To: <4BF49360.80707@gmail.com> References: <4BF48F22.9090502@yahoo.com> <4BF49360.80707@gmail.com> Message-ID: <33890.28322.qm@web111402.mail.gq1.yahoo.com> > I admit it's not a very satisfying solution. Maybe someone else has a better idea, > but I think fixing this in Boost.Python would require a lot of work. Troy did the work already but it isn't in the trunk. IIUC the additional price is increased extension sizes (but Troy's changes seem to go far beyond an enhanced overload mechanism so it is difficult to tell). There is another way to achieve what you want: just do the overload resolution yourself. Wrap a function that takes boost::python::object as the argument, then use extract<> or the raw Python API to inspect the object and call the best C++ overload. Ralf From kun.hong at uqconnect.edu.au Thu May 20 15:03:41 2010 From: kun.hong at uqconnect.edu.au (Kun Hong) Date: Thu, 20 May 2010 23:03:41 +1000 Subject: [C++-sig] How to wrap reference to abstract base class to get C++ polymorphism Message-ID: <294D37C7-573E-43E6-92A3-2621FB236846@uqconnect.edu.au> Hi, I have a maybe entry-level question. But it really troubled me for a while. I need to wrap a C++ library which has some factory method to return references to some abstract base classes. So the implementation is hidden. If I want a python equivalent to the abstract base class reference type (so the polymorphism can work), how should I do it? Please see a very simplified example below which demonstrates what I mean. === test.cpp (wrapper and library code) #include using namespace boost::python; class B { public: virtual const char *getName() const = 0; }; class C : public B { public: virtual const char *getName() const { return "Class C"; } }; const B& getB() { static boost::shared_ptr b(new C); return (*b.get()); } BOOST_PYTHON_MODULE(Test) { class_ ("B", no_init) .def("getName", pure_virtual(&B::getName)) ; def("getB", &getB, return_value_policy()); } === main.cpp (C++ test code) #include "test.h" #include #include using namespace std; int main() { printf("%s\n", getB().getName()); return 0; } === test.py (python test code utilize the C++ extension) from Test import * print getB().getName() === end Using the above code, when I run test.py, it gives the below error: Traceback (most recent call last): File "test.py", line 3, in getB().getName() RuntimeError: Pure virtual function called Then I tried changin this line def("getB", &getB, return_value_policy()); to: def("getB", &getB, return_value_policy()); Then when I run test.py, the error becomes: Traceback (most recent call last): File "test.py", line 3, in getB().getName() TypeError: No to_python (by-value) converter found for C++ type: B What is the correct way to wrap this abstract class reference to the the polymorphic behavior? Thanks in advance. Kun From brokenn at gmail.com Fri May 21 12:05:31 2010 From: brokenn at gmail.com (Brian O'Kennedy) Date: Fri, 21 May 2010 11:05:31 +0100 Subject: [C++-sig] [Py++] Undefined symbols for static const int member of class Message-ID: Hi, I'm wrapping a very simple class using PyPlusPlus. ---- snip ---- class testclass { public: static const int a = 99; }; ---- /snip ---- And Py++ exposes the static member with a def_readonly("a", testclass::a ) call. When I import this module into python I get an undefined symbol for testclass::a. ImportError: ./testwrapper.so: undefined symbol: _ZN9testclass1aE I understand that the static member has no storage and thus boost python cannot find the address of it. However, I was hoping there is a way to solve this kind of (common?) problem using Py++ to perhaps generate a getter function or something similar. How would one do this? thanks, Brian -------------- next part -------------- An HTML attachment was scrubbed... URL: From roman.yakovenko at gmail.com Fri May 21 13:49:12 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 21 May 2010 14:49:12 +0300 Subject: [C++-sig] [Py++] Undefined symbols for static const int member of class In-Reply-To: References: Message-ID: On Fri, May 21, 2010 at 1:05 PM, Brian O'Kennedy wrote: > Hi, > I'm wrapping a very simple class using PyPlusPlus. > ---- snip ---- > ?class testclass > ?{ > ??public: > ?? static const int a = 99; > ?}; > ---- /snip ---- > And Py++ exposes the static member with a def_readonly("a", testclass::a ) > call. When I import this module into python I get an undefined symbol for > testclass::a. > ??ImportError: ./testwrapper.so: undefined symbol: _ZN9testclass1aE > I understand that the static member has no storage and thus boost python > cannot find the address of it. However, I was hoping there is a way to solve > this kind of (common?) problem using Py++ to perhaps generate a getter > function or something similar. How would one do this? Try to move "a" initialization to a .cpp file. I also suggest you to take a look on Py++ unittests, especially on "statics_tester.py" and "statics_to_be_exported.[c|h]pp" files. They deal with exactly your use case -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From kun.hong at uqconnect.edu.au Fri May 21 13:53:39 2010 From: kun.hong at uqconnect.edu.au (Kun Hong) Date: Fri, 21 May 2010 21:53:39 +1000 Subject: [C++-sig] [Py++] Undefined symbols for static const int member of class In-Reply-To: References: Message-ID: <7A836F4B-48DF-480A-8447-28C5BCC42693@uqconnect.edu.au> On 21/05/2010, at 8:05 PM, Brian O'Kennedy wrote: > Hi, > > I'm wrapping a very simple class using PyPlusPlus. > > ---- snip ---- > class testclass > { > public: > static const int a = 99; > }; > ---- /snip ---- > > And Py++ exposes the static member with a def_readonly("a", > testclass::a ) call. When I import this module into python I get an > undefined symbol for testclass::a. > > ImportError: ./testwrapper.so: undefined symbol: _ZN9testclass1aE > > I understand that the static member has no storage and thus boost > python cannot find the address of it. I think your understanding of static member has no storage is not really correct. What I think is, you have declaration of 'a' but no definition of it. There is no linkage error unless you reference 'a'. The reason is static const data member of integral type initialized with a constant value is treated as a constant expression. So it means it is ok to use 'a' as a constant in your C++ code, but when you reference it in runtime, you need the definition of it, which in your example, you don't have. In your wrapper code put something like this: const int testclass:a; HTH, Kun From brokenn at gmail.com Fri May 21 15:38:53 2010 From: brokenn at gmail.com (Brian O'Kennedy) Date: Fri, 21 May 2010 14:38:53 +0100 Subject: [C++-sig] [Py++] Undefined symbols for static const int member of class In-Reply-To: References: Message-ID: Hi Roman, Thanks for the reply - I've had a look at those unit tests, but were unable to reproduce the same results in my code. I get an error at import: ?"AttributeError: 'Boost.Python.StaticProperty' object attribute '__doc__' is read-only" How do I go about running the Py++ unit tests? As a matter of interest, I'm using Boost 1.42.0, Python 2.6.5, CVS Head of GCCXML, CVS Head of pygccxml, CVS head of pyplusplus. In any case, I don't actually have write access to the library I want to wrap, so I needed another solution. I'm currently simply excluding those static const int members with mb.class_('someclass').variable('myvar').exclude() In that case, the particular class I'm wrapping works fine! Thanks Brian On 21 May 2010 12:49, Roman Yakovenko wrote: > > On Fri, May 21, 2010 at 1:05 PM, Brian O'Kennedy wrote: > > Hi, > > I'm wrapping a very simple class using PyPlusPlus. > > ---- snip ---- > > ?class testclass > > ?{ > > ??public: > > ?? static const int a = 99; > > ?}; > > ---- /snip ---- > > And Py++ exposes the static member with a def_readonly("a", testclass::a ) > > call. When I import this module into python I get an undefined symbol for > > testclass::a. > > ??ImportError: ./testwrapper.so: undefined symbol: _ZN9testclass1aE > > I understand that the static member has no storage and thus boost python > > cannot find the address of it. However, I was hoping there is a way to solve > > this kind of (common?) problem using Py++ to perhaps generate a getter > > function or something similar. How would one do this? > > Try to move "a" initialization to a .cpp file. > > I also suggest you to take a look on Py++ unittests, especially on > "statics_tester.py" and "statics_to_be_exported.[c|h]pp" files. They > deal with exactly your use case > > > -- > 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 From nicolas.colombe at gmail.com Mon May 24 10:42:11 2010 From: nicolas.colombe at gmail.com (Nicolas Colombe) Date: Mon, 24 May 2010 10:42:11 +0200 Subject: [C++-sig] How to wrap reference to abstract base class to get C++ polymorphism In-Reply-To: <294D37C7-573E-43E6-92A3-2621FB236846@uqconnect.edu.au> References: <294D37C7-573E-43E6-92A3-2621FB236846@uqconnect.edu.au> Message-ID: 2010/5/20 Kun Hong > > > class B > { > public: > virtual const char *getName() const = 0; > }; > > BOOST_PYTHON_MODULE(Test) > { > > class_ > ("B", no_init) > .def("getName", pure_virtual(&B::getName)) > ; > > def("getB", &getB, > return_value_policy()); > } > > When you expose your method getName, expose it like a classic method, without the pure_virtual. The C++ will perform the virtual call itself. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cournia at gmail.com Tue May 25 00:00:22 2010 From: cournia at gmail.com (Nathan Cournia) Date: Mon, 24 May 2010 15:00:22 -0700 Subject: [C++-sig] automatic conversion of tuple to vector Message-ID: Hi All, The FAQ at: http://www.boost.org/doc/libs/1_43_0/libs/python/doc/v2/faq.html lists the following code as not possible with boost.python: C++: void foo(std::vector& array) { for(std::size_t i=0;i>> l = [1, 2, 3] >>> foo(l) >>> print l [2, 4, 6] Basically, the desired behavior is to have a C++ function which accepts a vector in C++; accept a tuple/list when in Python. I'm not sure if the FAQ is up-to-date. Is this still not possible in boost.python? How do people generally get the behavior I outlined above? Thanks! nathan From talljimbo at gmail.com Tue May 25 01:04:07 2010 From: talljimbo at gmail.com (Jim Bosch) Date: Mon, 24 May 2010 16:04:07 -0700 Subject: [C++-sig] automatic conversion of tuple to vector In-Reply-To: References: Message-ID: <4BFB05E7.8080105@gmail.com> On 05/24/2010 03:00 PM, Nathan Cournia wrote: > > Basically, the desired behavior is to have a C++ function which > accepts a vector in C++; accept a tuple/list when in Python. > > I'm not sure if the FAQ is up-to-date. Is this still not possible in > boost.python? How do people generally get the behavior I outlined > above? > > In general, you'll have to write your own custom wrapper that takes a boost::python::object argument, and wrap that, to get this behavior: void py_foo(boost::python::object const & py_array) { std::vector array; // foo(array); // } boost::python::def("foo", &py_foo); The copies are unavoidable if your C++ function takes a container, rather than a templated iterator range, simply because your Python object doesn't actually contain a std::vector. If you can work with an iterator range, check out my from_python_iterator class, part of the Python extensions in the boost sandbox: https://svn.boost.org/trac/boost/browser/sandbox/python_extensions/boost/python/from_python/iterator.hpp Along with the to-python iterator found in the main Boost.Python distribution, it should also help in the case where you have to make copies. HTH Jim Bosch From vishal.bayskar at nechclst.in Tue May 25 06:03:32 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Mon, 24 May 2010 21:03:32 -0700 (PDT) Subject: [C++-sig] No to_python (by-value) converter found for C++ type Message-ID: <28664090.post@talk.nabble.com> I am facing a problem while creating python binding for some code in my application. I have created a sample program for that and attached the same to this mail. Here after the generation of python bindings when I try to use that it fails with error No to_python (by-value) converter found for C++ type: CallResult Where as for this example code it even does not shows warning that CallResult is unknown type, still I used sizeof and even in the generated code I could find CallResult class. Attached tar file contains: structEx.cpp (cpp code) ex.py (python code generator script) exWithSizeOf.py (python code generator script using size of) sizeof.h include.h t.py(example python script to use the generated binding ) g++ -fpic -shared -o testModule.so -I/usr/include/python2.4/ -L/usr/lib64/ -lboost_python testModule.cpp (command for compilation) Can you please suggest what I am missing in this case. After some analysis I found, in my case python bindings generated contains line: typedef bp::class_< CallResult< Simple >, boost::noncopyable > CallResult_less__Simple__greater__exposer_t; I could not understand why boost::noncopyable is added as an extra template parameter and this seems to be be culprit in my case. I don't think my class contains object of any class which is derived from boost::noncopyable. Can you please suggest, what I am missing in this case? http://old.nabble.com/file/p28664090/structEx.tar.gz structEx.tar.gz -- View this message in context: http://old.nabble.com/No-to_python-%28by-value%29-converter-found-for-C%2B%2B-type-tp28664090p28664090.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From talljimbo at gmail.com Tue May 25 07:23:54 2010 From: talljimbo at gmail.com (Jim Bosch) Date: Mon, 24 May 2010 22:23:54 -0700 Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: <28664090.post@talk.nabble.com> References: <28664090.post@talk.nabble.com> Message-ID: <4BFB5EEA.809@gmail.com> On 05/24/2010 09:03 PM, vishal bayskar wrote: > After some analysis I found, in my case python bindings generated contains > line: > typedef bp::class_< CallResult< Simple>, boost::noncopyable> > CallResult_less__Simple__greater__exposer_t; > I could not understand why boost::noncopyable is added as an extra template > parameter and this seems to be be > culprit in my case. > It definitely is the culprit. The error you see is Boost.Python doing exactly what the code generator told it to do. > I don't think my class contains object of any class which is derived from > boost::noncopyable. > > Can you please suggest, what I am missing in this case? > http://old.nabble.com/file/p28664090/structEx.tar.gz structEx.tar.gz > > I didn't see anything attached to your email, and my tar doesn't like what's at the link above. I'm not sure whatever binding generator you're using (Py++?) uses to determine whether to thrown noncopyable in, there, but I suspect it would do so if you don't have a public copy constructor defined available, even if you didn't use boost::noncopyable to get rid of it. Maybe you can try posting the example again? Jim Bosch From vishal.bayskar at nechclst.in Tue May 25 07:46:12 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Mon, 24 May 2010 22:46:12 -0700 (PDT) Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: <4BFB5EEA.809@gmail.com> References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> Message-ID: <28664525.post@talk.nabble.com> >I didn't see anything attached to your email, and my tar doesn't like >what's at the link above. I'm not sure whatever binding generator >you're using (Py++?) uses to determine whether to thrown noncopyable in, >there, but I suspect it would do so if you don't have a public copy >constructor defined available, even if you didn't use boost::noncopyable >to get rid of it. >Maybe you can try posting the example again? zip file is attached below http://old.nabble.com/file/p28664525/structEx.zip structEx.zip I am also attaching important files structEx.cpp http://old.nabble.com/file/p28664525/structEx.cpp structEx.cpp ex.py http://old.nabble.com/file/p28664525/ex.py ex.py I have used py++ (latest version) for code generation -- View this message in context: http://old.nabble.com/No-to_python-%28by-value%29-converter-found-for-C%2B%2B-type-tp28664090p28664525.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Tue May 25 07:53:43 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 25 May 2010 08:53:43 +0300 Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: <28664525.post@talk.nabble.com> References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> <28664525.post@talk.nabble.com> Message-ID: On Tue, May 25, 2010 at 8:46 AM, vishal bayskar wrote: > > >>I didn't see anything attached to your email, and my tar doesn't like >>what's at the link above. ?I'm not sure whatever binding generator >>you're using (Py++?) uses to determine whether to thrown noncopyable in, >>there, but I suspect it would do so if you don't have a public copy >>constructor defined available, even if you didn't use boost::noncopyable >>to get rid of it. > >>Maybe you can try posting the example again? > > zip file is attached below > http://old.nabble.com/file/p28664525/structEx.zip structEx.zip > > I am also attaching important files > structEx.cpp > http://old.nabble.com/file/p28664525/structEx.cpp structEx.cpp > > ex.py > http://old.nabble.com/file/p28664525/ex.py ex.py > > I have used py++ (latest version) for code generation Please post a small and complete code directly to the mailing list. Did you try to remove "noncopyable" from the generated code? If so what happened? If you think, that py++ wrong, you can always override its decision: mb = module_builder_t( ... ) mb.class_(...).noncopyable = False HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From vishal.bayskar at nechclst.in Tue May 25 07:58:17 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Mon, 24 May 2010 22:58:17 -0700 (PDT) Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: <4BFB5EEA.809@gmail.com> References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> Message-ID: <28664564.post@talk.nabble.com> >I suspect it would do so if you don't have a public copy constructor defined available, even if you didn't use boost::noncopyable to get rid of it. Thanks Jim, I have defined public copy constructor explicitly now it is working. _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig at python.org http://mail.python.org/mailman/listinfo/cplusplus-sig -- View this message in context: http://old.nabble.com/No-to_python-%28by-value%29-converter-found-for-C%2B%2B-type-tp28664090p28664564.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From vishal.bayskar at nechclst.in Tue May 25 08:06:58 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Mon, 24 May 2010 23:06:58 -0700 (PDT) Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> <28664525.post@talk.nabble.com> Message-ID: <28664599.post@talk.nabble.com> >Did you try to remove "noncopyable" from the generated code? If so >what happened? Thanks, yes it works when "noncopyable" is removed. >If you think, that py++ wrong, you can always override its decision: >mb = module_builder_t( ... ) >mb.class_(...).noncopyable = False Thanks again it is helpful for us. -- View this message in context: http://old.nabble.com/No-to_python-%28by-value%29-converter-found-for-C%2B%2B-type-tp28664090p28664599.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Tue May 25 08:17:19 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 25 May 2010 09:17:19 +0300 Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: <28664599.post@talk.nabble.com> References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> <28664525.post@talk.nabble.com> <28664599.post@talk.nabble.com> Message-ID: On Tue, May 25, 2010 at 9:06 AM, vishal bayskar wrote: > > >>Did you try to remove "noncopyable" from the generated code? If so >>what happened? > > Thanks, yes it works when "noncopyable" is removed. > >>If you think, that py++ wrong, you can always override its decision: > >>mb = module_builder_t( ... ) >>mb.class_(...).noncopyable = False > > Thanks again it is helpful for us. It seems, that py++ was wrong :-(. Can you specify gccxml version, OS and your compiler? Do you mind to submit a small test case that reproduce the problem? Thanks -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From vishal.bayskar at nechclst.in Tue May 25 08:56:08 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Mon, 24 May 2010 23:56:08 -0700 (PDT) Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> <28664525.post@talk.nabble.com> <28664599.post@talk.nabble.com> Message-ID: <28664876.post@talk.nabble.com> >It seems, that py++ was wrong :-(. Can you specify gccxml version, OS >and your compiler? Do you mind to submit a small test case that >reproduce the problem? gccxml: gccxml-0.9 OS: x86_64-redhat-linux Compiler: gcc version 4.1.2 20080704 (Red Hat 4.1.2-44) You can use the code attached below (please modify it as you require :-)) just run unittest.sh it will give result fail or pass. tar file and zip file both are attached tarfile http://old.nabble.com/file/p28664876/unittest.tar.gz unittest.tar.gz zipfile http://old.nabble.com/file/p28664876/unittest.zip unittest.zip Thanks -- View this message in context: http://old.nabble.com/No-to_python-%28by-value%29-converter-found-for-C%2B%2B-type-tp28664090p28664876.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From abhishek.srivastava at nechclst.in Tue May 25 08:58:59 2010 From: abhishek.srivastava at nechclst.in (abhi.sri45) Date: Mon, 24 May 2010 23:58:59 -0700 (PDT) Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> <28664525.post@talk.nabble.com> <28664599.post@talk.nabble.com> Message-ID: <28664895.post@talk.nabble.com> Hi, >mb = module_builder_t( ... ) >mb.class_(...).noncopyable = False This does not works in my case. Example code is: enum CallStatus { SUCCESS = 423, FAILURE = 764 }; template struct CallResult { public: CallResult(CallStatus const callStatus) : status(callStatus) { } CallResult(CallStatus const callStatus, Result const & result) : status(callStatus), result(result) { } /* CallResult(CallResult const& callResult) { status = callResult.status; result = callResult.result; } */ CallStatus const status; Result const result; // boost::optional const result; }; class Simple { public: Simple() { } /* Simple(Simple const&) { } */ }; //typedef CallResult cRI; CallResult getCallResult() { Simple si; return CallResult(SUCCESS, si); } And the code generator that I used import os from pyplusplus import module_builder from pyplusplus.module_builder import call_policies from pyplusplus import code_creators mb=module_builder.module_builder_t(["./include.h"] ) mb.enum('CallStatus').include() mb.class_('Simple').include() mb.class_('VerifyResult').include() mb.class_('CallResult').include() mb.class_('CallResult').noncopyable = False mb.free_function('getCallResult').include() mb.build_code_creator( module_name = 'testModule') mb.write_module('testModule.cpp') I would like to mention here that, if I remove const from both status and result, boost::noncopyable is removed. versions are g++ (GCC) 4.1.2 20080704, gccxml-0.9 and os 2.6.18-128.el5 . With Regards, Abhishek Srivastava Roman Yakovenko wrote: > > On Tue, May 25, 2010 at 9:06 AM, vishal bayskar > wrote: >> >> >>>Did you try to remove "noncopyable" from the generated code? If so >>>what happened? >> >> Thanks, yes it works when "noncopyable" is removed. >> >>>If you think, that py++ wrong, you can always override its decision: >> >>>mb = module_builder_t( ... ) >>>mb.class_(...).noncopyable = False >> >> Thanks again it is helpful for us. > > It seems, that py++ was wrong :-(. Can you specify gccxml version, OS > and your compiler? Do you mind to submit a small test case that > reproduce the problem? > > Thanks > > -- > 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 > > -- View this message in context: http://old.nabble.com/No-to_python-%28by-value%29-converter-found-for-C%2B%2B-type-tp28664090p28664895.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From micdestefano at gmail.com Tue May 25 09:06:35 2010 From: micdestefano at gmail.com (Michele De Stefano) Date: Tue, 25 May 2010 09:06:35 +0200 Subject: [C++-sig] automatic conversion of tuple to vector In-Reply-To: <4BFB05E7.8080105@gmail.com> References: <4BFB05E7.8080105@gmail.com> Message-ID: Nathan, if you want to take a boost::numeric::ublas::vector instead of std::vector, you can use my library (mds-utils, the link is at the end of this e-mail), which already provides the proper converters. Bye, Michele 2010/5/25 Jim Bosch : > On 05/24/2010 03:00 PM, Nathan Cournia wrote: >> >> Basically, the desired behavior is to have a C++ function which >> accepts a vector in C++; accept a tuple/list when in Python. >> >> I'm not sure if the FAQ is up-to-date. ?Is this still not possible in >> boost.python? ?How do people generally get the behavior I outlined >> above? >> >> > > In general, you'll have to write your own custom wrapper that takes a > boost::python::object argument, and wrap that, to get this behavior: > > > void py_foo(boost::python::object const & py_array) { > ? ?std::vector array; > ? ?// > ? ?foo(array); > ? ?// > } > > boost::python::def("foo", &py_foo); > > > The copies are unavoidable if your C++ function takes a container, rather > than a templated iterator range, simply because your Python object doesn't > actually contain a std::vector. ?If you can work with an iterator range, > check out my from_python_iterator class, part of the Python extensions in > the boost sandbox: > > https://svn.boost.org/trac/boost/browser/sandbox/python_extensions/boost/python/from_python/iterator.hpp > > Along with the to-python iterator found in the main Boost.Python > distribution, it should also help in the case where you have to make copies. > > HTH > > Jim Bosch > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Michele De Stefano http://www.linkedin.com/in/micdestefano http://code.google.com/p/mds-utils http://xoomer.virgilio.it/michele_de_stefano From roman.yakovenko at gmail.com Tue May 25 09:31:03 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Tue, 25 May 2010 10:31:03 +0300 Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: <28664895.post@talk.nabble.com> References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> <28664525.post@talk.nabble.com> <28664599.post@talk.nabble.com> <28664895.post@talk.nabble.com> Message-ID: On Tue, May 25, 2010 at 9:58 AM, abhi.sri45 wrote: > > Hi, > >>mb = module_builder_t( ... ) >>mb.class_(...).noncopyable = False > > This does not works in my case. > > Example code is: > > enum CallStatus > { > ? ? ? ?SUCCESS = 423, > ? ? ? ?FAILURE = 764 > }; > > template > struct CallResult > { > public: > ? ? ? ?CallResult(CallStatus const callStatus) > ? ? ? ?: status(callStatus) > ? ? ? ?{ > ? ? ? ?} > > ? ? ? ?CallResult(CallStatus const callStatus, Result const & result) > ? ? ? ?: status(callStatus), result(result) > ? ? ? ?{ > ? ? ? ?} > ? ? ? ?/* > ? ? ? ?CallResult(CallResult const& callResult) > ? ? ? ?{ > ? ? ? ? ? ? ? ?status = callResult.status; > ? ? ? ? ? ? ? ?result = callResult.result; > ? ? ? ?} > ? ? ? ?*/ > ? ? ? ?CallStatus const status; > ? ? ? ?Result const result; > ? ? // ? boost::optional const result; > }; May be I miss something, but this class has "const member variable" and doesn't have user define copy constructor ( it is commented out ). In this case the class indeed noncopyable. Am I wrong? -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From kun.hong at uqconnect.edu.au Tue May 25 09:34:08 2010 From: kun.hong at uqconnect.edu.au (Kun Hong) Date: Tue, 25 May 2010 17:34:08 +1000 Subject: [C++-sig] How to wrap reference to abstract base class to get C++ polymorphism In-Reply-To: <294D37C7-573E-43E6-92A3-2621FB236846@uqconnect.edu.au> References: <294D37C7-573E-43E6-92A3-2621FB236846@uqconnect.edu.au> Message-ID: <978391E0-6C70-49FF-8F12-E85C3F04EA28@uqconnect.edu.au> On 20/05/2010, at 11:03 PM, Kun Hong wrote: > Hi, > > I have a maybe entry-level question. But it really troubled me for a > while. > > I need to wrap a C++ library which has some factory method to return > references to some abstract base classes. So the implementation is > hidden. If I want a python equivalent to the abstract base class > reference type > (so the polymorphism can work), how should I do it? > > Please see a very simplified example below which demonstrates what I > mean. > Finally, I get to understand it and be able to answer my own question. Post my own answer so future reader can get a pointer directly. The answer is in the test code that comes with boost python library. The code is in libs/python/test/polymorphism.{cpp,py} I got my code working like below: ========== test.cpp ========== #include using namespace boost::python; class B { public: virtual const char *getName() const = 0; virtual int getValue() const { return 10; } }; class C : public B { public: virtual const char *getName() const { return "Class C"; } }; struct BWrap : public B { BWrap(PyObject *self) : mSelf(self) {} PyObject *mSelf; const char *getName() const { return call_method(mSelf, "getName"); } }; const B &getB() { static boost::shared_ptr b(new C); return (*b.get()); } BOOST_PYTHON_MODULE(Test) { class_ ("B", no_init) .def("getName", pure_virtual(&B::getName)) .def("getValue", &B::getValue) ; class_ >("C") .def("getName", &C::getName) ; def("getB", &getB, return_value_policy()); } ======== test.py ========= from Test import * print getB().getName() print getB().getValue() c = C() print c.getName() print c.getValue() #b = B() #print b.getName() #print b.getValue() === end Not fully understand the pure_virtual call though, which doesn't prevent B instance be created in python. "no_init" is needed for preventing B creation. But, fun learning boost python, a lot to learn. Kun From talljimbo at gmail.com Tue May 25 10:06:28 2010 From: talljimbo at gmail.com (Jim Bosch) Date: Tue, 25 May 2010 01:06:28 -0700 Subject: [C++-sig] automatic conversion of tuple to vector In-Reply-To: References: <4BFB05E7.8080105@gmail.com> Message-ID: <4BFB8504.7040302@gmail.com> Hope I'm not thread-hijacking too much here, but this piqued my curiosity: On 05/25/2010 12:06 AM, Michele De Stefano wrote: > Nathan, > > if you want to take a boost::numeric::ublas::vector instead of > std::vector, you can use my library (mds-utils, the link is at the end > of this e-mail), which already provides the proper converters. > > Do you handle the non-const reference case automatically? More precisely, can you do: void func(boost::numeric::ublas::vector & arg); boost::python::def("func", func); ...pass it a python list, and find that list modified after the call? I've been stumped by this problem in boost python before, and I had previously come the conclusion that it was impossible, so I'd be very interested to hear it if you have a solution. Jim Bosch From micdestefano at gmail.com Tue May 25 10:16:31 2010 From: micdestefano at gmail.com (Michele De Stefano) Date: Tue, 25 May 2010 10:16:31 +0200 Subject: [C++-sig] automatic conversion of tuple to vector In-Reply-To: <4BFB8504.7040302@gmail.com> References: <4BFB05E7.8080105@gmail.com> <4BFB8504.7040302@gmail.com> Message-ID: Jim, actually, this IS impossible, for a limit of Boost.Python. The explanation is into the FAQs of the Boost.Python documentation. Basically it is not possible to develop an l-value converter, so it's not possible to solve this problem. The author of Boost.Python explains that for solving this problem a complete review of the library is needed (and I hope he will do it so that in the future that feature will be available). Bye, Michele 2010/5/25 Jim Bosch : > Hope I'm not thread-hijacking too much here, but this piqued my curiosity: > > On 05/25/2010 12:06 AM, Michele De Stefano wrote: >> >> Nathan, >> >> if you want to take a boost::numeric::ublas::vector instead of >> std::vector, you can use my library (mds-utils, the link is at the end >> of this e-mail), which already provides the proper converters. >> >> > > Do you handle the non-const reference case automatically? ?More precisely, > can you do: > > void func(boost::numeric::ublas::vector & arg); > boost::python::def("func", func); > > ...pass it a python list, and find that list modified after the call? > > I've been stumped by this problem in boost python before, and I had > previously come the conclusion that it was impossible, so I'd be very > interested to hear it if you have a solution. > > > Jim Bosch > -- Michele De Stefano http://www.linkedin.com/in/micdestefano http://code.google.com/p/mds-utils http://xoomer.virgilio.it/michele_de_stefano From abhishek.srivastava at nechclst.in Tue May 25 11:08:03 2010 From: abhishek.srivastava at nechclst.in (abhi.sri45) Date: Tue, 25 May 2010 02:08:03 -0700 (PDT) Subject: [C++-sig] No to_python (by-value) converter found for C++ type In-Reply-To: References: <28664090.post@talk.nabble.com> <4BFB5EEA.809@gmail.com> <28664525.post@talk.nabble.com> <28664599.post@talk.nabble.com> <28664895.post@talk.nabble.com> Message-ID: <28665970.post@talk.nabble.com> Hi, > In this case the class indeed noncopyable. I could not get this because, in my example program which I send earlier, there is a function CallResult getCallResult() { Simple si; return CallResult(SUCCESS, si); } Temporary object is created in return statement and hence it calls the default copy constructor successfully. You can verify this by compiling this. main() { CallResult obj = getCallResult(); std::cout << obj.status << std::endl; } There may be some problem while assignment but copy will work properly. Correct me if there is some gap in my understanding. Is there any way to disable this noncopyable because mb.class_(...).noncopyable = False doesn't worked in my case. With Regards, Abhishek Srivastava -- View this message in context: http://old.nabble.com/No-to_python-%28by-value%29-converter-found-for-C%2B%2B-type-tp28664090p28665970.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From wangx0802 at yahoo.com Tue May 25 16:50:22 2010 From: wangx0802 at yahoo.com (X Wang) Date: Tue, 25 May 2010 07:50:22 -0700 (PDT) Subject: [C++-sig] How to expose to a python user defined type with to/from_python convertors? In-Reply-To: <405902.42251.qm@web112603.mail.gq1.yahoo.com> Message-ID: <581548.31231.qm@web112615.mail.gq1.yahoo.com> > How do I expose my C++ type to a user > defined python type? I think I could use boost python > class_<> wrapper to wrap around, but if I want to use > to_python/from_python convertors? > > Say I want to expose my C++ class(CA) to a python > type(PA): > > C++: > class CA > { > ? public: > ? ? //ctors, accessors... > ? private: > ? ? std::string name_; > ? ? int value_; > }; > > Python: > class PA(object): > ? def __init__(self, name, value): > ? ? self.name=name > ? ? self.value=value > > If I use, > > void initializeConverters() > { > ???using namespace boost::python; > ???to_python_converter CA_to_pA>(); > } > > with > > struct CA_to_PA > { > ? ? ? static PyObject* convert(CA const& > s) > ? ? ? { > ? ? ? ???printf("In C++ -> > python converter?\n"); > > ? ? ? ???std::string > name=s.name(); > ? ? ? ???int > value=s.value(); > > #if 0 > ? ? ? ???static > boost::python::object obj; > ? ? ? ???PyObject* > obj_ptr=obj.ptr(); > > ? ? ? > ???PyObject_SetAttrString(obj_ptr, "name", > ? ? ? ? ? ? ? ? > PyString_FromString(name.c_str())); > ? ? ? > ???PyObject_SetAttrString(obj_ptr, "value", > ? ? ? ? ? ? ? ? > PyInt_FromLong(value)); > > ? ? ? ???return > boost::python::incref(obj_ptr); > #else > ? ? ? ???return > boost::python::incref( > ? ? ? ? ? ? > //PyInt_FromLong(1999) > ? ? ? ? ? ? > Py_BuildValue("s", "native string works") > ? ? ? ? ? ? > //Py_BuildValue("{s:i,s:i}", "abc", 123, "def", 456) > ? ? ? ? ? ? ); > #endif > ? ? ? } > }; > > How do I create a boost::python::object (or PyObject*) with > "name" and "value" attributes? I could get creation and > return of python native types to work, but can not get user > defined types returned to the python side -- always get > NoneType returned. > > boost::python::object C2Py() > { > ???printf("Pre assignment\n"); > > #if 0 > ???boost::python::object obj(new CA("From > C++ to python", 987654321)); > ???PyObject* o=obj.ptr(); > #else > ???CA a("From C++ to python", 987654321); > ???PyObject *o=CA_to_PA::convert(a); > #endif > > ???printf("Post assignment\n"); > > ???PyObject *tmp=NULL; > > ???std::string name="No Name"; > ???int value=-1; > > ???tmp=PyObject_GetAttrString(o, "name"); > ???if (tmp!=NULL) > name=PyString_AsString(tmp); > > ???tmp=PyObject_GetAttrString(o, "value"); > ???if (tmp!=NULL) value=PyInt_AsLong(tmp); > > ???printf("(%s, %d)\n", name.c_str(), > value); > > ???return obj; > } > > $ python > Python 2.5.1 (r251:54863, Oct? 5 2007, 11:16:46) > [GCC 4.1.1 20070105 (Red Hat 4.1.1-53)] on linux2 > Type "help", "copyright", "credits" or "license" for more > information. > >>> import mytypes > >>> pa=mytypes.C2Py() > ("No Name", -1) > >>>type(pa) > NoneType > > Any help is appreciated, thanks! > Any comments? Am I asking the right question? Basically I have a simple C++ type and want to extend it to python. However, I haven't figured out a way to do the C++ to python conversion with boost python's to_python/from_python converter scheme. Am I missing something? From micdestefano at gmail.com Tue May 25 17:52:32 2010 From: micdestefano at gmail.com (Michele De Stefano) Date: Tue, 25 May 2010 17:52:32 +0200 Subject: [C++-sig] How to expose to a python user defined type with to/from_python convertors? In-Reply-To: <581548.31231.qm@web112615.mail.gq1.yahoo.com> References: <405902.42251.qm@web112603.mail.gq1.yahoo.com> <581548.31231.qm@web112615.mail.gq1.yahoo.com> Message-ID: There's a very good tutorial on writing from/to python converters. This tutorial was suggested to me by one of the members of this mailing list and I've found it very useful. The link is this one: http://misspent.wordpress.com/2009/09/27/how-to-write-boost-python-converters/ After reading this very useful article, I managed to write my mds-utils library (the link is at the end of this e-mail) which you can download to see how you can write a from/to python converter. Bye. Michele 2010/5/25 X Wang : > >> How do I expose my C++ type to a user >> defined python type? I think I could use boost python >> class_<> wrapper to wrap around, but if I want to use >> to_python/from_python convertors? >> >> Say I want to expose my C++ class(CA) to a python >> type(PA): >> >> C++: >> class CA >> { >> ? public: >> ? ? //ctors, accessors... >> ? private: >> ? ? std::string name_; >> ? ? int value_; >> }; >> >> Python: >> class PA(object): >> ? def __init__(self, name, value): >> ? ? self.name=name >> ? ? self.value=value >> >> If I use, >> >> void initializeConverters() >> { >> ???using namespace boost::python; >> ???to_python_converter> CA_to_pA>(); >> } >> >> with >> >> struct CA_to_PA >> { >> ? ? ? static PyObject* convert(CA const& >> s) >> ? ? ? { >> ? ? ? ???printf("In C++ -> >> python converter?\n"); >> >> ? ? ? ???std::string >> name=s.name(); >> ? ? ? ???int >> value=s.value(); >> >> #if 0 >> ? ? ? ???static >> boost::python::object obj; >> ? ? ? ???PyObject* >> obj_ptr=obj.ptr(); >> >> >> ???PyObject_SetAttrString(obj_ptr, "name", >> >> PyString_FromString(name.c_str())); >> >> ???PyObject_SetAttrString(obj_ptr, "value", >> >> PyInt_FromLong(value)); >> >> ? ? ? ???return >> boost::python::incref(obj_ptr); >> #else >> ? ? ? ???return >> boost::python::incref( >> >> //PyInt_FromLong(1999) >> >> Py_BuildValue("s", "native string works") >> >> //Py_BuildValue("{s:i,s:i}", "abc", 123, "def", 456) >> ? ? ? ? ? ? ); >> #endif >> ? ? ? } >> }; >> >> How do I create a boost::python::object (or PyObject*) with >> "name" and "value" attributes? I could get creation and >> return of python native types to work, but can not get user >> defined types returned to the python side -- always get >> NoneType returned. >> >> boost::python::object C2Py() >> { >> ???printf("Pre assignment\n"); >> >> #if 0 >> ???boost::python::object obj(new CA("From >> C++ to python", 987654321)); >> ???PyObject* o=obj.ptr(); >> #else >> ???CA a("From C++ to python", 987654321); >> ???PyObject *o=CA_to_PA::convert(a); >> #endif >> >> ???printf("Post assignment\n"); >> >> ???PyObject *tmp=NULL; >> >> ???std::string name="No Name"; >> ???int value=-1; >> >> ???tmp=PyObject_GetAttrString(o, "name"); >> ???if (tmp!=NULL) >> name=PyString_AsString(tmp); >> >> ???tmp=PyObject_GetAttrString(o, "value"); >> ???if (tmp!=NULL) value=PyInt_AsLong(tmp); >> >> ???printf("(%s, %d)\n", name.c_str(), >> value); >> >> ???return obj; >> } >> >> $ python >> Python 2.5.1 (r251:54863, Oct? 5 2007, 11:16:46) >> [GCC 4.1.1 20070105 (Red Hat 4.1.1-53)] on linux2 >> Type "help", "copyright", "credits" or "license" for more >> information. >> >>> import mytypes >> >>> pa=mytypes.C2Py() >> ("No Name", -1) >> >>>type(pa) >> NoneType >> >> Any help is appreciated, thanks! >> > Any comments? Am I asking the right question? Basically I have a simple C++ type and want to extend it to python. However, I haven't figured out a way to do the C++ to python conversion with boost python's to_python/from_python converter scheme. Am I missing something? > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -- Michele De Stefano http://www.linkedin.com/in/micdestefano http://code.google.com/p/mds-utils http://xoomer.virgilio.it/michele_de_stefano From talljimbo at gmail.com Wed May 26 06:28:54 2010 From: talljimbo at gmail.com (Jim Bosch) Date: Tue, 25 May 2010 21:28:54 -0700 Subject: [C++-sig] Boost.Python Problem In-Reply-To: References: Message-ID: <4BFCA386.1070605@gmail.com> On 05/18/2010 12:44 PM, alexandre monti wrote: > object main_module((handle<>(borrowed(PyImport_AddModule("__main__"))))); > object main_namespace = main_module.attr("__dict__"); What's the context? At least on my Ubuntu system, the following code: #include using namespace boost::python; BOOST_PYTHON_MODULE(example) { object main_module((handle<>(borrowed(PyImport_AddModule("__main__"))))); object main_namespace = main_module.attr("__dict__"); } ...compiles and imports just fine. Jim Bosch From amont65 at hotmail.fr Wed May 26 15:41:28 2010 From: amont65 at hotmail.fr (alexandre monti) Date: Wed, 26 May 2010 15:41:28 +0200 Subject: [C++-sig] Boost.Python Problem In-Reply-To: <4BFCA386.1070605@gmail.com> References: , <4BFCA386.1070605@gmail.com> Message-ID: Hello ! I would extend embedded python, so the code is in the main : int main(int argc, char* argv[]) { Py_Initialize(); object main_module((handle<>(borrowed(PyImport_AddModule("__main__"))))); object main_namespace = main_module.attr("__dict__"); } I've read that the error is specific to Mac OSX in this case, but the post was very old. Thanks, Alexandre MONTI _________________________________________________________________ Installez gratuitement les nouvelles Emoch'ticones ! http://www.ilovemessenger.fr/emoticones/telecharger-emoticones-emochticones.aspx -------------- next part -------------- An HTML attachment was scrubbed... URL: From vishal.bayskar at nechclst.in Thu May 27 13:46:36 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Thu, 27 May 2010 04:46:36 -0700 (PDT) Subject: [C++-sig] The declaration is unexposed Message-ID: <28692839.post@talk.nabble.com> Hi All, I know below problem is very wired, but let me know what is going wrong. The declaration (class someClassB) is defended in file b.h in the same directory (same as a.h) but this declaration is not getting exposed. Please let me know why the declaration is not getting exposed though it is in the same directory. a.h -------- include "b.h" class someClassA { public: someClassA( someClassB const &); }; b.h -------- class someClassB { public: someClassB(); }; export.py ------------- import os import pygccxml from pyplusplus import module_builder from pyplusplus.module_builder import call_policies from pyplusplus import decl_wrappers mb=module_builder.module_builder_t(["./a.h"], ) mb.build_code_creator(module_name = 'testModule') mb.write_module('testModule.cpp') Error -------------------- vbayskar at machine03 decl]$ python export.py INFO Parsing source file "./a.h" ... INFO gccxml cmd: /usr/local/bin/gccxml -I"." "./a.h" -fxml="/tmp/tmpB13bTu.xml" INFO GCCXML version - 0.9( 1.132 ) WARNING: someClassB [class] > execution error W1040: The declaration is unexposed, but there are other > declarations, which refer to it. This could > cause "no to_python converter found" run time error. Declarations: > someClassA::someClassA(someClassB const & arg0) > [constructor] Thanks Vishal Bayskar -- View this message in context: http://old.nabble.com/The-declaration-is-unexposed-tp28692839p28692839.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Thu May 27 20:52:11 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Thu, 27 May 2010 21:52:11 +0300 Subject: [C++-sig] The declaration is unexposed In-Reply-To: <28692839.post@talk.nabble.com> References: <28692839.post@talk.nabble.com> Message-ID: On Thu, May 27, 2010 at 2:46 PM, vishal bayskar wrote: > > Hi All, > > I know below problem is very wired, but let me know what is going wrong. > > The declaration (class someClassB) is defended in file b.h in the same > directory (same as a.h) but this declaration is not getting exposed. Do you want to expose it? > Please let me know why the declaration is not getting exposed though it is > in the same directory. > > a.h > -------- > include "b.h" > class someClassA > { > ? ? ? ?public: > ? ? ? ? ? ? ? ?someClassA( someClassB const &); > }; > > b.h > -------- > class someClassB > { > public: > ? ? ? ?someClassB(); > }; > > > export.py > ------------- > import os > import pygccxml > from pyplusplus import module_builder > from pyplusplus.module_builder import call_policies > from pyplusplus import decl_wrappers > > mb=module_builder.module_builder_t(["./a.h"], > ) This is the reason why declaration "b" is unexposed. In this case, declarations "b" was declared in a header file, which was not "asked" to be exposed. Consider use case with standard or system header files, you don't really want to expose declaration from them, even so they were included. In any case, it seems that I selected the wrong defaults and I suggest you to expose the desired declarations set manually. See http://language-binding.net/pygccxml/query_interface.html document for more information about queryAPI > mb.build_code_creator(module_name = 'testModule') > mb.write_module('testModule.cpp') > > > > Error > -------------------- > vbayskar at machine03 decl]$ python export.py > INFO Parsing source file "./a.h" ... > INFO gccxml cmd: /usr/local/bin/gccxml ?-I"." ? "./a.h" > -fxml="/tmp/tmpB13bTu.xml" > INFO GCCXML version - 0.9( 1.132 ) > > WARNING: someClassB [class] >> execution error W1040: The declaration is unexposed, but there are other >> declarations, which refer to it. This could >> cause "no to_python converter found" run time error. Declarations: >> someClassA::someClassA(someClassB const & arg0) >> [constructor] HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/ From vishal.bayskar at nechclst.in Fri May 28 08:12:11 2010 From: vishal.bayskar at nechclst.in (vishal bayskar) Date: Thu, 27 May 2010 23:12:11 -0700 (PDT) Subject: [C++-sig] The declaration is unexposed In-Reply-To: References: <28692839.post@talk.nabble.com> Message-ID: <28703090.post@talk.nabble.com> >This is the reason why declaration "b" is unexposed. In this case, >declarations "b" was declared in a header file, which was not "asked" >to be exposed. Thanks again Roman for your prompt response. Actually my doubt was like pyplusplus exposes declaration from all included file that are in current directory (in this particular case a.h file includes b.h file that are in the same directory where a.h is) In the same example if I provide the absolute path for a.h (/home/vishal/a.h) it automatically exposed the class present in b.h file. Please explain why this happened Actually in my project I am using find_out_dependency(class) function which gives list of declaration (on which the class is depending on). And after getting the list I am iterating on this list and including all the declaration, but I don't know why mysteriously some classes are not getting included (and pyplusplus is not even giving any error while including them, in my assumption pyplusplus should give some error if some class in not getting include). and at the end I am seeing W1040 warnings that some classes are not getting exposed (suppose class myName) I have checked the find_out_dependency() output and it is giving the class myName also in the output. Please let me know what could be the reason behind it, and what could I do to include those class (for example class myName). Thanks in advance. -- View this message in context: http://old.nabble.com/The-declaration-is-unexposed-tp28692839p28703090.html Sent from the Python - c++-sig mailing list archive at Nabble.com. From roman.yakovenko at gmail.com Fri May 28 10:44:37 2010 From: roman.yakovenko at gmail.com (Roman Yakovenko) Date: Fri, 28 May 2010 11:44:37 +0300 Subject: [C++-sig] The declaration is unexposed In-Reply-To: <28703090.post@talk.nabble.com> References: <28692839.post@talk.nabble.com> <28703090.post@talk.nabble.com> Message-ID: On Fri, May 28, 2010 at 9:12 AM, vishal bayskar wrote: > >>This is the reason why declaration "b" is unexposed. In this case, >>declarations "b" was declared in a header file, which was not "asked" >>to be exposed. > > Thanks again Roman for your prompt response. > > Actually my doubt was like pyplusplus exposes declaration from all included > file that are in current directory (in this particular case a.h file > includes b.h file that are in the same directory where a.h is) Your understanding is right, but there are other factors that influence the selected declarations. As I said, it was a mistake to implement this "select by default" algorithm. You can take a look on module_builder_t class. > In the same example if I provide the absolute path for a.h > (/home/vishal/a.h) it automatically exposed the class present in b.h file. This is one of the factors I mentioned earlier. > Please explain why this happened Sorry, this days I am really busy and can devote much time to the project. All critical issues is still address in a day or two, but if the issue has acceptable work-around - I just propose it. This is exactly your case. You can "include" all declarations you want or ... to investigate by yourself :-) > > Actually in my project I am using find_out_dependency(class) function which > gives list of declaration (on which the class is depending on). > And after getting the list I am iterating on this list and including all the > declaration, but I don't know why mysteriously some classes are not getting > included (and pyplusplus is not even giving any error while including them, > in my assumption pyplusplus should give some error if some class in not > getting include). Wrong, py++ gives you a warning ( may be not in all cases, but it tries hard ) > and at the end I am seeing W1040 warnings that some classes are not getting > exposed (suppose class myName) This is exactly the warning. Since py++ allows multi-module development and integration of user code any where it can't decide whether "unexposed" declaration is erroneous situation or not. > I have checked the find_out_dependency() output and it is giving the class > myName also in the output. > > Please let me know what could be the reason behind it, and what could I do > to include those class (for example class myName). I don't know, may be father code excludes it, may be the class is private, may be bug in py++, may be bug in your "find_out_dependency-include" loop. I really can't say you. You will have to investigate this. -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/