8000 gh-106320: Remove private _PyDict C API · python/cpython@0bc07c2 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0bc07c2

Browse files
committed
gh-106320: Remove private _PyDict C API
Move private _PyDict functions to the internal C API (pycore_dict.h): * _PyDict_Contains_KnownHash() * _PyDict_DebugMallocStats() * _PyDict_DelItemIf() * _PyDict_DelItem_KnownHash() * _PyDict_GetItemWithError() * _PyDict_GetItem_KnownHash() * _PyDict_HasOnlyStringKeys() * _PyDict_MaybeUntrack() * _PyDict_MergeEx() * _PyDict_NewPresized() * _PyDict_SetItem_KnownHash() No longer export most of these functions, except of the ones used by the _asyncio shared extension. Move dict_getitem_knownhash() from _testcapi to _testinternalcapi.
1 parent 956b3de commit 0bc07c2

20 files changed

+89
-56
lines changed

Include/cpython/dictobject.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,17 @@ typedef struct {
3232
PyDictValues *ma_values;
3333
} PyDictObject;
3434

35-
PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
36-
Py_hash_t hash);
37-
PyAPI_FUNC(PyObject *) _PyDict_GetItemWithError(PyObject *dp, PyObject *key);
35+
PyAPI_FUNC(PyObject*) _PyDict_GetItemStringWithError(PyObject *, const char *);
36+
37+
// _Py_Identifier variants
3838
PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
3939
_Py_Identifier *key);
40-
PyAPI_FUNC(PyObject *) _PyDict_GetItemStringWithError(PyObject *, const char *);
40+
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, _Py_Identifier *);
41+
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, _Py_Identifier *key, PyObject *item);
42+
PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, _Py_Identifier *key);
43+
4144
PyAPI_FUNC(PyObject *) PyDict_SetDefault(
4245
PyObject *mp, PyObject *key, PyObject *defaultobj);
43-
PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
44-
PyObject *item, Py_hash_t hash);
45-
PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
46-
Py_hash_t hash);
47-
PyAPI_FUNC(int) _PyDict_DelItemIf(PyObject *mp, PyObject *key,
48-
int (*predicate)(PyObject *value));
4946
PyAPI_FUNC(int) _PyDict_Next(
5047
PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, Py_h A93C ash_t *hash);
5148

@@ -58,26 +55,10 @@ static inline Py_ssize_t PyDict_GET_SIZE(PyObject *op) {
5855
}
5956
#define PyDict_GET_SIZE(op) PyDict_GET_SIZE(_PyObject_CAST(op))
6057

61-
PyAPI_FUNC(int) _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
62-
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, _Py_Identifier *);
63-
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
64-
PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
65-
PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
6658
PyAPI_FUNC(Py_ssize_t) _PyDict_SizeOf(PyDictObject *);
6759
PyAPI_FUNC(PyObject *) _PyDict_Pop(PyObject *, PyObject *, PyObject *);
6860
#define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)
6961

70-
/* Like PyDict_Merge, but override can be 0, 1 or 2. If override is 0,
71-
the first occurrence of a key wins, if override is 1, the last occurrence
72-
of a key wins, if override is 2, a KeyError with conflicting key as
73-
argument is raised.
74-
*/
75-
PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
76-
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, _Py_Identifier *key, PyObject *item);
77-
78-
PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, _Py_Identifier *key);
79-
PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);
80-
8162
/* _PyDictView */
8263

8364
typedef struct {

Include/internal/pycore_dict.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,39 @@ extern "C" {
1212
#include "pycore_dict_state.h"
1313
#include "pycore_runtime.h" // _PyRuntime
1414

15+
extern PyObject* _PyDict_NewPresized(Py_ssize_t minused);
16+
17+
// Unsafe flavor of PyDict_GetItemWithError(): no error checking
18+
extern PyObject* _PyDict_GetItemWithError(PyObject *dp, PyObject *key);
19+
20+
// Similar to PyDict_GetItem(), PyDict_SetItem(), PyDict_DelItem(),
21+
// and PyDict_Contains(), but with the key hash.
22+
//
23+
// Export _PyDict_GetItem_KnownHash(), _PyDict_SetItem_KnownHash(),
24+
// and _PyDict_DelItem_KnownHash for shared _asyncio extension.
25+
PyAPI_FUNC(PyObject*) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
26+
Py_hash_t hash);
27+
PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
28+
PyObject *item, Py_hash_t hash);
29+
PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
30+
Py_hash_t hash);
31+
extern int _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
32+
33+
extern int _PyDict_DelItemIf(PyObject *mp, PyObject *key,
34+
int (*predicate)(PyObject *value));
35+
36+
extern int _PyDict_HasOnlyStringKeys(PyObject *mp);
37+
38+
extern void _PyDict_MaybeUntrack(PyObject *mp);
39+
40+
/* Like PyDict_Merge, but override can be 0, 1 or 2. If override is 0,
41+
the first occurrence of a key wins, if override is 1, the last occurrence
42+
of a key wins, if override is 2, a KeyError with conflicting key as
43+
argument is raised.
44+
*/
45+
extern int _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
46+
47+
extern void _PyDict_DebugMallocStats(FILE *out);
1548

