mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
[3.14] gh-140061: Use _PyObject_IsUniquelyReferenced() to check if objects are uniquely referenced (gh-140062) (gh-140157)
The previous `Py_REFCNT(x) == 1` checks can have data races in the free
threaded build. `_PyObject_IsUniquelyReferenced(x)` is a more conservative
check that is safe in the free threaded build and is identical to
`Py_REFCNT(x) == 1` in the default GIL-enabled build.
(cherry picked from commit 32c264982e)
Co-authored-by: Sergey Miryanov <sergey.miryanov@gmail.com>
This commit is contained in:
parent
6b94c7ceeb
commit
045e34964a
10 changed files with 29 additions and 36 deletions
|
|
@ -118,7 +118,7 @@ int
|
|||
PyTuple_SetItem(PyObject *op, Py_ssize_t i, PyObject *newitem)
|
||||
{
|
||||
PyObject **p;
|
||||
if (!PyTuple_Check(op) || Py_REFCNT(op) != 1) {
|
||||
if (!PyTuple_Check(op) || !_PyObject_IsUniquelyReferenced(op)) {
|
||||
Py_XDECREF(newitem);
|
||||
PyErr_BadInternalCall();
|
||||
return -1;
|
||||
|
|
@ -923,7 +923,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
|
|||
|
||||
v = (PyTupleObject *) *pv;
|
||||
if (v == NULL || !Py_IS_TYPE(v, &PyTuple_Type) ||
|
||||
(Py_SIZE(v) != 0 && Py_REFCNT(v) != 1)) {
|
||||
(Py_SIZE(v) != 0 && !_PyObject_IsUniquelyReferenced(*pv))) {
|
||||
*pv = 0;
|
||||
Py_XDECREF(v);
|
||||
PyErr_BadInternalCall();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue