[pypy-svn] r72517 - in pypy/trunk/pypy/module/cpyext: . test
xoraxax at codespeak.net
xoraxax at codespeak.net
Mon Mar 22 01:29:59 CET 2010
Author: xoraxax
Date: Mon Mar 22 01:29:57 2010
New Revision: 72517
Modified:
pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
pypy/trunk/pypy/module/cpyext/tupleobject.py
Log:
Implement reference stealing correctly.
Modified: pypy/trunk/pypy/module/cpyext/test/test_cpyext.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/test/test_cpyext.py (original)
+++ pypy/trunk/pypy/module/cpyext/test/test_cpyext.py Mon Mar 22 01:29:57 2010
@@ -232,13 +232,31 @@
printf("REFCNT %i %i\\n", refcnt, refcnt_after);
return PyBool_FromLong(refcnt_after == refcnt+2 && refcnt < 3);
}
+ static PyObject* foo_bar(PyObject* self, PyObject *args)
+ {
+ PyObject *true = Py_True;
+ PyObject *tup = NULL;
+ int refcnt = Py_REFCNT(true);
+ int refcnt_after;
+
+ tup = PyTuple_New(1);
+ Py_INCREF(true);
+ if (PyTuple_SetItem(tup, 0, true) < 0)
+ return NULL;
+ refcnt_after = Py_REFCNT(true);
+ printf("REFCNT2 %i %i\\n", refcnt, refcnt_after);
+ return PyBool_FromLong(refcnt_after == refcnt && refcnt < 3);
+ }
+
static PyMethodDef methods[] = {
{ "test_refcount", foo_pi, METH_NOARGS },
+ { "test_refcount2", foo_bar, METH_NOARGS },
{ NULL }
};
"""
module = self.import_module(name='foo', init=init, body=body)
assert module.test_refcount()
+ assert module.test_refcount2()
def test_init_exception(self):
Modified: pypy/trunk/pypy/module/cpyext/tupleobject.py
==============================================================================
--- pypy/trunk/pypy/module/cpyext/tupleobject.py (original)
+++ pypy/trunk/pypy/module/cpyext/tupleobject.py Mon Mar 22 01:29:57 2010
@@ -1,5 +1,6 @@
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.module.cpyext.api import cpython_api, PyObject, Py_ssize_t
+from pypy.module.cpyext.macros import Py_DECREF
from pypy.objspace.std.tupleobject import W_TupleObject
@@ -11,4 +12,5 @@
def PyTuple_SetItem(space, w_t, pos, w_obj):
assert isinstance(w_t, W_TupleObject)
w_t.wrappeditems[pos] = w_obj
+ Py_DECREF(space, w_obj) # SetItem steals a reference!
return 0
More information about the Pypy-commit
mailing list