classification
Title: PyList_APPEND (append without incref)
Type: Stage:
Components: Versions: Python 3.2
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: ideasman42, rhettinger
Priority: normal Keywords: patch

Created on 2009-08-01 20:43 by ideasman42, last changed 2009-08-02 21:44 by rhettinger. This issue is now closed.

Files
File name Uploaded Description Edit
py3_APPEND.diff ideasman42, 2009-08-01 20:43 patch on r74276
Messages (4)
msg91167 - (view) Author: Campbell Barton (ideasman42) * Date: 2009-08-01 20:43
This patch was made on python r74276

Often when writing in C/Python I want to append to a list within a C
loop of an unknown length.
When this is done for newly created PyObject (which is quite common) -
you need to assign each item to a variable and then decref it.

eg:
 PyObject *item= PyFloat_FromDouble(x);
 PyList_Append(list, item);
 Py_DECREF(item);

I have seen people make mistakes with this (in pygame code and
blender3d), and run  PyList_Append(list, PyFloat_FromDouble(x)),
ofcourse this is not the fault of python/c api that devs do not read
docs properly but I think it would be very convenient to have an append
that works in a similar way to PyList_SET_ITEM

This simple patch allows...
 PyList_APPEND(list, PyFloat_FromDouble(x))

doc included.
msg91169 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-08-01 21:05
> This simple patch allows...
>  PyList_APPEND(list, PyFloat_FromDouble(x))

This isn't a good idea because the error checking 
for the inner function is lost.  The current API
encourages keeping one function per line so that 
the ref counting and error-checking are more obvious.

Also, I'm not too keen on adding second-ways-to-do-it.
The C API is already somewhat fat, making it harder
to learn and remember.  The current API has standard
patterns of creating and consuming references.
Introducing variants that don't follow those patterns
makes it harder to review code and determine that it 
is correct.

It is better to learn to use the API as designed than
to commingle two different styles.
msg91179 - (view) Author: Campbell Barton (ideasman42) * Date: 2009-08-02 03:02
Hi Raymond, in general I agree with your comments that adding 2 ways to
do things is not great. The reason I still think this is an advantage is
that I know the API well enough (with manipulating dicts/lists etc) and
I still want this function for the sake of convenience.

For the same reason that PyList_SET_ITEM is useful, this is useful too.

Loosing the error checking is a disadvantage but in many cases API's
dont do error checking for every new item allocated.

Python's own code does this in a few cases with PyList_SET_ITEM...
eg.
 ./Python/_warnings.c:857: PyList_SET_ITEM(filters, 1,
create_filter(PyExc_ImportWarning, "ignore"))
./Python/Python-ast.c:2757: PyList_SET_ITEM(value, i,
ast2obj_cmpop((cmpop_ty)asdl_seq_GET(o->v.Compare.ops, i)));
msg91207 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-08-02 21:44
> I know the API well enough (with manipulating dicts/lists etc) 
> and I still want this function for the sake of convenience.

Given that your level of skill is higher than the average user,
I recommend adding this to your own standard libraries or headers.
No need to muck-up the environment for everyone else.
History
Date User Action Args
2009-08-02 21:44:27rhettingersetstatus: open -> closed
assignee: rhettinger
resolution: rejected
messages: + msg91207
2009-08-02 03:02:13ideasman42setmessages: + msg91179
2009-08-01 21:05:20rhettingersetnosy: + rhettinger
messages: + msg91169
2009-08-01 20:43:46ideasman42create