[Python-Dev] Identifier API

Georg Brandl g.brandl at gmx.net
Sat Oct 8 18:15:23 CEST 2011


Am 08.10.2011 17:43, schrieb Antoine Pitrou:
> On Sat, 08 Oct 2011 16:54:06 +0200
> "Martin v. Löwis" <martin at v.loewis.de> wrote:
>> 
>> I find the ad-hoc approach of declaring and initializing variables
>> inadequate, in particular since it is difficult to clean up all
>> those string objects at interpreter shutdown.
>> 
>> I propose to add an explicit API to deal with such identifiers.
> 
> That sounds like a good idea.
> 
>> With this API,
>> 
>>      tmp = PyObject_CallMethod(result, "update", "O", other);
>> 
>> would be replaced with
>> 
>>       PyObject *tmp;
>>       Py_identifier(update);
>>       ...
>>       tmp = PyObject_CallMethodId(result, &PyId_update, "O", other);
> 
> Surely there is something missing to initialize the "const char *" in
> the structure? Or is "Py_identifier()" actually a macro?

Yes (note the parenthesized usage).

>> string will be initialized by the compiler, next and object on
>> first use. The new API for that will be
>> 
>> PyObject* PyUnicode_FromId(Py_Identifier*);
>> PyObject* PyObject_CallMethodId(PyObject*, Py_Identifier*, char*, ...);
>> PyObject* PyObject_GetAttrId(PyObject*, Py_Identifier*);
>> int PyObject_SetAttrId(PyObject*, Py_Identifier*, PyObject*);
>> int PyObject_HasAttrId(PyObject*, Py_Identifier*);
> 
> Do we want to make these APIs public?

Probably not at first.  I'd suggest making them private for Python 3.3, and if
the approach proves satisfying, we can think about adding them to the public
API in Python 3.4.

Georg



More information about the Python-Dev mailing list