[Python-Dev] Identifier API (original) (raw)

Hrvoje Niksic hrvoje.niksic at avl.com
Tue Oct 11 14:36:56 CEST 2011


On 10/08/2011 04:54 PM, "Martin v. Löwis" wrote:

tmp = PyObjectCallMethod(result, "update", "O", other);

would be replaced with PyObject *tmp; Pyidentifier(update); ... tmp = PyObjectCallMethodId(result,&PyIdupdate, "O", other);

An alternative I am fond of is to to avoid introducing a new type, and simply initialize a PyObject * and register its address. For example:

PyObject *tmp; static PyObject *s_update; // pick a naming convention

PY_IDENTIFIER_INIT(update); tmp = PyObject_CallMethodObj(result, s_update, "O", other);

(but also PyObject_GetAttr(o, s_update), etc.)

PY_IDENTIFIER_INIT(update) might expand to something like:

if (!s_update) { s_update = PyUnicode_InternFromString("update"); _Py_IdentifierAdd(&s_update); }

_PyIdentifierAdd adds the address of the variable to a global set of C variables that need to be decreffed and zeroed-out at interpreted shutdown.

The benefits of this approach is:

Hrvoje



More information about the Python-Dev mailing list