Python:获取 PyObject 的字符串表示形式?
我有一个 C python 扩展,我想打印一些诊断信息。
我收到一个 PyObject*
形式的字符串。
获取该对象的字符串表示形式的规范方法是什么,以便它可以用作 const char * ?
I've got a C python extension, and I would like to print out some diagnostics.
I'm receiving a string as a PyObject*
.
What's the canonical way to obtain a string representation of this object, such that it usable as a const char *
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
PyObject_Repr
(模仿 Python 的repr
函数)或PyObject_Str
(模仿str
),然后调用PyString_AsString
来获取char *
(您可以而且通常应该将其用作const char*
,例如:此方法适用于任何
>PyObject
。如果您绝对确定yourObject
是一个 Python 字符串而不是其他东西,例如数字,您可以跳过第一行,只需执行以下操作:Use
PyObject_Repr
(to mimic Python'srepr
function) orPyObject_Str
(to mimicstr
), and then callPyString_AsString
to getchar *
(you can, and usually should, use it asconst char*
, for example:This method is OK for any
PyObject
. If you are absolutely sureyourObject
is a Python string and not something else, like for instance a number, you can skip the first line and just do:如果您使用的是 Python 3,以下是正确答案:
Here is the correct answer if you are using Python 3:
如果您只需要在 Python 3 中打印对象,您可以使用以下函数之一:
If you need just print the object in Python 3 you can use one of these functions:
尝试使用
PyObject_Repr
(模仿 Python 的repr
)或PyObject_Str
(模仿 Python 的str
)。文件:
Try
PyObject_Repr
(to mimic Python'srepr
) orPyObject_Str
(to mimic Python'sstr
).Docs:
对于 python >=3.3:
是的,这是一个非常量指针,您可以通过它修改(不可变)字符串。
For python >=3.3:
Yes, this is a non-const pointer, you can potentially modify the (immutable) string via it.
PyObject *模块名称;
PyUnicode_AsUTF8(模块名称)
PyObject *module_name;
PyUnicode_AsUTF8(module_name)
对于任意
PyObject*
,首先调用PyObject_Repr()
或PyObject_Str()
获取PyUnicode*
对象。在 Python 3.3 及更高版本中,调用
PyUnicode_AsUTF8AndSize
。除了需要const char *
的 Python 字符串之外,此函数还需要一个可选地址来存储长度。Python字符串是具有显式长度字段的对象,可能包含空字节,而 < code>const char* 本身通常是指向以 null 结尾的 C 字符串的指针。将 Python 字符串转换为 C 字符串可能是有损操作。因此,可以从字符串返回
const char*
的所有其他 Python C-API 函数是 已弃用。如果您不介意在字符串恰好包含嵌入的空字节时丢失一堆字符串,则可以为
size
参数传递NULL
。例如,打印
但是您也可以传入
size
变量的地址,与const char*
一起使用,以确保您获得整个字符串。在我的终端上,输出
For an arbitrary
PyObject*
, first callPyObject_Repr()
orPyObject_Str()
to get aPyUnicode*
object.In Python 3.3 and up, call
PyUnicode_AsUTF8AndSize
. In addition to the Python string you want aconst char *
for, this function takes an optional address to store the length in.Python strings are objects with explicit length fields that may contain null bytes, while a
const char*
by itself is typically a pointer to a null-terminated C string. Converting a Python string to a C string is a potentially lossy operation. For that reason, all the other Python C-API functions that could return aconst char*
from a string are deprecated.If you do not care about losing a bunch of the string if it happens to contain an embedded null byte, you can pass
NULL
for thesize
argument. For example,prints
But you can also pass in the address of a
size
variable, to use with theconst char*
, to make sure that you’re getting the entire string.On my terminal, that outputs