Skip to content

Commit 73850a8

Browse files
author
Release Manager
committed
gh-41137: Refactor reference counting to use _Py_REFCNT and Py_SET_REFCNT This change replaces direct access to ob_refcnt with proper API functions: - Use _Py_REFCNT() for reading reference counts - Use Py_SET_REFCNT() for setting reference counts This ensures compatibility with Python's free-threaded mode and follows Python's recommended practices for reference count manipulation. <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes #12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes #12345". --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - #12345: short description why this is a dependency --> <!-- - #34567: ... --> URL: #41137 Reported by: Chenxin Zhong Reviewer(s): Tobias Diez
2 parents 7de2941 + cedb4c9 commit 73850a8

File tree

4 files changed

+12
-5
lines changed

4 files changed

+12
-5
lines changed

src/sage/matrix/args.pxd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from cpython.object cimport PyObject
2+
from cpython.ref cimport _Py_REFCNT
23
from sage.structure.element cimport Element, Matrix
34
from sage.structure.parent cimport Parent
45

@@ -64,7 +65,7 @@ cdef class MatrixArgs:
6465
Can we safely return self.entries without making a copy?
6566
A refcount of 1 means that self.entries is the only reference.
6667
"""
67-
return (<PyObject*>self.entries).ob_refcnt == 1
68+
return _Py_REFCNT(<PyObject*>self.entries) == 1
6869

6970
cdef inline bint need_to_convert(self, x) noexcept:
7071
"""Is ``x`` not an element of ``self.base``?"""

src/sage/rings/integer.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ new_gen_from_integer = None
207207
cdef extern from *:
208208
int unlikely(int) nogil # Defined by Cython
209209

210+
cdef extern from "Python.h":
211+
void Py_SET_REFCNT(PyObject*, Py_ssize_t) nogil
212+
210213
cdef object numpy_long_interface = {'typestr': '=i4' if sizeof(long) == 4 else '=i8'}
211214
cdef object numpy_int64_interface = {'typestr': '=i8'}
212215
cdef object numpy_object_interface = {'typestr': '|O'}
@@ -7724,7 +7727,7 @@ cdef PyObject* fast_tp_new(type t, args, kwds) except NULL:
77247727
# Objects from the pool have reference count zero, so this
77257728
# needs to be set in this case.
77267729

7727-
new.ob_refcnt = 1
7730+
Py_SET_REFCNT(<PyObject*>new, 1)
77287731

77297732
return new
77307733

src/sage/rings/real_double.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ from libc.string cimport memcpy
4545
from cpython.object cimport *
4646
from cpython.float cimport *
4747

48+
cdef extern from "Python.h":
49+
void Py_SET_REFCNT(PyObject*, Py_ssize_t) nogil
50+
4851
from sage.ext.stdsage cimport PY_NEW
4952
from sage.cpython.python_debug cimport if_Py_TRACE_REFS_then_PyObject_INIT
5053

@@ -2157,7 +2160,7 @@ cdef PyObject* fast_tp_new(type t, args, kwds) noexcept:
21572160
# Objects from the pool have reference count zero, so this
21582161
# needs to be set in this case.
21592162

2160-
new.ob_refcnt = 1
2163+
Py_SET_REFCNT(<PyObject*>new, 1)
21612164

21622165
return new
21632166

src/sage/symbolic/ginac/numeric.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@
8181

8282
#define Py_INCREF(op) ( \
8383
_Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \
84-
((PyObject*)(op))->ob_refcnt++) ; \
84+
Py_SET_REFCNT((PyObject*)(op), Py_REFCNT(op) + 1)) ; \
8585
std::cerr << "+ " << long(op) << ", " << Py_REFCNT(op) << ", " << Py_TYPE(op)->tp_name << std::endl; std::cerr.flush();
8686

8787
#define Py_DECREF(op) \
8888
do { \
8989
std::cerr << "- " << long(op) << ", " << Py_REFCNT(op) << ", " << Py_TYPE(op)->tp_name << std::endl; std::cerr.flush(); \
9090
if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \
91-
--((PyObject*)(op))->ob_refcnt != 0) \
91+
(Py_SET_REFCNT((PyObject*)(op), Py_REFCNT(op) - 1), Py_REFCNT(op) != 0)) \
9292
_Py_CHECK_REFCNT(op) \
9393
else \
9494
_Py_Dealloc((PyObject *)(op)); \

0 commit comments

Comments
 (0)