Author douglas-raillard-arm
Recipients douglas-raillard-arm
Date 2021-02-02.14:46:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1612277204.9.0.79430264778.issue43102@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2021-02-02 14:46:44douglas-raillard-armsetrecipients: + douglas-raillard-arm
2021-02-02 14:46:44douglas-raillard-armsetmessageid: <1612277204.9.0.79430264778.issue43102@roundup.psfhosted.org>
2021-02-02 14:46:44douglas-raillard-armlinkissue43102 messages
2021-02-02 14:46:44douglas-raillard-armcreate