Author: Armin Rigo <[email protected]>
Branch: cpyext-gc-support
Changeset: r80369:7fd91c0e2266
Date: 2015-10-21 09:33 +0200
http://bitbucket.org/pypy/pypy/changeset/7fd91c0e2266/
Log: Add some object-management functions of pyobject to INTERPLEVEL_API
to make them easier to access from tests (useful in pdb to call them
even if the current module didn't explicitly import them)
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
@@ -378,7 +378,7 @@
TYPES[configname] = forward
return forward
-INTERPLEVEL_API = {}
+INTERPLEVEL_API = {} # only for untranslated tests
FUNCTIONS = {}
def constant_pyobj(space, name):
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
@@ -5,7 +5,8 @@
from rpython.rtyper.extregistry import ExtRegistryEntry
from pypy.module.cpyext.api import (
cpython_api, bootstrap_function, PyObject, PyObjectP, ADDR,
- CANNOT_FAIL, Py_TPFLAGS_HEAPTYPE, PyTypeObjectPtr, is_PyObject)
+ CANNOT_FAIL, Py_TPFLAGS_HEAPTYPE, PyTypeObjectPtr, is_PyObject,
+ INTERPLEVEL_API)
from pypy.module.cpyext.state import State
from pypy.objspace.std.typeobject import W_TypeObject
from pypy.objspace.std.objectobject import W_ObjectObject
@@ -280,16 +281,19 @@
assert not is_pyobj(w_obj)
return w_obj.cpyext_as_pyobj(space)
as_pyobj._always_inline_ = True
+INTERPLEVEL_API['as_pyobj'] = as_pyobj
def as_xpyobj(space, w_obj):
if w_obj is not None:
return as_pyobj(space, w_obj)
else:
return lltype.nullptr(PyObject.TO)
+INTERPLEVEL_API['as_xpyobj'] = as_xpyobj
def pyobj_has_w_obj(pyobj):
return rawrefcount.to_obj(W_Root, pyobj) is not None
+INTERPLEVEL_API['pyobj_has_w_obj'] = staticmethod(pyobj_has_w_obj)
@specialize.ll()
def from_pyobj(space, pyobj):
@@ -301,6 +305,7 @@
w_obj = _create_w_obj_from_pyobj(space, pyobj)
return w_obj
from_pyobj._always_inline_ = True
+INTERPLEVEL_API['from_pyobj'] = from_pyobj
@specialize.ll()
def from_xpyobj(space, pyobj):
@@ -308,6 +313,7 @@
return from_pyobj(space, pyobj)
else:
return None
+INTERPLEVEL_API['from_xpyobj'] = from_xpyobj
def is_pyobj(x):
@@ -317,6 +323,7 @@
return True
else:
raise TypeError(repr(type(x)))
+INTERPLEVEL_API['is_pyobj'] = staticmethod(is_pyobj)
class Entry(ExtRegistryEntry):
_about_ = is_pyobj
@@ -342,6 +349,7 @@
if not is_pyobj(obj):
keepalive_until_here(obj)
return pyobj
+INTERPLEVEL_API['get_pyobj_and_incref'] = get_pyobj_and_incref
@specialize.ll()
def get_pyobj_and_xincref(space, obj):
@@ -349,6 +357,7 @@
return get_pyobj_and_incref(space, obj)
else:
return lltype.nullptr(PyObject.TO)
+INTERPLEVEL_API['get_pyobj_and_xincref'] = get_pyobj_and_xincref
@specialize.ll()
def get_w_obj_and_decref(space, obj):
@@ -367,6 +376,7 @@
assert pyobj.c_ob_refcnt >= rawrefcount.REFCNT_FROM_PYPY
keepalive_until_here(w_obj)
return w_obj
+INTERPLEVEL_API['get_w_obj_and_decref'] = get_w_obj_and_decref
@specialize.ll()
@@ -381,6 +391,7 @@
ob.c_ob_type = ob_type
ob.c_ob_pypy_link = 0
return ob
+INTERPLEVEL_API['new_pyobj'] = staticmethod(new_pyobj)
def make_ref(space, w_obj):
diff --git a/pypy/module/cpyext/test/test_number.py
b/pypy/module/cpyext/test/test_number.py
--- a/pypy/module/cpyext/test/test_number.py
+++ b/pypy/module/cpyext/test/test_number.py
@@ -1,7 +1,6 @@
from rpython.rtyper.lltypesystem import lltype
from pypy.module.cpyext.test.test_api import BaseApiTest
-from pypy.module.cpyext.pyobject import PyObjectP, as_pyobj
-from pypy.module.cpyext.pyobject import get_w_obj_and_decref
+from pypy.module.cpyext.pyobject import PyObjectP
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
class TestIterator(BaseApiTest):
@@ -44,12 +43,12 @@
w_obj1 = space.wrap(123)
w_obj2 = space.wrap(456.789)
pp1 = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
- pp1[0] = as_pyobj(space, w_obj1)
+ pp1[0] = api.as_pyobj(w_obj1)
pp2 = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
- pp2[0] = as_pyobj(space, w_obj2)
+ pp2[0] = api.as_pyobj(w_obj2)
assert api.PyNumber_Coerce(pp1, pp2) == 0
- w_res1 = get_w_obj_and_decref(space, pp1[0])
- w_res2 = get_w_obj_and_decref(space, pp2[0])
+ w_res1 = api.get_w_obj_and_decref(pp1[0])
+ w_res2 = api.get_w_obj_and_decref(pp2[0])
lltype.free(pp1, flavor='raw')
lltype.free(pp2, flavor='raw')
assert space.str_w(space.repr(w_res1)) == '123.0'
@@ -60,14 +59,14 @@
w_objf = space.wrap(42.5)
ppl = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
ppf = lltype.malloc(PyObjectP.TO, 1, flavor='raw')
- ppl[0] = as_pyobj(space, w_objl)
- ppf[0] = as_pyobj(space, w_objf)
+ ppl[0] = api.as_pyobj(w_objl)
+ ppf[0] = api.as_pyobj(w_objf)
ret = api.PyNumber_CoerceEx(ppl, ppf)
assert ret == 0
- w_resl = get_w_obj_and_decref(space, ppl[0])
- w_resf = get_w_obj_and_decref(space, ppf[0])
+ w_resl = api.get_w_obj_and_decref(ppl[0])
+ w_resf = api.get_w_obj_and_decref(ppf[0])
lltype.free(ppl, flavor='raw')
lltype.free(ppf, flavor='raw')
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,8 +1,7 @@
import py
from rpython.rlib.rawrefcount import REFCNT_FROM_PYPY_LIGHT
-from pypy.module.cpyext.pyobject import PyObject, PyObjectP, from_pyobj
-from pypy.module.cpyext.pyobject import pyobj_has_w_obj, get_pyobj_and_incref
+from pypy.module.cpyext.pyobject import PyObject, PyObjectP
from pypy.module.cpyext.test.test_api import BaseApiTest
from rpython.rtyper.lltypesystem import rffi, lltype
@@ -11,7 +10,7 @@
def test_tupleobject(self, space, api):
assert not api.PyTuple_Check(space.w_None)
- py_none = get_pyobj_and_incref(space, space.w_None)
+ py_none = api.get_pyobj_and_incref(space.w_None)
assert api.PyTuple_SetItem(space.w_None, 0, py_none) == -1
atuple = space.newtuple([space.wrap(0), space.wrap(1),
space.wrap('yay')])
@@ -23,8 +22,8 @@
def test_tupleobject_spec_ii(self, space, api):
atuple = space.newtuple([space.wrap(10), space.wrap(11)])
assert api.PyTuple_Size(atuple) == 2
- w_obj1 = from_pyobj(space, api.PyTuple_GetItem(atuple, 0))
- w_obj2 = from_pyobj(space, api.PyTuple_GetItem(atuple, 1))
+ w_obj1 = api.from_pyobj(api.PyTuple_GetItem(atuple, 0))
+ w_obj2 = api.from_pyobj(api.PyTuple_GetItem(atuple, 1))
assert space.eq_w(w_obj1, space.wrap(10))
assert space.eq_w(w_obj2, space.wrap(11))
@@ -33,14 +32,14 @@
w_obj2 = space.newlist([])
atuple = space.newtuple([w_obj1, w_obj2])
assert api.PyTuple_Size(atuple) == 2
- assert from_pyobj(space, api.PyTuple_GetItem(atuple, 0)) is w_obj1
- assert from_pyobj(space, api.PyTuple_GetItem(atuple, 1)) is w_obj2
+ assert api.from_pyobj(api.PyTuple_GetItem(atuple, 0)) is w_obj1
+ assert api.from_pyobj(api.PyTuple_GetItem(atuple, 1)) is w_obj2
def test_new_setitem(self, space, api):
w_obj1 = space.newlist([])
- pyobj1 = get_pyobj_and_incref(space, w_obj1)
+ pyobj1 = api.get_pyobj_and_incref(w_obj1)
w_obj2 = space.newlist([])
- pyobj2 = get_pyobj_and_incref(space, w_obj2)
+ pyobj2 = api.get_pyobj_and_incref(w_obj2)
py_tuple = api.PyTuple_New(2)
assert not pyobj_has_w_obj(py_tuple)
@@ -59,8 +58,8 @@
assert pyobj2.c_ob_refcnt == REFCNT_FROM_PYPY_LIGHT + 1
assert not pyobj_has_w_obj(py_tuple)
- w_tup = from_pyobj(space, py_tuple)
- assert w_tup is from_pyobj(space, py_tuple)
+ w_tup = api.from_pyobj(py_tuple)
+ assert w_tup is api.from_pyobj(py_tuple)
assert api.PyTuple_GetItem(py_tuple, 1) == pyobj2
assert pyobj1.c_ob_refcnt == REFCNT_FROM_PYPY_LIGHT + 0
assert pyobj2.c_ob_refcnt == REFCNT_FROM_PYPY_LIGHT + 1
diff --git a/pypy/module/cpyext/typeobject.py b/pypy/module/cpyext/typeobject.py
--- a/pypy/module/cpyext/typeobject.py
+++ b/pypy/module/cpyext/typeobject.py
@@ -289,55 +289,6 @@
fill_pypy=type_fill_pypy)
#dealloc=type_dealloc)
- # some types are difficult to create because of cycles.
- # - object.ob_type = type
- # - type.ob_type = type
- # - tuple.ob_type = type
- # - type.tp_base = object
- # - tuple.tp_base = object
- # - type.tp_bases is a tuple
- # - object.tp_bases is a tuple
- # - tuple.tp_bases is a tuple
-
- return # ZZZ
-
- # insert null placeholders to please create_ref()
- track_reference(space, lltype.nullptr(PyObject.TO), space.w_type)
- track_reference(space, lltype.nullptr(PyObject.TO), space.w_object)
- track_reference(space, lltype.nullptr(PyObject.TO), space.w_tuple)
- track_reference(space, lltype.nullptr(PyObject.TO), space.w_str)
-
- # create the objects
- py_type = create_ref(space, space.w_type)
- py_object = create_ref(space, space.w_object)
- py_tuple = create_ref(space, space.w_tuple)
- py_str = create_ref(space, space.w_str)
-
- # form cycles
- pto_type = rffi.cast(PyTypeObjectPtr, py_type)
- py_type.c_ob_type = pto_type
- py_object.c_ob_type = pto_type
- py_tuple.c_ob_type = pto_type
-
- pto_object = rffi.cast(PyTypeObjectPtr, py_object)
- pto_type.c_tp_base = pto_object
- pto_tuple = rffi.cast(PyTypeObjectPtr, py_tuple)
- pto_tuple.c_tp_base = pto_object
-
- pto_type.c_tp_bases.c_ob_type = pto_tuple
- pto_object.c_tp_bases.c_ob_type = pto_tuple
- pto_tuple.c_tp_bases.c_ob_type = pto_tuple
-
- for typ in (py_type, py_object, py_tuple, py_str):
- heaptype = rffi.cast(PyHeapTypeObject, typ)
- heaptype.c_ht_name.c_ob_type = pto_type
-
- # Restore the mapping
- track_reference(space, py_type, space.w_type, replace=True)
- track_reference(space, py_object, space.w_object, replace=True)
- track_reference(space, py_tuple, space.w_tuple, replace=True)
- track_reference(space, py_str, space.w_str, replace=True)
-
@cpython_api([PyObject], lltype.Void, external=False)
def subtype_dealloc(space, obj):
diff --git a/rpython/rlib/rawrefcount.py b/rpython/rlib/rawrefcount.py
--- a/rpython/rlib/rawrefcount.py
+++ b/rpython/rlib/rawrefcount.py
@@ -82,7 +82,7 @@
global _p_list, _o_list
wr_p_list = []
new_p_list = []
- for ob in _p_list:
+ for ob in reversed(_p_list):
if ob.c_ob_refcnt not in (REFCNT_FROM_PYPY, REFCNT_FROM_PYPY_LIGHT):
new_p_list.append(ob)
else:
@@ -93,7 +93,7 @@
_p_list = Ellipsis
wr_o_list = []
- for ob in _o_list:
+ for ob in reversed(_o_list):
detach(ob, wr_o_list)
_o_list = Ellipsis
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit