Skip to content

Commit 18f8581

Browse files
committed
gh-128863: Deprecate private C API functions
* _PyBytes_Join() * _PyDict_GetItemStringWithError() * _PyDict_Pop() * _PyThreadState_UncheckedGet() * _PyUnicode_AsString() * _Py_HashPointer() * _Py_fopen_obj() Replace _Py_HashPointer() with Py_HashPointer(). Remove references to deprecated functions.
1 parent ae7f621 commit 18f8581

16 files changed

+92
-30
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Pending removal in Python 3.16
2+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3+
4+
* Deprecated private functions (:gh:`128863`):
5+
6+
* :c:func:`!_PyBytes_Join`: use :c:func:`PyBytes_Join`.
7+
* :c:func:`!_PyDict_GetItemStringWithError`: use :c:func:`PyDict_GetItemStringRef`.
8+
* :c:func:`!_PyDict_Pop()`: :c:func:`PyDict_Pop`.
9+
* :c:func:`!_PyThreadState_UncheckedGet`: use :c:func:`PyThreadState_GetUnchecked`.
10+
* :c:func:`!_PyUnicode_AsString`: use :c:func:`PyUnicode_AsUTF8`.
11+
* :c:func:`!_Py_HashPointer`: use :c:func:`Py_HashPointer`.
12+
* :c:func:`!_Py_fopen_obj`: use :c:func:`Py_fopen`.

Doc/whatsnew/3.14.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,12 +1356,28 @@ Deprecated
13561356
13571357
.. include:: ../deprecations/c-api-pending-removal-in-3.15.rst
13581358

1359+
.. include:: ../deprecations/c-api-pending-removal-in-3.16.rst
1360+
13591361
.. include:: ../deprecations/c-api-pending-removal-in-future.rst
13601362

13611363
* The ``PyMonitoring_FireBranchEvent`` function is deprecated and should
13621364
be replaced with calls to :c:func:`PyMonitoring_FireBranchLeftEvent`
13631365
and :c:func:`PyMonitoring_FireBranchRightEvent`.
13641366

1367+
* The following private functions are deprecated and planned for removal in
1368+
Python 3.16:
1369+
1370+
* :c:func:`!_PyBytes_Join`: use :c:func:`PyBytes_Join`.
1371+
* :c:func:`!_PyDict_GetItemStringWithError`: use :c:func:`PyDict_GetItemStringRef`.
1372+
* :c:func:`!_PyDict_Pop()`: :c:func:`PyDict_Pop`.
1373+
* :c:func:`!_PyThreadState_UncheckedGet`: use :c:func:`PyThreadState_GetUnchecked`.
1374+
* :c:func:`!_PyUnicode_AsString`: use :c:func:`PyUnicode_AsUTF8`.
1375+
* :c:func:`!_Py_HashPointer`: use :c:func:`Py_HashPointer`.
1376+
* :c:func:`!_Py_fopen_obj`: use :c:func:`Py_fopen`.
1377+
1378+
(Contributed by Victor Stinner in :gh:`128863`.)
1379+
1380+
13651381
Removed
13661382
-------
13671383

Include/cpython/bytesobject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ static inline Py_ssize_t PyBytes_GET_SIZE(PyObject *op) {
3434

3535
PyAPI_FUNC(PyObject*) PyBytes_Join(PyObject *sep, PyObject *iterable);
3636

37-
// Alias kept for backward compatibility
38-
#define _PyBytes_Join PyBytes_Join
37+
// Deprecated alias kept for backward compatibility
38+
Py_DEPRECATED(3.14) static inline PyObject*
39+
_PyBytes_Join(PyObject *sep, PyObject *iterable)
40+
{
41+
return PyBytes_Join(sep, iterable);
42+
}

Include/cpython/dictobject.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
6868

6969
PyAPI_FUNC(int) PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result);
7070
PyAPI_FUNC(int) PyDict_PopString(PyObject *dict, const char *key, PyObject **result);
71-
PyAPI_FUNC(PyObject *) _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value);
71+
72+
// Use PyDict_Pop() instead
73+
Py_DEPRECATED(3.14) PyAPI_FUNC(PyObject *) _PyDict_Pop(
74+
PyObject *dict,
75+
PyObject *key,
76+
PyObject *default_value);
7277

