GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Use _PyObject_IsUniquelyReferenced instead of `Py_REFCNT(x) == 1` for free-threaded compatibility · Issue #140061 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Use _PyObject_IsUniquelyReferenced instead of Py_REFCNT(x) == 1 for free-threaded compatibility#140061
Copy link
Labels
extension-modulesC modules in the Modules dirC modules in the Modules dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)

Description

@sergey-miryanov
sergey-miryanov
opened on Oct 13, 2025
Issue body actions

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

  • GH-140061: Use _PyObject_IsUniquelyReferenced to check if object is uniquely referenced #140062
  • [3.14] gh-140061: Use _PyObject_IsUniquelyReferenced() to check if objects are uniquely referenced (gh-140062) #140157

Activity

  1. sergey-miryanov
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Oct 13, 2025
  2. picnixz
    added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    type-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)
    topic-free-threading
    and removed
    type-featureA feature request or enhancementA feature request or enhancement
    on Oct 13, 2025
  3. picnixz
    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
  4. colesbury
    added a commit that references this issue on Oct 15, 2025

    gh-140061: Use `_PyObject_IsUniquelyReferenced()` to check if objects…

    32c2649
  5. miss-islington
    added a commit that references this issue on Oct 15, 2025

    pythongh-140061: Use `_PyObject_IsUniquelyReferenced()` to check if o…

    2b285c1
  6. colesbury
    added a commit that references this issue on Oct 15, 2025

    [3.14] gh-140061: Use `_PyObject_IsUniquelyReferenced()` to check if …

    045e349
  7. sergey-miryanov
    closed this as completedon Oct 15, 2025
  8. StanFromIreland
    added a commit that references this issue on Dec 6, 2025

    pythongh-140061: Use `_PyObject_IsUniquelyReferenced()` to check if o…

    f4baa5c
  9. cmaloney commented on Sep 14, 2026

    @cmaloney
    cmaloney
    on Sep 14, 2026
    Contributor
    More actions

    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.

  10. vstinner commented on Sep 15, 2026

    @vstinner
    vstinner
    on Sep 15, 2026
    Member
    More actions

    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

    extension-modulesC modules in the Modules dirC modules in the Modules dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-free-threadingtype-refactorCode refactoring (with no changes in behavior)Code refactoring (with no changes in behavior)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.