Skip to content

Commit

Permalink
gh-106320: Remove private _PyDict C API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vstinner committed Jul 23, 2023
1 parent 956b3de commit 0bc07c2
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 56 deletions.
33 changes: 7 additions & 26 deletions Include/cpython/dictobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,17 @@ typedef struct {
PyDictValues *ma_values;
} PyDictObject;

PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
Py_hash_t hash);
PyAPI_FUNC(PyObject *) _PyDict_GetItemWithError(PyObject *dp, PyObject *key);
PyAPI_FUNC(PyObject*) _PyDict_GetItemStringWithError(PyObject *, const char *);

// _Py_Identifier variants
PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
_Py_Identifier *key);
PyAPI_FUNC(PyObject *) _PyDict_GetItemStringWithError(PyObject *, const char *);
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, _Py_Identifier *);
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, _Py_Identifier *key, PyObject *item);
PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, _Py_Identifier *key);

PyAPI_FUNC(PyObject *) PyDict_SetDefault(
PyObject *mp, PyObject *key, PyObject *defaultobj);
PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
PyObject *item, Py_hash_t hash);
PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
Py_hash_t hash);
PyAPI_FUNC(int) _PyDict_DelItemIf(PyObject *mp, PyObject *key,
int (*predicate)(PyObject *value));
PyAPI_FUNC(int) _PyDict_Next(
PyObject *mp, Py_ssize_t *pos, PyObject **key, PyObject **value, Py_hash_t *hash);

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

PyAPI_FUNC(int) _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
PyAPI_FUNC(int) _PyDict_ContainsId(PyObject *, _Py_Identifier *);
PyAPI_FUNC(PyObject *) _PyDict_NewPresized(Py_ssize_t minused);
PyAPI_FUNC(void) _PyDict_MaybeUntrack(PyObject *mp);
PyAPI_FUNC(int) _PyDict_HasOnlyStringKeys(PyObject *mp);
PyAPI_FUNC(Py_ssize_t) _PyDict_SizeOf(PyDictObject *);
PyAPI_FUNC(PyObject *) _PyDict_Pop(PyObject *, PyObject *, PyObject *);
#define _PyDict_HasSplitTable(d) ((d)->ma_values != NULL)

/* Like PyDict_Merge, but override can be 0, 1 or 2. If override is 0,
the first occurrence of a key wins, if override is 1, the last occurrence
of a key wins, if override is 2, a KeyError with conflicting key as
argument is raised.
*/
PyAPI_FUNC(int) _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, _Py_Identifier *key, PyObject *item);

PyAPI_FUNC(int) _PyDict_DelItemId(PyObject *mp, _Py_Identifier *key);
PyAPI_FUNC(void) _PyDict_DebugMallocStats(FILE *out);

/* _PyDictView */

typedef struct {
Expand Down
33 changes: 33 additions & 0 deletions Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,39 @@ extern "C" {
#include "pycore_dict_state.h"
#include "pycore_runtime.h" // _PyRuntime

extern PyObject* _PyDict_NewPresized(Py_ssize_t minused);

// Unsafe flavor of PyDict_GetItemWithError(): no error checking
extern PyObject* _PyDict_GetItemWithError(PyObject *dp, PyObject *key);

// Similar to PyDict_GetItem(), PyDict_SetItem(), PyDict_DelItem(),
// and PyDict_Contains(), but with the key hash.
//
// Export _PyDict_GetItem_KnownHash(), _PyDict_SetItem_KnownHash(),
// and _PyDict_DelItem_KnownHash for shared _asyncio extension.
PyAPI_FUNC(PyObject*) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
Py_hash_t hash);
PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
PyObject *item, Py_hash_t hash);
PyAPI_FUNC(int) _PyDict_DelItem_KnownHash(PyObject *mp, PyObject *key,
Py_hash_t hash);
extern int _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);

extern int _PyDict_DelItemIf(PyObject *mp, PyObject *key,
int (*predicate)(PyObject *value));

extern int _PyDict_HasOnlyStringKeys(PyObject *mp);

extern void _PyDict_MaybeUntrack(PyObject *mp);

/* Like PyDict_Merge, but override can be 0, 1 or 2. If override is 0,
the first occurrence of a key wins, if override is 1, the last occurrence
of a key wins, if override is 2, a KeyError with conflicting key as
argument is raised.
*/
extern int _PyDict_MergeEx(PyObject *mp, PyObject *other, int override);

extern void _PyDict_DebugMallocStats(FILE *out);

