Skip to content

py/parse: Remove explicit checks for invalid folding operations. #17883

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

Merged

Conversation

dpgeorge
Copy link
Member

Summary

They are instead checked by binary_op_maybe(), which catches exceptions for invalid int/float operations.

This is a follow-up to 69ead7d / #16666.

Testing

Tested with the unix port, with floats disabled.

The following do not raise exceptions (as expected) because the folding is not done:

compile("1/2","file","eval")
compile("1**-2","file","eval")
compile("1@2","file","eval")

Then I validated that the bytecode has the explicit operator calls:

$ micropython -v -v -v
...
>>> (1+2)@4
File <stdin>, code block '<module>' (descriptor: 7f0d487eed80, bytecode @7f0d487eeca0 13 bytes)
Raw bytecode (code_info_size=3, bytecode_size=10):
 10 02 01 11 02 83 84 f5 34 01 59 51 63
arg names:
(N_STATE 3)
(N_EXC_STACK 0)
  bc=0 line=1
00 LOAD_NAME __repl_print__
02 LOAD_CONST_SMALL_INT 3
03 LOAD_CONST_SMALL_INT 4
04 BINARY_OP 30 __matmul__
05 CALL_FUNCTION n=1 nkw=0
07 POP_TOP
08 LOAD_CONST_NONE
09 RETURN_VALUE
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported types for __matmul__: 'int', 'int'

You can see that the (1+2) is folded but not the @ operator, which is then evaluated at runtime. And also:

>>> 1**(-2)
File <stdin>, code block '<module>' (descriptor: 7f0d487eef00, bytecode @7f0d487eee80 13 bytes)
Raw bytecode (code_info_size=3, bytecode_size=10):
 10 02 01 11 02 81 7e f9 34 01 59 51 63
arg names:
(N_STATE 3)
(N_EXC_STACK 0)
  bc=0 line=1
00 LOAD_NAME __repl_print__
02 LOAD_CONST_SMALL_INT 1
03 LOAD_CONST_SMALL_INT -2
04 BINARY_OP 34 __pow__
05 CALL_FUNCTION n=1 nkw=0
07 POP_TOP
08 LOAD_CONST_NONE
09 RETURN_VALUE
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: negative power with no float support
>>> 1/2
File <stdin>, code block '<module>' (descriptor: 7f0d487ef300, bytecode @7f0d487ef120 13 bytes)
Raw bytecode (code_info_size=3, bytecode_size=10):
 10 02 01 11 02 81 82 f7 34 01 59 51 63
arg names:
(N_STATE 3)
(N_EXC_STACK 0)
  bc=0 line=1
00 LOAD_NAME __repl_print__
02 LOAD_CONST_SMALL_INT 1
03 LOAD_CONST_SMALL_INT 2
04 BINARY_OP 32 __truediv__
05 CALL_FUNCTION n=1 nkw=0
07 POP_TOP
08 LOAD_CONST_NONE
09 RETURN_VALUE
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported types for __truediv__: 'int', 'int'

Trade-offs and Alternatives

This means the test is delegated to binary_op_maybe(), making the check for invalid folding operations a little slower. But it saves code size and it's rare that the user does invalid binary operations on literal numbers.

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Aug 11, 2025
@dpgeorge
Copy link
Member Author

@yoctopuce FYI. I think this is a safe change to make.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   -16 -0.002% standard
      stm32:    -8 -0.002% PYBV10
     mimxrt:    -8 -0.002% TEENSY40
        rp2:    -8 -0.001% RPI_PICO_W
       samd:    -8 -0.003% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   -14 -0.003% VIRT_RV32

@yoctopuce
Copy link
Contributor

Yes, I agree, this looks completely safe.

They are instead checked by `binary_op_maybe()`, which catches exceptions
for invalid int/float operations.

This is a follow-up to 69ead7d

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge dpgeorge force-pushed the py-parse-simplify-invalid-folding-checks branch from 000d1d8 to 3efbd72 Compare August 15, 2025 01:38
@dpgeorge dpgeorge merged commit 3efbd72 into micropython:master Aug 15, 2025
68 of 69 checks passed
@dpgeorge dpgeorge deleted the py-parse-simplify-invalid-folding-checks branch August 15, 2025 01:44
Copy link

codecov bot commented Aug 15, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.38%. Comparing base (141f7d0) to head (3efbd72).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #17883      +/-   ##
==========================================
- Coverage   98.38%   98.38%   -0.01%     
==========================================
  Files         171      171              
  Lines       22298    22297       -1     
==========================================
- Hits        21939    21938       -1     
  Misses        359      359              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants