# C API

**URL:** https://discuss.python.org/c/core-dev/c-api/30.md

[Latest](https://discuss.python.org/latest.md) · [Categories](https://discuss.python.org/categories.md) · [Tags](https://discuss.python.org/tags.md)

---

## [About the C API category](https://discuss.python.org/t/about-the-c-api-category/20838)

<div class="topic-metadata">

**Author:** [@ambv](https://discuss.python.org/u/ambv)\
**Replies:** 0

</div>

This sub-category is for discussions specifically about the Python C API. Covers both the development of as well as the development with (usage of) the C API. It supersedes the legacy capi-sig mailing list.

---

## [Getting exceptions & built-in types using \`Py\_GetConstant\`](https://discuss.python.org/t/getting-exceptions-built-in-types-using-py-getconstant/108861)

<div class="topic-metadata">

**Author:** [@encukou](https://discuss.python.org/u/encukou)\
**Replies:** 5\
**Last updated:** [October 5, 2026, 1:40pm UTC](https://discuss.python.org/t/getting-exceptions-built-in-types-using-py-getconstant/108861 "2026-10-05T13:40:21Z")

</div>

I propose defining identifiers for use with Py\_GetConstant for exceptions (e.g. Py\_CONSTANT\_Exc\_ValueError to get ValueError) and other built-in types (e.g. Py\_CONSTANT\_Bool\_Type to get bool). When compiling for Stabl…

---

## [C API: Migrate embedded Python to PyConfig and PyInitConfig APIs](https://discuss.python.org/t/c-api-migrate-embedded-python-to-pyconfig-and-pyinitconfig-apis/109215)

<div class="topic-metadata">

**Author:** [@vstinner](https://discuss.python.org/u/vstinner)\
**Replies:** 0\
**Last updated:** [September 24, 2026, 4:18pm UTC](https://discuss.python.org/t/c-api-migrate-embedded-python-to-pyconfig-and-pyinitconfig-apis/109215 "2026-09-24T16:18:01Z")

</div>

Hi, I removed 21 deprecated global configuration variables and 4 deprecated functions in Python 3.16 C API. If your project embeds Python, it may affected if it doesn’t use PyConfig or PyInitConfig C API yet. I created …

---

## [C API: Add \`Py\_GetBuildInType\` and \`Py\_GetBuildInTypeBorrowed\`](https://discuss.python.org/t/c-api-add-py-getbuildintype-and-py-getbuildintypeborrowed/107010)

<div class="topic-metadata">

**Author:** [@bschoenmaeckers](https://discuss.python.org/u/bschoenmaeckers)\
**Replies:** 21\
**Last updated:** [September 2, 2026, 1:08pm UTC](https://discuss.python.org/t/c-api-add-py-getbuildintype-and-py-getbuildintypeborrowed/107010 "2026-09-02T13:08:34Z")

</div>

This was originally posted at C API: Add \`Py\_GetBuildInType\` and \`Py\_GetBuildInTypeBorrowed\` · Issue #148643 · python/cpython · GitHub Currently, the C API exports all build-in types as a static data export. This limits…

---

## [Add \`PyUnstable\_InterpreterFrame\_GetLocals()\`: read a frame’s locals from the interpreter-frame C API](https://discuss.python.org/t/add-pyunstable-interpreterframe-getlocals-read-a-frame-s-locals-from-the-interpreter-frame-c-api/108520)

<div class="topic-metadata">

**Author:** [@guilhermeleobas](https://discuss.python.org/u/guilhermeleobas)\
**Replies:** 11\
**Last updated:** [August 21, 2026, 12:05am UTC](https://discuss.python.org/t/add-pyunstable-interpreterframe-getlocals-read-a-frame-s-locals-from-the-interpreter-frame-c-api/108520 "2026-08-21T00:05:33Z")

</div>

I work on TorchDynamo, PyTorch’s JIT compiler. Dynamo relies on the eval frame API (PEP 523), and one of the things it needs to do when a frame is traced is to read the frame’s local variables. CPython has 3 public APIs…

---

## [We're out out of type flags, let's add new API for new ones](https://discuss.python.org/t/were-out-out-of-type-flags-lets-add-new-api-for-new-ones/108540)

<div class="topic-metadata">

**Author:** [@encukou](https://discuss.python.org/u/encukou)\
**Replies:** 2\
**Last updated:** [August 17, 2026, 12:16pm UTC](https://discuss.python.org/t/were-out-out-of-type-flags-lets-add-new-api-for-new-ones/108540 "2026-08-17T12:16:14Z")

</div>

We almost ran out of type flag bits. Let’s add more space in 3.16, and while we’re at it, decouple the public API from the internal storage (kinda what PyType\_Spec/PySlot do for PyTypeObject struct itself). That way, we…

---

## [Can Py\_buffer format be const qualified?](https://discuss.python.org/t/can-py-buffer-format-be-const-qualified/108522)

<div class="topic-metadata">

**Author:** [@hpkfft](https://discuss.python.org/u/hpkfft)\
**Replies:** 6\
**Last updated:** [August 15, 2026, 4:16am UTC](https://discuss.python.org/t/can-py-buffer-format-be-const-qualified/108522 "2026-08-15T04:16:05Z")

</div>

The char \*format of a Py\_buffer is often set to a string literal such as "B" or "d". For example, see get\_native\_fmtstr() in Objects/memoryobject.c, which is used to set view-\>format. These strings cannot be modified i…

---

## [Adding PyMethod\_New and/or PyMethod\_Type to stable ABI / limited API](https://discuss.python.org/t/adding-pymethod-new-and-or-pymethod-type-to-stable-abi-limited-api/108482)

<div class="topic-metadata">

**Author:** [@NicolasT](https://discuss.python.org/u/NicolasT)\
**Replies:** 0\
**Last updated:** [August 9, 2026, 4:25pm UTC](https://discuss.python.org/t/adding-pymethod-new-and-or-pymethod-type-to-stable-abi-limited-api/108482 "2026-08-09T16:25:08Z")

</div>

(Bringing Adding \`PyMethod\_New\` to stable ABI / limited API · Issue #108 · capi-workgroup/decisions · GitHub to Discuss) Projects enabling the implementation of Python modules using foreign (non-C) languages often inclu…

---

## [Provide a C-API to efficently convert \`dict\` into \`frozendict\`](https://discuss.python.org/t/provide-a-c-api-to-efficently-convert-dict-into-frozendict/107754)

<div class="topic-metadata">

**Author:** [@sobolevn](https://discuss.python.org/u/sobolevn)\
**Replies:** 35\
**Last updated:** [July 27, 2026, 8:32am UTC](https://discuss.python.org/t/provide-a-c-api-to-efficently-convert-dict-into-frozendict/107754 "2026-07-27T08:32:09Z")

</div>

Context: I was working on frozendict support for msgspec serialization library. It is written in pure C and is really focused on being extremely fast. Issue: Add \`frozendict\` support for 3.15+ by sobolevn · Pull Request …

---

## [Dict watchers: the documented callback contract cannot be satisfied (gh-154710)](https://discuss.python.org/t/dict-watchers-the-documented-callback-contract-cannot-be-satisfied-gh-154710/108282)

<div class="topic-metadata">

**Author:** [@devdanzin](https://discuss.python.org/u/devdanzin)\
**Replies:** 1\
**Last updated:** [July 26, 2026, 3:51pm UTC](https://discuss.python.org/t/dict-watchers-the-documented-callback-contract-cannot-be-satisfied-gh-154710/108282 "2026-07-26T15:51:16Z")

</div>

Doc/c-api/dict.rst places three requirements on a PyDict\_WatchCallback, and they are jointly unsatisfiable. I would like input on which one should yield, because every available fix changes either a documented guarantee …

---

## [Clarify object identity semantics for repeated evaluations — literals vs. function calls](https://discuss.python.org/t/clarify-object-identity-semantics-for-repeated-evaluations-literals-vs-function-calls/107453)

<div class="topic-metadata">

**Author:** [@kevrod07](https://discuss.python.org/u/kevrod07)\
**Replies:** 23\
**Last updated:** [May 22, 2026, 12:48am UTC](https://discuss.python.org/t/clarify-object-identity-semantics-for-repeated-evaluations-literals-vs-function-calls/107453 "2026-05-22T00:48:28Z")

</div>

The Language Reference explicitly calls out that repeated evaluation of literals may or may not produce the same object (§6.2.3.1 “Literals and object identity”): Multiple evaluations of literals with the same value …

---

## [Stable ABI for PyCriticalSection](https://discuss.python.org/t/stable-abi-for-pycriticalsection/106212)

<div class="topic-metadata">

**Author:** [@encukou](https://discuss.python.org/u/encukou)\
**Replies:** 32\
**Last updated:** [March 31, 2026, 3:28pm UTC](https://discuss.python.org/t/stable-abi-for-pycriticalsection/106212 "2026-03-31T15:28:55Z")

</div>

With PEP 793 (PyModExport), it’s possible to build Stable ABI extensions compatible with both free-threaded and “GIL-ful” builds. We’re still not there: We need a way to select name/describe the ABI and the associate…

---

## [Let tools know when tracing/profiling is enabled/disabled](https://discuss.python.org/t/let-tools-know-when-tracing-profiling-is-enabled-disabled/106143)

<div class="topic-metadata">

**Author:** [@Subbarao](https://discuss.python.org/u/Subbarao)\
**Replies:** 5\
**Last updated:** [March 30, 2026, 3:58pm UTC](https://discuss.python.org/t/let-tools-know-when-tracing-profiling-is-enabled-disabled/106143 "2026-03-30T15:58:26Z")

</div>

While audit hooks notify us when sys.setprofile/sys.settrace is called, they do not report the argument passed to these functions. This means we cannot distinguish between tracing/profiling being enabled (a function is s…

---

## [Add a contextvars.ContextVar.get\_changed() method](https://discuss.python.org/t/add-a-contextvars-contextvar-get-changed-method/106714)

<div class="topic-metadata">

**Author:** [@nas](https://discuss.python.org/u/nas)\
**Replies:** 1\
**Last updated:** [March 30, 2026, 12:31pm UTC](https://discuss.python.org/t/add-a-contextvars-contextvar-get-changed-method/106714 "2026-03-30T12:31:01Z")

</div>

With the addition of the thread\_inherit\_context system flag, we now have an issue with decimal.getcontext(). It is supposed to return a separate decimal.Context instance for each thread. Before the thread\_inherit\_conte…

---

## [Add an Unstable C API for immortalizing objects](https://discuss.python.org/t/add-an-unstable-c-api-for-immortalizing-objects/105461)

<div class="topic-metadata">

**Author:** [@ngoldbaum](https://discuss.python.org/u/ngoldbaum)\
**Replies:** 10\
**Last updated:** [February 11, 2026, 4:40pm UTC](https://discuss.python.org/t/add-an-unstable-c-api-for-immortalizing-objects/105461 "2026-02-11T16:40:21Z")

</div>

I’ve been looking at a multithreaded scaling issue in NumPy. This has uncovered a number of scaling issues in NumPy and CPython that we are in the process of resolving. In the process, we noticed that there is some refe…

---

## [Adding C API for use in tp\_traverse](https://discuss.python.org/t/adding-c-api-for-use-in-tp-traverse/105331)

<div class="topic-metadata">

**Author:** [@encukou](https://discuss.python.org/u/encukou)\
**Replies:** 34\
**Last updated:** [February 10, 2026, 11:19am UTC](https://discuss.python.org/t/adding-c-api-for-use-in-tp-traverse/105331 "2026-02-10T11:19:39Z")

</div>

There are newly documented restrictions on tp\_traverse: gh-123241: Document restrictions for \`tp\_traverse\` implementations by colesbury · Pull Request #142272 · python/cpython · GitHub The traversal function must not h…

---

## [Proper handling of PyList\_SetItem return value and reference stealing in CPython C extensions](https://discuss.python.org/t/proper-handling-of-pylist-setitem-return-value-and-reference-stealing-in-cpython-c-extensions/105805)

<div class="topic-metadata">

**Author:** [@wr-web](https://discuss.python.org/u/wr-web)\
**Replies:** 7\
**Last updated:** [February 4, 2026, 2:33pm UTC](https://discuss.python.org/t/proper-handling-of-pylist-setitem-return-value-and-reference-stealing-in-cpython-c-extensions/105805 "2026-02-04T14:33:01Z")

</div>

I have observed a recurring pattern in many third‑party CPython extension codes: if (PyList\_SetItem(a, b, item) \< 0) { Py\_DECREF(item); } else { ... } CPython PyList\_SetItem Source Code As far as I understand,…

---

## [Stable ABI/Limited API for free-threaded builds](https://discuss.python.org/t/stable-abi-limited-api-for-free-threaded-builds/86458)

<div class="topic-metadata">

**Author:** [@encukou](https://discuss.python.org/u/encukou)\
**Replies:** 51\
**Last updated:** [January 29, 2026, 7:59pm UTC](https://discuss.python.org/t/stable-abi-limited-api-for-free-threaded-builds/86458 "2026-01-29T19:59:25Z")

</div>

Hello, I’ve been looking into stable ABI for free-threading builds for the past few months. I think I have plans for all the pieces, but I don’t think I can assemble them for 3.14. I now need to take a break for a mont…

---

## [C-API for initializing statically linked extension modules](https://discuss.python.org/t/c-api-for-initializing-statically-linked-extension-modules/43396)

<div class="topic-metadata">

**Author:** [@itamaro](https://discuss.python.org/u/itamaro)\
**Replies:** 35\
**Last updated:** [January 29, 2026, 3:26pm UTC](https://discuss.python.org/t/c-api-for-initializing-statically-linked-extension-modules/43396 "2026-01-29T15:26:33Z")

</div>

At Meta, our Python “applications” (conceptually similar to a packaged venv) are statically linked binaries, with the main exe containing the Python interpreter, as well as native extensions statically linked into it. T…

---

## [C API for querying whether the GIL is enabled (PyInterpreterState\_IsGilEnabled)](https://discuss.python.org/t/c-api-for-querying-whether-the-gil-is-enabled-pyinterpreterstate-isgilenabled/105776)

<div class="topic-metadata">

**Author:** [@VanshAgarwal24036](https://discuss.python.org/u/VanshAgarwal24036)\
**Replies:** 14\
**Last updated:** [January 19, 2026, 5:45pm UTC](https://discuss.python.org/t/c-api-for-querying-whether-the-gil-is-enabled-pyinterpreterstate-isgilenabled/105776 "2026-01-19T17:45:15Z")

</div>

Hello, I’m looking for guidance on whether CPython should expose a public C API to allow extension modules to query whether the GIL is currently enabled, for example: int PyInterpreterState\_IsGilEnabled(PyInterpreterSt…

---

## [C API: How much private is the private \_Py\_IDENTIFIER() API?](https://discuss.python.org/t/c-api-how-much-private-is-the-private-py-identifier-api/29190)

<div class="topic-metadata">

**Author:** [@vstinner](https://discuss.python.org/u/vstinner)\
**Replies:** 8\
**Last updated:** [December 16, 2025, 11:53am UTC](https://discuss.python.org/t/c-api-how-much-private-is-the-private-py-identifier-api/29190 "2025-12-16T11:53:08Z")

</div>

Hi, I removed 181 privates functions in Python 3.13 C API. A code search on PyPI top 5,000 projects shows that the private \_Py\_Identifier API is widely used. Examples of lines using removed functions: \_PyObject\_CallMe…

---

## [Bikeshedding: Public function for \_PyObject\_Dump()](https://discuss.python.org/t/bikeshedding-public-function-for-pyobject-dump/105102)

<div class="topic-metadata">

**Author:** [@ZeroIntensity](https://discuss.python.org/u/ZeroIntensity)\
**Replies:** 5\
**Last updated:** [December 16, 2025, 10:18am UTC](https://discuss.python.org/t/bikeshedding-public-function-for-pyobject-dump/105102 "2025-12-16T10:18:14Z")

</div>

Recently, @vstinner proposed to promote the internal \_PyObject\_Dump function to a public API, per my request. The C API WG approved the public API as PyUnstable\_Object\_Dump, which I found to be a bit unfortunate. When d…

---

## [Proposal: Documenting CPython Reference Counting Semantics via Automated Analysis](https://discuss.python.org/t/proposal-documenting-cpython-reference-counting-semantics-via-automated-analysis/105283)

<div class="topic-metadata">

**Author:** [@wr-web](https://discuss.python.org/u/wr-web)\
**Replies:** 6\
**Last updated:** [December 15, 2025, 8:55am UTC](https://discuss.python.org/t/proposal-documenting-cpython-reference-counting-semantics-via-automated-analysis/105283 "2025-12-15T08:55:58Z")

</div>

Hello everyone, I’ve been working on a third-party project to systematically document CPython’s reference counting semantics—including internal APIs—through automated analysis. So far, I’ve collected 1,534 entries cover…

---

## [Frame state always \`running\` in \`sys.setprofile\` callback](https://discuss.python.org/t/frame-state-always-running-in-sys-setprofile-callback/104384)

<div class="topic-metadata">

**Author:** [@sumerc](https://discuss.python.org/u/sumerc)\
**Replies:** 1\
**Last updated:** [October 21, 2025, 10:46am UTC](https://discuss.python.org/t/frame-state-always-running-in-sys-setprofile-callback/104384 "2025-10-21T10:46:45Z")

</div>

Hi, During adding support for 3.14in Yappi profiler, we realized incorrect wall-time calculations in tests. Digging the issue it turns out that it basically fails to return if a frame is suspended or not in a profiler c…

---

## [Inefficiencies in "writer" APIs](https://discuss.python.org/t/inefficiencies-in-writer-apis/104289)

<div class="topic-metadata">

**Author:** [@markshannon](https://discuss.python.org/u/markshannon)\
**Replies:** 32\
**Last updated:** [October 20, 2025, 5:10pm UTC](https://discuss.python.org/t/inefficiencies-in-writer-apis/104289 "2025-10-20T17:10:16Z")

</div>

We have or are getting “writer” APIs for incrementally creating immutable objects like string, bytes and tuples. I am concerned that these APIs are inefficient and will thus be ignored by C extension authors and we will…

---

## [API for special attribute lookup](https://discuss.python.org/t/api-for-special-attribute-lookup/104288)

<div class="topic-metadata">

**Author:** [@encukou](https://discuss.python.org/u/encukou)\
**Replies:** 4\
**Last updated:** [October 13, 2025, 3:16pm UTC](https://discuss.python.org/t/api-for-special-attribute-lookup/104288 "2025-10-13T15:16:25Z")

</div>

Special attribute lookup is something I’ve been thinking about, and collecting notes, off and on for a while. Now Victor proposed to make \_PyType\_Lookup public, which I think is something of a special case, so let me tu…

---

## [What do you think of deprecating C APIs to modify immutable strings?](https://discuss.python.org/t/what-do-you-think-of-deprecating-c-apis-to-modify-immutable-strings/67057)

<div class="topic-metadata">

**Author:** [@vstinner](https://discuss.python.org/u/vstinner)\
**Replies:** 11\
**Last updated:** [September 24, 2025, 5:58pm UTC](https://discuss.python.org/t/what-do-you-think-of-deprecating-c-apis-to-modify-immutable-strings/67057 "2025-09-24T17:58:16Z")

</div>

Hi, Python C API allows modifying immutable strings. It’s a common pattern to create new strings. Examples of functions: PyUnicode\_New() PyUnicode\_FromStringAndSize(NULL, size) PyUnicode\_Resize() PyUnicode\_WriteChar() …

---

## [PyUnicode\_SubtypeFromData draft](https://discuss.python.org/t/pyunicode-subtypefromdata-draft/103255)

<div class="topic-metadata">

**Author:** [@encukou](https://discuss.python.org/u/encukou)\
**Replies:** 0\
**Last updated:** [August 22, 2025, 4:44pm UTC](https://discuss.python.org/t/pyunicode-subtypefromdata-draft/103255 "2025-08-22T16:44:51Z")

</div>

As identified in previous discussion, the remaining use case for PyUnicodeObject internals is initializing sublasses. Let’s solve this, so we can eventually deprecate – and then change – the internal layout. My design …

---

## [Free-threading: expose atomic operations](https://discuss.python.org/t/free-threading-expose-atomic-operations/101660)

<div class="topic-metadata">

**Author:** [@nedbat](https://discuss.python.org/u/nedbat)\
**Replies:** 10\
**Last updated:** [August 8, 2025, 7:55pm UTC](https://discuss.python.org/t/free-threading-expose-atomic-operations/101660 "2025-08-08T19:55:10Z")

</div>

I’ve gotten a great pull request to fix some threading problems in coverage.py: use atomics for \`started\` and \`activity\` flags by kumaraditya303 · Pull Request #2019 · nedbat/coveragepy · GitHub. This is great! But it …

---

## [\_run\_exitfuncs in atexit module](https://discuss.python.org/t/run-exitfuncs-in-atexit-module/101004)

<div class="topic-metadata">

**Author:** [@soolabettu](https://discuss.python.org/u/soolabettu)\
**Replies:** 2\
**Last updated:** [August 4, 2025, 12:26pm UTC](https://discuss.python.org/t/run-exitfuncs-in-atexit-module/101004 "2025-08-04T12:26:06Z")

</div>

This is a snippet from Modules/atexitmodule.c static PyMethodDef atexit\_methods\[\] = { {"run\_exitfuncs", atexit\_run\_exitfuncs, METH\_NOARGS, atexit\_run\_exitfuncs\_\_doc\_}, }; Looks like even if we comment run\_exitfuncs, r…

[Next page](https://discuss.python.org/c/core-dev/c-api/30.md?page=1)