/* runtime lifecycle */

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1582,8 +1582,8 @@ class CAPITest(unittest.TestCase):
# Test _PyDict_GetItem_KnownHash()
@support.cpython_only
def test_getitem_knownhash(self):
_testcapi = import_helper.import_module('_testcapi')
dict_getitem_knownhash = _testcapi.dict_getitem_knownhash
_testinternalcapi = import_helper.import_module('_testinternalcapi')
dict_getitem_knownhash = _testinternalcapi.dict_getitem_knownhash

d = {'x': 1, 'y': 2, 'z': 3}
self.assertEqual(dict_getitem_knownhash(d, 'x', hash('x')), 1)
Expand Down
1 change: 1 addition & 0 deletions Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#endif

#include "Python.h"
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_pyerrors.h" // _PyErr_ClearExcState()
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
Expand Down
2 changes: 2 additions & 0 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "pycore_typeobject.h" // _PyType_GetModuleState()

#include "structmember.h" // PyMemberDef
#include <stddef.h>

Expand Down
1 change: 1 addition & 0 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static const char copyright[] =
" SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB ";

#include "Python.h"
#include "pycore_dict.h" // _PyDict_SetItem_KnownHash()
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // _PyModule_GetState()
#include "structmember.h" // PyMemberDef
Expand Down
21 changes: 0 additions & 21 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,26 +260,6 @@ test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored))
Py_RETURN_NONE;
}

static PyObject*
dict_getitem_knownhash(PyObject *self, PyObject *args)
{
PyObject *mp, *key, *result;
Py_ssize_t hash;

if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
&mp, &key, &hash)) {
return NULL;
}

result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
if (result == NULL && !PyErr_Occurred()) {
_PyErr_SetKeyError(key);
return NULL;
}

return Py_XNewRef(result);
}

