Skip to content

Fix more operations that can return small ints #17785

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jepler
Copy link
Contributor

@jepler jepler commented Jul 29, 2025

Summary

#17730 identified some code paths that could still return long ints when small ints would suffice. Fix them.

Testing

I wrote a new test based on sites that called new_int_from_{u,}ll, including the case I'd noticed of pow3 (already reported in #9531 as well).

Trade-offs and Alternatives

I haven't seen the code size change summary yet, but this is likely to increase code size. I'm open to suggestions about how to reduce the increase.

Making the new long_from functions always return longs, and changing the existing new_int_from functions to return ints when possible is just one alternative. Another alternative would be to add calls to mp_int_maybe_narrow in paths that used new_int_from, but this seemed like the worse alternative, with more added code.

Copy link

codecov bot commented Jul 29, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.37%. Comparing base (ab4af2c) to head (bd3bc55).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #17785      +/-   ##
==========================================
- Coverage   98.39%   98.37%   -0.02%     
==========================================
  Files         171      171              
  Lines       22257    22265       +8     
==========================================
+ Hits        21899    21904       +5     
- Misses        358      361       +3     

☔ 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.

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:   +20 +0.011% 
   unix x64:  +160 +0.019% standard
      stm32:  +136 +0.035% PYBV10
     mimxrt:  +136 +0.036% TEENSY40
        rp2:  +136 +0.015% RPI_PICO_W
       samd:  +136 +0.051% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:  +166 +0.037% VIRT_RV32

Signed-off-by: Jeff Epler <jepler@gmail.com>
jepler added 2 commits July 30, 2025 10:03
Since adafruit#9531 a few ways remained to produce long ints that were
not reduced to small ints.

Explicitly introduce "mp_obj_new_long_from_ll" and make
the "new_int" functions always produce small ints when possible.

At sites where a long int is really intended (or will
eventually be reduced to a small int if the result fits),
the called function is changed to be the new _long_ function.

Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Jeff Epler <jepler@gmail.com>
@jepler
Copy link
Contributor Author

jepler commented Jul 30, 2025

the nrf port is failing due to a "403 Forbidden" error downloading some files from nordic:


####################################
### Downloading s140_nrf52_6.1.1 ###
####################################

--2025-07-30 15:05:28--  https://www.nordicsemi.com/api/sitecore/Products/MedialibraryZipDownload2
Resolving www.nordicsemi.com (www.nordicsemi.com)... 104.18.26.60, 104.18.27.60, 2606:4700::6812:1b3c, ...
Connecting to www.nordicsemi.com (www.nordicsemi.com)|104.18.26.60|:443... connected.
HTTP request sent, awaiting response... 403 Forbidden
2025-07-30 15:05:29 ERROR 403: Forbidden.

mv: cannot stat 'MedialibraryZipDownload2': No such file or directory
unzip:  cannot find or open temp.zip, temp.zip.zip or temp.zip.ZIP.
unzip:  cannot find or open s140nrf52611.zip, s140nrf52611.zip.zip or s140nrf52611.zip.ZIP.
rm: cannot remove 's140nrf52611.zip': No such file or directory
rm: cannot remove 'temp.zip': No such file or directory

furthermore, the failure is not detected at the time, but the build continues until this message is shown


drivers/bluetooth/bluetooth_common.mk:48: *** 

###### ERROR: Bluetooth LE Stack not found ############
#                                                     #
# The build target requires a Bluetooth LE stack.     #
# s140_nrf52_6.1.1 Bluetooth LE stack not found.      #
#                                                     #
# Please run the download script:                     #
#                                                     #
#      drivers/bluetooth/download_ble_stack.sh        #
#                                                     #
#######################################################
.  Stop.

with any luck this is a transient error and nordic will fix the download link.

@robert-hh
Copy link
Contributor

The same error popped up at PR #17365.

@dhalbert
Copy link
Contributor

with any luck this is a transient error and nordic will fix the download link.

CircuitPython GitHub actions builds are also seeing this. It looks like wget has become a forbidden user agent for certain nordicsemi.com URL's (though not all). I reported this to NordicSemi: https://devzone.nordicsemi.com/f/nordic-q-a/123372/https-developer-nordicsemi-com-is-blocking-some-user-agent-values-on-downloads

Copy link
Contributor

@projectgus projectgus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, thanks @jepler.

I tried to think of ways to combine mp_obj_new_int, mp_obj_new_int_from_ll, and mp_obj_new_int_from_ull into a single inner static implementation but they were all going to involve some level of compiler crimes I think. 😆

I've linked this PR to close the issue as IIUC it addresses everything enumerated there, but let me know if I'm missing a case.

mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // this must return a multi-precision integer object (or raise an overflow exception)
mp_obj_t mp_obj_new_int_from_ll(long long val); // returns a small int if value fits
mp_obj_t mp_obj_new_int_from_ull(unsigned long long val); // returns a small int if value fits
mp_obj_t mp_obj_new_long_from_ll(long long val); // this must return a multi-precision integer object (or raise an overflow exception)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fully separate to this PR, but at some point we should probably rename all the longint references in MicroPython to bigint to avoid this ambiguity about what long means.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

code paths that return large ints where small ints would suffice
4 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy