开发者

Is there a method to convert PyUnicodeObject variable to PyObject type?

A value which is a PyUnicodeObject need to be passed to PyObject variable. Is there any conversion method for that?

thanks karnol开发者_开发知识库


You can just use a cast in your C code for this purpose:

PyUnicodeObject *p = ...whatever...;
callsomefun((PyObject*)p);

All the various specific, concrete types PyWhateverObject can be thought of as being "derived from" PyObject. Now C doesn't have the concept of inheritance so there's no "derived" in it, but the Python VM synthesizes it very simply, by ensuring every such object's first struct member (or first member's first member, or...) is a PyObject struct (there's a macro for that). This guarantees that normal C casting of pointers (although technically "risky" as the compiler cannot check that correctness -- if you cast the wrong thing you'll just crash during runtime;-) works as intended when used correctly between a pointer to PyObject and any pointer to a specific, concrete Python type struct.


PyUnicodeObject is a subset of PyObject so there shouldn't be any problem passing it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