def test_create_link_pyobj(self): p = W_Root(42) ob = lltype.malloc(PyObjectS, flavor='raw', zero=True) assert rawrefcount.from_obj(PyObject, p) == lltype.nullptr(PyObjectS) assert rawrefcount.to_obj(W_Root, ob) == None rawrefcount.create_link_pyobj(p, ob) assert ob.c_ob_refcnt == 0 ob.c_ob_refcnt += REFCNT_FROM_PYPY assert rawrefcount.from_obj(PyObject, p) == lltype.nullptr(PyObjectS) assert rawrefcount.to_obj(W_Root, ob) == p lltype.free(ob, flavor='raw')
def w_root_as_pyobj(w_obj, space): from rpython.rlib.debug import check_annotation # make sure that translation crashes if we see this while not translating # with cpyext check_annotation(space.config.objspace.usemodules.cpyext, check_true) # default implementation of _cpyext_as_pyobj return rawrefcount.from_obj(PyObject, w_obj)
def main(argv): rawrefcount.create_link_pypy(w1, ob1) w = None ob = lltype.nullptr(PyObjectS) oblist = [] for op in argv[1:]: revdb.stop_point() w = W_Root(42) ob = lltype.malloc(PyObjectS, flavor='raw', zero=True) ob.c_ob_refcnt = rawrefcount.REFCNT_FROM_PYPY rawrefcount.create_link_pypy(w, ob) oblist.append(ob) del oblist[-1] # rgc.collect() assert rawrefcount.from_obj(PyObject, w) == ob assert rawrefcount.to_obj(W_Root, ob) == w while True: ob = rawrefcount.next_dead(PyObject) if not ob: break assert ob in oblist oblist.remove(ob) objectmodel.keepalive_until_here(w) revdb.stop_point() return 9
def make_p(): p = W_Root(42) ob = lltype.malloc(PyObjectS, flavor='raw', zero=True) rawrefcount.create_link_pypy(p, ob) ob.c_ob_refcnt += REFCNT_FROM_PYPY assert rawrefcount.from_obj(PyObject, p) == ob assert rawrefcount.to_obj(W_Root, ob) == p return ob, p
def test_collect_p_keepalive_w_root(self): p = W_Root(42) ob = lltype.malloc(PyObjectS, flavor='raw', zero=True) rawrefcount.create_link_pypy(p, ob) ob.c_ob_refcnt += REFCNT_FROM_PYPY_LIGHT assert rawrefcount._p_list == [ob] wr_ob = weakref.ref(ob) del ob # p remains rawrefcount._collect() ob = wr_ob() assert ob is not None assert rawrefcount._p_list == [ob] assert rawrefcount.to_obj(W_Root, ob) == p assert rawrefcount.from_obj(PyObject, p) == ob lltype.free(ob, flavor='raw')
def as_pyobj(space, w_obj): """ Returns a 'PyObject *' representing the given intepreter object. This doesn't give a new reference, but the returned 'PyObject *' is valid at least as long as 'w_obj' is. **To be safe, you should use keepalive_until_here(w_obj) some time later.** In case of doubt, use the safer make_ref(). """ if w_obj is not None: assert not is_pyobj(w_obj) py_obj = rawrefcount.from_obj(PyObject, w_obj) if not py_obj: py_obj = create_ref(space, w_obj) return py_obj else: return lltype.nullptr(PyObject.TO)
def as_pyobj(space, w_obj, w_userdata=None, immortal=False): """ Returns a 'PyObject *' representing the given intepreter object. This doesn't give a new reference, but the returned 'PyObject *' is valid at least as long as 'w_obj' is. **To be safe, you should use keepalive_until_here(w_obj) some time later.** In case of doubt, use the safer make_ref(). """ if w_obj is not None: assert not is_pyobj(w_obj) py_obj = rawrefcount.from_obj(PyObject, w_obj) if not py_obj: py_obj = create_ref(space, w_obj, w_userdata, immortal=immortal) # # Try to crash here, instead of randomly, if we don't keep w_obj alive ll_assert( py_obj.c_ob_refcnt >= rawrefcount.REFCNT_FROM_PYPY, "Bug in cpyext: The W_Root object was garbage-collected " "while being converted to PyObject.") return py_obj else: return lltype.nullptr(PyObject.TO)
def w_obj_has_pyobj(w_obj): return bool(rawrefcount.from_obj(PyObject, w_obj))