Skip to content

py/runtime: Check that __dict__ is a dict in import *. - #19673

Open
pablogventura wants to merge 1 commit into
micropython:masterfrom
pablogventura:py-import-all-dict-check
Open

pablogventura wants to merge 1 commit into
micropython:masterfrom
pablogventura:py-import-all-dict-check

Conversation

@pablogventura

Copy link
Copy Markdown
Contributor

Summary

Fixes #19621. mp_import_all loaded __dict__ and passed it to mp_obj_dict_get_map without checking the type. A user object in sys.modules with a non-dict __dict__ (e.g. an instance attribute shadowing the special getter) caused a segfault.

Check with mp_obj_is_dict_or_ordereddict and raise TypeError (same approach as validating other special-method return types).

Testing

  • tests/import/import_star_non_dict.py (PoC from the issue; CPython raises on assign, MicroPython on import *; both print TypeError)
  • tests/run-tests.py import/import_star_non_dict.py import/import_star.py import/import_star_error.py on unix port

Trade-offs and Alternatives

Code size (unix standard, size text section, clean rebuild):

Version bytes vs master
master 796390 -
this commit 796390 0

Does not change instance __dict__ store semantics (still possible to assign a non-dict attribute named __dict__); only import * is hardened.

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.

@codecov

codecov Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.59%. Comparing base (8162451) to head (2751abe).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #19673   +/-   ##
=======================================
  Coverage   98.59%   98.59%           
=======================================
  Files         182      182           
  Lines       23335    23337    +2     
  Branches        5        5           
=======================================
+ Hits        23006    23008    +2     
  Misses        328      328           
  Partials        1        1           
Flag Coverage Δ
unix-coverage-32bit 98.59% <100.00%> (+<0.01%) ⬆️
unix-coverage-64bit 98.51% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@github-actions

github-actions Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Code size report:

Reference:  tests/extmod/machine_mem_backup: Add slice read/write test. [8162451]
Comparison: py/runtime: Check that __dict__ is a dict in import *. [merge of 2751abe]
  mpy-cross:    +0 +0.000% 
   bare-arm:   +12 +0.021% 
minimal x86:   +21 +0.011% 
   unix x64:   +24 +0.003% standard
      stm32:    +8 +0.002% PYBV10
      esp32:    +8 +0.000% ESP32_GENERIC
     mimxrt:   +24 +0.006% TEENSY40
        rp2:   +16 +0.002% RPI_PICO_W
       samd:   +16 +0.006% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:   +12 +0.003% VIRT_RV32

Reject a non-dict __dict__ in mp_import_all before mp_obj_dict_get_map,
so from m import * raises TypeError instead of crashing when a user
object in sys.modules shadows __dict__ with a non-dict value.

Signed-off-by: Pablo Ventura <[email protected]>
@pablogventura
pablogventura force-pushed the py-import-all-dict-check branch from 33bad1e to 2751abe Compare September 1, 2026 13:25
@projectgus projectgus added the py-core Relates to py/ directory in source label Sep 3, 2026
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.

MicroPython type confusion in star imports causes interpreter crash

2 participants