Repository navigation
Custom str subclass as keyword argument leads to System Error #94938
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 17, 2022 I'm not sure this should be supported. The discuss.python.org discussion was focused on strings that aren't valid identifiers, not on subclasses of
str.Restricting kwargs to only str may allow some optimizations and saves us from possible subtle bugs (what if a str subclass overrides
__hash__). In addition, there's no known use case for using str subclasses.I suggest we:
- Document that kwargs must be exactly of type
str - Fix the code so that attempting to use a str subclass raises TypeError instead of SystemError
- Document that kwargs must be exactly of type
The user should not be able to provoke a SystemError from pure Python code. This exception is reserved for misuse of the C API. There is a bug somewhere in
_PyArg_UnpackKeywords(). In other examples it can lead to crash, memory corruption, or just silent produce incorrect result.Reacted by Václav Brožík and Neil Girdhar- added3.11only security fixesonly security fixes3.10 (EOL)end of lifeend of life3.12only security fixesonly security fixes
on Jul 18, 2022 The discuss.python.org discussion was focused on strings that aren't valid identifiers, not on subclasses of
str.It starts with the question why integer keys are not allowed. When I looked where this is enforced, I was interested to find that it accepted sub-classes of
str. So I tried it, idly, and idle restarted (in 3.8).Restricting kwargs to only str may allow some optimizations and saves us from possible subtle bugs (what if a str subclass overrides
__hash__).I agree, and favour that, but I cannot say for certain no-one is relying on it. Also @serhiy-storchaka is right that something is wrong elsewhere, and may go wrong in other circumstances, which this daft trick just serves to discover.
Thank you for your report @jeff5. It was an interesting bug.
Reacted by C.A.M. Gerlach, Neil Girdhar and Wim Jeantine-Glenn- added 5 commits that reference this issue
on Jul 28, 2022 See also #96397
When arguments are given by keyword using a dictionary, CPython requires that the key be of type
str, or a sub-class. If a customstrsubclass defines a badly-behaved__eq__, and is used that way, aSystemError(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
Output
Environment