7378
/* Dictionary watchers */
7479

Include/cpython/fileutils.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ PyAPI_FUNC(FILE*) Py_fopen(
66
PyObject *path,
77
const char *mode);
88

9-
// Deprecated alias to Py_fopen() kept for backward compatibility
10-
Py_DEPRECATED(3.14) PyAPI_FUNC(FILE*) _Py_fopen_obj(
11-
PyObject *path,
12-
const char *mode);
9+
// Deprecated alias kept for backward compatibility
10+
Py_DEPRECATED(3.14) static inline FILE*
11+
_Py_fopen_obj(PyObject *path, const char *mode)
12+
{
13+
return Py_fopen(path, mode);
14+
}
1315

1416
PyAPI_FUNC(int) Py_fclose(FILE *file);

Include/cpython/pyhash.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@
2929
/* Helpers for hash functions */
3030
PyAPI_FUNC(Py_hash_t) _Py_HashDouble(PyObject *, double);
3131

32-
// Kept for backward compatibility
33-
#define _Py_HashPointer Py_HashPointer
34-
3532

3633
/* hash function definition */
3734
typedef struct {
@@ -44,6 +41,14 @@ typedef struct {
4441
PyAPI_FUNC(PyHash_FuncDef*) PyHash_GetFuncDef(void);
4542

4643
PyAPI_FUNC(Py_hash_t) Py_HashPointer(const void *ptr);
44+
45+
// Deprecated alias kept for backward compatibility
46+
Py_DEPRECATED(3.14) static inline Py_hash_t
47+
_Py_HashPointer(const void *ptr)
48+
{
49+
return Py_HashPointer(ptr);
50+
}
51+
4752
PyAPI_FUNC(Py_hash_t) PyObject_GenericHash(PyObject *);
4853

4954
PyAPI_FUNC(Py_hash_t) Py_HashBuffer(const void *ptr, Py_ssize_t len);

Include/cpython/pystate.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,12 @@ struct _ts {
239239
* if it is NULL. */
240240
PyAPI_FUNC(PyThreadState *) PyThreadState_GetUnchecked(void);
241241

242-
// Alias kept for backward compatibility
243-
#define _PyThreadState_UncheckedGet PyThreadState_GetUnchecked
242+
// Deprecated alias kept for backward compatibility
243+
Py_DEPRECATED(3.14) static inline PyThreadState*
244+
_PyThreadState_UncheckedGet(void)
245+
{
246+
return PyThreadState_GetUnchecked();
247+
}
244248

245249

246250
// Disable tracing and profiling.

Include/cpython/unicodeobject.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,12 @@ _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer);
630630

631631
PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
632632

633-
// Alias kept for backward compatibility
634-
#define _PyUnicode_AsString PyUnicode_AsUTF8
633+
// Deprecated alias kept for backward compatibility
634+
Py_DEPRECATED(3.14) static inline const char*
635+
_PyUnicode_AsString(PyObject *unicode)
636+
{
637+
return PyUnicode_AsUTF8(unicode);
638+
}
635639

636640

637641
/* === Characters Type APIs =============================================== */

Lib/test/audit-tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class C(A):
187187

188188

189189
def test_open(testfn):
190-
# SSLContext.load_dh_params uses _Py_fopen_obj rather than normal open()
190+
# SSLContext.load_dh_params uses Py_fopen() rather than normal open()
191191
try:
192192
import ssl
193193

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The following private functions are deprecated and planned for removal in
2+
Python 3.16:
3+
4+
* :c:func:`!_PyBytes_Join`: use :c:func:`PyBytes_Join`.
5+
* :c:func:`!_PyDict_GetItemStringWithError`: use :c:func:`PyDict_GetItemStringRef`.
6+
* :c:func:`!_PyDict_Pop()`: :c:func:`PyDict_Pop`.
7+
* :c:func:`!_PyThreadState_UncheckedGet`: use :c:func:`PyThreadState_GetUnchecked`.
8+
* :c:func:`!_PyUnicode_AsString`: use :c:func:`PyUnicode_AsUTF8`.
9+
* :c:func:`!_Py_HashPointer`: use :c:func:`Py_HashPointer`.
10+
* :c:func:`!_Py_fopen_obj`: use :c:func:`Py_fopen`.
11+
12+
Patch by Victor Stinner.

Objects/descrobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1349,7 +1349,7 @@ wrapper_hash(PyObject *self)
13491349
wrapperobject *wp = (wrapperobject *)self;
13501350
Py_hash_t x, y;
13511351
x = PyObject_GenericHash(wp->self);
1352-
y = _Py_HashPointer(wp->descr);
1352+
y = Py_HashPointer(wp->descr);
13531353
x = x ^ y;
13541354
if (x == -1)
13551355
x = -2;

Objects/dictobject.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,8 +3088,8 @@ PyDict_PopString(PyObject *op, const char *key, PyObject **result)
30883088
}
30893089

30903090

3091-
PyObject *
3092-
_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value)
3091+
static PyObject *
3092+
dict_pop_default(PyObject *dict, PyObject *key, PyObject *default_value)
30933093
{
30943094
PyObject *result;
30953095
if (PyDict_Pop(dict, key, &result) == 0) {
@@ -3102,6 +3102,12 @@ _PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value)
31023102
return result;
31033103
}
31043104

