小编Dan*_*117的帖子

Python Unicode对象和C API(从pyunicode对象中检索char*)

我目前正在将所有C++引擎类绑定到python以用于游戏脚本编写目的.最新的挑战是,当你说在脚本中创建一个变量时,如

string = 'hello world'
Run Code Online (Sandbox Code Playgroud)

这成为一个PyUnicodeObject.接下来,我们要在绑定的C端函数的脚本中调用此对象的函数.PrintToLog(字符串),作为一个例子,我们可以说这个c函数是这样的

void PrintToLog( const char * thisString )
{
   //file IO stuff as expected
   myLog << thisString;
   //more stuff involving fileIO
}
Run Code Online (Sandbox Code Playgroud)

所以我的绑定需要从PyUnicodeObject中提取一个char*,它首先会被python传递给我的泛型函数处理程序,后者又会将pyobjects提取并转换为正确的c-side类型并将其传递给我的函数.

我能找到的唯一函数提取wchar_t*...无论如何都得到ascii表示,因为我们只使用ascii字符集.

编辑:我使用的是Python 3.2,其中所有字符串都是unicode

python string unicode binding ascii

10
推荐指数
2
解决办法
5071
查看次数

检查PyObjects C类型

我使用的是Python 3.2和C++.

我需要提取当前存储在PyObject中的C类型.我已检查过文档并用谷歌搜索它,似乎没有其他人需要这样做.

所以我有一个PyObject,并试图提取C值.我有实际需要从对象中提取值的函数列表,但我首先需要知道的是存储在对象本身中以调用正确的函数.

以防这有助于理解这里是我正在尝试的一些示例代码.

//Variable is a custom variant type to allow for generic functionality.
Variable ExtractArgument( PyObject * arg )
{
  Variable value;
  PyObject* result;
  //now here is the problem, I need to know which Python function to call in order to
  //extract the correct type;
  value = PyLong_FromLong( arg );
  //or 
  value = PyFloat_FromDouble( arg )
  //ect.
  return value;
}
Run Code Online (Sandbox Code Playgroud)

希望我能看到类似这样的东西

Variable ExtractArgument( PyObject * arg )
{
  Variable value;
  PyObject* result;
  //PyType is not the actual …
Run Code Online (Sandbox Code Playgroud)

c++ python python-c-api

5
推荐指数
1
解决办法
5564
查看次数

标签 统计

python ×2

ascii ×1

binding ×1

c++ ×1

python-c-api ×1

string ×1

unicode ×1