New submission from Patrick Reader <[email protected]>:
When a frame's __builtins__ is a subclass of dict with an overridden
__getitem__ method, this overriden method is not used by the IMPORT_NAME
instruction to lookup __import__ in the dictionary; it uses the lookup function
of normal dictionaries (via _PyDict_GetItemIdWithError). This is contrary to
the behaviour of the similar LOAD_BUILD_CLASS, as well as the typical name
lookup semantics of LOAD_GLOBAL/LOAD_NAME, which all use PyDict_CheckExact for
a "fast path" before defaulting to PyObject_GetItem, which is not the behaviour
I expected.
Perhaps more seriously, if __builtins__ is not a dict at all, then it gets
erroneously passed to some internal dict functions resulting in a mysterious
SystemError ("Objects/dictobject.c:1440: bad argument to internal function")
which, to me, indicates fragile behaviour that isn't supposed to happen.
Could this be changed, so that __builtins__ is used dynamically? I understand
this is a highly specific use case and changing it would probably cause a bit
of a slow-down in module importing so is perhaps not worth doing, but I've
posted this issue to track it anyway.
I cannot find evidence that this behaviour has changed at all in recent history
and it seems to be the same on the main branch as in 3.9.6.
Per a brief discussion with Brett Cannon on python-dev
(https://mail.python.org/archives/list/[email protected]/thread/ZQMF6XC76J4APJPB3X6PGATG6CV5NN44/),
I have labelled this a feature request because it has never really been
expected behaviour.
A short demo of these things is attached.
Links to relevant CPython code in v3.9.6:
IMPORT_NAME: https://github.com/python/cpython/blob/v3.9.6/Python/ceval.c#L5179
BUILD_CLASS: https://github.com/python/cpython/blob/v3.9.6/Python/ceval.c#L2316
LOAD_NAME: https://github.com/python/cpython/blob/v3.9.6/Python/ceval.c#L2488
LOAD_GLOBAL: https://github.com/python/cpython/blob/v3.9.6/Python/ceval.c#L2546
----------
components: Interpreter Core
files: dict_lookup_demo.py
messages: 397531
nosy: brett.cannon, pxeger
priority: normal
severity: normal
status: open
title: importing does not dispatch to __builtins__.__getitem__ to lookup
__import__
type: enhancement
versions: Python 3.10, Python 3.11, Python 3.9
Added file: https://bugs.python.org/file50151/dict_lookup_demo.py
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue44643>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com