8000 [3.13] gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject (gh-133039) by miss-islington · Pull Request #133121 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.13] gh-132070: Use _PyObject_IsUniquelyReferenced in unicodeobject (gh-133039) #133121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add _PyObject_IsUniquelyReferenced
  • Loading branch information
corona10 committed Apr 29, 2025
commit 143bba0ddf3c66a65f0721cd214b426ffcc0c6d3
17 changes: 17 additions & 0 deletions Include/internal/pycore_object.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ static inline void _Py_RefcntAdd(PyObject* op, Py_ssize_t n)
extern void _Py_SetImmortal(PyObject *op);
extern void _Py_SetImmortalUntracked(PyObject *op);

// Checks if an object has a single, unique reference. If the caller holds a
// unique reference, it may be able to safely modify the object in-place.
static inline int
_PyObject_IsUniquelyReferenced(PyObject *ob)
{
#if !defined(Py_GIL_DISABLED)
return Py_REFCNT(ob) == 1;
#else
// NOTE: the entire ob_ref_shared field must be zero, including flags, to
// ensure that other threads cannot concurrently create new references to
// this object.
return (_Py_IsOwnedByCurrentThread(ob) &&
_Py_atomic_load_uint32_relaxed(&ob->ob_ref_local) == 1 &&
_Py_atomic_load_ssize_relaxed(&ob->ob_ref_shared) == 0);
#endif
}

// Makes an immortal object mortal again with the specified refcnt. Should only
// be used during runtime finalization.
static inline void _Py_SetMortal(PyObject *op, Py_ssize_t refcnt)
Expand Down
Loading
0