@@ -394,13 +394,12 @@ The append operation *must* behave this way, consider this Python code
394
394
l = []
395
395
a = 400
396
396
# The integer object '400' has a reference count of 1 as only
397
- # one symbol references it: ``a`` .
397
+ # one symbol references it: a .
398
398
l.append(a)
399
399
# The integer object '400' must now have a reference count of
400
- # 2 as two symbols reference it: ``a`` and ``l``,
401
- # specifically ``l[-1]``.
400
+ # 2 as two symbols reference it: a and l, specifically l[-1].
402
401
403
- The fix for this code is to do this, error checking omitted:
402
+ The fix is to create a temporary item and then decref * that * once appended ( error checking omitted) :
404
403
405
404
.. code-block :: c
406
405
@@ -411,14 +410,13 @@ The fix for this code is to do this, error checking omitted:
411
410
for (int i = 400; i < 405; ++i) {
412
411
/* Create the object to append to the list. */
413
412
temporary_item = PyLong_FromLong(i);
414
- /* Append it. This will increment the reference count. */
413
+ /* temporary_item->ob_refcnt == 1 now */
414
+ /* Append it. This will increment the reference count to 2. */
415
415
PyList_Append(list, temporary_item);
416
416
/* Decrement our reference to it leaving the list having the only reference. */
417
417
Py_DECREF(temporary_item);
418
- /* Implementation detail really. */
419
- assert(temporary_item->ob_refcnt == 1);
420
- /* Good practice... */
421
- temporary_item = NULL;
418
+ /* temporary_item->ob_refcnt == 1 now */
419
+ temporary_item = NULL; /* Good practice... */
422
420
}
423
421
Py_RETURN_NONE;
424
422
}
0 commit comments