[pypy-commit] pypy cpyext-avoid-roundtrip: (antocuni, arigo, ronan): rewrite Py_DecRef in C
antocuni
pypy.commits at gmail.com
Sun Oct 1 10:58:06 EDT 2017
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: cpyext-avoid-roundtrip
Changeset: r92531:730786d85e6f
Date: 2017-10-01 16:55 +0200
http://bitbucket.org/pypy/pypy/changeset/730786d85e6f/
Log: (antocuni, arigo, ronan): rewrite Py_DecRef in C
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -596,7 +596,7 @@
'Py_FrozenFlag', 'Py_TabcheckFlag', 'Py_UnicodeFlag', 'Py_IgnoreEnvironmentFlag',
'Py_DivisionWarningFlag', 'Py_DontWriteBytecodeFlag', 'Py_NoUserSiteDirectory',
'_Py_QnewFlag', 'Py_Py3kWarningFlag', 'Py_HashRandomizationFlag', '_Py_PackageContext',
- '_PyTraceMalloc_Track', '_PyTraceMalloc_Untrack', 'PyMem_Malloc',
+ '_PyTraceMalloc_Track', '_PyTraceMalloc_Untrack', 'PyMem_Malloc', 'Py_DecRef',
]
TYPES = {}
FORWARD_DECLS = []
@@ -1381,6 +1381,7 @@
source_dir / "pythread.c",
source_dir / "missing.c",
source_dir / "pymem.c",
+ source_dir / "object.c",
]
def build_eci(code, use_micronumpy=False, translating=False):
diff --git a/pypy/module/cpyext/include/object.h b/pypy/module/cpyext/include/object.h
--- a/pypy/module/cpyext/include/object.h
+++ b/pypy/module/cpyext/include/object.h
@@ -47,7 +47,9 @@
#define Py_XDECREF(op) do { if ((op) == NULL) ; else Py_DECREF(op); } while (0)
#endif
-#define Py_CLEAR(op) \
+PyAPI_FUNC(void) Py_DecRef(PyObject *);
+
+#define Py_CLEAR(op) \
do { \
if (op) { \
PyObject *_py_tmp = (PyObject *)(op); \
diff --git a/pypy/module/cpyext/pyobject.py b/pypy/module/cpyext/pyobject.py
--- a/pypy/module/cpyext/pyobject.py
+++ b/pypy/module/cpyext/pyobject.py
@@ -336,9 +336,10 @@
def Py_IncRef(space, obj):
incref(space, obj)
- at cpython_api([PyObject], lltype.Void)
-def Py_DecRef(space, obj):
- decref(space, obj)
+## @cpython_api([PyObject], lltype.Void)
+## def Py_DecRef(space, obj):
+## decref(space, obj)
+Py_DecRef = decref # XXX remove me and kill all the Py_DecRef usages from RPython
@cpython_api([PyObject], lltype.Void)
def _Py_NewReference(space, obj):
diff --git a/pypy/module/cpyext/src/object.c b/pypy/module/cpyext/src/object.c
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/src/object.c
@@ -0,0 +1,10 @@
+/* Generic object operations; and implementation of None (NoObject) */
+
+#include "Python.h"
+
+void
+Py_DecRef(PyObject *o)
+{
+ Py_XDECREF(o);
+}
+
diff --git a/pypy/module/cpyext/test/test_memoryobject.py b/pypy/module/cpyext/test/test_memoryobject.py
--- a/pypy/module/cpyext/test/test_memoryobject.py
+++ b/pypy/module/cpyext/test/test_memoryobject.py
@@ -4,7 +4,7 @@
from pypy.module.cpyext.test.test_api import BaseApiTest
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from rpython.rlib.buffer import StringBuffer
-from pypy.module.cpyext.pyobject import make_ref, from_ref
+from pypy.module.cpyext.pyobject import make_ref, from_ref, decref
from pypy.module.cpyext.memoryobject import PyMemoryViewObject
only_pypy ="config.option.runappdirect and '__pypy__' not in sys.builtin_module_names"
@@ -31,8 +31,8 @@
w_f = space.wrap(f)
assert space.eq_w(space.getattr(w_mv, w_f),
space.getattr(w_memoryview, w_f))
- api.Py_DecRef(ref)
- api.Py_DecRef(w_memoryview)
+ decref(space, ref)
+ decref(space, w_memoryview)
class AppTestPyBuffer_FillInfo(AppTestCpythonExtensionBase):
def test_fillWithObject(self):
diff --git a/pypy/module/cpyext/test/test_traceback.py b/pypy/module/cpyext/test/test_traceback.py
--- a/pypy/module/cpyext/test/test_traceback.py
+++ b/pypy/module/cpyext/test/test_traceback.py
@@ -1,6 +1,6 @@
from rpython.rtyper.lltypesystem import lltype, rffi
from pypy.module.cpyext.test.test_api import BaseApiTest
-from pypy.module.cpyext.pyobject import PyObject, make_ref, from_ref
+from pypy.module.cpyext.pyobject import PyObject, make_ref, from_ref, decref
from pypy.module.cpyext.pytraceback import PyTracebackObject
from pypy.interpreter.pytraceback import PyTraceback
from pypy.interpreter.baseobjspace import AppExecCache
@@ -39,6 +39,6 @@
assert lltype.normalizeptr(py_traceback) is None
- api.Py_DecRef(py_obj)
+ decref(space, py_obj)
# hack to allow the code object to be freed
del space.fromcache(AppExecCache).content[src]
diff --git a/pypy/module/cpyext/test/test_tupleobject.py b/pypy/module/cpyext/test/test_tupleobject.py
--- a/pypy/module/cpyext/test/test_tupleobject.py
+++ b/pypy/module/cpyext/test/test_tupleobject.py
@@ -1,6 +1,6 @@
import py
-from pypy.module.cpyext.pyobject import PyObject, PyObjectP, make_ref, from_ref
+from pypy.module.cpyext.pyobject import PyObject, PyObjectP, make_ref, from_ref, decref
from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from rpython.rtyper.lltypesystem import rffi, lltype
@@ -24,7 +24,7 @@
def test_tuple_realize_refuses_nulls(self, space, api):
py_tuple = api.PyTuple_New(1)
py.test.raises(FatalError, from_ref, space, py_tuple)
- api.Py_DecRef(py_tuple)
+ decref(space, py_tuple)
def test_tuple_resize(self, space, api):
w_42 = space.wrap(42)
@@ -43,7 +43,7 @@
assert space.int_w(space.len(w_tuple)) == 2
assert space.int_w(space.getitem(w_tuple, space.wrap(0))) == 42
assert space.int_w(space.getitem(w_tuple, space.wrap(1))) == 43
- api.Py_DecRef(ar[0])
+ decref(space, ar[0])
py_tuple = api.PyTuple_New(3)
rffi.cast(PyTupleObject, py_tuple).c_ob_item[0] = make_ref(space, w_42)
@@ -59,7 +59,7 @@
assert space.int_w(space.len(w_tuple)) == 10
for i in range(10):
assert space.int_w(space.getitem(w_tuple, space.wrap(i))) == 42 + i
- api.Py_DecRef(ar[0])
+ decref(space, ar[0])
lltype.free(ar, flavor='raw')
@@ -71,7 +71,7 @@
w_tuple = from_ref(space, py_tuple)
assert space.eq_w(w_tuple, space.newtuple([space.wrap(42),
space.wrap(43)]))
- api.Py_DecRef(py_tuple)
+ decref(space, py_tuple)
def test_getslice(self, space, api):
w_tuple = space.newtuple([space.wrap(i) for i in range(10)])
diff --git a/pypy/module/cpyext/test/test_typeobject.py b/pypy/module/cpyext/test/test_typeobject.py
--- a/pypy/module/cpyext/test/test_typeobject.py
+++ b/pypy/module/cpyext/test/test_typeobject.py
@@ -3,7 +3,7 @@
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
from pypy.module.cpyext.test.test_api import BaseApiTest
from pypy.module.cpyext.api import generic_cpy_call
-from pypy.module.cpyext.pyobject import make_ref, from_ref
+from pypy.module.cpyext.pyobject import make_ref, from_ref, decref
from pypy.module.cpyext.typeobject import PyTypeObjectPtr
class AppTestTypeObject(AppTestCpythonExtensionBase):
@@ -487,7 +487,7 @@
assert py_type.c_tp_alloc
assert from_ref(space, py_type.c_tp_mro).wrappeditems is w_class.mro_w
- api.Py_DecRef(ref)
+ decref(space, ref)
def test_type_dict(self, space, api):
w_class = space.appexec([], """():
@@ -515,7 +515,7 @@
return C
""")
ref = make_ref(space, w_class)
- api.Py_DecRef(ref)
+ decref(space, ref)
def test_lookup(self, space, api):
w_type = space.w_bytes
@@ -531,7 +531,7 @@
import _numpypy
return _numpypy.multiarray.dtype('int64').type(2)""")
ref = make_ref(space, w_obj)
- api.Py_DecRef(ref)
+ decref(space, ref)
class AppTestSlots(AppTestCpythonExtensionBase):
def setup_class(cls):
More information about the pypy-commit
mailing list