bpo-28869: Skip one additional frame when type.__new__ is called not directly from type.__call__. by serhiy-storchaka · Pull Request #14166 · python/cpython
* Move handling of one-argument call of type() from type.__new__() to type.__call__(). * Move deducing __module__ from the caller's frame from type.__new__() to type.__call__().
@serhiy-storchaka I like this approach, please let me know when this is ready for review (I assume tests are needed and some comments explaining this behavior).
Yes, it needs tests and documenting.
Also this PR contains actually two changes. I am not sure that both of them should be backported, but it is difficult to make one without making other.
I just have tested, and seems there is no undesired behavior change. So I just need to write tests and a NEWS entry.
Unfortunately it does not work. Not setting __module__ in the class dictionary does not help with the original issue, because __module__ will be looked up in the metaclass dictionary. Setting it in __call__() opens other questions and significantly complicates the code.
serhiy-storchaka
changed the title
[WIP] bpo-28869: Change some details in type().
bpo-28869: Skip one additional frame when type.__new__ is called not directly from type.__call__.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have one question/comment. Also if we are going this way, I think this deserves a comment in the code explaining the motivation.
| } | ||
| while (depth-- > 0 && f->f_back) { | ||
| f = f->f_back; | ||
| } |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I am missing something, depth can be only either 0 or 1, maybe it is better to use a name like skip or nested and write this as:
if (skip_frame && f->f_back) { f = f->f_back; }