Commit 6c62fbe9 authored by Victor Stinner's avatar Victor Stinner

Issue #14383: Add _PyDict_GetItemId() and _PyDict_SetItemId() functions

These functions simplify the usage of static constant Unicode strings.
Generalize the usage of _Py_Identifier in ceval.c and typeobject.c.
parent 33a6e05f
...@@ -155,7 +155,9 @@ PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d, ...@@ -155,7 +155,9 @@ PyAPI_FUNC(int) PyDict_MergeFromSeq2(PyObject *d,
int override); int override);
PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key); PyAPI_FUNC(PyObject *) PyDict_GetItemString(PyObject *dp, const char *key);
PyAPI_FUNC(PyObject *) _PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key);
PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item); PyAPI_FUNC(int) PyDict_SetItemString(PyObject *dp, const char *key, PyObject *item);
PyAPI_FUNC(int) _PyDict_SetItemId(PyObject *dp, struct _Py_Identifier *key, PyObject *item);
PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key); PyAPI_FUNC(int) PyDict_DelItemString(PyObject *dp, const char *key);
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -2198,6 +2198,16 @@ PyTypeObject PyDict_Type = { ...@@ -2198,6 +2198,16 @@ PyTypeObject PyDict_Type = {
PyObject_GC_Del, /* tp_free */ PyObject_GC_Del, /* tp_free */
}; };
PyObject *
_PyDict_GetItemId(PyObject *dp, struct _Py_Identifier *key)
{
PyObject *kv;
kv = _PyUnicode_FromId(key); /* borrowed */
if (kv == NULL)
return NULL;
return PyDict_GetItem(dp, kv);
}
/* For backward compatibility with old dictionary interface */ /* For backward compatibility with old dictionary interface */
PyObject * PyObject *
...@@ -2212,6 +2222,16 @@ PyDict_GetItemString(PyObject *v, const char *key) ...@@ -2212,6 +2222,16 @@ PyDict_GetItemString(PyObject *v, const char *key)
return rv; return rv;
} }
int
_PyDict_SetItemId(PyObject *v, struct _Py_Identifier *key, PyObject *item)
{
PyObject *kv;
kv = _PyUnicode_FromId(key); /* borrowed */
if (kv == NULL)
return -1;
return PyDict_SetItem(v, kv, item);
}
int int
PyDict_SetItemString(PyObject *v, const char *key, PyObject *item) PyDict_SetItemString(PyObject *v, const char *key, PyObject *item)
{ {
......
This diff is collapsed.
...@@ -822,6 +822,8 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -822,6 +822,8 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
PyObject *names; PyObject *names;
PyObject *consts; PyObject *consts;
_Py_IDENTIFIER(__ltrace__);
/* Computed GOTOs, or /* Computed GOTOs, or
the-optimization-commonly-but-improperly-known-as-"threaded code" the-optimization-commonly-but-improperly-known-as-"threaded code"
using gcc's labels-as-values extension using gcc's labels-as-values extension
...@@ -1198,7 +1200,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -1198,7 +1200,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
} }
#ifdef LLTRACE #ifdef LLTRACE
lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL; lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
#endif #endif
why = WHY_NOT; why = WHY_NOT;
...@@ -1926,8 +1928,9 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -1926,8 +1928,9 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
break; break;
TARGET(LOAD_BUILD_CLASS) TARGET(LOAD_BUILD_CLASS)
x = PyDict_GetItemString(f->f_builtins, {
"__build_class__"); _Py_IDENTIFIER(__build_class__);
x = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
if (x == NULL) { if (x == NULL) {
PyErr_SetString(PyExc_ImportError, PyErr_SetString(PyExc_ImportError,
"__build_class__ not found"); "__build_class__ not found");
...@@ -1936,6 +1939,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -1936,6 +1939,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Py_INCREF(x); Py_INCREF(x);
PUSH(x); PUSH(x);
break; break;
}
TARGET(STORE_NAME) TARGET(STORE_NAME)
w = GETITEM(names, oparg); w = GETITEM(names, oparg);
...@@ -2283,8 +2287,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -2283,8 +2287,10 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
DISPATCH(); DISPATCH();
TARGET(IMPORT_NAME) TARGET(IMPORT_NAME)
{
_Py_IDENTIFIER(__import__);
w = GETITEM(names, oparg); w = GETITEM(names, oparg);
x = PyDict_GetItemString(f->f_builtins, "__import__"); x = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
if (x == NULL) { if (x == NULL) {
PyErr_SetString(PyExc_ImportError, PyErr_SetString(PyExc_ImportError,
"__import__ not found"); "__import__ not found");
...@@ -2325,6 +2331,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag) ...@@ -2325,6 +2331,7 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
SET_TOP(x); SET_TOP(x);
if (x != NULL) DISPATCH(); if (x != NULL) DISPATCH();
break; break;
}
TARGET(IMPORT_STAR) TARGET(IMPORT_STAR)
v = POP(); v = POP();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment