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
Need to implement proper __new__ handling for instance classes #606
Comments
Relevant clauses from https://docs.python.org/3.4/reference/datamodel.html :
https://docs.python.org/3.4/reference/datamodel.html#object.__new__ That doc doesn't saying anything about implicitly calling base types' |
Initial implementation pushed in 13684fd , likely more elaboration needed is we want to achieve real Python compliance. |
#622 split off from this. |
And what's left here is support proper We also don't want to 2 methods for native types - that will hurt performance. How it is now (make_new represents both new + init) is ok for 95% of cases, we just need to allow to call "new" vs "init" parts separately for remaining 5% cases. Proposal is to use the same tricks as with print method - pass additional enum value which will specify which ops to perform - new, init, or both. And for init case, the make_new should just expect that it will be given an object, not type, pointer. @dpgeorge : Any objections? |
Example of currently broken code due to lack of the above: 66ab571 |
Also, it turns out that argument handling to (object's) Canonical reference: http://hg.python.org/cpython/file/44ed0cd3dc6d/Objects/typeobject.c#l2818 Questions & commentary:
I have no idea how we'd handle this mind-boggling stuff, apparently, we just should accept arbitrary params to |
This patch cleans up and generalises part of the code which handles overriding and calling a native base-class's __init__ method. It defers the call to the native make_new() function until after the user (Python) __init__() method has run. That user method now has the chance to call the native __init__/make_new and pass it different arguments. If the user doesn't call the super().__init__ method then it will be called automatically after the user code finishes, to finalise construction of the instance.
Snippet from CPy stdlib urlparse.py:
You see, it doesn't call
super().__init__()
. That's because it relies on__new__
to initialize base class automatically. Specifically for CPy, collections.defaultdict is at all native type, and to emulate its behavior we need__new__
.Another testcase:
The text was updated successfully, but these errors were encountered: