boost-python

Using Boost Python & std::shared_ptr

扶醉桌前 提交于 2019-12-04 16:26:38
问题 I'm trying to get Boost Python to play nicely with std::shared_ptr. Currently, I'm receiving this error: Traceback (most recent call last): File "test.py", line 13, in <module> comp.place_annotation(circle.centre()) TypeError: No to_python (by-value) converter found for C++ type: std::shared_ptr<cgl::Anchor> From calling circle.centre(), which returns an std::shared_ptr. I could change every std::shared_ptr to a boost::shared_ptr (which Boost Python plays nicely with) however the amount of

Embedding python and running for multiple times

▼魔方 西西 提交于 2019-12-04 13:12:04
I'm using boost::python to embed python, this is how I do it: void runCode(){ Py_Initialize(); //boost::python code goes here and embedded python code runs Py_Finalize(); } it runs nicely for the first time, but when it is run again, I get this error: LookupError: unknown encoding: utf8 and code does not run as expected, any help is appreciated. Uri Cohen Since you didn't get an expert answer, I'm offering my learning from working on a similar problem. Python have issues with reinitialization support . This is unfortunate if you need to restart the interpreter due to some error, or want to run

How do I pass a pre-populated “unsigned char*” buffer to a C++ method using boost.python?

↘锁芯ラ 提交于 2019-12-04 12:10:58
I have a C++ class with a member function that takes an unsigned char* buffer and an unsigned int length as arguments and operates on them. I've wrapped this class with Boost::Python and would like to pass a pre-populated buffer to the class from a Python script. The Python-side buffer is created with struct.pack. I can't figure out how to make the argument type match and keep getting Boost.Python.ArgumentError. include/Example.h #ifndef EXAMPLECLASS_H_ #define EXAMPLECLASS_H_ #include <cstdio> class ExampleClass { public: ExampleClass() {} virtual ~ExampleClass() {} void printBuffer(unsigned

running c++ code from python

断了今生、忘了曾经 提交于 2019-12-04 11:35:36
问题 I want to execute a code helloword.cpp which takes in some argument from console parses those arguments and then prints "hello world" in the console. Now, I want to parse these arguments from a python scripts parsearguments.py So for example: def parse_arguments: ...# some code return arguments Now, how do i communicate between python and c++. I have been reading and see that cython, boost python are the options but I have a hard time finding the right simple hello world example. Any

Unable to link against Boost.Python on OS X

可紊 提交于 2019-12-04 11:08:25
I am trying to build a really trivial example with Boost.Python. I have installed boost and boost-python with homebrew. I am using python 3.4.3 and boost 1.59. My OS is El Capitan. Boost.Python was installed with python3 like this: brew install boost-python --with-python3 I have 3 files: /* greet.hpp */ #ifndef BOOSTPYTHONHELLOWORLD_GREET_HPP #define BOOSTPYTHONHELLOWORLD_GREET_HPP char const* greet(); #endif //BOOSTPYTHONHELLOWORLD_GREET_HPP /* greet.cpp */ #include "greet.hpp" char const* greet() { return "Hello world"; } /* greet_ext.cpp */ #include "greet.hpp" #include <boost/python.hpp>

How to wrap a C++ function that returns boost::optional<T>?

爱⌒轻易说出口 提交于 2019-12-04 08:45:15
问题 I want to wrap a function that returns a boost::optional<T> . That is, given: class Foo { boost::optional<T> func(); }; I'd like to wrap that somehow so that Python either gets a T by value, or None : class_<Foo>("Foo") .def("func", func, return_value_policy<return_boost_optional???>); Normally if it just returned a T , I could use: class_<Foo>("Foo") .def("func", func, return_value_policy<return_by_value>()); But since it returns a boost::optional<T> , it could also return boost::none ,

Pickling an enum exposed by Boost.Python

主宰稳场 提交于 2019-12-04 08:01:57
Is it possible to pickle (using cPickle) an enum that has been exposed with Boost.Python? I have successfully pickled other objects using the first method described here , but none of that seems to apply for an enum type, and the objects don't seem to be pickleable by default. Not as they are in the module. I am given to understand that this is SUPPOSED to be possible, but the way the enum_ statement works prevents this. You can work around this on the python side. Somewhere (probably in a __init__.py file) do something like this: import yourmodule def isEnumType(o): return isinstance(o, type)

How does import work with Boost.Python from inside python files

∥☆過路亽.° 提交于 2019-12-04 05:46:27
I am using Boost.Python to embed an interpreter in my C++ executable and execute some prewritten scripts. I have got it working so that I can call functions in the python file but the python code I want to use imports external files and these imports fail because 'no module named '. If I run the script directly from python everything works as expected however. So my question is what is the correct way of importing modules in python scripts that are being run via C++ bindings? C++ Code: #include "boost/python.hpp" int main(int argc, char** argv) { try { Py_Initialize(); boost::python::object

How to import a function from python file by Boost.Python

青春壹個敷衍的年華 提交于 2019-12-04 05:32:23
I am totally new to boost.python. I reviewed a lot of recommending of using boost.python to apply with python, however still not easy to understand and find a solution for me. What I want is to import a function or class that directly from a python "SourceFile" Example File: Main.cpp MyPythonClass.py Let's says if there is a "Dog" class in "MyPythonClass.py" with "bark()" function, how do I get callback and send argument in cpp? I have no idea what I should do! Please help me! When one needs to call Python from C++, and C++ owns the main function, then one must embed the Python interrupter

Boost.python automatically convert parameter

你说的曾经没有我的故事 提交于 2019-12-04 04:34:01
问题 I am using boost.python to wrap a C++ class 'A' which takes a string as constructor. I then have a function 'fun(A& arg)' which takes a reference to an 'A' as parameter. I would like to have a python wrapper for 'fun' which is such that if I pass a variable which is a reference to a python string, this reference is first automatically converted to a reference to an 'A'. An example might help. On the python side, I would like to be able to do this: a = 'some string' fun(a) and then have 'a'