diff --git a/src/sage/matrix/args.pxd b/src/sage/matrix/args.pxd index ba26cfbafbe..5f29dac712d 100644 --- a/src/sage/matrix/args.pxd +++ b/src/sage/matrix/args.pxd @@ -1,4 +1,5 @@ from cpython.object cimport PyObject +from cpython.ref cimport _Py_REFCNT from sage.structure.element cimport Element, Matrix from sage.structure.parent cimport Parent @@ -64,7 +65,7 @@ cdef class MatrixArgs: Can we safely return self.entries without making a copy? A refcount of 1 means that self.entries is the only reference. """ - return (self.entries).ob_refcnt == 1 + return _Py_REFCNT(self.entries) == 1 cdef inline bint need_to_convert(self, x) noexcept: """Is ``x`` not an element of ``self.base``?""" diff --git a/src/sage/rings/integer.pyx b/src/sage/rings/integer.pyx index bdd2a7ce05b..3a9e238b6c9 100644 --- a/src/sage/rings/integer.pyx +++ b/src/sage/rings/integer.pyx @@ -207,6 +207,9 @@ new_gen_from_integer = None cdef extern from *: int unlikely(int) nogil # Defined by Cython +cdef extern from "Python.h": + void Py_SET_REFCNT(PyObject*, Py_ssize_t) nogil + cdef object numpy_long_interface = {'typestr': '=i4' if sizeof(long) == 4 else '=i8'} cdef object numpy_int64_interface = {'typestr': '=i8'} cdef object numpy_object_interface = {'typestr': '|O'} @@ -7724,7 +7727,7 @@ cdef PyObject* fast_tp_new(type t, args, kwds) except NULL: # Objects from the pool have reference count zero, so this # needs to be set in this case. - new.ob_refcnt = 1 + Py_SET_REFCNT(new, 1) return new diff --git a/src/sage/rings/real_double.pyx b/src/sage/rings/real_double.pyx index cac66b4ac75..543802dbba0 100644 --- a/src/sage/rings/real_double.pyx +++ b/src/sage/rings/real_double.pyx @@ -45,6 +45,9 @@ from libc.string cimport memcpy from cpython.object cimport * from cpython.float cimport * +cdef extern from "Python.h": + void Py_SET_REFCNT(PyObject*, Py_ssize_t) nogil + from sage.ext.stdsage cimport PY_NEW from sage.cpython.python_debug cimport if_Py_TRACE_REFS_then_PyObject_INIT @@ -2157,7 +2160,7 @@ cdef PyObject* fast_tp_new(type t, args, kwds) noexcept: # Objects from the pool have reference count zero, so this # needs to be set in this case. - new.ob_refcnt = 1 + Py_SET_REFCNT(new, 1) return new diff --git a/src/sage/symbolic/ginac/numeric.cpp b/src/sage/symbolic/ginac/numeric.cpp index bcb470f911b..6b1c2524a86 100644 --- a/src/sage/symbolic/ginac/numeric.cpp +++ b/src/sage/symbolic/ginac/numeric.cpp @@ -81,14 +81,14 @@ #define Py_INCREF(op) ( \ _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \ - ((PyObject*)(op))->ob_refcnt++) ; \ + Py_SET_REFCNT((PyObject*)(op), Py_REFCNT(op) + 1)) ; \ std::cerr << "+ " << long(op) << ", " << Py_REFCNT(op) << ", " << Py_TYPE(op)->tp_name << std::endl; std::cerr.flush(); #define Py_DECREF(op) \ do { \ std::cerr << "- " << long(op) << ", " << Py_REFCNT(op) << ", " << Py_TYPE(op)->tp_name << std::endl; std::cerr.flush(); \ if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \ - --((PyObject*)(op))->ob_refcnt != 0) \ + (Py_SET_REFCNT((PyObject*)(op), Py_REFCNT(op) - 1), Py_REFCNT(op) != 0)) \ _Py_CHECK_REFCNT(op) \ else \ _Py_Dealloc((PyObject *)(op)); \