Skip to content

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

Merged
youknowone merged 5 commits into
RustPython:mainfrom
theshubhamp:pybool-asnumber-unwrap
Dec 30, 2024
Merged

youknowone merged 5 commits into
RustPython:mainfrom
theshubhamp:pybool-asnumber-unwrap

Conversation

@theshubhamp

Copy link
Copy Markdown
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

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

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.

Comment thread vm/src/vm/vm_ops.rs Outdated
///
/// 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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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.

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
Contributor Author

Thanks for the review! Made both changes, PTAL

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

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
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