Text of peps.python.org › pep-0743 as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

PEP 743 – Add Py_OMIT_LEGACY_API to the Python C API | peps.python.org

PEP 743 – Add Py_OMIT_LEGACY_API to the Python C API PEP 743 – Add Py_OMIT_LEGACY_API to the Python C API Author : Victor Stinner <vstinner at python.org>, Petr Viktorin <encukou at gmail.com> Discussions-To : Discourse thread Status : Rejected Type : Standards Track Created : 11-Mar-2024 Python-Version : 3.15 Post-History : 11-Mar-2024 , 27-Jul-2024 Resolution : 20-Feb-2026 Table of Contents Rejection Notice Abstract Motivation Adding the Py prefix Rationale Specification Requirements for omitted API Location Documentation Initial set Implementation Backwards Compatibility Discussions Prior Art Copyright Rejection Notice The Steering Council has rejected this PEP in its current form: While we agree that the problem the PEP tries to solve is one worth solving, we’re not convinced this is the correct approach.

See the full post . Abstract Add Py_OMIT_LEGACY_API C macro that hides deprecated and soft-deprecated symbols, allowing users to opt out of using API with known issues that other API solves. Also, add namespaced alternatives for API without the Py_ prefix, and soft-deprecate the original names. Motivation Some of Python’s C API has flaws that are only obvious in hindsight. If an API prevents adding features or optimizations, or presents a serious security risk or maintenance burden, we can deprecate and remove it as described in PEP 387 .

However, this leaves us with some API that has “sharp edges” – it works fine for its current users, but should be avoided in new code. For example: API that cannot signal an exception, so failures are either ignored or exit the process with a fatal error. For example PyObject_HasAttr . API that is not thread-safe, for example by borrowing references from mutable objects, or exposing unfinished mutable objects. For example PyDict_GetItemWithError .

API with names that don’t use the Py / _Py prefix, and so can clash with other code. For example: setter . It is important to note that despite such flaws, it’s usually possible to use the API correctly. For example, in a single-threaded environment, thread safety is not an issue. We do not want to break working code, even if it uses API that would be wrong in some – or even most – other contexts. On the other hand, we want to steer users away from such “undesirable” API in new code, especially if a safer alternative exists.

Adding the Py prefix Some names defined in CPython headers is not namespaced: it that lacks the Py prefix (or a variant: _Py , and alternative capitalizations). For example, we declare a function type named simply setter . While such names are not exported in the ABI (as checked by make smelly ), they can clash with user code and, more importantly, with libraries linked to third-party extensions. While it would be possible to provide namespaced aliases and (soft-)deprecate these names, the only way to make them not clash with third-party code is to not define them in Python headers at all.

Rationale We want to allow an easy way for users to avoid “undesirable” API if they choose to do so. It might be be sufficient to leave this to third-party linters. For that we’d need a good way to expose a list of (soft-)deprecated API to such linters. While adding that, we can – rather easily – do the linter’s job directly in CPython headers, avoiding the need for an extra tool. Unlike Python, C makes it rather easy to limit available API – for a whole project or for each individual source file – by having users define an “opt-in” macro.

We already do something similar with Py_LIMITED_API , which limits the available API to a subset that compiles to stable ABI. (In hindsight, we should have used a different macro name for that particular kind of limiting, but it’s too late to change that now.) To be clear, this mechanism is not a replacement for deprecation. Deprecation is for API that prevents new features or optimizations, or presents a security risk or maintenance burden.

This mechanism, on the other hand, is meant for cases where “we found a slightly better way of doing things” – perhaps one that’s harder to misuse, or just has a less misleading name. (On a lighter note: many people configure a code quality checker to shout at them about the number of blank lines between functions. Let’s help them identify more substantial “code smells”!) The proposed macro does not change any API definitions; it only hides them.

So, if code compiles with the macro, it’ll also compile without it, with identical behaviour. This has implications for core devs: to deal with undesirable behaviour, we’ll need to introduce new, better API, and then discourage the old one. In turn, this implies that we should look at an individual API and fix all its known issues at once, rather than do codebase-wide sweeps for a single kind of issue, so that we avoid multiple renames of the same function.

Specification We introduce a Py_OMIT_LEGACY_API macro. If this macro is defined before #include <Python.h> , some API definitions – as described below – will be omitted from the Python header files. The macro only omits complete top-level definitions exposed from <Python.h> . Other things (the ABI, structure definitions, macro expansions, static inline function bodies, etc.) are not affected. The C API working group ( PEP 731 ) has authority over the set of omitted definitions.

The set of omitted definitions will be tied to a particular feature release of CPython, and is finalized in each 3.x.0 Beta 1 release. In rare cases, entries can be removed (i.e. made available for use) at any time. Requirements for omitted API An API that is omitted with Py_OMIT_LEGACY_API must: be soft-deprecated (see PEP 387 ); for all known use cases of the API, have a documented alternative or workaround; have tests to ensure it keeps working (except for 1:1 renames using #define or typedef ); be documented (except if it was never mentioned in previous versions of the documentation); and be approved by the C API working group. (The WG may give blanket approvals for groups of related API; see Initial set below for examples.) Note that Py_OMIT_LEGACY_API is meant for API that can be trivially replaced by a better alternative.

API without a replacement should generally be deprecated instead. Location All API definitions omitted by Py_OMIT_LEGACY_API will be moved to a new header, Include/legacy.h . This is meant to help linter authors compile lists, so they can flag the API with warnings rather than errors. Note that for simple renaming of source-only constructs (macros, types), we expect names to be omitted in the same version – or the same PR – that adds a replacement.

This means that the original definition will be renamed, and a typedef or #define for the old name added to Include/legacy.h . Documentation Documentation for omitted API should generally: appear after the recommended replacement, reference the replacement (e.g. “Similar to X, but…”), and focus on differences from the replacement and migration advice. Exceptions are possible if there is a good reason for them. Initial set The following API will be omitted with Py_OMIT_LEGACY_API set: Omit API returning borrowed references: Omitted API Replacement PyDict_GetItem() PyDict_GetItemRef() PyDict_GetItemString() PyDict_GetItemStringRef() PyImport_AddModule() PyImport_AddModuleRef() PyList_GetItem() PyList_GetItemRef() Omit deprecated APIs: Omitted Deprecated API Replacement PY_FORMAT_SIZE_T "z" PY_UNICODE_TYPE wchar_t PyCode_GetFirstFree() PyUnstable_Code_GetFirstFree() PyCode_New() PyUnstable_Code_New() PyCode_NewWithPosOnlyArgs() PyUnstable_Code_NewWithPosOnlyArgs() PyImport_ImportModuleNoBlock() PyImport_ImportModule() PyMem_DEL() PyMem_Free() PyMem_Del() PyMem_Free() PyMem_FREE() PyMem_Free() PyMem_MALLOC() PyMem_Malloc() PyMem_NEW() PyMem_New() PyMem_REALLOC() PyMem_Realloc() PyMem_RESIZE() PyMem_Resize() PyModule_GetFilename() PyModule_GetFilenameObject() PyOS_AfterFork()

The rest of this page is on the live site.