Repository navigation
[mypyc] Allow weak references to classes that allow interpreted subclasses - #22468
Open
Jahongir-Qurbonov wants to merge 1 commit into
Open
Jahongir-Qurbonov wants to merge 1 commit into
Jahongir-Qurbonov wants to merge 1 commit into
Conversation
…asses mypyc reported a compile error for weakref.ref(a) or weakref.proxy(a) when 'a' has the type of a native class without weak reference support. This is a false positive if the class has @mypyc_attr(allow_interpreted_subclasses=True), since the value may be an instance of an interpreted subclass, and interpreted subclasses always support weak references. Don't report the error in this case; an instance of the native class itself still raises TypeError at runtime. Since all base classes of a class that allows interpreted subclasses must also allow them, checking the class itself is enough. Also remove an incorrect TODO comment in the generated tp_dealloc claiming that weak references created inside __del__ are not cleared. The weak reference list is cleared after the finalizer has run, so these are cleared as well. Add a test for this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Weak reference support for native classes was added in #22387. Along with it, mypyc reports a compile error when the target of
weakref.ref()orweakref.proxy()has the type of a native class without weak reference support. This gave a false positive for classes that allow interpreted subclasses:mypyc reported
Cannot create weak reference to instance of native class "A"forf. Butamay be an instance of an interpreted subclass ofA, and interpreted subclasses always support weak references (this is also what the docs say). Now the error is not reported if the class allows interpreted subclasses. Callingfwith an instance ofAitself still raisesTypeErrorat runtime, like it does when the static type isobject. All base classes of a class that allows interpreted subclasses must allow them as well, so it's enough to check the class itself.I also removed a TODO comment in the generated
tp_deallocthat said weak references created inside__del__are not cleared. The weak reference list is cleared afterPyObject_CallFinalizerFromDealloc()returns, so these are cleared too. (CPython's second clearing pass insubtype_deallocis for the legacytp_delslot, which mypyc doesn't use.)Tests: I added a class that allows interpreted subclasses to the irbuild error test case. In
run-weakref.test, an interpreted subclass instance passed to a compiledweakref.ref()call now works, and an instance of the native class still raisesTypeError. The existing__del__test now also creates a weak reference inside__del__and checks that it's dead after the object is freed.