]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-106320: Remove _PyDict_GetItemStringWithError() function (#108313)
authorVictor Stinner <vstinner@python.org>
Tue, 22 Aug 2023 18:17:25 +0000 (20:17 +0200)
committerGitHub <noreply@github.com>
Tue, 22 Aug 2023 18:17:25 +0000 (18:17 +0000)
Remove private _PyDict_GetItemStringWithError() function of the
public C API: the new PyDict_GetItemStringRef() can be used instead.

* Move private _PyDict_GetItemStringWithError() to the internal C API.
* _testcapi get_code_extra_index() uses PyDict_GetItemStringRef().
  Avoid using private functions in _testcapi which tests the public C
  API.

Include/cpython/dictobject.h
Include/internal/pycore_dict.h
Modules/_testcapi/code.c
Objects/structseq.c
Python/codecs.c
Python/import.c
Python/initconfig.c
Python/pythonrun.c

index 2a42794fdf0c85aa02f49c22974c28b8cd3b4e46..470f59436748fde39b9667092dff6c151c6368e6 100644 (file)
@@ -36,7 +36,6 @@ PyAPI_FUNC(PyObject *) _PyDict_GetItem_KnownHash(PyObject *mp, PyObject *key,
                                        Py_hash_t hash);
 PyAPI_FUNC(PyObject *) _PyDict_GetItemIdWithError(PyObject *dp,
                                                   _Py_Identifier *key);
-PyAPI_FUNC(PyObject *) _PyDict_GetItemStringWithError(PyObject *, const char *);
 PyAPI_FUNC(PyObject *) PyDict_SetDefault(
     PyObject *mp, PyObject *key, PyObject *defaultobj);
 PyAPI_FUNC(int) _PyDict_SetItem_KnownHash(PyObject *mp, PyObject *key,
index df7bc7e58f6c9798221e3bf3bea76c43b7c5174b..8e9c27e3b294b8f28c6bed9b923dc3fcd335aa31 100644 (file)
@@ -13,6 +13,7 @@ extern "C" {
 
 // Unsafe flavor of PyDict_GetItemWithError(): no error checking
 extern PyObject* _PyDict_GetItemWithError(PyObject *dp, PyObject *key);
+extern PyObject* _PyDict_GetItemStringWithError(PyObject *, const char *);
 
 extern int _PyDict_Contains_KnownHash(PyObject *, PyObject *, Py_hash_t);
 
index cadaf5eb94692ea4f51a7c8d585931d2ef3385e2..691dd5fe043811d102ab77bfd090271253d36340 100644 (file)
@@ -9,12 +9,12 @@ get_code_extra_index(PyInterpreterState* interp) {
     PyObject *interp_dict = PyInterpreterState_GetDict(interp); // borrowed
     assert(interp_dict);  // real users would handle missing dict... somehow
 
-    PyObject *index_obj = _PyDict_GetItemStringWithError(interp_dict, key); // borrowed
+    PyObject *index_obj;
+    if (PyDict_GetItemStringRef(interp_dict, key, &index_obj) < 0) {
+        goto finally;
+    }
     Py_ssize_t index = 0;
     if (!index_obj) {
-        if (PyErr_Occurred()) {
-            goto finally;
-        }
         index = PyUnstable_Eval_RequestCodeExtraIndex(NULL);
         if (index < 0 || PyErr_Occurred()) {
             goto finally;
@@ -31,6 +31,7 @@ get_code_extra_index(PyInterpreterState* interp) {
     }
     else {
         index = PyLong_AsSsize_t(index_obj);
+        Py_DECREF(index_obj);
         if (index == -1 && PyErr_Occurred()) {
             goto finally;
         }
index 700f67c09c9e5758ffb316fbfaa625bd0a1d0a2e..95c4c15710d16998541fcb6a9ea5e02b2e32e219 100644 (file)
@@ -8,6 +8,7 @@
 */
 
 #include "Python.h"
+#include "pycore_dict.h"          // _PyDict_GetItemStringWithError()
 #include "pycore_tuple.h"         // _PyTuple_FromArray()
 #include "pycore_object.h"        // _PyObject_GC_TRACK()
 
index 4e47ff93a3691b6b642c748e841fb2f7e4391e1a..3c418512e3aa0b329137d7190d03f8a85fb7b724 100644 (file)
@@ -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()
index 56b2dc1a4ada2cd47536378ee96e9972fa402612..4abb2f6a06b48c494cd05020f57c65655d568969 100644 (file)
@@ -1,7 +1,7 @@
 /* Module definition and import implementation */
 
 #include "Python.h"
-
+#include "pycore_dict.h"          // _PyDict_GetItemStringWithError()
 #include "pycore_hashtable.h"     // _Py_hashtable_new_full()
 #include "pycore_import.h"        // _PyImport_BootstrapImp()
 #include "pycore_initconfig.h"    // _PyStatus_OK()
@@ -15,6 +15,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()
index 787e583262406c81ce7f1e93c02ce82f85d21813..c017abeb90563e5d4ddbc745bb23ed49111af798 100644 (file)
@@ -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()
index b2e04cfa317c00131337bdee9d88532cb3b9645e..a3de7792cf5bc5909f8a7a159e746220c10733c5 100644 (file)
@@ -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()