[pypy-issue] Issue #2397: PyPy should export REFCNT_FROM_PYPY (pypy/pypy)

mattip issues-reply at bitbucket.org
Sun Sep 11 16:37:30 EDT 2016


New issue 2397: PyPy should export REFCNT_FROM_PYPY
https://bitbucket.org/pypy/pypy/issues/2397/pypy-should-export-refcnt_from_pypy

mattip:

Numpy has this test, which raises on CPython

```
#!python

x = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
y = x.view('int8')
x.resize(5,1)

```
The reason it raises, is this code


```
#!c

int refcnt = self->ob_refcnt;
if ((refcnt > 2)
{
     PyErr_SetString(PyExc_ValueError,
     "cannot resize an array that "\
     "references or is referenced\n"\
     "by another array in this way.  Use the resize function");
     return NULL;
}

```
``refcnt`` is 3 when ``x.resize`` is called; 1 from `x = ...`, 1 from ``y=x...`` and 1 from the C code being inside a function (CPython increfs all function arguments on ``__call__``)

The test does not raise the error on PyPy, ``self.ob_refcnt`` is 2305843009213693953L, which when cast to int is 1. Possible fix:

* export rawrefcount.REFCNT_FROM_PYPY to a header
* change the assigment of refcnt to
  ``refcnt = (self.ob_refcnt > REFCNT_FROM_PYPY ? self.ob_refcnt - REFCNT_FROM_PYPY : self.ob_refcnt;``
* change the limit for the test to 2 (1 from ``x=...`` and one from ``y=x...``

The changes would be inside an ``#ifdef PYPY_VERSION``

Opinions?





More information about the pypy-issue mailing list