Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement Number Protocol forPyBool#4639

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

xiaozhiyan
Copy link
Contributor

@xiaozhiyanxiaozhiyan commentedMar 5, 2023
edited
Loading

Closes#4638

In CPython,bool is a subclass ofint, and itsas_number method overridesand,xor andor.

In RustPython,asPyBool is not a subclass ofPyInt, theas_number "overriding" is done by including all other methods ofPyInt.

@qingshi163
Copy link
Contributor

In RustPython PyBool is a subclass of PyInt

>>>>> isinstance(True, int)True
xiaozhiyan reacted with eyes emoji

Comment on lines +186 to +198
add: int_method!(add),
subtract: int_method!(subtract),
multiply: int_method!(multiply),
remainder: int_method!(remainder),
divmod: int_method!(divmod),
power: int_method!(power),
negative: int_method!(negative),
positive: int_method!(positive),
absolute: int_method!(absolute),
boolean: int_method!(boolean),
invert: int_method!(invert),
lshift: int_method!(lshift),
rshift: int_method!(rshift),
Copy link
Member

Choose a reason for hiding this comment

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

Not sure yet, but I think those fields in bool have to beNone.
In my opinion, If that's not working with None, any inherited object of numbers can make similar problem.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

It seemsint operations are needed forbool in CPython.

>>> (True + True) ** True2

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

SincePyBool is a subclass ofPyInt and inherits its methods, so(True + True) ** True should somehow work without the above overriding inas_number I think.

Copy link
Member

Choose a reason for hiding this comment

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

I am sorry to make confusion. I agree bool need int operations.
But I'd like to say manually fillingPyNumberMethods is not the way to do.
We can do that job only for builtin types written in Rust.
Making themNone and finding a way to make it work will be helpful to solve the problem in general.

xiaozhiyan reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

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

Did not test but I think PyBool is working with int number methods via mro search in PyNumber::find_methods().

But Maybe we can override it in PyBool to have some optimize

xiaozhiyan reacted with eyes emoji
Copy link
Member

Choose a reason for hiding this comment

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

No, it wasn't. Because that is a static variable. We have no way to edit it later.

xiaozhiyan reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

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

@qingshi163 do you have any suggestion?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can inherit slots when initializing the subclass

Copy link
Contributor

Choose a reason for hiding this comment

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

https://github.com/python/cpython/blob/71db5dbcd714b2e1297c43538188dd69715feb9a/Objects/typeobject.c#L8849-L8902

slots will be inherit if subclass does not override it.

when there is a change of slots on the type, cpython recusively update slots for all the subclasses.

Copy link
Member

Choose a reason for hiding this comment

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

slots will be inherit if subclass does not override it.

It is true in cpython but not here yet. That's the reason we always callmro_find_map() to find slots.

Comment on lines +199 to +207
and: atomic_func!(|number, other, vm| {
PyBool::and(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
xor: atomic_func!(|number, other, vm| {
PyBool::xor(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
or: atomic_func!(|number, other, vm| {
PyBool::or(number.obj.to_owned(), other.to_owned(), vm).to_pyresult(vm)
}),
Copy link
Member

Choose a reason for hiding this comment

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

Here is more room to optimize because and, xor, or actually doesn't require to increase reference count.
But it looks working fine.

To fully optimize it,PyBool::and can be changed like this and AsNumber can callinner_and

#[pymethod(name ="__rand__")]#[pymethod(magic)]fnand(lhs:PyObjectRef,rhs:PyObjectRef,vm:&VirtualMachine) ->PyObjectRef{Self::inner_and(&lhs,&rhs, vm)}fninner_and(lhs:&PyObject,rhs:&PyObject,vm:&VirtualMachine) ->PyObjectRef{if lhs.fast_isinstance(vm.ctx.types.bool_type)            && rhs.fast_isinstance(vm.ctx.types.bool_type){let lhs =get_value(&lhs);let rhs =get_value(&rhs);(lhs && rhs).to_pyobject(vm)}else{get_py_int(&lhs).and(rhs, vm).to_pyobject(vm)}}

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This approach seems not working directly, sinceget_py_int(&lhs).and(rhs, vm) requiresrhs to be anPyObjectRef as defined inint.rs.

Copy link
Member

Choose a reason for hiding this comment

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

mayberhs.to_owned() fornow. we can optimize int.and also in same way later

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I'm not sure but wouldrhs.to_owned() be an extra burden? If it's ok (for now), I will adopt the above practice.

Copy link
Member

Choose a reason for hiding this comment

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

Yes,rhs.to_owned() is extra cost, and still same as previous one. AddingTODO: orFIXME: can be helpful.
It is on the way to go to the right design.

xiaozhiyan reacted with thumbs up emoji
@youknowone
Copy link
Member

The inheritance is set like this:

#[pyclass(name = "bool", module = false, base = "PyInt")]  // this `PyInt` partstruct PyBool;
xiaozhiyan reacted with eyes emoji

@xiaozhiyan
Copy link
ContributorAuthor

In RustPython PyBool is a subclass of PyInt

>>>>> isinstance(True, int)True

I'm not sure, but does that meanPyBool will inherit existing methods ofPyInt please?

@youknowone
Copy link
Member

Yes,int.to_bytes is a method ofint.

>>>>>int.to_bytes==bool.to_bytesTrue
xiaozhiyan reacted with thumbs up emoji

@youknowoneyouknowone added the z-ls-2023Tag to track Line OSS Sprint 2023 labelMar 7, 2023
@xiaozhiyanxiaozhiyan deleted the implement-number-protocol-for-pybool branchMarch 9, 2023 18:28
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@youknowoneyouknowoneyouknowone left review comments

@qingshi163qingshi163Awaiting requested review from qingshi163

Assignees
No one assigned
Labels
z-ls-2023Tag to track Line OSS Sprint 2023
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Implement Number Protocol forPyBool
3 participants
@xiaozhiyan@qingshi163@youknowone

[8]ページ先頭

©2009-2025 Movatter.jp