Repository navigation
picogame: its types cannot be subclassed in Python - #12048
Open
lynt-smitka wants to merge 1 commit into
Open
lynt-smitka wants to merge 1 commit into
lynt-smitka wants to merge 1 commit into
Conversation
CircuitPython passes a subclass instance as self to native methods and properties, and picogame uses self as its own struct, so a subclass of Sprite read and wrote the wrong memory. Defining such a subclass now raises TypeError, from a new MP_TYPE_FLAG_NOT_SUBCLASSABLE.
Collaborator
|
This has always been true for many (but not all) natives types, and is inherited from MicroPython. I agree that it would be nice to raise an error. I can't remember the exact semantics. Discussion here: https://docs.micropython.org/en/latest/genrst/core_language.html, but I am pretty sure this has been discussed multiple times in the MicroPython issues and maybe in a few CircuitPython issues too. |
Author
|
Thanks, I've been reading the discussions, and it seems that, according to MPy, the right approach is to allow subclasses. I don't think it's worth 800B, and the issue isn't that significant, so I'd probably pass this PR... |
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.
Subclassing picogame types never worked: a Scene rejects subclass instances ("Invalid item"), and the native code used the subclass instance as its own struct, so properties read and wrote the wrong memory.
Making it work would cost about 800 bytes of flash for a feature nobody uses, so this makes such a class definition fail right away with TypeError instead.
That needs a small core change, a new
MP_TYPE_FLAG_NOT_SUBCLASSABLEchecked intype_new, because a native type'smake_newis called with the native type and cannot tell that a subclass is being created.