我想在我的C++应用程序中嵌入python.我正在使用Boost库 - 很棒的工具.但我有一个问题.
如果python函数抛出异常,我想抓住它并在我的应用程序中打印错误或获取一些详细信息,如python脚本中的行号导致错误.
我该怎么做?我找不到任何函数来获取Python API或Boost中的详细异常信息.
try {
module=import("MyModule"); //this line will throw excetion if MyModule contains an error
} catch ( error_already_set const & ) {
//Here i can said that i have error, but i cant determine what caused an error
std::cout << "error!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
PyErr_Print()只是将错误文本输出到stderr并清除错误,因此无法解决问题
我在c ++中有一个从python调用的方法,需要返回一个python列表对象.
我已经创建了这个方法,它附加到一个公开的类,现在可以从python中调用...(它返回void).
所以问题是,如何从这里创建一个python列表:
std::vector<std::string> results;
我真的不明白构造函数如何从这个文档中工作:
http://www.boost.org/doc/libs/1_37_0/libs/python/doc/v2/list.html
另外......我真的不想返回那种包装的矢量......我只是想用矢量中的字符串值创建一个新的python列表.
我很抱歉,如果这是重复...我找到了很多矢量问题的列表,但我找不到任何关于创建一个新的python列表.
我可以扩展这个问题,以包括一些其他问题,如:
从a创建一个新的python字典std::map<std::string, std::string>,依此类推.
我正在开发一个使用Boost.Python嵌入Python解释器的应用程序.这用于运行与主程序交互的用户生成的"脚本".
不幸的是,一个用户在尝试运行脚本时报告运行时错误R6034.主程序启动正常,但我认为加载python27.dll时可能会出现问题.
我使用的是Visual Studio 2005,Python 2.7和Boost.Python 1.46.1.问题仅发生在一个用户的计算机上.我以前处理过明显的问题,并设法解决它们,但在这种情况下,我有点不知所措.
有没有其他人遇到过类似的问题?你能解决吗?怎么样?
visual-studio-2005 manifest boost-python visual-c++ python-2.7
有一些问题,现在我已经阅读了以下内容:
我已经尝试在我的桌面上安装boost,并按照链接方面建议的帖子完成.我有以下代码:
#include <boost/python.hpp>
#include <Python.h>
using namespace boost::python;
Run Code Online (Sandbox Code Playgroud)
现在我尝试连接以下内容:
g++ testing.cpp -I /usr/include/python2.7/pyconfig.h -L /usr/include/python2.7/Python.h
-lpython2.7
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下内容:
g++ testing.cpp -I /home/username/python/include/ -L /usr/include/python2.7/Python.h -lpython2.7
Run Code Online (Sandbox Code Playgroud)
我一直收到以下错误:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such
file or directory
# include <pyconfig.h>
Run Code Online (Sandbox Code Playgroud)
我不知道我哪里错了.我确实安装了boost.python,链接只是一个问题?
我有一个用C++编写的类接口.我有一些实现此接口的类也是用C++编写的.这些是在更大的C++程序的上下文中调用的,它基本上实现了"main".我希望能够在Python中编写这个接口的实现,并允许它们在更大的C++程序的上下文中使用,就好像它们只是用C++编写的一样.
有很多关于连接python和C++的文章,但我无法弄清楚如何做我想要的.我能找到的最近的是:http://www.cs.brown.edu/~jwicks/boost/libs/python/doc/tutorial/doc/html/python/exposing.html#python.class_virtual_functions,但这不是没错.
更具体地说,假设我有一个现有的C++接口定义如下:
// myif.h
class myif {
public:
virtual float myfunc(float a);
};
Run Code Online (Sandbox Code Playgroud)
我想要做的是:
// mycl.py
... some magic python stuff ...
class MyCl(myif):
def myfunc(a):
return a*2
Run Code Online (Sandbox Code Playgroud)
然后,回到我的C++代码中,我希望能够说出类似的话:
// mymain.cc
void main(...) {
... some magic c++ stuff ...
myif c = MyCl(); // get the python class
cout << c.myfunc(5) << endl; // should print 10
}
Run Code Online (Sandbox Code Playgroud)
我希望这很清楚;)
我正在尝试编写cmake规则来使用linux上的boost.python为python构建动态加载库.我想将'foo'用于python模块名称.因此,必须调用库foo.so.但是默认情况下,cmake使用标准规则进行库命名,所以如果我写的话
add_library(foo foo.cpp)
Run Code Online (Sandbox Code Playgroud)
我会得到libfoo.so输出.甚至set_target_properties(foo PROPERTIES OUTPUT_NAME "foobar")
会创造libfoobar.so.
如何改变这种行为?
最后我可以使用[]运算符在python中使用std :: vector.诀窍是简单地在boost C++包装器中提供一个处理内部向量内容的容器:
#include <boost/python.hpp>
#include <vector>
class world
{
std::vector<double> myvec;
void add(double n)
{
this->myvec.push_back(n);
}
std::vector<double> show()
{
return this->myvec;
}
};
BOOST_PYTHON_MODULE(hello)
{
class_<std::vector<double> >("double_vector")
.def(vector_indexing_suite<std::vector<double> >())
;
class_<World>("World")
.def("show", &World::show)
.def("add", &World::add)
;
}
Run Code Online (Sandbox Code Playgroud)
另一个挑战是:如何将python列表转换为std :: vectors?我试图添加一个c ++类,期望std :: vector作为参数,并添加了相应的包装代码:
#include <boost/python.hpp>
#include <vector>
class world
{
std::vector<double> myvec;
void add(double n)
{
this->myvec.push_back(n);
}
void massadd(std::vector<double> ns)
{
// Append ns to this->myvec
}
std::vector<double> show()
{
return this->myvec;
}
};
BOOST_PYTHON_MODULE(hello)
{ …Run Code Online (Sandbox Code Playgroud) 我想从c ++代码中返回一些数据作为numpy.array对象.我看了一下boost::python::numeric,但它的文档非常简洁.我可以得到一个例子,例如将一个(不是很大)返回vector<double>给python吗?我不介意做数据的副本.
[me@hostname python]$ cat hello_world.cc
#include <string>
#include <Python.h>
#include <boost/python.hpp>
namespace {
std::string greet() { return "Helloworld"; }
}
using namespace boost::python;
BOOST_PYTHON_MODULE(hello_world)
{
def("greet",greet);
}
[me@hostnmae python]$ g++ -c -fPIC hello_world.cc -I/path/to/boost/headers -I/path/to/python/headers -o hello_world.o
[me@hostname python]$ g++ -shared -Wl,-soname,libhello_world.so -o libhello_world.so hello_world.o
[me@hostname python]$ python
Python 2.7.1 (r271:86832, Jan 10 2011, 09:46:57)
[GCC 3.4.5 20051201 (Red Hat 3.4.5-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('.')
>>> import hello_world
Traceback …Run Code Online (Sandbox Code Playgroud) 我已经使用Boost :: Python了一段时间,而且一切都很好.然而昨天我试图找出为什么我认为我注册的特定类型(一个元组)在我尝试从Python访问它时给了我错误.
事实证明,虽然元组实际上已经注册,但是当试图通过std::vector包裹来访问它时,vector_indexing_suite这已经不够了.
我在想,为什么它不起作用?有没有办法让这项工作?我应该尝试用手包裹矢量吗?
以下是我的MVE:
#include <tuple>
#include <vector>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>
template <typename T>
struct TupleToPython {
TupleToPython() {
boost::python::to_python_converter<T, TupleToPython<T>>();
}
template<int...>
struct sequence {};
template<int N, int... S>
struct generator : generator<N-1, N-1, S...> { };
template<int... S>
struct generator<0, S...> {
using type = sequence<S...>;
};
template <int... I>
static boost::python::tuple boostConvertImpl(const T& t, sequence<I...>) {
return boost::python::make_tuple(std::get<I>(t)...);
}
template <typename... Args>
static boost::python::tuple boostConvert(const std::tuple<Args...> & t) {
return …Run Code Online (Sandbox Code Playgroud)