New submission from Douglas Raillard <[email protected]>:
When creating a namedtuple such as this one:
from collections import namedtuple
class C(namedtuple('C', ('hello', 'world'))):
pass
print(C.__new__.__globals__)
The globals' dict of __new__ contains a "__builtins__" key which is set to None
in collections/__init__.py:
namespace = {
'_tuple_new': tuple_new,
'__builtins__': None,
'__name__': f'namedtuple_{typename}',
}
When such globals are used with eval(), it will raise a TypeError such as:
>>> eval('X', {'__builtins__': None})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
TypeError: 'NoneType' object is not subscriptable
If an empty dict was used instead, we get the expected exception:
>>> eval('X', {'__builtins__': {}})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'X' is not defined
Given that both ways allow preventing references to any builtin, please
consider switching to an empty dict. Also, even though this is documented as
implementation detail, this would agree more with the current documentation
stating:
The value of __builtins__ is normally either this module or the value of
this module’s __dict__ attribute
https://docs.python.org/3/library/builtins.html
----------
components: Library (Lib)
messages: 386145
nosy: douglas-raillard-arm
priority: normal
severity: normal
status: open
title: namedtuple's __new__.__globals__['__builtins__'] is None
type: behavior
versions: Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue43102>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com