1649
/* runtime lifecycle */
1750

Lib/test/test_dict.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,8 +1582,8 @@ class CAPITest(unittest.TestCase):
15821582
# Test _PyDict_GetItem_KnownHash()
15831583
@support.cpython_only
15841584
def test_getitem_knownhash(self):
1585-
_testcapi = import_helper.import_module('_testcapi')
1586-
dict_getitem_knownhash = _testcapi.dict_getitem_knownhash
1585+
_testinternalcapi = import_helper.import_module('_testinternalcapi')
1586+
dict_getitem_knownhash = _testinternalcapi.dict_getitem_knownhash
15871587

15881588
d = {'x': 1, 'y': 2, 'z': 3}
15891589
self.assertEqual(dict_getitem_knownhash(d, 'x', hash('x')), 1)

Modules/_asynciomodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#endif
44

55
#include "Python.h"
6+
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
67
#include "pycore_moduleobject.h" // _PyModule_GetState()
78
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
89
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()

Modules/_collectionsmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "Python.h"
22
#include "pycore_call.h" // _PyObject_CallNoArgs()
3+
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
34
#include "pycore_long.h" // _PyLong_GetZero()
45
#include "pycore_moduleobject.h" // _PyModule_GetState()
56
#include "pycore_typeobject.h" // _PyType_GetModuleState()
7+
68
#include "structmember.h" // PyMemberDef
79
#include <stddef.h>
810

Modules/_sre/sre.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static const char copyright[] =
3939
" SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB ";
4040

4141
#include "Python.h"
42+
#include "pycore_dict.h" // _PyDict_SetItem_KnownHash()
4243
#include "pycore_long.h" // _PyLong_GetZero()
4344
#include "pycore_moduleobject.h" // _PyModule_GetState()
4445
#include "structmember.h" // PyMemberDef

Modules/_testcapimodule.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -260,26 +260,6 @@ test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored))
260260
Py_RETURN_NONE;
261261
}
262262

