Skip to content
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

Can't call __new__ directly to create instances. #126

Closed
cthulahoops opened this issue Sep 2, 2018 · 6 comments
Closed

Can't call __new__ directly to create instances. #126

cthulahoops opened this issue Sep 2, 2018 · 6 comments

Comments

@cthulahoops
Copy link
Collaborator

This should work, but doesn't:

int.__new__(int, 5)

I think type.__new__ gets called instead of int.__new__, given this:

>>>>> object.__new__(object) 
<class 'type'>

I was pretty sure there was a problem with getattribute, but I've been waiting for a good example to fix.

@windelbouwman
Copy link
Contributor

This works now! I do not know what fixed it, but it works :)

@cthulahoops
Copy link
Collaborator Author

cthulahoops commented Nov 2, 2018

builtins.rs:

    // Special case: __new__ must be looked up on the metaclass, not the meta-metaclass as
    // per vm.call(metaclass, "__new__", ...)
    let new = metaclass.get_attr("__new__").unwrap();
    let wrapped = vm.call_get_descriptor(new, metaclass)?;

@cthulahoops
Copy link
Collaborator Author

cthulahoops commented Nov 2, 2018

Uh... no, I'm wrong. That's only during type creation, not object creation.

The correct implementation of type.__getattribute__ fixes this. (And we don't even need another special case.)

@Mec-iS
Copy link

Mec-iS commented Feb 14, 2019

In theory directly calling __new__ in Python should never be done to avoid weird side-effects. The constructor to override is always __init__.

@windelbouwman
Copy link
Contributor

@cthulahoops this issue can now be closed, right?

@cthulahoops
Copy link
Collaborator Author

Yes, I think this is all fixed now.

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

No branches or pull requests

3 participants