From jab at ecs.soton.ac.uk Mon Feb 7 12:42:13 2011 From: jab at ecs.soton.ac.uk (Julian Bailey) Date: Mon, 7 Feb 2011 11:42:13 +0000 Subject: [C++-sig] Import causes access violation References: <853FC6B008030C479F8C5A0529576D0F80E00537@EXCHANGE4.ecs.soton.ac.uk> Message-ID: Hi, I am running through the basic examples on the boost website for embedding boost::python. My code currently reads: #include #include "./include/Python.h" #include "./boost/python.hpp" using namespace boost::python; int main(void) { Py_Initialize(); object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); object ignored = exec("print Hello world",main_namespace); return 0; } The program links successfully and begins running, however an access violation occurs (NULL pointer) at the first line after "py_initialize();" when i call import("__main__"); Visual studio 2010s debugger says the exception occurs at: line 74 in object_attributes.hpp: inline object const_attribute_policies::get(object const& target, char const* key) { return python::getattr(target, key); } Python was built using Visual studio 2010 so i had access to the python debug library. Boost is version 1.45 and was built using : bjam --build-type=complete msvc stage --with-python I am linking against: boost_python-vc100-mt-gd-1_45.lib and boost_python-vc100-mt-gd-1_45.dll I have looked for soutions on the internet and have found nothing right now. Can anyone help me? note: i am building my application in MSVC 2010, this should work though shouldn't it? Dr Julian Bailey Electronic Systems & Devices, School of Electronics and Computer Science, University of Southampton, Southampton, SO17 1BJ, United Kingdom. Tel. +44 (0)2380 593545 email: jab at ecs.soton.ac.uk -------------- next part -------------- An HTML attachment was scrubbed... URL: From jab at ecs.soton.ac.uk Tue Feb 8 07:43:52 2011 From: jab at ecs.soton.ac.uk (Julian Bailey) Date: Tue, 8 Feb 2011 06:43:52 +0000 Subject: [C++-sig] Import causes access violation In-Reply-To: References: <853FC6B008030C479F8C5A0529576D0F80E00537@EXCHANGE4.ecs.soton.ac.uk> <853FC6B008030C479F8C5A0529576D0F80E0105C@EXCHANGE4.ecs.soton.ac.uk> Message-ID: Fixed, I think...., needed to define BOOST_DEBUG_PYTHON in the c++ pre-processor definitions Now having a different problem...will get back to you guys if I still have problems Dr Julian Bailey Electronic Systems & Devices, School of Electronics and Computer Science, University of Southampton, Southampton, SO17 1BJ, United Kingdom. Tel. +44 (0)2380 593545 email: jab at ecs.soton.ac.uk From: cplusplus-sig-bounces+jab=ecs.soton.ac.uk at python.org [mailto:cplusplus-sig-bounces+jab=ecs.soton.ac.uk at python.org] On Behalf Of Julian Bailey Sent: 07 February 2011 11:42 To: cplusplus-sig at python.org Subject: [C++-sig] Import causes access violation Hi, I am running through the basic examples on the boost website for embedding boost::python. My code currently reads: #include #include "./include/Python.h" #include "./boost/python.hpp" using namespace boost::python; int main(void) { Py_Initialize(); object main_module = import("__main__"); object main_namespace = main_module.attr("__dict__"); object ignored = exec("print Hello world",main_namespace); return 0; } The program links successfully and begins running, however an access violation occurs (NULL pointer) at the first line after "py_initialize();" when i call import("__main__"); Visual studio 2010s debugger says the exception occurs at: line 74 in object_attributes.hpp: inline object const_attribute_policies::get(object const& target, char const* key) { return python::getattr(target, key); } Python was built using Visual studio 2010 so i had access to the python debug library. Boost is version 1.45 and was built using : bjam --build-type=complete msvc stage --with-python I am linking against: boost_python-vc100-mt-gd-1_45.lib and boost_python-vc100-mt-gd-1_45.dll I have looked for soutions on the internet and have found nothing right now. Can anyone help me? note: i am building my application in MSVC 2010, this should work though shouldn't it? Dr Julian Bailey Electronic Systems & Devices, School of Electronics and Computer Science, University of Southampton, Southampton, SO17 1BJ, United Kingdom. Tel. +44 (0)2380 593545 email: jab at ecs.soton.ac.uk -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael_andronov at sd-kyber.com Thu Feb 10 21:19:08 2011 From: michael_andronov at sd-kyber.com (Michael Andronov) Date: Thu, 10 Feb 2011 15:19:08 -0500 Subject: [C++-sig] C++ derived from bp::dict Message-ID: Hi, Being a newbie to boost::python, I have a question ( hopefully simple), which failed to find the answer in mail logs: let's say a. I have a C++ class, derived from boost::python::dict ; b. I would like later to get access to myLong, and to the dictionary, my class derived from. Would mentioning the boost::python::dict as a base wtihin wrapping section be enough to get access to dictionary? (Something like... " .... class myd :boost::python::dict { ... public: unsigned long myLong; } BOOST_PYTHON_MODULE(mymod) { using namespace boost::python; class_ > ("mydict") .def_readwrite("pd", &myd::pd); ... } Thanks. Michael. From seefeld at sympatico.ca Thu Feb 10 21:30:23 2011 From: seefeld at sympatico.ca (Stefan Seefeld) Date: Thu, 10 Feb 2011 15:30:23 -0500 Subject: [C++-sig] C++ derived from bp::dict In-Reply-To: References: Message-ID: <4D544ADF.6080101@sympatico.ca> Hi Michael, I strongly doubt that what you want is possible. While class_ generates a Python type that wraps 'A', boost::python::dict itself is not built that way. Rather, it is a proxy to a Python dict object. Types defined via "class_<>" are added to a type registrar, which is inspected whenever a C++<->Python conversion is requested. I strongly doubt that the boost::python::object derivatives are themselves registered there. Besides, Python has strong requirements concerning what types are allowed to derive from what base types (as far as the associated meta-classes are concerned), which I don't think are met by the above. Stefan -- ...ich hab' noch einen Koffer in Berlin... From michael_andronov at sd-kyber.com Thu Feb 10 22:03:37 2011 From: michael_andronov at sd-kyber.com (Michael Andronov) Date: Thu, 10 Feb 2011 16:03:37 -0500 Subject: [C++-sig] C++ derived from bp::dict In-Reply-To: <4D544ADF.6080101@sympatico.ca> References: <4D544ADF.6080101@sympatico.ca> Message-ID: Thanks for a speedy reply! Michael. From ephelon at gmail.com Wed Feb 16 04:12:15 2011 From: ephelon at gmail.com (James Emerton) Date: Tue, 15 Feb 2011 19:12:15 -0800 Subject: [C++-sig] Boost.Python Metaclass Message-ID: Given a C++ class wrapped with Boost.Python: class CPPClass { void test(); } BOOST_PYTHON_MODULE(testmodule) { class_("CPPClass") .def("test", &CPPClass::test) ; } I've created a python subclass and redefined the metaclass: class Meta(CPPClass.__class__): def __new__(cls, name, bases, attrs): return super(Meta, cls).__new__(cls, name, bases, attrs) class PyClass(CPPClass): __metaclass__ = Meta Any attempt to call methods of PyClass, or to pass it as an argument to a C++ function results in an ArgumentError. It appears that ob_type is being tested for equivalence to the Boost.Python.class type. I am currently working around this by using a plain function as the metaclass, but it's not ideal as each base class must set the metaclass explicitly. I browsed through object/class.cpp, and I think using PyType_IsSubtype() in place of == &class_metatype_object should be sufficient to allow me to derive metaclasses from Boost.Python.class. Does anyone have any advice, comments or objections before I proceed? James From rwgk at yahoo.com Wed Feb 16 08:08:40 2011 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Tue, 15 Feb 2011 23:08:40 -0800 (PST) Subject: [C++-sig] Boost.Python Metaclass In-Reply-To: References: Message-ID: <868446.79851.qm@web111403.mail.gq1.yahoo.com> Hi James, > I browsed through object/class.cpp, and I think using PyType_IsSubtype() > in place of == &class_metatype_object should be sufficient to allow me > to derive metaclasses from Boost.Python.class. Could (or did?) you try it out? Ideally based on the current boost svn trunk. If you send me the patches I'll check them in after testing locally. Could you try to fit your test example into one of the existing files in libs/python/test? Ralf From wichert at wiggy.net Mon Feb 21 11:36:52 2011 From: wichert at wiggy.net (Wichert Akkerman) Date: Mon, 21 Feb 2011 11:36:52 +0100 Subject: [C++-sig] Creating a unicode instrance from utf-8 encoded string Message-ID: <4D624044.70004@wiggy.net> I'm trying to do something which should be very simple, but I'm not having much luck figuring out how from the existing documentation. For a python 2 project I am trying to return a gettext-translated string as a unicode instance to python. The return value for gettext() is a UTF-8 encoded char*, which should be pretty simple to convert to a python unicode instrance using PyUnicode_FromString. I have a feeling this is trivial to do, but I can't seem to figure out how. Wichert. From hans_meine at gmx.net Mon Feb 21 14:33:03 2011 From: hans_meine at gmx.net (Hans Meine) Date: Mon, 21 Feb 2011 14:33:03 +0100 Subject: [C++-sig] Creating a unicode instrance from utf-8 encoded string In-Reply-To: <4D624044.70004@wiggy.net> References: <4D624044.70004@wiggy.net> Message-ID: <201102211433.03756.hans_meine@gmx.net> Am Montag, 21. Februar 2011, um 11:36:52 schrieb Wichert Akkerman: > I'm trying to do something which should be very simple, but I'm not > having much luck figuring out how from the existing documentation. > > For a python 2 project I am trying to return a gettext-translated string > as a unicode instance to python. The return value for gettext() is a > UTF-8 encoded char*, which should be pretty simple to convert to a > python unicode instrance using PyUnicode_FromString. I have a feeling > this is trivial to do, but I can't seem to figure out how. According to http://docs.python.org/c-api/unicode.html , this is exactly what PyUnicode_FromString is supposed to do. What's the problem? Do you get an error? Some code might help. HTH, Hans From wichert at wiggy.net Mon Feb 21 14:35:42 2011 From: wichert at wiggy.net (Wichert Akkerman) Date: Mon, 21 Feb 2011 14:35:42 +0100 Subject: [C++-sig] Creating a unicode instrance from utf-8 encoded string In-Reply-To: <201102211433.03756.hans_meine@gmx.net> References: <4D624044.70004@wiggy.net> <201102211433.03756.hans_meine@gmx.net> Message-ID: <4D626A2E.9080102@wiggy.net> On 2/21/11 14:33 , Hans Meine wrote: > Am Montag, 21. Februar 2011, um 11:36:52 schrieb Wichert Akkerman: >> I'm trying to do something which should be very simple, but I'm not >> having much luck figuring out how from the existing documentation. >> >> For a python 2 project I am trying to return a gettext-translated string >> as a unicode instance to python. The return value for gettext() is a >> UTF-8 encoded char*, which should be pretty simple to convert to a >> python unicode instrance using PyUnicode_FromString. I have a feeling >> this is trivial to do, but I can't seem to figure out how. > > According to http://docs.python.org/c-api/unicode.html , this is exactly what > PyUnicode_FromString is supposed to do. > > What's the problem? Do you get an error? Some code might help. For a single unicode instance this is indeed working correctly. Unfortunately it breaks down when I need to return a list of unicode instances. My simple approach looks like this: boost::python::list PyMyFunc() { std::vector raw_strings = BaseClass::MyFunc(); std::vector::const_iterator i; boost::python::list result; for (i=raw_strings.begin(); i!=raw_strings.end(); i++) result.append(PyUnicode_FromString(i->c_str())); return result; } but that results in a lot of compile errors suggesting that bp::list does not know how to handle PyObject* values. Wichert. From rwgk at yahoo.com Mon Feb 21 17:29:29 2011 From: rwgk at yahoo.com (Ralf W. Grosse-Kunstleve) Date: Mon, 21 Feb 2011 08:29:29 -0800 (PST) Subject: [C++-sig] Creating a unicode instrance from utf-8 encoded string In-Reply-To: <4D626A2E.9080102@wiggy.net> References: <4D624044.70004@wiggy.net> <201102211433.03756.hans_meine@gmx.net> <4D626A2E.9080102@wiggy.net> Message-ID: <457487.11242.qm@web111412.mail.gq1.yahoo.com> > result.append(PyUnicode_FromString(i->c_str())); try result.append(object(handle<>(PyUnicode_FromString(i->c_str())))); The handle<> is a way to tell Boost.Python if you own the reference or if it is borrowed (among other things). The suggested code assumes you own the reference. Ralf From ndbecker2 at gmail.com Tue Feb 22 16:30:12 2011 From: ndbecker2 at gmail.com (Neal Becker) Date: Tue, 22 Feb 2011 10:30:12 -0500 Subject: [C++-sig] Can't add attribute to function Message-ID: I can add attributes to instances of classes created by boost::python, but not to boost::python functions: limiter.delay = 0 AttributeError: 'Boost.Python.function' object has no attribute 'delay' Any ideas? From adrian.baetu at plato-ag.com Wed Feb 23 12:27:25 2011 From: adrian.baetu at plato-ag.com (AdrianB) Date: Wed, 23 Feb 2011 11:27:25 +0000 (UTC) Subject: [C++-sig] init< LPCTSTR > error if used in static member of same class Message-ID: Anny Idea why following situation failes to compile? class A { public: A( LPCTSTR szParam ){}; ~A(){}; }; class B: public A { public: B( LPCTSTR szParam ) A(szParam){}; ~B(){}; static void includePyModuleDefinition(); }; void B::includePyModuleDefinition() { class_("MyB", init< LPCTSTR >() ); } Compiler result is: error C2275: 'LPCTSTR' : illegal use of this type as an expression c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(391) : see declaration of 'LPCTSTR' error C2059: syntax error : ')' The eonly way to solve this issue by now are: - use no_init => not an option for me because I need the parameter - move the static member function out of the class B => unhappy because this would be a breach of my codeing std. for this module - remove the class inheritance for B to 'public A' The problem arises only if following three facts come togather: 1) inheritance from a base class 2) the includePyModuleDefinition() is a static member of the exposed class iteself 3) the init has to tale at least one param Regards, Adrian From rogeeff at gmail.com Sun Feb 27 08:54:57 2011 From: rogeeff at gmail.com (Gennadiy Rozental) Date: Sun, 27 Feb 2011 07:54:57 +0000 (UTC) Subject: [C++-sig] intercepting uncaught exceptions Message-ID: Hi, Is there any way to intercept uncaught exception in embedded Python code? I need to add some information to an exception/traceback in this case. I have full control over environment before embedded code is executed. I thought I'll be able to use sys.excepthook, but it does not seem to be invoked. Please advise, Gennadiy