[pypy-dev] [PATCH] Rework borrowed-ref bookkeeping, to fix a crash

Greg Price greg at quora.com
Sat Mar 12 23:34:30 CET 2011


On Sat, Mar 12, 2011 at 6:40 AM, Amaury Forgeot d'Arc
<amauryfa at gmail.com> wrote:
> 2011/3/12 Greg Price <greg at quora.com>:
>> +                 """
>> +                    PyObject *t = PyTuple_New(1);
>> +                    PyObject *i = PyInt_FromLong(42);
>> +                    PyTuple_SetItem(t, 0, i);
>> +                    i = PyTuple_GetItem(t, 0);
>> +                    PyTuple_GetItem(t, 0);
>> +                    Py_DECREF(t);
>> +                    return PyInt_FromLong(PyInt_AsLong(i));
>> +                 """),
>
> This example is wrong: you don't own the reference to int(42);
> after the tuple is released, PyInt_AsLong(i) is illegal.

Yes, that's the intent of the test; see the comment just below. An
earlier version of my patch would have left 'i' alive, so I wanted to
check that in a test. This C code returns 42 if the int stays alive, 0
if it is destroyed when it should be.

This way of testing is a little ugly, though, so if you can suggest a
better way to check for the InvalidPointerException I'd be glad to
hear it.

Greg



More information about the Pypy-dev mailing list