Skip to content

Check operand types in bool or, and, xor to be PyInt #5461

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

Merged
merged 5 commits into from
Dec 30, 2024

Conversation

theshubhamp
Copy link
Contributor

PyNumber methods are expected to type check both arguments.

Dispatch is not done by inverting parameter order for r (example ror) when calls are handled via PyNumberMethods

Other changes:

  • Add tests to assert a TypeError is raised when either of the parameter types boolean & other is not convertible to int (str or float for example)
  • Sync binary operator order comment with actual implementation

Fixes: #5460

Flipping of parameter order to satisfy __r<op>__ dunder is done via macros
PyNumber methods are expected to type check both arguments.

Dispatch is not done by inverting parameter order for __r<op>__ (example __ror__) when calls are handled via PyNumberMethods
Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thank you.
Code changes looks perfect. Please check the comment part.

@@ -151,7 +151,7 @@ impl VirtualMachine {
/// Calling scheme used for binary operations:
///
/// Order operations are tried until either a valid result or error:
/// b.rop(b,a)[*], a.op(a,b), b.rop(b,a)
/// b.rop(a,b)[*], a.op(a,b), b.rop(a,b)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rop is a reversed version of op. So (b,a) is intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, thank you! we'd better to follow it later.

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, please also remove get_py_int from bool.rs which is not used anymore

@theshubhamp
Copy link
Contributor Author

Thanks for the review! Made both changes, PTAL

Copy link
Member

@youknowone youknowone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@youknowone youknowone merged commit 38593fb into RustPython:main Dec 30, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Boolean methods cause interpreter panic when presented with the wrong type
2 participants