From micdestefano at gmail.com Sun Nov 4 15:42:03 2012 From: micdestefano at gmail.com (Michele De Stefano) Date: Sun, 4 Nov 2012 15:42:03 +0100 Subject: [C++-sig] mds-utils 1.3.0 release Message-ID: After 3 years of "no enhancement" I'm pleased to announce a new release of the mds-utils library (general purpose utilities for C++ and Python developers). The library contains useful C++ code for developing Python extensions through Boost.Python but also through SWIG or the simple C API. At present mds-utils contains: 1. a tool for detecting machine endianity. 2. some useful classes that allow to treat the old C FILE pointer as a C++ stream. 3. C++ classes that help on treating Python file objects as C++ streams. 4. simple utilities for indexing support in Python extensions. 5. new C++ to-Python and from-Python converters for some Boost uBlas objects. 6. a new sequence iterator that is able to wrap Python sequences and allows also to modify them. This feature does not depend on Boost.Python. 7. new from/to Python conversion utilities that do not depend on Boost.Python. Furthermore, the new release has been tested with Python 2.7.3 too, while the previous one had some issues with Python >= 2.5. Best regards to all. -- Michele De Stefano Web Site Linked In mds-utils: a general purpose Open Source library -------------- next part -------------- An HTML attachment was scrubbed... URL: From brandsmeier at gmx.de Sun Nov 4 22:09:06 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Sun, 4 Nov 2012 22:09:06 +0100 Subject: [C++-sig] boost numpy and boost python exported float128 / dd_real Message-ID: Dear list, there is a C++ library that supports 128bit (and 256bit) floats: libqd. In C++ that data type is called dd_real and I exported dd_real to python via boost python. Sometimes I send the same `dd_real` to python via a boost numpy by creating and ndarray with the dtype given by `np::dtype::get_builtin()`. In python that ends up beeing `np.float128`. When I want to pass this `np.float128` back to a C++ functions expecting `dd_real` (exported via boost python), this doesn't work (see below). Can I somehow register an automatic converter in boost python so that I can pass `np.float128` to a function that expects `dd_real`? How would I do that? Note that for `np.float64` and `np.complex128` (where there are buildin data types in python) these problems do not occur. I also tried to avoid exporting `dd_real` from boost python, and I wanted to always use `np.float128` from python, but that doesn't work either: No to_python (by-value) converter found for C++ type: dd_real apparently `np.float128` is not in the boost python type registry. -Holger ArgumentError: Python argument types in parfem.scalarPy.todouble(numpy.float128) did not match C++ signature: todouble(double) todouble(dd_real) From brandsmeier at gmx.de Mon Nov 5 11:27:38 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Mon, 5 Nov 2012 11:27:38 +0100 Subject: [C++-sig] buildin converter for long double Message-ID: Dear list, the boost python buildin converter for long double: BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, ::PyFloat_FromDouble(x), &PyFloat_Type) converter/builtin_converters.hpp identifies long double, which on my system is a 128bit float, with a 64bit float in python. I would like to identify it with numpy`s float128. Can you give me any hings how I would be able to do that? -Holger From mhellmic at cern.ch Mon Nov 5 18:51:44 2012 From: mhellmic at cern.ch (Martin Hellmich) Date: Mon, 5 Nov 2012 18:51:44 +0100 Subject: [C++-sig] export custom C++ exception to Python so it can be raised by python Message-ID: <5097FCB0.2000102@cern.ch> Hi, I have a custom exception in C++ that I would like to use in Python. Let's call it MyException with custom constructor and members (an implementation is below). I would like to expose it to python so that I can raise it: > raise MyException(-1, 'tragic error') The perfect way that I can imagine is to tell boost::python to base the class on PyExc_Exception, but that doesn't seem to work. Furthermore I have found various solutions how to translate exceptions thrown in C++ into proper Python exceptions, but in my case these solutions sit at the wrong place. I would like to avoid to create a corresponding python class to the C++ exception, because there would be added effort to keep the two descriptions consistent. I am happy about any comments and questions! Cheers Martin class MyException: public std::exception { MyException(); MyException(int code, const std::string &string); int code(); const char* what(); int errorCode; std::string errorMessage; }; From jiangzuoyan at gmail.com Tue Nov 6 03:25:04 2012 From: jiangzuoyan at gmail.com (jiangzuoyan at gmail.com) Date: Tue, 6 Nov 2012 10:25:04 +0800 Subject: [C++-sig] buildin converter for long double In-Reply-To: References: Message-ID: how about return a wrap type of long double to cheating boost/python, and register youself converter as PyObject * o = PyArrayScalar_New(LongDouble); PyLongDoubleScalarObject * s = (PyLongDoubleScalarObject*)o; s->obval = value; Don't forget to call import_array() in your model init function. BTW, I think float128 is not useful, it's slow. and in most case, change algorithm to avoid precision problem is another *better* approach. Changsheng Jiang On Mon, Nov 5, 2012 at 6:27 PM, Holger Brandsmeier wrote: > Dear list, > > the boost python buildin converter for long double: > BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, > ::PyFloat_FromDouble(x), &PyFloat_Type) > converter/builtin_converters.hpp > identifies long double, which on my system is a 128bit float, with a > 64bit float in python. I would like to identify it with numpy`s > float128. Can you give me any hings how I would be able to do that? > > -Holger > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From giuseppe.corbelli at copanitalia.com Tue Nov 6 10:08:43 2012 From: giuseppe.corbelli at copanitalia.com (Giuseppe Corbelli) Date: Tue, 06 Nov 2012 10:08:43 +0100 Subject: [C++-sig] export custom C++ exception to Python so it can be raisedby python In-Reply-To: <5097FCB0.2000102@cern.ch> References: <5097FCB0.2000102@cern.ch> Message-ID: <5098D39B.30604@copanitalia.com> On 05/11/2012 18:51, Martin Hellmich wrote: > Hi, > > I have a custom exception in C++ that I would like to use in Python. > > Let's call it MyException with custom constructor and members (an > implementation is below). > I would like to expose it to python so that I can raise it: > > > raise MyException(-1, 'tragic error') > > The perfect way that I can imagine is to tell boost::python to base the class > on PyExc_Exception, but that doesn't seem to work. Furthermore I have found > various solutions how to translate exceptions thrown in C++ into proper Python > exceptions, but in my case these solutions sit at the wrong place. I've based my code on. http://stackoverflow.com/questions/9620268/boost-python-custom-exception-class > I would like to avoid to create a corresponding python class to the C++ > exception, because there would be added effort to keep the two descriptions > consistent. True. > class MyException: public std::exception { > MyException(); > MyException(int code, const std::string &string); > > int code(); > const char* what(); > > int errorCode; > std::string errorMessage; > }; The only unusual stuff is that you need both code and message. 2 ideas come to mind. 1) you can base the python exception not on PyExc_Exception but on a custom class which can handle both error code and message, so the exception translator can do both a PyErr_SetString and a "PyErr_SetCode". This way you can use the exception in both C and Python in the same style, no need to keep anything in sync. 2) you can base the Python exception on the standard PyExc_Exception but use PyErr_SetObject in the exception translator. A quick search finds this http://stackoverflow.com/questions/2261858/boostpython-export-custom-exception (does not inherit from Exception, though) -- Giuseppe Corbelli WASP Software Engineer, Copan Italia S.p.A Phone: +390303666318 Fax: +390302659932 E-mail: giuseppe.corbelli at copanitalia.com From herzog at mpi-cbg.de Tue Nov 6 11:38:44 2012 From: herzog at mpi-cbg.de (Ronny Herzog) Date: Tue, 06 Nov 2012 11:38:44 +0100 Subject: [C++-sig] how to boost.build using two different python versions Message-ID: <5098E8B4.8020303@mpi-cbg.de> Dear all, I need to cross compile a python extension using boost.build. I have Python 2.6 (32 and 64 Bit) and Python 2.7 (64 Bit) installed on my computer. I compiled boost with Python 2.6 (32Bit). My goal is to compile the python extension for Python 2.7 (64Bit). The problem is that it does not try to not find the python27.lib. Is there a way to do this without re-compiling Boost with Python 2.7? Thanks, Ronny Jamroot: # Copyright David Abrahams 2006. Distributed under the Boost # Software License, Version 1.0. (See accompanying # file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) # using msvc ; # Make the definition of the python-extension rule available import python ; #using python : 2.6 : c:/Python26/python.exe # : c:/Python26/include # : c:/Python26/libs # : 32 # ; using python : 2.7 : c:/Python27_64/python.exe : c:/Python27_64/include : c:/Python27_64/libs : 64 ; # Specify the path to the Boost project. If you move this project, # adjust the path to refer to the Boost root directory. use-project boost : c:/boost_1_50_0 ; # Set up the project-wide requirements that everything uses the # boost_python library defined in the project whose global ID is # /boost/python. project boost-python-quickstart : requirements /boost/python//boost_python ; # Declare a Python extension python-extension calcsf : lipidxexception.cpp CalcSF.cpp ; install convenient_copy : calcsf : on SHARED_LIB PYTHON_EXTENSION 32:c:/users/duke/my_projects/lipidxplorer/trunk/lx/mfql/calcsf26_32 64:c:/users/duke/my_projects/lipidxplorer/trunk/lx/mfql/calcsf27_64 ; The output: C:\Users\Duke\My_Projects\boost_python\calcsf>bjam address-model=64 release link =static -d+2 file bin\msvc-9.0\release\address-model-64\link-static\threading-multi\calcsf.py d.rsp "bin\msvc-9.0\release\address-model-64\link-static\threading-multi\lipidxexcepti on.obj" "bin\msvc-9.0\release\address-model-64\link-static\threading-multi\CalcSF.obj" "C:\boost_1_50_0\bin.v2\libs\python\build\msvc-9.0\release\address-model-64\link -static\threading-multi\libboost_python-vc90-mt-1_50.lib" "python26.lib" msvc.link.dll bin\msvc-9.0\release\address-model-64\link-static\threading-multi\ calcsf.pyd call "C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" x86 _amd64 >nul link /NOLOGO /INCREMENTAL:NO /DLL /MACHINE:X64 /subsystem:console /out:"bin\msvc -9.0\release\address-model-64\link-static\threading-multi\calcsf.pyd" /IMPLIB:"b in\msvc-9.0\release\address-model-64\link-static\threading-multi\calcsf.lib" /LI BPATH:"c:\Python64\libs" @"bin\msvc-9.0\release\address-model-64\link-static\t hreading-multi\calcsf.pyd.rsp" if %ERRORLEVEL% NEQ 0 EXIT %ERRORLEVEL% LINK : fatal error LNK1104: cannot open file 'python27.lib' ...skipped ca lcsf.pyd for lack of calcsf.pyd... ...failed updating 2 targets... -------------- next part -------------- A non-text attachment was scrubbed... Name: herzog.vcf Type: text/x-vcard Size: 467 bytes Desc: not available URL: From mhellmic at cern.ch Tue Nov 6 15:35:10 2012 From: mhellmic at cern.ch (Martin Hellmich) Date: Tue, 6 Nov 2012 15:35:10 +0100 Subject: [C++-sig] export custom C++ exception to Python so it can be raisedby python In-Reply-To: <5098D39B.30604@copanitalia.com> References: <5097FCB0.2000102@cern.ch> <5098D39B.30604@copanitalia.com> Message-ID: <5099201E.5080604@cern.ch> Hi Giuseppe, On 11/06/2012 10:08 AM, Giuseppe Corbelli wrote: > On 05/11/2012 18:51, Martin Hellmich wrote: >> Hi, >> >> I have a custom exception in C++ that I would like to use in Python. >> >> Let's call it MyException with custom constructor and members (an >> implementation is below). >> I would like to expose it to python so that I can raise it: >> >> > raise MyException(-1, 'tragic error') >> >> The perfect way that I can imagine is to tell boost::python to base >> the class >> on PyExc_Exception, but that doesn't seem to work. Furthermore I have >> found >> various solutions how to translate exceptions thrown in C++ into >> proper Python >> exceptions, but in my case these solutions sit at the wrong place. > > I've based my code on. > http://stackoverflow.com/questions/9620268/boost-python-custom-exception-class I have used the same reference and also this suggestion for including the other attributes shown here: http://stackoverflow.com/questions/11448735/boostpython-export-custom-exception-and-inherit-from-pythons-exception However, this doesn't solve my problem. It works fine when I throw the MyException in C++, but when I raise the MyCPPException in Python, the translator is not called (which makes sense), so the attribute 'cause' does not change. Actually, before I throw the MyException for the first time in C++, MyCPPException does not have the attribute 'cause' at all. This issue should be the same, if I set the exception with PyErr_SetObject, since the translator is not called, when I 'raise' it. My current attempt is quite different (and all in Python): import exc class MyException(Exception): def __init__(self, *args): self.e = exc.MyException(*args) def __getattr__(self, name): return self.e.__getattribute__(name) def __str__(self): return type(self).__name__ + ': ' + self.e.what() e=MyException(1, 'bla') I can 'raise MyException(1, 'some error')' and get the attributes I have in C++. If the attributes change, it should all work except for the __str__ function, which is hardcoded. The drawback is that it's in python. The python part will be developed by third parties and I then have to make sure that they see and use this provided exception ... Also I don't know yet how translating this back to C++ will play out. I guess I am jumping a bit ahead ... is it correct from me to assume that this behaviour is not possible with the solutions posted on stackoverflow or am I overlooking something? I am very happy about any replies :) Cheers Martin > > >> I would like to avoid to create a corresponding python class to the C++ >> exception, because there would be added effort to keep the two >> descriptions >> consistent. > > True. > >> class MyException: public std::exception { >> MyException(); >> MyException(int code, const std::string &string); >> >> int code(); >> const char* what(); >> >> int errorCode; >> std::string errorMessage; >> }; > > The only unusual stuff is that you need both code and message. 2 ideas > come to mind. > 1) you can base the python exception not on PyExc_Exception but on a > custom class which can handle both error code and message, so the > exception translator can do both a PyErr_SetString and a > "PyErr_SetCode". This way you can use the exception in both C and Python > in the same style, no need to keep anything in sync. > > 2) you can base the Python exception on the standard PyExc_Exception but > use PyErr_SetObject in the exception translator. A quick search finds this > http://stackoverflow.com/questions/2261858/boostpython-export-custom-exception > > (does not inherit from Exception, though) > -- Martin Hellmich Information Technology Department mhellmic at cern.ch CERN +41 22 76 765 26 CH-1211 Geneva 23 From cory at codeware.com Tue Nov 6 20:40:18 2012 From: cory at codeware.com (Cory Riddell) Date: Tue, 06 Nov 2012 13:40:18 -0600 Subject: [C++-sig] problem embedding python - very simple example Message-ID: <509967A2.3050208@codeware.com> I downloaded and installed the 32-bit version of Python 3.3. I downloaded and compiled boost-1.52.0. I've been able to build and run the tutorial Python extension but haven't been able to get anything but the most trivial example of embedding to work. My code (which compiles with no warnings): #include using namespace boost::python; int main() { Py_Initialize(); object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); exec("print('hello')\n", main_namespace); exec("print('world')\n", main_namespace); exec("f = file('hello.txt', 'w')\n", main_namespace); return 0; } It prints "hello\nworld\n" then throws an exception executing the line that creates the hello.txt file: First-chance exception at 0x755ac41f in Test.exe: Microsoft C++ exception: boost::python::error_already_set at memory location 0x0039fb60.. Unhandled exception at 0x773915de (ntdll.dll) in Test.exe: Microsoft C++ exception: boost::python::error_already_set at memory location 0x0039fb60.. In the exec method, the exception is being thrown when result comes back as null: PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), local.ptr()); if (!result) throw_error_already_set(); I'm using VS2010SP1 on Windows7 which I believe is what Python 3.3 is built with. I'm dynamically linking against boost. Any suggestions? From cory at codeware.com Tue Nov 6 20:48:48 2012 From: cory at codeware.com (Cory Riddell) Date: Tue, 06 Nov 2012 13:48:48 -0600 Subject: [C++-sig] problem embedding python - very simple example In-Reply-To: <509967A2.3050208@codeware.com> References: <509967A2.3050208@codeware.com> Message-ID: <509969A0.70804@codeware.com> Argh! I didn't notice that the example code I was trying was using file() rather than open(). Sheeesh. After correcting this, I was able to get it to work successfully! Step one on the thousand mile journey is complete! Cory On 11/6/2012 1:40 PM, Cory Riddell wrote: > I downloaded and installed the 32-bit version of Python 3.3. I > downloaded and compiled boost-1.52.0. I've been able to build and run > the tutorial Python extension but haven't been able to get anything but > the most trivial example of embedding to work. > > My code (which compiles with no warnings): > > #include > > using namespace boost::python; > > int main() > { > Py_Initialize(); > object main_module = import("__main__"); > object main_namespace = main_module.attr("__dict__"); > > exec("print('hello')\n", main_namespace); > exec("print('world')\n", main_namespace); > exec("f = file('hello.txt', 'w')\n", main_namespace); > > return 0; > } > > It prints "hello\nworld\n" then throws an exception executing the line > that creates the hello.txt file: > First-chance exception at 0x755ac41f in Test.exe: Microsoft C++ > exception: boost::python::error_already_set at memory location 0x0039fb60.. > Unhandled exception at 0x773915de (ntdll.dll) in Test.exe: Microsoft > C++ exception: boost::python::error_already_set at memory location > 0x0039fb60.. > > In the exec method, the exception is being thrown when result comes back > as null: > PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), > local.ptr()); > if (!result) throw_error_already_set(); > > I'm using VS2010SP1 on Windows7 which I believe is what Python 3.3 is > built with. I'm dynamically linking against boost. > > Any suggestions? > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > From stefan at seefeld.name Tue Nov 6 20:49:31 2012 From: stefan at seefeld.name (Stefan Seefeld) Date: Tue, 06 Nov 2012 14:49:31 -0500 Subject: [C++-sig] problem embedding python - very simple example In-Reply-To: <509967A2.3050208@codeware.com> References: <509967A2.3050208@codeware.com> Message-ID: <509969CB.9070104@seefeld.name> On 11/06/2012 02:40 PM, Cory Riddell wrote: > It prints "hello\nworld\n" then throws an exception executing the line > that creates the hello.txt file: > First-chance exception at 0x755ac41f in Test.exe: Microsoft C++ > exception: boost::python::error_already_set at memory location 0x0039fb60.. > Unhandled exception at 0x773915de (ntdll.dll) in Test.exe: Microsoft > C++ exception: boost::python::error_already_set at memory location > 0x0039fb60.. > > In the exec method, the exception is being thrown when result comes back > as null: > PyObject* result = PyRun_String(s, Py_file_input, global.ptr(), > local.ptr()); > if (!result) throw_error_already_set(); I suggest you wrap your C++ code in a try block, then catch the error_already_set error and inspect the Python exception using PyErr_Print() or somesuch. Stefan -- ...ich hab' noch einen Koffer in Berlin... From giuseppe.corbelli at copanitalia.com Wed Nov 7 08:56:38 2012 From: giuseppe.corbelli at copanitalia.com (Giuseppe Corbelli) Date: Wed, 07 Nov 2012 08:56:38 +0100 Subject: [C++-sig] export custom C++ exception to Python so it can beraisedby python In-Reply-To: <5099201E.5080604@cern.ch> References: <5097FCB0.2000102@cern.ch> <5098D39B.30604@copanitalia.com> <5099201E.5080604@cern.ch> Message-ID: <509A1436.7090200@copanitalia.com> On 06/11/2012 15:35, Martin Hellmich wrote: >> I've based my code on. >> http://stackoverflow.com/questions/9620268/boost-python-custom-exception-class > > I have used the same reference and also this suggestion for including the > other attributes shown here: > http://stackoverflow.com/questions/11448735/boostpython-export-custom-exception-and-inherit-from-pythons-exception > > However, this doesn't solve my problem. > It works fine when I throw the MyException in C++, but when I raise the > MyCPPException in Python, the translator is not called (which makes sense), so > the attribute 'cause' does not change. > > Actually, before I throw the MyException for the first time in C++, > MyCPPException does not have the attribute 'cause' at all. Of course. > This issue should be the same, if I set the exception with PyErr_SetObject, > since the translator is not called, when I 'raise' it. Very likely the SetString uses SetObject under the hood, but I never used SetObject. I'd try to: *) As you were writing before, declare a C++ class MyException: public std::exception { MyException(); MyException(int code, const std::string &string); int code(); const char* what(); int errorCode; std::string errorMessage; }; *) Wrap it as a bpy::class_ so you can use it like a standard exception with all the attributes you need. *) Register an exception translator as defined before. Maybe the to-python conversion performed in the exception translator does work even if you have wrapped MyException as a standard class. -- Giuseppe Corbelli WASP Software Engineer, Copan Italia S.p.A Phone: +390303666318 Fax: +390302659932 E-mail: giuseppe.corbelli at copanitalia.com From mhellmic at cern.ch Wed Nov 7 09:24:26 2012 From: mhellmic at cern.ch (Martin Hellmich) Date: Wed, 7 Nov 2012 09:24:26 +0100 Subject: [C++-sig] export custom C++ exception to Python so it can beraisedby python In-Reply-To: <509A1436.7090200@copanitalia.com> References: <5097FCB0.2000102@cern.ch> <5098D39B.30604@copanitalia.com> <5099201E.5080604@cern.ch> <509A1436.7090200@copanitalia.com> Message-ID: <509A1ABA.2050006@cern.ch> Hi Giuseppe, thank you for the detailed response! On 11/07/2012 08:56 AM, Giuseppe Corbelli wrote: > On 06/11/2012 15:35, Martin Hellmich wrote: >>> I've based my code on. >>> http://stackoverflow.c om/questions/9620268/boost-python-custom-exception-class >>> >> >> I have used the same reference and also this suggestion for including the >> other attributes shown here: >> http://stackoverflow.com/questions/11448735/boostpython-export-custom-exception-and-inherit-from-pythons-exception >> >> >> However, this doesn't solve my problem. >> It works fine when I throw the MyException in C++, but when I raise the >> MyCPPException in Python, the translator is not called (which makes >> sense), so >> the attribute 'cause' does not change. >> >> Actually, before I throw the MyException for the first time in C++, >> MyCPPException does not have the attribute 'cause' at all. > > Of course. > >> This issue should be the same, if I set the exception with >> PyErr_SetObject, >> since the translator is not called, when I 'raise' it. > > Very likely the SetString uses SetObject under the hood, but I never > used SetObject. > I'd try to: > > *) As you were writing before, declare a C++ class MyException: public > std::exception { > MyException(); > MyException(int code, const std::string &string); > > int code(); > const char* what(); > > int errorCode; > std::string errorMessage; > }; > *) Wrap it as a bpy::class_ so you can use it like a standard exception > with all the attributes you need. This is the part that I miss, I guess. How can I wrap a class so that it works like an exception in python? In the solutions on stackoverflow the translation to an exception only happens in the translator, meaning that I cannot call 'raise MyException' in python, as it doesn't inherit from (the python) Exception. PyErr_NewException has to be based on a type of python Exception, so I cannot base it on my c++ class, right? Have you ever used the 3rd parameter to specify a dict? Cheers Martin > *) Register an exception translator as defined before. Maybe the > to-python conversion performed in the exception translator does work > even if you have wrapped MyException as a standard class. > -- Martin Hellmich Information Technology Department mhellmic at cern.ch CERN +41 22 76 765 26 CH-1211 Geneva 23 From giuseppe.corbelli at copanitalia.com Wed Nov 7 10:32:28 2012 From: giuseppe.corbelli at copanitalia.com (Giuseppe Corbelli) Date: Wed, 07 Nov 2012 10:32:28 +0100 Subject: [C++-sig] export custom C++ exception to Python so it can beraisedbypython In-Reply-To: <509A1ABA.2050006@cern.ch> References: <5097FCB0.2000102@cern.ch> <5098D39B.30604@copanitalia.com> <5099201E.5080604@cern.ch> <509A1436.7090200@copanitalia.com> <509A1ABA.2050006@cern.ch> Message-ID: <509A2AAC.4080704@copanitalia.com> On 07/11/2012 09:24, Martin Hellmich wrote: >> *) As you were writing before, declare a C++ class MyException: public >> std::exception { >> MyException(); >> MyException(int code, const std::string &string); >> >> int code(); >> const char* what(); >> >> int errorCode; >> std::string errorMessage; >> }; >> *) Wrap it as a bpy::class_ so you can use it like a standard exception >> with all the attributes you need. > > This is the part that I miss, I guess. How can I wrap a class so that it works > like an exception in python? In the solutions on stackoverflow the translation > to an exception only happens in the translator, meaning that I cannot call > 'raise MyException' in python, as it doesn't inherit from (the python) Exception. I've had a closer look. What if we take WindowsError as an example? *) Define a new python exception CustomError with python/C APIs based on WindowsError *) Define a new C++ exception CustomException based on std::exception *) Register a translator from C++ to python using CustomError as a target Seems a reasonable approach. -- Giuseppe Corbelli WASP Software Engineer, Copan Italia S.p.A Phone: +390303666318 Fax: +390302659932 E-mail: giuseppe.corbelli at copanitalia.com From brandsmeier at gmx.de Wed Nov 7 11:28:58 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Wed, 7 Nov 2012 11:28:58 +0100 Subject: [C++-sig] buildin converter for long double In-Reply-To: References: Message-ID: Dear Jiang, thanks I got it to work by using my own version of `builtin_converters.hpp` locally together with the code that you recommended. -Holger On Tue, Nov 6, 2012 at 3:25 AM, jiangzuoyan at gmail.com wrote: > how about return a wrap type of long double to cheating boost/python, and > register youself converter as > > PyObject * o = PyArrayScalar_New(LongDouble); > PyLongDoubleScalarObject * s = (PyLongDoubleScalarObject*)o; > s->obval = value; > > Don't forget to call import_array() in your model init function. > > BTW, I think float128 is not useful, it's slow. and in most case, change > algorithm to avoid precision problem is another *better* approach. > > > Changsheng Jiang > > > On Mon, Nov 5, 2012 at 6:27 PM, Holger Brandsmeier > wrote: >> >> Dear list, >> >> the boost python buildin converter for long double: >> BOOST_PYTHON_TO_PYTHON_BY_VALUE(long double, >> ::PyFloat_FromDouble(x), &PyFloat_Type) >> converter/builtin_converters.hpp >> identifies long double, which on my system is a 128bit float, with a >> 64bit float in python. I would like to identify it with numpy`s >> float128. Can you give me any hings how I would be able to do that? >> >> -Holger >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From brandsmeier at gmx.de Thu Nov 8 12:23:32 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Thu, 8 Nov 2012 12:23:32 +0100 Subject: [C++-sig] From python converter for builtin types In-Reply-To: References: Message-ID: Dear list, where does boost python define the from python converters for buildin types such as double / PyLong_Type? With the help of Changsheng Jiang I was able to modify builtin_converters.hpp and was able to change a to python converter for long double to produce numpy.float128. I do not find the place where the from python conversion is done, though. -Holger From jiangzuoyan at gmail.com Thu Nov 8 14:38:01 2012 From: jiangzuoyan at gmail.com (jiangzuoyan at gmail.com) Date: Thu, 8 Nov 2012 21:38:01 +0800 Subject: [C++-sig] From python converter for builtin types In-Reply-To: References: Message-ID: I'm afraid of you can't, according to the code, it's implemented in builtin_converters.cpp: slot_rvalue_from_python(); Boost/Python using global registration only according type saw in C++, I don't known is it possible to overrides these. Changsheng Jiang On Thu, Nov 8, 2012 at 7:23 PM, Holger Brandsmeier wrote: > Dear list, > > where does boost python define the from python converters for buildin > types such as double / PyLong_Type? > > With the help of Changsheng Jiang I was able to modify > builtin_converters.hpp and was able to change a to python converter > for long double to produce numpy.float128. I do not find the place > where the from python conversion is done, though. > > -Holger > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From brandsmeier at gmx.de Sat Nov 10 00:38:58 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Sat, 10 Nov 2012 00:38:58 +0100 Subject: [C++-sig] From python converter for builtin types In-Reply-To: References: Message-ID: Dear list, well thanks to point out `builtin_converters.cpp`, I wasn't aware that that file exists. Usually everything is in header files. I tried to implement some converter using `slot_rvalue_from_python` in my own code. But I did not have any success with having my function called by boost python. Can I somehow deregister the converter for `long double` that boost python adds via `builtin_converters.cpp`? If I change the source for `builtin_converters.cpp` to not export any converter for `long double`. Can I then later in my code register a converter via `slot_rvalue_from_python` that will be called? I never compiled my own version of boost before, so it would help me to know if this would actually work. -Holger On Thu, Nov 8, 2012 at 2:38 PM, jiangzuoyan at gmail.com wrote: > I'm afraid of you can't, according to the code, it's implemented in > builtin_converters.cpp: > > slot_rvalue_from_python(); > > Boost/Python using global registration only according type saw in C++, I > don't known is it possible to overrides these. > > Changsheng Jiang > > > On Thu, Nov 8, 2012 at 7:23 PM, Holger Brandsmeier > wrote: >> >> Dear list, >> >> where does boost python define the from python converters for buildin >> types such as double / PyLong_Type? >> >> With the help of Changsheng Jiang I was able to modify >> builtin_converters.hpp and was able to change a to python converter >> for long double to produce numpy.float128. I do not find the place >> where the from python conversion is done, though. >> >> -Holger >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From planetmarshalluk at hotmail.com Mon Nov 12 00:40:17 2012 From: planetmarshalluk at hotmail.com (Andrew Marshall) Date: Sun, 11 Nov 2012 23:40:17 +0000 Subject: [C++-sig] boost::python::iterator throws exception when using std::vector Message-ID: Hi there, I've just started using Boost::Python. I was following the example given in the tutorial regarding iterators at http://www.boost.org/doc/libs/1_52_0/libs/python/doc/tutorial/doc/html/python/iterators.html I am attempting to fill a python::list from an std::vector with the following code, based on the snippet in the tutorial above: namespace py = boost::python; std::vector v;v.push_back(1); py::object get_iterator = py::iterator >(); py::object iter = get_iterator(v); py::list l(iter); However the line "py::object iter = get_iter(v);" throws the following exception in python : TypeError: No to_python (by-value) converter found for C++ type: class std::vector > Any help would be appreciated. Thanks,Andrew. ----------------------------- Andrew Marshall http://www.planetmarshall.co.uk -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sat Nov 17 16:25:59 2012 From: hapopen at gmail.com (simon zhang) Date: Sat, 17 Nov 2012 23:25:59 +0800 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. Message-ID: This is a complex problem. I provide some code. c++ ======================================= #include #include #include class Rectangle { public: Rectangle(); virtual ~Rectangle(); void set(int lg,int wd) { long=lg; width=wd; }; int area() { return long*width; } ; int long; int width; } void runtest( boost::python::object &func ) { Rectangle rt; rt.set(2,3); func( &rt ); // The parameter is a pointer of instance.The function come from python. } BOOST_PYTHON_MODULE(ctopython) { using namespace boost::python; def("runtest",runtest); } python ======================================= import ctopython def pfunc( ret ) print ret.long #how to get right from a C++ instance print ret.area() ctopython.runtest( pfunc ) #Pass parameters that is custom python object to c++ function ======================================= how to do? -------------- next part -------------- An HTML attachment was scrubbed... URL: From sybren at stuvel.eu Sat Nov 17 16:28:10 2012 From: sybren at stuvel.eu (=?UTF-8?Q?Sybren_A=2E_St=C3=BCvel?=) Date: Sat, 17 Nov 2012 16:28:10 +0100 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: On 17 November 2012 16:25, simon zhang wrote: > how to do? > So... what's your question exactly? -- Sybren A. St?vel http://stuvel.eu/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sat Nov 17 16:46:47 2012 From: hapopen at gmail.com (simon zhang) Date: Sat, 17 Nov 2012 23:46:47 +0800 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: > func( &rt ); How to make a parameter of c++ instance's pointer to a python function. > def pfunc( ret ) > print ret.long > print ret.area() How to use c++ instance in python code. parameter 2012/11/17 Sybren A. St?vel > On 17 November 2012 16:25, simon zhang wrote: > >> how to do? >> > > So... what's your question exactly? > > -- > Sybren A. St?vel > > http://stuvel.eu/ > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sybren at stuvel.eu Sat Nov 17 16:53:48 2012 From: sybren at stuvel.eu (=?UTF-8?Q?Sybren_A=2E_St=C3=BCvel?=) Date: Sat, 17 Nov 2012 16:53:48 +0100 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: On 17 November 2012 16:46, simon zhang wrote: > > func( &rt ); > How to make a parameter of c++ instance's pointer to a python function. > > > def pfunc( ret ) > > print ret.long > > print ret.area() > How to use c++ instance in python code. > parameter > Have you tried what you coded already? Did it give you any error messages? There is a lot of documentation out there (although not all of it equally clear) , have you tried that? -- Sybren A. St?vel http://stuvel.eu/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From brandsmeier at gmx.de Sat Nov 17 17:34:03 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Sat, 17 Nov 2012 17:34:03 +0100 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: Have a look at http://stackoverflow.com/questions/4331599/calling-python-functions-from-c On Sat, Nov 17, 2012 at 4:25 PM, simon zhang wrote: > This is a complex problem. I provide some code. > > c++ > ======================================= > #include > #include > #include > > class Rectangle { > public: > Rectangle(); > virtual ~Rectangle(); > void set(int lg,int wd) { long=lg; width=wd; }; > int area() { return long*width; } ; > int long; > int width; > } > > void runtest( boost::python::object &func ) { > Rectangle rt; > rt.set(2,3); > func( &rt ); // The parameter is a pointer of instance.The > function come from python. > } > > BOOST_PYTHON_MODULE(ctopython) { > using namespace boost::python; > def("runtest",runtest); > } > > python > ======================================= > import ctopython > > def pfunc( ret ) > print ret.long #how to get right from a C++ instance > print ret.area() > > ctopython.runtest( pfunc ) #Pass parameters that is custom python object > to c++ function > > ======================================= > how to do? > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From hapopen at gmail.com Sat Nov 17 18:02:29 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 18 Nov 2012 01:02:29 +0800 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: yes..I try it.. Error:undefined symbol:_ZRI9Rectangle 2012/11/17 Sybren A. St?vel > > On 17 November 2012 16:46, simon zhang wrote: > >> > func( &rt ); >> How to make a parameter of c++ instance's pointer to a python function. >> >> > def pfunc( ret ) >> > print ret.long >> > print ret.area() >> How to use c++ instance in python code. >> parameter >> > > Have you tried what you coded already? Did it give you any error messages? > There is a lot of documentation out there (although not all of it equally > clear) , have you tried that? > > -- > Sybren A. St?vel > > http://stuvel.eu/ > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sat Nov 17 18:11:56 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 18 Nov 2012 01:11:56 +0800 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: I have write more code.what documentation? http://www.boost.org/? Looks some confused.I have not found the description of this problem. BOOST_PYTHON_MODULE(ctopython) { using namespace boost::python; class_ ("Rectangle") .def("area",&Rectangle::area) ; def("runtest",runtest); } 2012/11/18 simon zhang > yes..I try it.. > Error:undefined symbol:_ZRI9Rectangle > > 2012/11/17 Sybren A. St?vel > >> >> On 17 November 2012 16:46, simon zhang wrote: >> >>> > func( &rt ); >>> How to make a parameter of c++ instance's pointer to a python function. >>> >>> > def pfunc( ret ) >>> > print ret.long >>> > print ret.area() >>> How to use c++ instance in python code. >>> parameter >>> >> >> Have you tried what you coded already? Did it give you any error >> messages? There is a lot of documentation out there (although not all of it >> equally clear) , have you tried that? >> >> -- >> Sybren A. St?vel >> >> http://stuvel.eu/ >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From brandsmeier at gmx.de Sat Nov 17 18:19:18 2012 From: brandsmeier at gmx.de (Holger Brandsmeier) Date: Sat, 17 Nov 2012 18:19:18 +0100 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: That error has nothing to do with boost python. Apparently you did not link against the definition for the class Rectangle. You need to link against the object file generated from the definiton of Rectangle, usually you would have a Rectangle.cpp and generate a Rectangle.o. Your python module needs to link against Rectangle.o. On Sat, Nov 17, 2012 at 6:02 PM, simon zhang wrote: > yes..I try it.. > Error:undefined symbol:_ZRI9Rectangle > > 2012/11/17 Sybren A. St?vel >> >> >> On 17 November 2012 16:46, simon zhang wrote: >>> >>> > func( &rt ); >>> How to make a parameter of c++ instance's pointer to a python function. >>> >>> > def pfunc( ret ) >>> > print ret.long >>> > print ret.area() >>> How to use c++ instance in python code. >>> >>> parameter >>> >>> >> >> Have you tried what you coded already? Did it give you any error messages? >> There is a lot of documentation out there (although not all of it equally >> clear) , have you tried that? >> >> -- >> Sybren A. St?vel >> >> http://stuvel.eu/ >> >> >> >> _______________________________________________ >> Cplusplus-sig mailing list >> Cplusplus-sig at python.org >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig From hapopen at gmail.com Sat Nov 17 18:29:28 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 18 Nov 2012 01:29:28 +0800 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: http://stackoverflow.com/questions/4331599/calling-python-functions-from-c This a simple problem.But they are not the same.My problem is: boost::python::object func; Rectangle ret; func(&ret); // How to make a parameter of c++ instance's pointer to a python function. 2012/11/18 Holger Brandsmeier > Have a look at > http://stackoverflow.com/questions/4331599/calling-python-functions-from-c > > On Sat, Nov 17, 2012 at 4:25 PM, simon zhang wrote: > > This is a complex problem. I provide some code. > > > > c++ > > ======================================= > > #include > > #include > > #include > > > > class Rectangle { > > public: > > Rectangle(); > > virtual ~Rectangle(); > > void set(int lg,int wd) { long=lg; width=wd; }; > > int area() { return long*width; } ; > > int long; > > int width; > > } > > > > void runtest( boost::python::object &func ) { > > Rectangle rt; > > rt.set(2,3); > > func( &rt ); // The parameter is a pointer of instance.The > > function come from python. > > } > > > > BOOST_PYTHON_MODULE(ctopython) { > > using namespace boost::python; > > def("runtest",runtest); > > } > > > > python > > ======================================= > > import ctopython > > > > def pfunc( ret ) > > print ret.long #how to get right from a C++ > instance > > print ret.area() > > > > ctopython.runtest( pfunc ) #Pass parameters that is custom python > object > > to c++ function > > > > ======================================= > > how to do? > > > > > > _______________________________________________ > > Cplusplus-sig mailing list > > Cplusplus-sig at python.org > > http://mail.python.org/mailman/listinfo/cplusplus-sig > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hapopen at gmail.com Sat Nov 17 18:44:23 2012 From: hapopen at gmail.com (simon zhang) Date: Sun, 18 Nov 2012 01:44:23 +0800 Subject: [C++-sig] a boost.python question about parameter that is a pointer of instance. In-Reply-To: References: Message-ID: thanks.I completed it...this is orther problem.The above code is correct.....Thanks for you... 2012/11/18 Holger Brandsmeier > That error has nothing to do with boost python. Apparently you did not > link against the definition for the class Rectangle. You need to link > against the object file generated from the definiton of Rectangle, > usually you would have a Rectangle.cpp and generate a Rectangle.o. > Your python module needs to link against Rectangle.o. > > On Sat, Nov 17, 2012 at 6:02 PM, simon zhang wrote: > > yes..I try it.. > > Error:undefined symbol:_ZRI9Rectangle > > > > 2012/11/17 Sybren A. St?vel > >> > >> > >> On 17 November 2012 16:46, simon zhang wrote: > >>> > >>> > func( &rt ); > >>> How to make a parameter of c++ instance's pointer to a python function. > >>> > >>> > def pfunc( ret ) > >>> > print ret.long > >>> > print ret.area() > >>> How to use c++ instance in python code. > >>> > >>> parameter > >>> > >>> > >> > >> Have you tried what you coded already? Did it give you any error > messages? > >> There is a lot of documentation out there (although not all of it > equally > >> clear) , have you tried that? > >> > >> -- > >> Sybren A. St?vel > >> > >> http://stuvel.eu/ > >> > >> > >> > >> _______________________________________________ > >> Cplusplus-sig mailing list > >> Cplusplus-sig at python.org > >> http://mail.python.org/mailman/listinfo/cplusplus-sig > > > > > > > > _______________________________________________ > > Cplusplus-sig mailing list > > Cplusplus-sig at python.org > > http://mail.python.org/mailman/listinfo/cplusplus-sig > _______________________________________________ > Cplusplus-sig mailing list > Cplusplus-sig at python.org > http://mail.python.org/mailman/listinfo/cplusplus-sig > -------------- next part -------------- An HTML attachment was scrubbed... URL: From chandlergegg at gmail.com Mon Nov 19 23:12:29 2012 From: chandlergegg at gmail.com (Chandler Gegg) Date: Mon, 19 Nov 2012 16:12:29 -0600 Subject: [C++-sig] please remove me from mailing list Message-ID: thank you -------------- next part -------------- An HTML attachment was scrubbed... URL: From greg.landrum at gmail.com Fri Nov 23 11:26:51 2012 From: greg.landrum at gmail.com (Greg Landrum) Date: Fri, 23 Nov 2012 11:26:51 +0100 Subject: [C++-sig] outdated info in the "projects using boost.python" docs Message-ID: Hi, A colleague pointed out to me that the "Project using Boost.Python" document (http://www.boost.org/doc/libs/1_52_0/libs/python/doc/projects.html) includes a link to Rational Discovery LLC, a company that no longer exists (we shut it down in 2006). That link, which now re-directs to some completely different organization) should probably be removed. The software we developed that uses boost.python to expose chemistry functionality in C++ to Python has been open-sourced as the rdkit (www.rdkit.org). If there's any interest in new additions to the "projects using boost.python" page, I would be happy to provide a blurb. Thanks, -greg From mhellmic at cern.ch Fri Nov 23 20:36:18 2012 From: mhellmic at cern.ch (Martin Hellmich) Date: Fri, 23 Nov 2012 20:36:18 +0100 Subject: [C++-sig] free() of null-pointing handle<> throws SIGABRT Message-ID: <50AFD032.8030906@cern.ch> Hi, in my exception handling (based on a tutorial, which I can dig up if it helps), I create handles for the exc, val, tb values of an exception thrown in python. The code is shown below in extractException(). It is a shortened version, which does not cover all possible cases. In some cases, val is defined, but tb is not. When then htb points to 0x0, the function fails with a SIGABRT as it returns. I believe that the free() for the htb fails, when it goes out of scope at the end of the function. I put the Error message and the GDB output at the end of the mail. The address at which the free() fails is likely the one of tb, as it sits inbetween exc and val in memory. Other calls to extractException() where tb is not null do not abort. Do you have an idea why the free() could fail? I am happy to post more information, if needed and would welcome any hints :) Cheers Martin void PythonExceptionHandler::extractException() throw (DmException) { using namespace boost::python; PyObject *exc,*val,*tb; PyErr_Fetch(&exc,&val,&tb); PyErr_NormalizeException(&exc,&val,&tb); handle<> hexc(exc),hval(allow_null(val)),htb(allow_null(tb)); int code = 0; std::string what = ""; if (PyObject_HasAttrString(val, "code") && PyObject_HasAttrString(val, "what")) { object oval(hval); extract get_code(oval.attr("code")()); extract get_what(oval.attr("what")()); code = get_code(); what = get_what(); throw DmException(code, what); } else { std::string excString; excString = extract(str(hexc)); throw DmException(DMLITE_SYSERR(DMLITE_UNKNOWN_ERROR), excString); } } Error Message: *** glibc detected *** /usr/lib64/dmlite/test/cpp/test-replicas: free(): invalid pointer: 0x0000003a4c5a0d60 *** GDB output: (gdb) p htb $6 = {m_p = 0x0} (gdb) p tb $7 = (PyObject *) 0x0 (gdb) info locals val = 0x3a4c5a0d80 tb = 0x0 hexc = {m_p = 0x3a4c579ea0} what = "" exc = 0x3a4c579ea0 hval = {m_p = 0x3a4c5a0d80} htb = {m_p = 0x0} code = -- Martin Hellmich Information Technology Department mhellmic at cern.ch CERN +41 22 76 765 26 CH-1211 Geneva 23 From wmamrak at gmail.com Thu Nov 29 01:54:18 2012 From: wmamrak at gmail.com (Wojciech Mamrak) Date: Thu, 29 Nov 2012 01:54:18 +0100 Subject: [C++-sig] Boost chooses wrong static library Message-ID: Hello, I have built Python's (2.7.3) debugging version under Windows, added "on" in user-config.jam, then run bjam: bjam -j4 --toolset=msvc --with-python python-debugging=on variant=debug link=static runtime-link=static,shared in a.cpp file I included boost python headers this way: #define BOOST_PYTHON_STATIC_LIB #define BOOST_DEBUG_PYTHON #include Since BOOST_DEBUG_PYTHON is defined, I linked against libboost_python-vc100-mt-gyd-1_52.lib, but when compiling, the linker complains: LINK : fatal error LNK1104: cannot open file 'libboost_python-vc100-mt-gd-1_52.lib' What am I missing? Why does boost expect different static library file? regards -------------- next part -------------- An HTML attachment was scrubbed... URL: From mhellmic at cern.ch Thu Nov 29 10:28:04 2012 From: mhellmic at cern.ch (Martin Hellmich) Date: Thu, 29 Nov 2012 10:28:04 +0100 Subject: [C++-sig] free() of null-pointing handle<> throws SIGABRT In-Reply-To: <50AFD032.8030906@cern.ch> References: <50AFD032.8030906@cern.ch> Message-ID: <50B72AA4.4060808@cern.ch> Hi, more investigation (single-stepping in gdb) lead to the discovery that the abrt comes from this line: (gdb) test-replicas: Objects/frameobject.c:633: PyFrame_New: Assertion `f->f_code == code' failed. .EE. Program received signal SIGABRT, Aborted. The complete stacktrace is this: (gdb) bt #0 0x0000003a3a2328a5 in raise () from /lib64/libc.so.6 #1 0x0000003a3a234085 in abort () from /lib64/libc.so.6 #2 0x0000003a3a22ba1e in __assert_fail_base () from /lib64/libc.so.6 #3 0x0000003a3a22bae0 in __assert_fail () from /lib64/libc.so.6 #4 0x0000003a4c26deeb in PyFrame_New () from /usr/lib64/libpython2.6.so.1.0 #5 0x0000003a4c2dfee9 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.6.so.1.0 #6 0x0000003a4c26edb0 in ?? () from /usr/lib64/libpython2.6.so.1.0 #7 0x0000003a4c244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0 #8 0x0000003a4c25970f in ?? () from /usr/lib64/libpython2.6.so.1.0 #9 0x0000003a4c244303 in PyObject_Call () from /usr/lib64/libpython2.6.so.1.0 #10 0x0000003a4c2d8dd3 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.6.so.1.0 #11 0x0000003a4c2f835a in PyEval_CallFunction () from /usr/lib64/libpython2.6.so.1.0 #12 0x00007ffff79529e6 in boost::python::call, std::allocator > > (callable=0x7ffff78f0fa0, a0=, a1=) at /usr/include/boost/python/call.hpp:66 #13 0x00007ffff795000e in operator(), std::allocator > > (this=, parent=0, name="/") at /usr/include/boost/python/object_call.hpp:19 #14 dmlite::PythonINode::extendedStat (this=, parent=0, name="/") at /usr/src/debug/dmlite-0.6.0/python/embedding/src/cpp/PythonINode.cpp:171 #15 0x0000003ab0a63a09 in dmlite::BuiltInCatalog::extendedStat (this=0x724640, path=, followSym=true) at /usr/src/debug/dmlite-0.6.0/src/core/builtin/Catalog.cpp:149 #16 0x0000003ab0a60d64 in dmlite::BuiltInCatalog::changeDir (this=0x724640, path="/") at /usr/src/debug/dmlite-0.6.0/src/core/builtin/Catalog.cpp:116 #17 0x0000000000406b4b in TestReplicas::setUp (this=0x620540) at /usr/src/debug/dmlite-0.6.0/tests/cpp/test-replicas.cpp:16 #18 0x0000003a3ce24d3a in CppUnit::TestCaseMethodFunctor::operator()() const () from /usr/lib64/libcppunit-1.12.so.1 #19 0x0000003a3ce1a064 in CppUnit::DefaultProtector::protect(CppUnit::Functor const&, CppUnit::ProtectorContext const&) () from /usr/lib64/libcppunit-1.12.so.1 #20 0x0000003a3ce21647 in CppUnit::ProtectorChain::protect(CppUnit::Functor const&, CppUnit::ProtectorContext const&) () from /usr/lib64/libcppunit-1.12.so.1 #21 0x0000003a3ce2a834 in CppUnit::TestResult::protect(CppUnit::Functor const&, CppUnit::Test*, std::basic_string, std::allocato r > const&) () from /usr/lib64/libcppunit-1.12.so.1 #22 0x0000003a3ce24989 in CppUnit::TestCase::run(CppUnit::TestResult*) () from /usr/lib64/libcppunit-1.12.so.1 #23 0x0000003a3ce250c3 in CppUnit::TestComposite::doRunChildTests(CppUnit::TestResult*) () from /usr/lib64/libcppunit-1.12.so.1 #24 0x0000003a3ce24fe6 in CppUnit::TestComposite::run(CppUnit::TestResult*) () from /usr/lib64/libcppunit-1.12.so.1 #25 0x0000003a3ce250c3 in CppUnit::TestComposite::doRunChildTests(CppUnit::TestResult*) () from /usr/lib64/libcppunit-1.12.so.1 #26 0x0000003a3ce24fe6 in CppUnit::TestComposite::run(CppUnit::TestResult*) () from /usr/lib64/libcppunit-1.12.so.1 #27 0x0000003a3ce2a5da in CppUnit::TestResult::runTest(CppUnit::Test*) () from /usr/lib64/libcppunit-1.12.so.1 #28 0x0000003a3ce2c553 in CppUnit::TestRunner::run(CppUnit::TestResult&, std::basic_string, std::allocator > const&) () from /usr/lib64/libcppunit-1.12.so.1 #29 0x0000003a3ce2ea2b in CppUnit::TextTestRunner::run(std::basic_string, std::allocator >, bool, bool, bool) () from /usr/lib64/libcppunit-1.12.so.1 #30 0x000000000040ef84 in testBaseMain (argn=, argv=) at /usr/src/debug/dmlite-0.6.0/tests/cpp/test-base.cpp:92 #31 0x0000003a3a21ecdd in __libc_start_main () from /lib64/libc.so.6 #32 0x00000000004063c9 in _start () On 11/23/2012 08:36 PM, Martin Hellmich wrote: > Hi, > > in my exception handling (based on a tutorial, which I can dig up if it > helps), I create handles for the exc, val, tb values of an exception > thrown in python. > The code is shown below in extractException(). It is a shortened > version, which does not cover all possible cases. > > In some cases, val is defined, but tb is not. When then htb points to > 0x0, the function fails with a SIGABRT as it returns. > I believe that the free() for the htb fails, when it goes out of scope > at the end of the function. > > I put the Error message and the GDB output at the end of the mail. > The address at which the free() fails is likely the one of tb, as it > sits inbetween exc and val in memory. > > Other calls to extractException() where tb is not null do not abort. > > Do you have an idea why the free() could fail? > I am happy to post more information, if needed and would welcome any > hints :) > > Cheers > Martin > > void PythonExceptionHandler::extractException() throw (DmException) > { > using namespace boost::python; > > PyObject *exc,*val,*tb; > PyErr_Fetch(&exc,&val,&tb); > PyErr_NormalizeException(&exc,&val,&tb); > handle<> hexc(exc),hval(allow_null(val)),htb(allow_null(tb)); > > int code = 0; > std::string what = ""; > > if (PyObject_HasAttrString(val, "code") && > PyObject_HasAttrString(val, "what")) { > object oval(hval); > extract get_code(oval.attr("code")()); > extract get_what(oval.attr("what")()); > code = get_code(); > what = get_what(); > throw DmException(code, what); > } else { > std::string excString; > excString = extract(str(hexc)); > throw DmException(DMLITE_SYSERR(DMLITE_UNKNOWN_ERROR), > excString); > } > } > > Error Message: > *** glibc detected *** /usr/lib64/dmlite/test/cpp/test-replicas: free(): > invalid pointer: 0x0000003a4c5a0d60 *** > > GDB output: > (gdb) p htb > $6 = {m_p = 0x0} > (gdb) p tb > $7 = (PyObject *) 0x0 > (gdb) info locals > val = 0x3a4c5a0d80 > tb = 0x0 > hexc = {m_p = 0x3a4c579ea0} > what = "" > exc = 0x3a4c579ea0 > hval = {m_p = 0x3a4c5a0d80} > htb = {m_p = 0x0} > code = > -- Martin Hellmich Information Technology Department mhellmic at cern.ch CERN +41 22 76 765 26 CH-1211 Geneva 23