[issue6616] PyList_APPEND (append without incref)

2009-08-02 Thread Raymond Hettinger

Raymond Hettinger rhettin...@users.sourceforge.net added the comment:

 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.

--
assignee:  - rhettinger
resolution:  - rejected
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6616
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6616] PyList_APPEND (append without incref)

2009-08-01 Thread Campbell Barton

New submission from Campbell Barton ideasma...@gmail.com:

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.

--
files: py3_APPEND.diff
keywords: patch
messages: 91167
nosy: ideasman42
severity: normal
status: open
title: PyList_APPEND (append without incref)
versions: Python 3.2
Added file: http://bugs.python.org/file14619/py3_APPEND.diff

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6616
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6616] PyList_APPEND (append without incref)

2009-08-01 Thread Raymond Hettinger

Raymond Hettinger rhettin...@users.sourceforge.net added the comment:

 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.

--
nosy: +rhettinger

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6616
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6616] PyList_APPEND (append without incref)

2009-08-01 Thread Campbell Barton

Campbell Barton ideasma...@gmail.com added the comment:

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

--

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6616
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com