(original) (raw)
2011/10/11 Hrvoje Niksic <hrvoje.niksic@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