[C++-sig] Boost::Python and Functions with String Inputs
Hesam
hesam.ilati2 at gmail.com
Fri Aug 24 17:32:11 CEST 2012
*Dear Sirs,*
*Goal:* I'm trying to use python interactively in my c++ code using
Boost::Python library. My goal is to change variables of some class which I
have defined in c++ from the python interpreter.
The code is attached below. Actually this comes from Boost::Python
tutorials
http://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html
*Problem:* I can load the library in python interface (i.e. load hello) and
make an object out of it (obj = hello.World()). I even can access functions
( obj.greet() ) but when I want to access functions with string input
(obj.set("Hello") ) I get memory access violation ("Access violation
reading location 0xffffffffffffffff"). *The problem is just with string
inputs. i.e. if I change function set(string) to set(char*) it works fine. *
If instead of interpreter I use PyRun_SimpleString("import hello;
w=hello.World(); w.set('Hi there');\n"); I get error "Expression
_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)". The point is when I make
breakpoint inside the function value of string is not what I want.
*Details*: Visual studio 2010 (MDd mode), Debug Mode, x64, Win7
struct World
{
void set(string _msg) {}
string greet() { return msg; }
string msg;
};
typedef boost::shared_ptr<World> World_ptr;
BOOST_PYTHON_MODULE(hello)
{
bp::class_<World, World_ptr>("World")
.def("greet", &World::greet)
.def("set", &World::set)
;
}
int main(int argc, char **argv)
{
Py_Initialize();
bp::object main =
bp::object(bp::handle<>(bp::borrowed(PyImport_AddModule("__main__"))));
bp::object global(main.attr("__dict__"));
inithello();
// Bring up Python interpreter
Py_Main(argc, argv);
Py_Finalize();
return 0;
}
Actually this comes from Boost::Python tutorials with some modification to
use Python interpreter
http://www.boost.org/doc/libs/1_51_0/libs/python/doc/tutorial/doc/html/python/exposing.html
Many thanks
[image: Inline image 1]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120824/432935c3/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 42055 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20120824/432935c3/attachment-0001.png>
More information about the Cplusplus-sig
mailing list