Author: Antonio Cuni <anto.c...@gmail.com> Branch: cpyext-nowrapper Changeset: r92628:d9e6994b8085 Date: 2017-10-07 10:39 +0100 http://bitbucket.org/pypy/pypy/changeset/d9e6994b8085/
Log: change strategy: instead of trying to mimic what CPython does, for now just try to refactor things in a way whih will allow us to remove all references to the space. We will need to refactor Py_IncRef first diff --git a/pypy/module/cpyext/object.py b/pypy/module/cpyext/object.py --- a/pypy/module/cpyext/object.py +++ b/pypy/module/cpyext/object.py @@ -49,6 +49,8 @@ @cpython_api([PyTypeObjectPtr, Py_ssize_t], PyObject, result_is_ll=True) def _PyObject_NewVar(space, tp, nitems): + from pypy.module.cpyext.pyobject import _allocate_generic_object + w_type = from_ref(space, rffi.cast(PyObject, tp)) assert isinstance(w_type, W_TypeObject) if w_type is space.w_type: @@ -56,29 +58,7 @@ typedescr = get_typedescr(w_type.layout.typedef) pyobj = typedescr.allocate(space, w_type, itemcount=nitems) else: - # std case: do the same logic as BaseCpyTypedescr.allocate - # CPython doesn't do this: investigate whether we can remove - flags = rffi.cast(lltype.Signed, tp.c_tp_flags) - if flags & Py_TPFLAGS_HEAPTYPE: - Py_IncRef(space, w_type) - # - size = _VAR_SIZE(tp, nitems) - assert size >= rffi.sizeof(PyObject.TO) - # XXX: zero=True? add_memory_pressure? - buf = lltype.malloc(rffi.VOIDP.TO, size, - flavor='raw', zero=True, - add_memory_pressure=True) - pyobj = rffi.cast(PyObject, buf) - # XXX CPython does this - ## if (pyobj == NULL) - ## return PyErr_NoMemory() - pyobj.c_ob_refcnt = 1 - if tp.c_tp_itemsize: - pyvarobj = rffi.cast(PyVarObject, pyobj) - pyvarobj.c_ob_size = nitems - pyobj.c_ob_refcnt = 1 - #pyobj.c_ob_pypy_link should get assigned very quickly - pyobj.c_ob_type = tp + return _allocate_generic_object(space, tp, nitems) # if tp.c_tp_itemsize == 0: w_obj = PyObject_Init(space, pyobj, tp) 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 @@ -21,6 +21,29 @@ #________________________________________________________ # type description +def _allocate_generic_object(space, pytype, itemcount, immortal=False): + # Don't increase refcount for non-heaptypes + flags = rffi.cast(lltype.Signed, pytype.c_tp_flags) + if flags & Py_TPFLAGS_HEAPTYPE: + Py_IncRef(space, pytype) + + size = pytype.c_tp_basicsize + if pytype.c_tp_itemsize: + size += itemcount * pytype.c_tp_itemsize + assert size >= rffi.sizeof(PyObject.TO) + buf = lltype.malloc(rffi.VOIDP.TO, size, + flavor='raw', zero=True, + add_memory_pressure=True, immortal=immortal) + pyobj = rffi.cast(PyObject, buf) + if pytype.c_tp_itemsize: + pyvarobj = rffi.cast(PyVarObject, pyobj) + pyvarobj.c_ob_size = itemcount + pyobj.c_ob_refcnt = 1 + #pyobj.c_ob_pypy_link should get assigned very quickly + pyobj.c_ob_type = pytype + return pyobj + + class BaseCpyTypedescr(object): basestruct = PyObject.TO W_BaseObject = W_ObjectObject @@ -32,33 +55,10 @@ def allocate(self, space, w_type, itemcount=0, immortal=False): # typically called from PyType_GenericAlloc via typedescr.allocate # this returns a PyObject with ob_refcnt == 1. - pytype = as_pyobj(space, w_type) pytype = rffi.cast(PyTypeObjectPtr, pytype) assert pytype - # Don't increase refcount for non-heaptypes - flags = rffi.cast(lltype.Signed, pytype.c_tp_flags) - if flags & Py_TPFLAGS_HEAPTYPE: - Py_IncRef(space, w_type) - - if pytype: - size = pytype.c_tp_basicsize - else: - size = rffi.sizeof(self.basestruct) - if pytype.c_tp_itemsize: - size += itemcount * pytype.c_tp_itemsize - assert size >= rffi.sizeof(PyObject.TO) - buf = lltype.malloc(rffi.VOIDP.TO, size, - flavor='raw', zero=True, - add_memory_pressure=True, immortal=immortal) - pyobj = rffi.cast(PyObject, buf) - if pytype.c_tp_itemsize: - pyvarobj = rffi.cast(PyVarObject, pyobj) - pyvarobj.c_ob_size = itemcount - pyobj.c_ob_refcnt = 1 - #pyobj.c_ob_pypy_link should get assigned very quickly - pyobj.c_ob_type = pytype - return pyobj + return _allocate_generic_object(space, pytype, itemcount, immortal) def attach(self, space, pyobj, w_obj, w_userdata=None): pass _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit