[New-bugs-announce] [issue43102] namedtuple's __new__.__globals__['__builtins__'] is None

Douglas Raillard report at bugs.python.org
Tue Feb 2 09:46:44 EST 2021

New submission from Douglas Raillard <douglas.raillard at arm.com>:

When creating a namedtuple such as this one:

    from collections import namedtuple

    class C(namedtuple('C', ('hello', 'world'))):


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


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 <report at bugs.python.org>

More information about the New-bugs-announce mailing list