Boost::Python 提取 -- 在 C++ 中访问 Python 字符串
Boost::Python Extract -- Accessing Python string in C++
我正在尝试使用 Boost Python 从 C++ 中的 Python 函数访问返回的字符串值。
我看到这个错误:
TypeError: No registered converter was able to produce a C++ rvalue of type std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > from this Python object of type function
错误是有道理的,Python 数据类型与 C++ 数据类型不匹配。但我不确定如何解决它。
C++代码为:
Py_Initialize();
try {
boost::python::object main = boost::python::import("__main__");
boost::python::object global(main.attr("__dict__"));
boost::python::object result = exec_file("/Users/tomconlin/Documents/495_496_Research/Current_Source_Code/Source_Files/test_hello.py", global, global);
boost::python::object test_hello = global["get_info"];
std::string message = boost::python::extract<std::string>(test_hello);
std::string m = message;
std::cout << message << std::endl;
return message;
}
catch (boost::python::error_already_set&) {
PyErr_Print();
}
Py_Finalize();
std::string error_string = "\nAn Error Occured\n";
return error_string;
Python代码是:
import sys
sys.path.append("/Users/tomconlin/anaconda/lib/python2.7/site-packages")
def get_info():
version = 2 # API version
block_io = BlockIo('2c18-7990-8d0d-7511', '7816902248', version)
credit_card_balance = block_io.get_address_balance(addresses='2Mytv6K7BkiBRuHhnHmVTTc51xyhLRNtDgr')
#print "Credit Card Balance Type:", type(credit_card_balance)
address = ""
for key in credit_card_balance:
address += str(credit_card_balance[key])
#print "This is the address:", address
address = address.split()
#print address
print "\n\n"
return_address = address[12].strip("u',")
print return_address
print "\n\n"
return return_address
def main():
get_info()
if __name__ == '__main__':
from block_io import BlockIo
main()
您可能希望调用函数并获取字符串形式的 return 值,而不是尝试将函数本身转换为字符串。
因此,您可能需要使用:
boost::python::extract<std::string>(test_hello());
而不是:
boost::python::extract<std::string>(test_hello);
这是一个通用的例子。在 hello.py
:
def hello_from_python ():
return 'Hello from Python!'
在example.cc
中:
#include <iostream>
#include <string>
#include <boost/python.hpp>
namespace py = boost::python;
int main (int argc, char * argv []) {
// Initialize the Python interpreter.
Py_Initialize();
// Add the current directory to the path.
py::import("sys").attr("path").attr("append")(".");
// Get the "hello_from_python" function from "hello.py".
py::object hello_from_python = py::import("hello").attr("hello_from_python");
// Call the "hello_from_python" and extract the return value as a string.
std::string return_value = py::extract<std::string>(hello_from_python());
// Print out the return value.
std::cout << "Return value: '" << return_value << "'" << std::endl;
}
编译,然后运行程序:
$ ./example
Return value: 'Hello from Python!'
我正在尝试使用 Boost Python 从 C++ 中的 Python 函数访问返回的字符串值。
我看到这个错误:
TypeError: No registered converter was able to produce a C++ rvalue of type std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > from this Python object of type function
错误是有道理的,Python 数据类型与 C++ 数据类型不匹配。但我不确定如何解决它。
C++代码为:
Py_Initialize();
try {
boost::python::object main = boost::python::import("__main__");
boost::python::object global(main.attr("__dict__"));
boost::python::object result = exec_file("/Users/tomconlin/Documents/495_496_Research/Current_Source_Code/Source_Files/test_hello.py", global, global);
boost::python::object test_hello = global["get_info"];
std::string message = boost::python::extract<std::string>(test_hello);
std::string m = message;
std::cout << message << std::endl;
return message;
}
catch (boost::python::error_already_set&) {
PyErr_Print();
}
Py_Finalize();
std::string error_string = "\nAn Error Occured\n";
return error_string;
Python代码是:
import sys
sys.path.append("/Users/tomconlin/anaconda/lib/python2.7/site-packages")
def get_info():
version = 2 # API version
block_io = BlockIo('2c18-7990-8d0d-7511', '7816902248', version)
credit_card_balance = block_io.get_address_balance(addresses='2Mytv6K7BkiBRuHhnHmVTTc51xyhLRNtDgr')
#print "Credit Card Balance Type:", type(credit_card_balance)
address = ""
for key in credit_card_balance:
address += str(credit_card_balance[key])
#print "This is the address:", address
address = address.split()
#print address
print "\n\n"
return_address = address[12].strip("u',")
print return_address
print "\n\n"
return return_address
def main():
get_info()
if __name__ == '__main__':
from block_io import BlockIo
main()
您可能希望调用函数并获取字符串形式的 return 值,而不是尝试将函数本身转换为字符串。
因此,您可能需要使用:
boost::python::extract<std::string>(test_hello());
而不是:
boost::python::extract<std::string>(test_hello);
这是一个通用的例子。在 hello.py
:
def hello_from_python ():
return 'Hello from Python!'
在example.cc
中:
#include <iostream>
#include <string>
#include <boost/python.hpp>
namespace py = boost::python;
int main (int argc, char * argv []) {
// Initialize the Python interpreter.
Py_Initialize();
// Add the current directory to the path.
py::import("sys").attr("path").attr("append")(".");
// Get the "hello_from_python" function from "hello.py".
py::object hello_from_python = py::import("hello").attr("hello_from_python");
// Call the "hello_from_python" and extract the return value as a string.
std::string return_value = py::extract<std::string>(hello_from_python());
// Print out the return value.
std::cout << "Return value: '" << return_value << "'" << std::endl;
}
编译,然后运行程序:
$ ./example
Return value: 'Hello from Python!'