3105+
PyObject *
3106+
_PyDict_Pop(PyObject *dict, PyObject *key, PyObject *default_value)
3107+
{
3108+
return dict_pop_default(dict, key, default_value);
3109+
}
3110+
31053111
static PyDictObject *
31063112
dict_dict_fromkeys(PyInterpreterState *interp, PyDictObject *mp,
31073113
PyObject *iterable, PyObject *value)
@@ -4463,7 +4469,7 @@ static PyObject *
44634469
dict_pop_impl(PyDictObject *self, PyObject *key, PyObject *default_value)
44644470
/*[clinic end generated code: output=3abb47b89f24c21c input=e221baa01044c44c]*/
44654471
{
4466-
return _PyDict_Pop((PyObject*)self, key, default_value);
4472+
return dict_pop_default((PyObject*)self, key, default_value);
44674473
}
44684474

44694475
/*[clinic input]

Objects/methodobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ meth_hash(PyObject *self)
331331
{
332332
PyCFunctionObject *a = _PyCFunctionObject_CAST(self);
333333
Py_hash_t x = PyObject_GenericHash(a->m_self);
334-
Py_hash_t y = _Py_HashPointer((void*)(a->m_ml->ml_meth));
334+
Py_hash_t y = Py_HashPointer((void*)(a->m_ml->ml_meth));
335335
x ^= y;
336336
if (x == -1) {
337337
x = -2;

Objects/odictobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ mp_length __len__ - dict_length
260260
mp_subscript __getitem__ - dict_subscript
261261
mp_ass_subscript __setitem__ - dict_ass_sub
262262
__delitem__
263-
tp_hash __hash__ _Py_HashPointer ..._HashNotImpl
263+
tp_hash __hash__ Py_HashPointer ..._HashNotImpl
264264
tp_str __str__ object_str -
265265
tp_getattro __getattribute__ ..._GenericGetAttr (repeated)
266266
__getattr__

Python/context.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ contextvar_generate_hash(void *addr, PyObject *name)
860860
return -1;
861861
}
862862

863-
Py_hash_t res = _Py_HashPointer(addr) ^ name_hash;
863+
Py_hash_t res = Py_HashPointer(addr) ^ name_hash;
864864
return res == -1 ? -2 : res;
865865
}
866866

Python/fileutils.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,14 +1841,6 @@ Py_fopen(PyObject *path, const char *mode)
18411841
}
18421842

18431843

1844-
// Deprecated alias to Py_fopen() kept for backward compatibility
1845-
FILE*
1846-
_Py_fopen_obj(PyObject *path, const char *mode)
1847-
{
1848-
return Py_fopen(path, mode);
1849-
}
1850-
1851-
18521844
// Call fclose().
18531845
//
18541846
// On Windows, files opened by Py_fopen() in the Python DLL must be closed by

0 commit comments

Comments
 (0)