|
3 | 3 | #include "Python.h" |
4 | 4 | #include "pycore_audit.h" // _PySys_Audit() |
5 | 5 | #include "pycore_ceval.h" |
| 6 | +#include "pycore_dict.h" // _PyDict_Contains_KnownHash() |
6 | 7 | #include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() |
7 | 8 | #include "pycore_hashtable.h" // _Py_hashtable_new_full() |
8 | 9 | #include "pycore_import.h" // _PyImport_BootstrapImp() |
@@ -3822,7 +3823,8 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import) |
3822 | 3823 | } |
3823 | 3824 | } |
3824 | 3825 |
|
3825 | | - int is_loading = PySet_Contains(importing, lazy_import); |
| 3826 | + assert(PyAnySet_CheckExact(importing)); |
| 3827 | + int is_loading = _PySet_Contains((PySetObject *)importing, lazy_import); |
3826 | 3828 | if (is_loading < 0) { |
3827 | 3829 | _PyImport_ReleaseLock(interp); |
3828 | 3830 | return NULL; |
@@ -4215,7 +4217,8 @@ static PyObject * |
4215 | 4217 | get_mod_dict(PyObject *module) |
4216 | 4218 | { |
4217 | 4219 | if (PyModule_Check(module)) { |
4218 | | - return Py_XNewRef(PyModule_GetDict(module)); |
| 4220 | + // Use internal function for direct md_dict access, avoiding function call overhead |
| 4221 | + return Py_NewRef(_PyModule_GetDict(module)); |
4219 | 4222 | } |
4220 | 4223 |
|
4221 | 4224 | return PyObject_GetAttr(module, &_Py_ID(__dict__)); |
@@ -4291,6 +4294,7 @@ register_lazy_on_parent(PyThreadState *tstate, PyObject *name, PyObject *builtin |
4291 | 4294 | goto done; |
4292 | 4295 | } |
4293 | 4296 | } |
| 4297 | + assert(PyAnySet_CheckExact(lazy_submodules)); |
4294 | 4298 | if (PySet_Add(lazy_submodules, child) < 0) { |
4295 | 4299 | Py_DECREF(lazy_submodules); |
4296 | 4300 | goto done; |
@@ -4387,6 +4391,7 @@ _PyImport_LazyImportModuleLevelObject(PyThreadState *tstate, |
4387 | 4391 | // Add the module name to sys.lazy_modules set (PEP 810) |
4388 | 4392 | PyObject *lazy_modules_set = interp->imports.lazy_modules_set; |
4389 | 4393 | if (lazy_modules_set != NULL) { |
| 4394 | + assert(PyAnySet_CheckExact(lazy_modules_set)); |
4390 | 4395 | if (PySet_Add(lazy_modules_set, abs_name) < 0) { |
4391 | 4396 | Py_DECREF(res); |
4392 | 4397 | Py_DECREF(abs_name); |
@@ -5418,11 +5423,13 @@ _imp__set_lazy_attributes_impl(PyObject *module, PyObject *child_module, |
5418 | 5423 | if (child_dict == NULL || !PyDict_CheckExact(child_dict)) { |
5419 | 5424 | goto done; |
5420 | 5425 | } |
| 5426 | + assert(PyAnySet_CheckExact(lazy_submodules)); |
5421 | 5427 | PyObject *attr_name; |
5422 | 5428 | Py_ssize_t pos = 0; |
5423 | 5429 | Py_hash_t hash; |
5424 | 5430 | while (_PySet_NextEntry(lazy_submodules, &pos, &attr_name, &hash)) { |
5425 | | - if (PyDict_Contains(child_dict, attr_name)) { |
| 5431 | + // Use _PyDict_Contains_KnownHash since we already have the hash from _PySet_NextEntry |
| 5432 | + if (_PyDict_Contains_KnownHash(child_dict, attr_name, hash)) { |
5426 | 5433 | continue; |
5427 | 5434 | } |
5428 | 5435 | PyObject *builtins = _PyEval_GetBuiltins(tstate); |
|
0 commit comments