# Remove (again) private APIs which now have new public replacement?

**URL:** <https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081>\
**Category:** C API\
**Created:** [October 16, 2024, 10:30am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081 "2024-10-16T10:30:00Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![vstinner](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vstinner/32/15130_2.png) [@vstinner](https://discuss.python.org/u/vstinner)\
**Post date:** [October 16, 2024, 10:30am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/1 "2024-10-16T10:30:01Z")

</div>

Hi,

In Python 3.13 alpha 1, I [removed many private functions](https://discuss.python.org/t/c-api-my-plan-to-clarify-private-vs-public-functions-in-python-3-13/30131). In Python 3.13 alpha 2, we decided to [revert around 50 private functions](https://discuss.python.org/t/revert-python-3-13-c-api-incompatible-changes-causing-most-troubles/38214) which caused most troubles. In the meanwhile, many public functions were added to Python 3.13 and 3.14 to replace private functions. A few examples: `Py_HashBuffer()`, `PyLong_GetSign()`, `PyTime_Time()`, `PyDict_Pop()`, etc. I added these functions to [pythoncapi-compat](https://pythoncapi-compat.readthedocs.io/). So it’s possible to use these new functions on Python 3.6 and newer right now using pythoncapi-compat.

What do you think of removing in Python 3.14 (alpha 2) the private functions which now have public functions to replace them?

Should it be **decided on a case by case basis** depending on how easy it is to upgrade the code, and how many times the function is used (e.g. in PyPI top 7,5000 projects)?

Should we **keep** the private functions **forever**?

Should we **deprecate** private functions first, add `Py_DEPRECATED()` and maybe even emit `DeprecationWarning` at runtime when possible? In previous discussions, this idea was not popular.

My motivation to remove private functions is to:

- Clarify the scope of the C API: define better what can be used outside Python, and which API is really “internal”.
- Provide better APIs, less error-prone, well tested, documented. For example, public `PyDict_Pop()` has a better API than private `_PyDict_Pop()`, the public API is easier to use and less error-prone.
- Make the C API smaller, and so easier to maintain in CPython, and easier to implement in other Python implementations.

The alternative of removing private APIs for everybody is the [PEP 743 – Add Py\_COMPAT\_API\_VERSION to the Python C API](https://discuss.python.org/t/pep-743-add-py-compat-api-version-to-the-python-c-api-take-2/59323) which offer a way for **opt-in** option for a cleaner and smaller C API. It has the advantage of not affecting the default API, and only users who choose the opt-in.

Victor

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [October 16, 2024, 9:22pm UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/2 "2024-10-16T21:22:22Z")

</div>

I’d vote for case-by-case cleanup based on those that would actually reduce our maintenance burden.

We’ve had one request already to remove functions asap, so let’s honour that, but if the function still works and isn’t costing us anything then we may as well leave it there.

Now that we have public alternatives, we don’t have to be concerned about breaking them if we need to change something. But I see no reason to break people just for the fun of it.

---

<div class="post-metadata">

**Author:** ![scoder](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/scoder/32/36095_2.png) [@scoder](https://discuss.python.org/u/scoder)\
**Post date:** [October 18, 2024, 3:32pm UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/3 "2024-10-18T15:32:19Z")

</div>

I second Steve’s comment. Why risk breaking existing code needlessly? If existing non-public C-API functions get in the way, consider breaking them, as always. If they don’t hurt anyone, wait with bothering the world.

---

<div class="post-metadata">

**Author:** ![ruang](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ruang/32/22210_2.png) [@ruang](https://discuss.python.org/u/ruang)\
**Post date:** [October 21, 2024, 9:10am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/4 "2024-10-21T09:10:00Z")

</div>

I agree with victor. In fact, too many private APIs will make public APIs that reuse these private APIs more weird. I think removing useless private APIs can make internal APIs more unified. I think necessary APIs should be split into minimal implementations, which can make reused code more flexible and easier to debug.  
I think it is a good idea to use `Py_COMPAT_API_VERSION`, which should be similar to `LINUX_VERSION_CODE`. This can make some old APIs optional, depending on whether the user needs the feature.

---

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [October 22, 2024, 9:28am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/5 "2024-10-22T09:28:44Z")

</div>

> [@vstinner](#):
>
> The alternative of removing private APIs for everybody is the [PEP 743 – Add Py\_COMPAT\_API\_VERSION to the Python C API](https://discuss.python.org/t/pep-743-add-py-compat-api-version-to-the-python-c-api-take-2/59323) which offer a way for **opt-in** option for a cleaner and smaller C API. It has the advantage of not affecting the default API, and only users who choose the opt-in.

That would be my preference. Be explicit about APIs we don’t like, and guide people toward best practices, but don’t break existing code unnecessarily.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [October 22, 2024, 9:24pm UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/6 "2024-10-22T21:24:25Z")

</div>

How is providing a public macro any kind of alternative to removing private APIs? Are they private, or are they public?

Can we please try to be a bit consistent here, rather than either pushing on pet projects for unrelated reasons or trying to make all APIs treated as public by stealth?

---

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [October 23, 2024, 9:23am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/7 "2024-10-23T09:23:29Z")

</div>

> [@steve.dower](#):
>
> Are they private, or are they public?

IMO, over the decades, our messaging has been inconsistent. We’re making it consistent – you really shouldn’t use underscored functions – but new messaging is not helpful for people who inherited existing code.  
I would like to break them as little as possible. When we can use public-API mechanisms (like a deprecation period or just leaving the thing in), that is the friendly thing to do.

> [@steve.dower](#):
>
> How is providing a public macro any kind of alternative to removing private APIs?

That depends which problem solve by removing private APIs.

- If we’re doing it for _users_, IMO the best we can do is close to what a linter does: a way to let them know they’re using API that can be removed without notice. But, not breaking their code _now_.

- If we’re doing it for _ourselves_, to reduce maintenance burden, IMO we should just leave public headers be until there’s a reason to change them.

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [October 23, 2024, 2:44pm UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/8 "2024-10-23T14:44:25Z")

</div>

> [@encukou](#):
>
> IMO, over the decades, our messaging has been inconsistent. We’re making it consistent – you really shouldn’t use underscored functions – but new messaging is not helpful for people who inherited existing code.

It must be decades, because for the last decade since I’ve been a core dev it’s been very consistent. And I suspect anyone who inherited code from more than ten years ago has likely already experienced breakage of some kind, if not from us, then from their OS.

Inventing a new mechanism for warnings doesn’t help people who are ignoring their inherited code and hoping it keeps working.\[1\] If they’re not ignoring it, then it’s very easy to search for `_Py` whenever they like - arguably easier than modifying compiler settings (again, for code that has worked since the 2000’s).

I think Petr and I keep landing at the same place, which is making the minimal possible changes, and only making them for our own benefit. I just can’t rationalise going through and modifying every function definition so that it is included/excluded based on brand new optional macros as “minimal” or “our own benefit”.

* * *

1. I don’t know of anyone like this, but I have to assume they exist because we keep trying to support them.

---

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [October 24, 2024, 11:27am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/9 "2024-10-24T11:27:26Z")

</div>

Yeah. If we _need_ to do something, let’s not break people who didn’t ask for it. But, we don’t need to do something.

> [@steve.dower](#):
>
> Inventing a new mechanism for warnings doesn’t help people who are ignoring their inherited code and hoping it keeps working.

You’re right – that is intended to help other users – the kind that sets up linters to yell about wrong amounts of whitespace. (Interestingly, that kind of “code quality” tool seems more common in C-and-Python projects than grepping the C code for `\b_Py`. Why is that?)

> [@steve.dower](#):
>
> don’t know of anyone [who inherited code and hoping it keeps working], but I have to assume they exist because we keep trying to support them.

That extreme is, IMO, most useful as the persona to think about when writing What’s New notes & deprecation notices, so that these docs work for everyone.  
(Writing those docs is hard, and making them required might help stability a lot.)

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [October 24, 2024, 4:05pm UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/10 "2024-10-24T16:05:46Z")

</div>

> [@encukou](#):
>
> That extreme is, IMO, most useful as the persona to think about when writing What’s New notes & deprecation notices, so that these docs work for everyone.  
> (Writing those docs is hard, and making them required might help stability a lot.)

+100 (also porting notes). I’m very much in favour of making it harder for us to change things that impact existing users at all, though preferably harder in _beneficial_ ways such as this (and not by making people manage hundred-post long discourse threads…)

---

<div class="post-metadata">

**Author:** ![vstinner](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vstinner/32/15130_2.png) [@vstinner](https://discuss.python.org/u/vstinner)\
**Post date:** [January 15, 2025, 11:24am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/11 "2025-01-15T11:24:58Z")

</div>

I created [PR gh-128864](https://github.com/python/cpython/pull/128864) to deprecate 7 private C API functions which now have a public replacement:

- `_PyBytes_Join()`
- `_PyDict_GetItemStringWithError()`
- `_PyDict_Pop()`
- `_PyThreadState_UncheckedGet()`
- `_PyUnicode_AsString()`
- `_Py_HashPointer()`
- `_Py_fopen_obj()`

The PR **documents** how to replace these private functions with public functions. The documentation points to the [pythoncapi-compat project](https://github.com/python/pythoncapi-compat/) to get replacement functions on Python 3.13 and older.

The PR also schedules the removal of these functions in Python 3.18.

**Update:** The removal is scheduled in Python 3.18 instead of Python 3.16.

---

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [January 24, 2025, 12:43pm UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/12 "2025-01-24T12:43:27Z")

</div>

I feel that this discussion has been ignored.

* * *

Let’s look at the first one:

```c
// Alias kept for backward compatibility
#define _PyBytes_Join PyBytes_Join

```

There is no maintenance burden for this function. There is no danger of us changing the signature or semantics – such a change would need to apply to the public function as well. If the public version is deprecated or removed, that will automatically apply to the private one too.  
There is no one who will be helped by deprecating or removing this function. All the deprecation does is force users \[1\] to change working (though not ideal) code, for no reason that I can see.

I’d much rather have users focus their time and attention on _necessary_ changes.

* * *

Let’s look at another one, removed just now: `_PyLong_FromDigits`. With this one, there _is_ a maintenance burden to keep working.  
However, the removal added no porting notes: the What’s New notice only says “`_PyLong_FromDigits` and `_PyLong_New`: use `PyLongWriter_Create`.”

And that’s the case for all functions removed so far: there is no mention of whether the replacements are different, and how to actually switch.

* * *

1. now or in the future, depending on whether they treat deprecation warnings as actionable

---

<div class="post-metadata">

**Author:** ![vstinner](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vstinner/32/15130_2.png) [@vstinner](https://discuss.python.org/u/vstinner)\
**Post date:** [January 25, 2025, 4:22pm UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/13 "2025-01-25T16:22:59Z")

</div>

In Python 3.13 and before, private functions have been removed immediately and the removal was not documented or announced. In Python 3.14, private functions are only deprecated, with a delay a 4 years to update impacted code. Moreover, only private functions with a public replacement have been deprecated, and the deprecation is documented with hints on how to update the code. So I don’t consider that this discussion has been ignored.

> [@encukou](#):
>
> ```python
> // Alias kept for backward compatibility
> #define _PyBytes_Join PyBytes_Join
> 
> ```
> 
> There is no maintenance burden for this function.

The maintenance burden of the C API is not the only motivation to deprecate private functions. See my first message in this thread.

> [@encukou](#):
>
> However, the removal added no porting notes: the What’s New notice only says “`_PyLong_FromDigits` and `_PyLong_New`: use `PyLongWriter_Create`.”
> 
> And that’s the case for all functions removed so far: there is no mention of whether the replacements are different, and how to actually switch.

You’re right, the documentation can be enhanced and completed. Contributions are welcomed 🙂

---

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [January 27, 2025, 10:31am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/14 "2025-01-27T10:31:44Z")

</div>

> [@vstinner](#):
>
> The maintenance burden of the C API is not the only motivation to deprecate private functions. See my first message in this thread.

Could you be more specific? For example for `_PyBytes_Join`:

> Clarify the scope of the C API: define better what can be used outside Python, and which API is really “internal”.

With porting notes asking people to move from `_PyBytes_Join` to `PyBytes_Join`, this should be

> Provide better APIs, less error-prone, well tested, documented. For example, public `PyDict_Pop()` has a better API than private `_PyDict_Pop()`, the public API is easier to use and less error-prone.

This is a fine goal, but I don’t think it’s related to removing existing API (which might be harder to use and more error-prone, _but_ it currently works in existing software.)

> Make the C API smaller, and so easier to maintain in CPython, and easier to implement in other Python implementations.

For making the API easier to implement, it does _helps_ to remove things, but I’m not sure if the benefits are worth the cost.  
As for “make the C API smaller”, that seems like another way of saying “remove things”. I don’t think it works as motivation for removing things.

* * *

> [@vstinner](#):
>
> You’re right, the documentation can be enhanced and completed. Contributions are welcomed 🙂

I don’t think it should work this way. It feels like you are creating work for others, at scale.

Do you think it would be fair to ask you to not merge or approve PRs that deprecate/remove functions without complete porting notes?

---

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [February 4, 2025, 11:04am UTC](https://discuss.python.org/t/remove-again-private-apis-which-now-have-new-public-replacement/68081/15 "2025-02-04T11:04:15Z")

</div>

It seems that for some of the APIs, the main reason to remove them is to allow us to change the internals – to add new features, optimizations, or simplifications.  
IMO, that’s a good reason to remove them _at the time when we need to make the change_.

I don’t think pre-emptively deprecating these, with a removal date set a few releases in the future, works well. If the internals need to be changed sooner, we’re either blocked or break the API before the documented time. If we don’t get around to changing the internals, but remove the API anyway, we break users without a good reason.

Would it be better to mark them as deprecated, but _without_ a planned removal date? Then, they can be removed when necessary. (Like any private API, of course – but with a warning and with porting notes. Good porting notes in particular should help to ensure that it’s _possible_ to switch to supported APIs, and make the switch as painless as possible.)
