Skip to content

make magic method new to follow cpython way#3057

Merged
youknowone merged 3 commits into
RustPython:mainfrom
eldpswp99:make-magic-method-new-to-follow-cpython
Sep 24, 2021
Merged

make magic method new to follow cpython way#3057
youknowone merged 3 commits into
RustPython:mainfrom
eldpswp99:make-magic-method-new-to-follow-cpython

Conversation

@eldpswp99

@eldpswp99 eldpswp99 commented Sep 14, 2021

Copy link
Copy Markdown
Contributor

this issue was first mentioned in #2974
This makes tp_new to be called by object.__new__

for example, with tuple.__new__

before: <bound method __new__ of <class 'tuple'>>
after: <bound method object.__new__ of <class 'tuple'>>

well, i have problem with controlling the new with pysuper, i remain this as draft now
I would be thankful for any advice!

Comment thread vm/src/builtins/object.rs Outdated
impl PyBaseObject {
#[pyclassmethod(magic)]
fn new(cls: PyTypeRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult {
let (subtype, args): (PyTypeRef, FuncArgs) = args.bind(vm)?;

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.

SlotConstructor will be preferred for newly added __new__ with binding.

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.

I thought there may be collision with object.tp_new. is it okay to put both?

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! I am sorry. you are right. this is not tp_new but just __new__.

@eldpswp99 eldpswp99 force-pushed the make-magic-method-new-to-follow-cpython branch from 7202125 to b3e1c84 Compare September 14, 2021 16:06
@youknowone youknowone force-pushed the make-magic-method-new-to-follow-cpython branch 2 times, most recently from 824e259 to 76370b2 Compare September 14, 2021 17:34
@youknowone youknowone force-pushed the make-magic-method-new-to-follow-cpython branch 5 times, most recently from 0a70569 to d436231 Compare September 14, 2021 22:22
@youknowone youknowone force-pushed the make-magic-method-new-to-follow-cpython branch 3 times, most recently from 45a6976 to e82989d Compare September 14, 2021 23:35
Comment thread vm/src/pyobject.rs
exceptions,
int_cache_pool,
string_cache,
tp_new_wrapper,

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.

we need this field yet in this implementation

@youknowone youknowone force-pushed the make-magic-method-new-to-follow-cpython branch from e82989d to 00b33f8 Compare September 15, 2021 00:01
@youknowone youknowone marked this pull request as ready for review September 16, 2021 15:31
@youknowone

Copy link
Copy Markdown
Member

__new__ will not be looked up from tp_new anymore and the wrapper function __new__ will be added to classes only when tp_new slot is defined.

@youknowone

Copy link
Copy Markdown
Member

@coolreader18 @DimitrisJim could you review this PR?

Comment thread vm/src/builtins/pytype.rs
#[pyimpl(with(SlotGetattro, SlotSetattro, Callable), flags(BASETYPE))]
impl PyType {
// bound method for every type
pub(crate) fn __new__(zelf: PyRef<Self>, args: FuncArgs, vm: &VirtualMachine) -> PyResult {

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.

__new__ is sort of confusing to me, I'd prefer if this kept tp_new_wrapper so we can easily see which function this maps to in CPython.

@youknowone youknowone Sep 23, 2021

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.

This is a body of bound method __new__ just like mro. The only difference of them is that __new__ is a bound method for every type including type but mro is an unbound method for type and bound as boundmethod for other types.
I think we'd better to keep this name as same as other methods when it works like other methods.

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.

I couldn't clarify this concepts well at this time, but now things seem well aligned.
new_wrapper is now meaning python-side wrapper of __new__. And this is a wrapper for type slot new.

@DimitrisJim

Copy link
Copy Markdown
Member

this looks just fine to me but I've not really looked into it much so I'll leave the approval for Noa.

@coolreader18 coolreader18 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.

LGTM, I think 😅

This whole part of the interpreter is pretty complex and I don't really have an intuition for how it works, so given that this seems simpler and still works it's got my approval 👍

@youknowone youknowone merged commit 6b84c2b into RustPython:main Sep 24, 2021
@youknowone youknowone added the z-ca-2021 Tag to track contrubution-academy 2021 label Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

z-ca-2021 Tag to track contrubution-academy 2021

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants