Closed
Description
When arguments are given by keyword using a dictionary, CPython requires that the key be of type str
, or a sub-class. If a custom str
subclass defines a badly-behaved __eq__
, and is used that way, a SystemError
(null argument) can result, which is not expected in the interpreter.
This report stems from a discussion on discuss.python.org about the types of keyword that ought to be allowed -- it's not code I actually want to run.
Reproducer
# kwargsbomb.py Use of a str subclass as a keyword
class S(str):
def __eq__(self, other):
print(other)
return True
def __hash__(self):
return 42
u = "my.domain.name"
a = u.split(**{'sep':'.'}) # ok
print(a)
b = u.split(**{S('sep'):'.'}) # ok
print(b)
c = u.split(**{S('xxx'):'.'}) # fails
print(c)
Output
['my', 'domain', 'name']
['my', 'domain', 'name']
sep
Traceback (most recent call last):
File " ... \kwargsbomb.py", line 18, in <module>
c = u.split(**{S('xxx'):'.'}) # fails
^^^^^^^^^^^^^^^^^^^^^^^^^
SystemError: null argument to internal routine
Environment
- CPython versions: 3.10.2 3.11.0b4 (in Idle and at the prompt)
- Operating system and architecture: Windows 10 64-bit