263-
static PyObject*
264-
dict_getitem_knownhash(PyObject *self, PyObject *args)
265-
{
266-
PyObject *mp, *key, *result;
267-
Py_ssize_t hash;
268-
269-
if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
270-
&mp, &key, &hash)) {
271-
return NULL;
272-
}
273-
274-
result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
275-
if (result == NULL && !PyErr_Occurred()) {
276-
_PyErr_SetKeyError(key);
277-
return NULL;
278-
}
279-
280-
return Py_XNewRef(result);
281-
}
282-
283263
/* Issue #4701: Check that PyObject_Hash implicitly calls
284264
* PyType_Ready if it hasn't already been called
285265
*/
@@ -3660,7 +3640,6 @@ static PyMethodDef TestMethods[] = {
36603640
{"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS},
36613641
{"test_list_api", test_list_api, METH_NOARGS},
36623642
{"test_dict_iteration", test_dict_iteration, METH_NOARGS},
3663-
{"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
36643643
{"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS},
36653644
{"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS},
36663645
{"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS},

Modules/_testinternalcapi.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "pycore_bytesobject.h" // _PyBytes_Find()
1717
#include "pycore_ceval.h" // _PyEval_AddPendingCall
1818
#include "pycore_compile.h" // _PyCompile_CodeGen, _PyCompile_OptimizeCfg, _PyCompile_Assemble, _PyCompile_CleanDoc
19+
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
1920
#include "pycore_fileutils.h" // _Py_normpath
2021
#include "pycore_frame.h" // _PyInterpreterFrame
2122
#include "pycore_gc.h" // PyGC_Head
@@ -1446,6 +1447,27 @@ test_atexit(PyObject *self, PyObject *Py_UNUSED(args))
14461447
}
14471448

14481449

1450+
static PyObject*
1451+
dict_getitem_knownhash(PyObject *self, PyObject *args)
1452+
{
1453+
PyObject *mp, *key, *result;
1454+
Py_ssize_t hash;
1455+
1456+
if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
1457+
&mp, &key, &hash)) {
1458+
return NULL;
1459+
}
1460+
1461+
result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
1462+
if (result == NULL && !PyErr_Occurred()) {
1463+
_PyErr_SetKeyError(key);
1464+
return NULL;
1465+
}
1466+
1467+
return Py_XNewRef(result);
1468+
}
1469+
1470+
14491471
static PyMethodDef module_functions[] = {
14501472
{"get_configs", get_configs, METH_NOARGS},
14511473
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@@ -1502,6 +1524,7 @@ static PyMethodDef module_functions[] = {
15021524
{"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL},
15031525
{"_PyUnicode_TransformDecimalAndSpaceToASCII", unicode_transformdecimalandspacetoascii, METH_O},
15041526
{"test_atexit", test_atexit, METH_NOARGS},
1527+
{"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
15051528
{NULL, NULL} /* sentinel */
15061529
};
15071530

Modules/_weakref.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_dict.h" // _PyDict_DelItemIf()
23
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
34
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()
45

Modules/gcmodule.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525

2626
#include "Python.h"
2727
#include "pycore_context.h"
28+
#include "pycore_dict.h" // _PyDict_MaybeUntrack()
2829
#include "pycore_initconfig.h"
29-
#include "pycore_interp.h" // PyInterpreterState.gc
30+
#include "pycore_interp.h" // PyInterpreterState.gc
3031
#include "pycore_object.h"
3132
#include "pycore_pyerrors.h"
32-
#include "pycore_pystate.h" // _PyThreadState_GET()
33-
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
33+
#include "pycore_pystate.h" // _PyThreadState_GET()
34+
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
3435
#include "pydtrace.h"
3536

3637
typedef struct _gc_runtime_state GCState;

Objects/setobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
*/
3333

3434
#include "Python.h"
35+
#include "pycore_dict.h" // _PyDict_Contains_KnownHash()
3536
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
3637
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
3738
#include "pycore_setobject.h" // _PySet_NextEntry() definition

Objects/structseq.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
*/
99

1010
#include "Python.h"
11-
#include "pycore_tuple.h" // _PyTuple_FromArray()
11+
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
12+
#include "pycore_initconfig.h" // _PyStatus_OK()
1213
#include "pycore_object.h" // _PyObject_GC_TRACK()
13-
#include "structmember.h" // PyMemberDef
1414
#include "pycore_structseq.h" // PyStructSequence_InitType()
15-
#include "pycore_initconfig.h" // _PyStatus_OK()
15+
#include "pycore_tuple.h" // _PyTuple_FromArray()
16+
#include "structmember.h" // PyMemberDef
1617

1718
static const char visible_length_key[] = "n_sequence_fields";
1819
static const char real_length_key[] = "n_fields";

Python/_warnings.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_dict.h" // _PyDict_GetItemWithError()
23
#include "pycore_frame.h"
34
#include "pycore_initconfig.h"
45
#include "pycore_interp.h" // PyInterpreterState.warnings

Python/bltinmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "pycore_call.h" // _PyObject_CallNoArgs()
77
#include "pycore_ceval.h" // _PyEval_Vector()
88
#include "pycore_compile.h" // _PyAST_Compile()
9+
#include "pycore_dict.h" // _PyDict_GetItemWithError()
910
#include "pycore_long.h" // _PyLong_CompactValue
1011
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
1112
#include "pycore_object.h" // _Py_AddToAllObjects()

Python/codecs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Copyright (c) Corporation for National Research Initiatives.
1010

1111
#include "Python.h"
1212
#include "pycore_call.h" // _PyObject_CallNoArgs()
13+
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
1314
#include "pycore_interp.h" // PyInterpreterState.codec_search_path
1415
#include "pycore_pyerrors.h" // _PyErr_FormatNote()
1516
#include "pycore_pystate.h" // _PyInterpreterState_GET()

Python/getargs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
/* New getargs implementation */
33

44
#include "Python.h"
5-
#include "pycore_tuple.h" // _PyTuple_ITEMS()
5+
#include "pycore_dict.h" // _PyDict_HasOnlyStringKeys()
66
#include "pycore_pylifecycle.h" // _PyArg_Fini
7+
#include "pycore_tuple.h" // _PyTuple_ITEMS()
78

89
#include <ctype.h>
910
#include <float.h>

Python/import.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "Python.h"
44

5+
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
56
#include "pycore_import.h" // _PyImport_BootstrapImp()
67
#include "pycore_initconfig.h" // _PyStatus_OK()
78
#include "pycore_interp.h" // struct _import_runtime_state
@@ -13,6 +14,7 @@
1314
#include "pycore_pystate.h" // _PyInterpreterState_GET()
1415
#include "pycore_sysmodule.h" // _PySys_Audit()
1516
#include "pycore_weakref.h" // _PyWeakref_GET_REF()
17+
1618
#include "marshal.h" // PyMarshal_ReadObjectFromString()
1719
#include "importdl.h" // _PyImport_DynLoadFiletab
1820
#include "pydtrace.h" // PyDTrace_IMPORT_FIND_LOAD_START_ENABLED()

Python/initconfig.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Python.h"
2+
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
23
#include "pycore_fileutils.h" // _Py_HasFileSystemDefaultEncodeErrors
34
#include "pycore_getopt.h" // _PyOS_GetOpt()
45
#include "pycore_initconfig.h" // _PyStatus_OK()

Python/pythonrun.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "pycore_ast.h" // PyAST_mod2obj
1616
#include "pycore_ceval.h" // _Py_EnterRecursiveCall
1717
#include "pycore_compile.h" // _PyAST_Compile()
18+
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
1819
#include "pycore_interp.h" // PyInterpreterState.importlib
1920
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
2021
#include "pycore_parser.h" // _PyParser_ASTFromString()

Python/sysmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Data members:
1717
#include "Python.h"
1818
#include "pycore_call.h" // _PyObject_CallNoArgs()
1919
#include "pycore_ceval.h" // _PyEval_SetAsyncGenFinalizer()
20+
#include "pycore_dict.h" // _PyDict_GetItemWithError()
2021
#include "pycore_frame.h" // _PyInterpreterFrame
2122
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
2223
#include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD

0 commit comments

Comments
 (0)
0