[Python-Dev] Identifier API

Amaury Forgeot d'Arc amauryfa at gmail.com
Tue Oct 11 14:45:46 CEST 2011


2011/10/11 Hrvoje Niksic <hrvoje.niksic at avl.com>

> 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);
>  }
>

It should also check for errors; in this case the initialization is a bit
more verbose:
if (PY_IDENTIFIER_INIT(update) < 0)
   <return NULL or return -1 or goto error>;

-- 
Amaury Forgeot d'Arc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20111011/a4062ce3/attachment.html>


More information about the Python-Dev mailing list