py/objtype: Validate that __len__ returns an int. - #19647
Open
pablogventura wants to merge 4 commits into
Open
Conversation
Fold MP_UNARY_OP_LEN into the existing INT_MAYBE arm so a non-int __len__ result raises TypeError, matching the zero-cost approach discussed in micropython#19512. Co-authored-by: Jeongseop Lim <jseoplim@gmail.com> Signed-off-by: Pablo Ventura <pablogventura@gmail.com>
Keep the test runnable on the unix minimal variant (no float literals, no bytearray). Signed-off-by: Pablo Ventura <pablogventura@gmail.com>
|
Code size report: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #19647 +/- ##
==========================================
- Coverage 98.58% 98.55% -0.04%
==========================================
Files 182 182
Lines 23322 23322
Branches 5 5
==========================================
- Hits 22993 22985 -8
- Misses 328 336 +8
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Signed-off-by: Pablo Ventura <pablogventura@gmail.com>
Signed-off-by: Pablo Ventura <pablogventura@gmail.com>
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.
Summary
Fixes #19512.
instance_unary_opalready validates that__hash__/__int__return an int, butMP_UNARY_OP_LENfell through unchecked, solen(obj)could returnNone/str/float.Fold
MP_UNARY_OP_LENinto the existingMP_UNARY_OP_INT_MAYBEarm (approach from @jseop-lim on the issue). Non-int__len__results raiseTypeError. This also covers callers that go through instanceunary_op.Testing
tests/basics/builtin_len_user.py(int OK;None/str/tuple->TypeError)tests/run-tests.py basics/builtin_len_user.pyon unixstandardandminimalTrade-offs and Alternatives
Code size (unix
standard, clean rebuild,sizetext section):instance_unary_opinobjtype.ogrows by 8 bytes; the linked binary text is unchanged (section padding).Does not reject a negative
__len__(not free; left for a separate discussion as on the issue).Deliberate divergence vs CPython:
__len__returningboolraisesTypeErrorhere (mp_obj_is_intexcludesbool), same as__int__/ #19203. Not covered in the test so the harness can compare against CPython.Generative AI
I used generative AI tools when creating this PR, but a human has checked the
code and is responsible for the code and the description above.