diff -r e710664668de pypy/module/cpyext/pyobject.py --- a/pypy/module/cpyext/pyobject.py Sat Mar 31 17:12:27 2018 +0200 +++ b/pypy/module/cpyext/pyobject.py Sat Mar 31 19:22:51 2018 +0200 @@ -60,7 +60,12 @@ def _cpyext_attach_pyobj(self, space, py_obj): self._cpy_ref = py_obj - rawrefcount.create_link_pyobj(self, py_obj) + if type(self) is cls: + rawrefcount.create_link_pyobj(self, py_obj) + else: + # optimization of create_link_pyobj() doesn't work for + # interp-level subclasses! + rawrefcount.create_link_pypy(self, py_obj) cls._cpyext_attach_pyobj = _cpyext_attach_pyobj add_direct_pyobj_storage(W_BaseCPyObject) @@ -128,9 +133,6 @@ w_type) raise track_reference(space, obj, w_obj) - if w_type.flag_cpytype: - assert isinstance(w_obj, W_BaseCPyObject) - w_obj._cpy_ref = obj return w_obj typedescr_cache = {}