pyobject

Pickling a Python Extension type defined as a C struct having PyObject* members

廉价感情. 提交于 2019-12-04 03:29:41
问题 I am running C++ code via Python and would like to pickle an extension type. So I have a C++ struct (py_db_manager) containing pointers to a database object and a object manager object (both written in C++) that I wrapped with a python type object (t_db_manager). My problem is that this python type needs to know how to pickle the two pointers in order to send it to some child multicore processes. So I registered the type with the copy_reg module (this is equivalent to writing a reduce ()

how does Cpython implement its type Objects, i.e. type's type is always type?

允我心安 提交于 2019-12-03 14:54:06
问题 I understand that everything in python is an Object and that the 'type' (or class) of these object is 'type'. Plus the type of type is also type itself. (as explained nicely here) What I do not understand is how is this circular reference implemented? So i looked here. To quote the portion which might explain what I am looking for: PyTypeObject* PyObject.ob_type This is the type’s type, in other words its metatype. It is initialized by the argument to the PyObject_HEAD_INIT macro, and its

Check if PyObject is None

一世执手 提交于 2019-12-03 05:50:35
I would just like to check if a PyObject that I have is None . I naively expected that any None Pyobject * returned from a function would be a NULL pointer, but that doesn't seem to be the case. So: how do I check if a PyObject * of mine points to a None object? I know that there are macros like PyInt_Check(PyObject *) around, but I couldn't find anything like PyNone_Check . I thought I could just check the equality between my PyObject and Py_None , but turns out I don't even know how to make equality comparisons with this library. You can just compare directly with Py_None using == : if (obj

how does Cpython implement its type Objects, i.e. type's type is always type?

跟風遠走 提交于 2019-12-03 04:58:06
I understand that everything in python is an Object and that the 'type' (or class) of these object is 'type'. Plus the type of type is also type itself. (as explained nicely here ) What I do not understand is how is this circular reference implemented? So i looked here . To quote the portion which might explain what I am looking for: PyTypeObject* PyObject.ob_type This is the type’s type, in other words its metatype. It is initialized by the argument to the PyObject_HEAD_INIT macro, and its value should normally be &PyType_Type. However, for dynamically loadable extension modules that must be

Python: get string representation of PyObject?

女生的网名这么多〃 提交于 2019-11-29 21:19:50
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 rep of this object, such that it usable as a const char *? update: clarified to emphasize access as const char *. Use PyObject_Repr (to mimic Python's repr function) or PyObject_Str (to mimic str ), and then call PyString_AsString to get char * (you can, and usually should, use it as const char* , for example: PyObject* objectsRepresentation = PyObject_Repr(yourObject); const char* s = PyString_AsString(objectsRepresentation); This

Python: get string representation of PyObject?

坚强是说给别人听的谎言 提交于 2019-11-28 17:24:10
问题 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 rep of this object, such that it usable as a const char *? update: clarified to emphasize access as const char *. 回答1: Use PyObject_Repr (to mimic Python's repr function) or PyObject_Str (to mimic str ), and then call PyString_AsString to get char * (you can, and usually should, use it as const char* , for example: PyObject*