-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Custom str subclass as keyword argument leads to System Error #94938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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 Restricting kwargs to only str may allow some optimizations and saves us from possible subtle bugs (what if a str subclass overrides I suggest we:
|
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 |
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
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. |
When keyword argument name is an instance of a str subclass with overloaded methods __eq__ and __hash__, the former code could not find the name of an extraneous keyword argument to report an error, and _PyArg_UnpackKeywords() returned success without setting the corresponding cell in linearized arguments array. But since the number of expected initialized cells is determined as total number of passed arguments, this lead to reading NULL as keyword parameter value, that caused SysTemError or crash or other undesired behavior.
Thank you for your report @jeff5. It was an interesting bug. |
) When keyword argument name is an instance of a str subclass with overloaded methods __eq__ and __hash__, the former code could not find the name of an extraneous keyword argument to report an error, and _PyArg_UnpackKeywords() returned success without setting the corresponding cell in the linearized arguments array. But since the number of expected initialized cells is determined as the total number of passed arguments, this lead to reading NULL as a keyword parameter value, that caused SystemError or crash or other undesired behavior.
…uments (pythonGH-94999) When keyword argument name is an instance of a str subclass with overloaded methods __eq__ and __hash__, the former code could not find the name of an extraneous keyword argument to report an error, and _PyArg_UnpackKeywords() returned success without setting the corresponding cell in the linearized arguments array. But since the number of expected initialized cells is determined as the total number of passed arguments, this lead to reading NULL as a keyword parameter value, that caused SystemError or crash or other undesired behavior. (cherry picked from commit ebad53a) Co-authored-by: Serhiy Storchaka <[email protected]>
…uments (pythonGH-94999) When keyword argument name is an instance of a str subclass with overloaded methods __eq__ and __hash__, the former code could not find the name of an extraneous keyword argument to report an error, and _PyArg_UnpackKeywords() returned success without setting the corresponding cell in the linearized arguments array. But since the number of expected initialized cells is determined as the total number of passed arguments, this lead to reading NULL as a keyword parameter value, that caused SystemError or crash or other undesired behavior.. (cherry picked from commit ebad53a) Co-authored-by: Serhiy Storchaka <[email protected]>
…GH-94999) (GH-95353) When keyword argument name is an instance of a str subclass with overloaded methods __eq__ and __hash__, the former code could not find the name of an extraneous keyword argument to report an error, and _PyArg_UnpackKeywords() returned success without setting the corresponding cell in the linearized arguments array. But since the number of expected initialized cells is determined as the total number of passed arguments, this lead to reading NULL as a keyword parameter value, that caused SystemError or crash or other undesired behavior. (cherry picked from commit ebad53a) Co-authored-by: Serhiy Storchaka <[email protected]>
…GH-94999) (GH-95354) When keyword argument name is an instance of a str subclass with overloaded methods __eq__ and __hash__, the former code could not find the name of an extraneous keyword argument to report an error, and _PyArg_UnpackKeywords() returned success without setting the corresponding cell in the linearized arguments array. But since the number of expected initialized cells is determined as the total number of passed arguments, this lead to reading NULL as a keyword parameter value, that caused SystemError or crash or other undesired behavior.. (cherry picked from commit ebad53a) Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit 0956b6d) Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit 0956b6d) Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit 0956b6d) Co-authored-by: Serhiy Storchaka <[email protected]>
(cherry picked from commit 0956b6d) Co-authored-by: Serhiy Storchaka <[email protected]>
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 customstr
subclass 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
The text was updated successfully, but these errors were encountered: