FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use _PyObject_IsUniquelyReferenced instead of `Py_REFCNT(x) == 1` for free-threaded compatibility · Issue #140061 · python/cpython · GitHub

Repository navigation

Use _PyObject_IsUniquelyReferenced instead of Py_REFCNT(x) == 1 for free-threaded compatibility #140061

Description

Feature or enhancement

Proposal:

There are a lot of places where Py_REFCNT(..)==1 used. IIUC it is not correct for no-gil version.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    type-refactorCode refactoring (with no changes in behavior)
    and removed
    type-featureA feature request or enhancement
    on Oct 13, 2025
  2. changed the title [-]Widely use _PyObject_IsUniquelyReferenced to check if object is uniquely referenced[/-] [+]Use _PyObject_IsUniquelyReferenced instead of `Py_REFCNT(x) == 1` for free-threaded compatibility[/+] on Oct 13, 2025
  3. added a commit that references this issue on Oct 15, 2025
  4. added a commit that references this issue on Oct 15, 2025
  5. added a commit that references this issue on Oct 15, 2025
  6. added a commit that references this issue on Dec 6, 2025
  7. cmaloney commented on Sep 14, 2026

    Contributor

    As a heads up this migration led to a subtle free-threading only bug in _PyBytes_Resize (GH-157373). _IsUniquelyReferenced returns false when a reference crosses a thread boundary while Py_REFCNT(x) == 1 returns true. That resulted in an immortal constant object being returned because resize thought the bytes was shared (and thus immutable) in a case where code could still modify the bytes.

    I used AI to spot-check the rest of the migrations here (Claude Fable 5.1) and it found one other behavior change worth noting: PySet_Add on a single reference frozenset which crosses thread boundaries now raises SystemError rather than passing.

  8. vstinner commented on Sep 15, 2026

    Member

    I used AI to spot-check the rest of the migrations here (Claude Fable 5.1) and it found one other behavior change worth noting: PySet_Add on a single reference frozenset which crosses thread boundaries now raises SystemError rather than passing.

    IMO PySet_Add(frozenset, key) works as expected here: you must not modify a frozenset from a thread different than the one which created it. If you think that the documentation isn't explicit about this point, maybe you can suggest a change for PySet_Add() documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL