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 — [C API] Deprecate _Py_Identifier API · Issue #142217 · python/cpython

[C API] Deprecate _Py_Identifier API #142217

New issue
Copy link
New issue
Copy link
Closed
Closed
[C API] Deprecate _Py_Identifier API#142217
Copy link
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

Description

@vstinner
vstinner
opened on Dec 3, 2025
Issue body actions

I propose to deprecate the private _Py_Identifier API.

  • Deprecate private _PyObject_CallMethodId(), _PyObject_GetAttrId(), _PyUnicode_FromId().
  • Remove immediately internal functions using _Py_Identifier.

See also the discussions:

  • Deprecate private C API functions #141049
  • https://discuss.python.org/t/c-api-how-much-private-is-the-private-py-identifier-api/29190 (2023).
  • https://mail.python.org/archives/list/[email protected]/thread/DNMZAMB4M6RVR76RDZMUK2WRLI6KAAYS/ (2022)

Since 2022, CPython has been modified to replace _Py_IDENTIFIER() with _Py_STR(). Python no longer uses the _Py_IDENTIFIER() API anymore.

See also issue #141049 "Deprecate private C API functions".

Linked PRs

  • gh-142217: Remove internal _Py_Identifier functions #142219
  • gh-142217: Deprecate the private _Py_Identifier C API #142221
  • gh-142217: Recommend PyUnicode_InternFromString() to replace _PyUnicode_FromId() #142746
  • gh-142217: Add tests on the deprecated _Py_Identifier C API #157638

Activity

  1. vstinner
    added
    topic-C-API
    on Dec 3, 2025
  2. vstinner
    added a commit that references this issue on Dec 3, 2025

    pythongh-142217: Remove internal _Py_Identifier functions

    ff6a2a0
  3. picnixz
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    on Dec 3, 2025
  4. vstinner
    added 2 commits that reference this issue on Dec 3, 2025

    gh-142217: Remove internal _Py_Identifier functions (#142219)

    7e5fcae

    pythongh-142217: Deprecate the private _Py_Identifier C API

    c7b7c51
  5. StanFromIreland
    added a commit that references this issue on Dec 6, 2025

    pythongh-142217: Remove internal _Py_Identifier functions (python#142219

    4d86e4e
  6. vstinner
    added a commit that references this issue on Dec 12, 2025

    gh-142217: Deprecate the private _Py_Identifier C API (#142221)

    7aa353c
  7. cdce8p commented on Dec 14, 2025

    @cdce8p
    cdce8p
    on Dec 14, 2025
    Contributor
    More actions

    I just tested the latest cpython build against mypy. #142221 is causing a lot of errors with -Werror,-Wdeprecated-declarations. Yes it's possible to replace most _Py_IDENTIFIER calls with PyUnicode_FromString, see python/mypy@f287f7d, but I'm not sure this is the right call here. The discuss thread mentions the need / desire for an API to get/create immortal strings, has there been any progress for it?

    cpython itself uses &_Py_ID in a lot of places. These could actually also be used by mypy in most cases though I don't think it's intended that way. python/mypy@dbbc992

  8. encukou commented on Dec 15, 2025

    @encukou
    encukou
    on Dec 15, 2025
    Member
    More actions

    The discuss thread mentions the need / desire for an API to get/create immortal strings, has there been any progress for it?

    Not really.
    Note that CPython's immortal strings (_Py_STR) are statically allocated, in a way that requires code generation with lots of knowledge about the internals, and taking advantage of the fact that there are no conflicts.
    We unfortunately can't provide that exact API to extensions -- if two of those intern the same string, only one can “win”.

    AFAIK, we can't really make _Py_IDENTIFIER substantially better than users calling if(!x) {x = PyUnicode_InternFromString("...");} on init, and Py_CLEAR(x) on teardown.

    The advantage of _Py_Identifier is the ease of use -- in particular, the teardown is automatic. But it comes at the cost of an elaborate (and largely untested) mechanism to make everything work correctly.


    cpython itself uses &_Py_ID in a lot of places. These could actually also be used by mypy in most cases though I don't think it's intended that way.

    Don't do that, this uses internal ABI that will break in patch releases. As in: it does break in practice in patch releases, it's not a CYA theoretical possibility.


    @vstinner: would PyUnicode_InternFromString be a better suggested replacement than PyUnicode_FromString?

  9. vstinner
    added a commit that references this issue on Dec 15, 2025

    pythongh-142217: Recommend PyUnicode_InternFromString() to replace _P…

    051a1fe
  10. vstinner commented on Dec 15, 2025

    @vstinner
    vstinner
    on Dec 15, 2025
    MemberAuthor
    More actions

    @vstinner: would PyUnicode_InternFromString be a better suggested replacement than PyUnicode_FromString?

    Oh, good idea: I wrote #142746 to update the documentation.

  11. encukou
    added a commit that references this issue on Dec 15, 2025

    gh-142217: Recommend PyUnicode_InternFromString() to replace _PyUnico…

    14d3974
  12. fatelei
    added a commit that references this issue on Dec 16, 2025

    pythongh-142217: Recommend PyUnicode_InternFromString() to replace _P…

    aa94cb6
  13. vstinner commented on Dec 16, 2025

    @vstinner
    vstinner
    on Dec 16, 2025
    MemberAuthor
    More actions

    Yes it's possible to replace most _Py_IDENTIFIER calls with PyUnicode_FromString, see python/mypy@f287f7d, but I'm not sure this is the right call here.

    Yes, it's a safe replacement. To avoid negative impact on performance, you might try to only decode bytes strings (char*) from UTF-8 once by storing the object (PyObject*) in a module state, if possible.

    I close the issue since the APIs are now deprecated.

  14. vstinner
    closed this as completedon Dec 16, 2025
  15. cdce8p commented on Dec 22, 2025

    @cdce8p
    cdce8p
    on Dec 22, 2025
    Contributor
    More actions

    This is what I got working for mypy: python/mypy#20460
    It's heavily inspired by the numpy implementation.

    Reacted by Victor Stinner
  16. vstinner
    added 3 commits that reference this issue on Sep 16, 2026

    pythongh-142217: Add tests on the deprecated _Py_Identifier C API

    d5b6369

    pythongh-142217: Add tests on the deprecated _Py_Identifier C API

    6d18912

    gh-142217: Add tests on the deprecated _Py_Identifier C API (#157638)

    21403af
  17. vstinner
    added 2 commits that reference this issue on Oct 16, 2026

    [3.15] gh-156939: Add tests on the PyUnicode C API (#159533)

    bcbe9bc

    [3.14] gh-156939: Add tests on the PyUnicode C API (#159533) (#159539)

    21aae35
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

    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — 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.