/* Issue #4701: Check that PyObject_Hash implicitly calls
* PyType_Ready if it hasn't already been called
*/
Expand Down Expand Up @@ -3660,7 +3640,6 @@ static PyMethodDef TestMethods[] = {
{"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS},
{"test_list_api", test_list_api, METH_NOARGS},
{"test_dict_iteration", test_dict_iteration, METH_NOARGS},
{"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
{"test_lazy_hash_inheritance", test_lazy_hash_inheritance,METH_NOARGS},
{"test_xincref_doesnt_leak",test_xincref_doesnt_leak, METH_NOARGS},
{"test_incref_doesnt_leak", test_incref_doesnt_leak, METH_NOARGS},
Expand Down
23 changes: 23 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "pycore_bytesobject.h" // _PyBytes_Find()
#include "pycore_ceval.h" // _PyEval_AddPendingCall
#include "pycore_compile.h" // _PyCompile_CodeGen, _PyCompile_OptimizeCfg, _PyCompile_Assemble, _PyCompile_CleanDoc
#include "pycore_dict.h" // _PyDict_GetItem_KnownHash()
#include "pycore_fileutils.h" // _Py_normpath
#include "pycore_frame.h" // _PyInterpreterFrame
#include "pycore_gc.h" // PyGC_Head
Expand Down Expand Up @@ -1446,6 +1447,27 @@ test_atexit(PyObject *self, PyObject *Py_UNUSED(args))
}


static PyObject*
dict_getitem_knownhash(PyObject *self, PyObject *args)
{
PyObject *mp, *key, *result;
Py_ssize_t hash;

if (!PyArg_ParseTuple(args, "OOn:dict_getitem_knownhash",
&mp, &key, &hash)) {
return NULL;
}

result = _PyDict_GetItem_KnownHash(mp, key, (Py_hash_t)hash);
if (result == NULL && !PyErr_Occurred()) {
_PyErr_SetKeyError(key);
return NULL;
}

return Py_XNewRef(result);
}


static PyMethodDef module_functions[] = {
{"get_configs", get_configs, METH_NOARGS},
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
Expand Down Expand Up @@ -1502,6 +1524,7 @@ static PyMethodDef module_functions[] = {
{"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL},
{"_PyUnicode_TransformDecimalAndSpaceToASCII", unicode_transformdecimalandspacetoascii, METH_O},
{"test_atexit", test_atexit, METH_NOARGS},
{"dict_getitem_knownhash", dict_getitem_knownhash, METH_VARARGS},
{NULL, NULL} /* sentinel */
};

Expand Down
1 change: 1 addition & 0 deletions Modules/_weakref.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_dict.h" // _PyDict_DelItemIf()
#include "pycore_object.h" // _PyObject_GET_WEAKREFS_LISTPTR
#include "pycore_weakref.h" // _PyWeakref_IS_DEAD()

Expand Down
7 changes: 4 additions & 3 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@

#include "Python.h"
#include "pycore_context.h"
#include "pycore_dict.h" // _PyDict_MaybeUntrack()
#include "pycore_initconfig.h"
#include "pycore_interp.h" // PyInterpreterState.gc
#include "pycore_interp.h" // PyInterpreterState.gc
#include "pycore_object.h"
#include "pycore_pyerrors.h"
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_weakref.h" // _PyWeakref_ClearRef()
#include "pydtrace.h"

typedef struct _gc_runtime_state GCState;
Expand Down
1 change: 1 addition & 0 deletions Objects/setobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*/

#include "Python.h"
#include "pycore_dict.h" // _PyDict_Contains_KnownHash()
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
#include "pycore_setobject.h" // _PySet_NextEntry() definition
Expand Down
7 changes: 4 additions & 3 deletions Objects/structseq.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
*/

#include "Python.h"
#include "pycore_tuple.h" // _PyTuple_FromArray()
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_object.h" // _PyObject_GC_TRACK()
#include "structmember.h" // PyMemberDef
#include "pycore_structseq.h" // PyStructSequence_InitType()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_tuple.h" // _PyTuple_FromArray()
#include "structmember.h" // PyMemberDef

static const char visible_length_key[] = "n_sequence_fields";
static const char real_length_key[] = "n_fields";
Expand Down
1 change: 1 addition & 0 deletions Python/_warnings.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_dict.h" // _PyDict_GetItemWithError()
#include "pycore_frame.h"
#include "pycore_initconfig.h"
#include "pycore_interp.h" // PyInterpreterState.warnings
Expand Down
1 change: 1 addition & 0 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_Vector()
#include "pycore_compile.h" // _PyAST_Compile()
#include "pycore_dict.h" // _PyDict_GetItemWithError()
#include "pycore_long.h" // _PyLong_CompactValue
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_object.h" // _Py_AddToAllObjects()
Expand Down
1 change: 1 addition & 0 deletions Python/codecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Copyright (c) Corporation for National Research Initiatives.

#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_interp.h" // PyInterpreterState.codec_search_path
#include "pycore_pyerrors.h" // _PyErr_FormatNote()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
Expand Down
3 changes: 2 additions & 1 deletion Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
/* New getargs implementation */

#include "Python.h"
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_dict.h" // _PyDict_HasOnlyStringKeys()
#include "pycore_pylifecycle.h" // _PyArg_Fini
#include "pycore_tuple.h" // _PyTuple_ITEMS()

#include <ctype.h>
#include <float.h>
Expand Down
2 changes: 2 additions & 0 deletions Python/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Python.h"

#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_import.h" // _PyImport_BootstrapImp()
#include "pycore_initconfig.h" // _PyStatus_OK()
#include "pycore_interp.h" // struct _import_runtime_state
Expand All @@ -13,6 +14,7 @@
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_sysmodule.h" // _PySys_Audit()
#include "pycore_weakref.h" // _PyWeakref_GET_REF()

#include "marshal.h" // PyMarshal_ReadObjectFromString()
#include "importdl.h" // _PyImport_DynLoadFiletab
#include "pydtrace.h" // PyDTrace_IMPORT_FIND_LOAD_START_ENABLED()
Expand Down
1 change: 1 addition & 0 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Python.h"
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_fileutils.h" // _Py_HasFileSystemDefaultEncodeErrors
#include "pycore_getopt.h" // _PyOS_GetOpt()
#include "pycore_initconfig.h" // _PyStatus_OK()
Expand Down
1 change: 1 addition & 0 deletions Python/pythonrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "pycore_ast.h" // PyAST_mod2obj
#include "pycore_ceval.h" // _Py_EnterRecursiveCall
#include "pycore_compile.h" // _PyAST_Compile()
#include "pycore_dict.h" // _PyDict_GetItemStringWithError()
#include "pycore_interp.h" // PyInterpreterState.importlib
#include "pycore_object.h" // _PyDebug_PrintTotalRefs()
#include "pycore_parser.h" // _PyParser_ASTFromString()
Expand Down
1 change: 1 addition & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Data members:
#include "Python.h"
#include "pycore_call.h" // _PyObject_CallNoArgs()
#include "pycore_ceval.h" // _PyEval_SetAsyncGenFinalizer()
#include "pycore_dict.h" // _PyDict_GetItemWithError()
#include "pycore_frame.h" // _PyInterpreterFrame
#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
#include "pycore_long.h" // _PY_LONG_MAX_STR_DIGITS_THRESHOLD
Expand Down

0 comments on commit 0bc07c2

Please sign in to comment.