Boost Python 实现C调用python错误解决方法
由于Boost Python跟不上Python版本更新,如下方法调用可能产生TypeError: 'NoneType' object does not support item assignment异常。
Boost Python文档中例子可能产生异常。
Py_Initialize();
object main_module = import("__main__");
object main_dict = main_module.attr("__dict__");
try{
object result = exec("result = 5 + 5",main_dict);
int five_squared = extract<int>(main_dict["result"]);
cout<<"extract value : "<<five_squared<<endl;
cin>>ij;
}
catch(error_already_set){
PyErr_Print();
cin>>ij;
}
可以如下方法解决
Py_Initialize();
object main_module = import("__main__");
object main_dict = main_module.attr("__dict__");
try{
handle<> ignored((PyRun_String(
"result = 5 ** 2"
, Py_file_input
, main_dict.ptr()
, main_dict.ptr())
));
int five_squared = extract<int>(main_dict["result"]);
cout<<"extract value : "<<five_squared<<endl;
cin>>ij;
}
catch(error_already_set){
PyErr_Print();
cin>>ij;
}
Boost Python中执行表达式函数也可以正常运行。
Py_Initialize();
object main_module = import("__main__");
object main_dict = main_module.attr("__dict__");
try{
object result = eval("5 + 5",main_dict);
int five_squared = extract<int>(result);
cout<<"extract value : "<<five_squared<<endl;
cin>>ij;
}
catch(error_already_set){
相关文档:
Delphi 与 C/C++ 数据类型对照表
Delphi数据类型C/C++
ShorInt
8位有符号整数
char
Byte
8位无符号整数
BYTE,unsigned short
SmallInt
16位有符号整数
short
Word
16位无符号整数
unsigned short
Integer,LongInt
32位有符号整数
int,long
Cardinal,LongWord/DWORD
32位无符号整数
unsigned long
Int6 ......
Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable.
There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various man ......
client:
import socket, sys
if __name__ == '__main__':
#处理参数
argv = sys.argv
if (len(argv)!=3) or (len(argv)==2 and argv[1]=='/?'):
print '>>>Useage:', argv[0], '<address> < ......
import sys
import os
import datetime
import time
class ArgsDealwith:
def arg_environment(self, args):
filepath = ('PYTHON_PATH', 'path')
for i in filepath:
&nbs ......
Python中的文件操作以及输入输出
我们可以分别使用raw_input和print语句来完成这些功能。对于输出,你也可以使用多种多样的str(字符串)类。例如,你能够使用rjust方法来得到一个按一定宽度右对齐的字符串。利用help(str)获得更多详情。
另一个常用的输入/输出类型是处理文件。创建、读和写文件的能力是 ......