Python的统一code对象和C API(从pyuni code对象检索的char *) [英] Python Unicode object and C API ( retrieving char* from pyunicode objects )

查看:176
本文介绍了Python的统一code对象和C API(从pyuni code对象检索的char *)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前所有绑定我的C ++引擎类来的Python游戏脚本的目的。
最新的挑战是说,当你犯了一个变量在脚本一个字​​符串,如

I am currently binding all of my C++ engine classes to python for game play scripting purposes. The latest challenge is that when say you make a variable in the script a string such as

string = 'hello world'

这成为PyUni codeObject的。下一步我们想从绑定的C面函数调用这个对象的脚本上的功能。 PrintToLog(串),作为一个例子让我们说这个C函数是这样

this becomes a PyUnicodeObject. Next we want to call a function on this object in the script from a bound C side function. PrintToLog( string ), as an example lets say this c-function is as such

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

所以我的绑定需要提取,这将首先被蟒蛇被传递给我的泛型函数处理,这反过来将提取和隐蔽的pyobjects到适当的C面型PyUni codeObject的一个char *并把它传递给我的功能。

So my binding needs to extract a char * from the PyUnicodeObject which will be passed at first by the python to my generic function handler, which in turn will extract and covert the pyobjects to the proper c-side type and pass it to my function.

我能找到一个提取wchar_t的* ...的唯一功能反正是有得到ASCII重新presentation,因为我们将只使用ASCII字符集。

The only function I can find extracts a wchar_t*... Is there anyway to get the ascii representation since we will only be using the ascii character set.

编辑:我使用Python 3.2,所有的字符串是单向code

I am using Python 3.2 where all strings are unicode

推荐答案

我相信你正在寻找的功能的 PyUni code_AsASCIIString 。这会给你一个非UNI code(ASCII)Python字符串。然后你就可以走正常的方式从该提取的char *

I believe the function you're looking for is PyUnicode_AsASCIIString. This will give you a non-unicode (ASCII) python string. And then you can take the normal approach for extracting a char* from that.

这篇关于Python的统一code对象和C API(从pyuni code对象检索的char *)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