← index #6800PR #18927
Off-topic · high · value 3.044
QUERY · ISSUE

Build process: mpy file in frozen dir produces spam

openby peterhinchopened 2021-01-26updated 2024-09-13
enhancementtools

I inadvertently included a .mpy file in a directory containing .py files for freezing. On building, so much output spewed that the Bash history buffer filled; I couldn't scroll back far enough to see where the error first occurred.

It would be good if the build process either stopped with a clear message or ignored files without .py extensions with a warning.

CANDIDATE · PULL REQUEST

tools/mpy-tool.py: Clean up escaped compiled module name.

openby agattiopened 2026-03-14updated 2026-03-16
tools

Summary

This PR lets "mpy-tool.py" filter unwanted characters that may end up in the compiled module name, even after the first encoding pass.

Certain characters are already ASCII-safe but cannot appear in a C identifier (eg. dots, dashes, etc.). Those characters, when appearing in a module name, won't be escaped and will be placed in the output C source code template when freezing files - leading to an invalid source file.

Now the code, after the first character encoding pass, will pre-process the compiled module name to use when filling out the C source code template. Every character that is not expected to be part of a C identifier will be replaced with an underscore.

This closes #3445.

Testing

A Python file was frozen with a custom module name containing characters that cannot be part of a C identifier, and the generated C file was compiled to make sure all generated identifiers are compliant:

$ cd ports/unix
$ make
$ printf "class Hello:\n  def __init__(self):\n    pass\n" > test.py
$ ../../mpy-cross/build/mpy-cross -o test.mpy -s "test.mpy" test.py
$ ../../tools/mpy-tool.py -f -q build-standard/genhdr/qstrdefs.preprocessed.h test.mpy > frozen.c
$ gcc -I. -I../.. -Ivariants/standard -Ibuild-standard -DMPZ_DIG_SIZE=16 -c frozen.c
$ size frozen.o
   text    data     bss     dec     hex filename
    155     176       0     331     14b frozen.o

Trade-offs and Alternatives

This PR does not help recovering from cases where the module gets named with incompatible names (eg. starting with a digit), but that wouldn't have worked in the first place.

Generative AI

I did not use generative AI tools when creating this PR.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied