Skip to content

Commit 6646ee0

Browse files
authoredApr 23, 2025··
[mypyc] Add hidden flag to skip the generation of C files (#18955)
This can be useful when debugging mypyc issues. For example, you can manually add some debug prints to the generated C and rerun mypyc with `--skip-c-gen`. Now mypyc will build the C code again, with your manual changes included (this assumes everything else is the same as during the previous run). I'm not planning to advertise this as an end-user feature.
1 parent 380cb8d commit 6646ee0

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed
 

‎mypy/main.py

+5
Original file line numberDiff line numberDiff line change
@@ -1128,6 +1128,11 @@ def add_invertible_flag(
11281128
report_group.add_argument(
11291129
"-a", dest="mypyc_annotation_file", type=str, default=None, help=argparse.SUPPRESS
11301130
)
1131+
# Hidden mypyc feature: do not write any C files (keep existing ones and assume they exist).
1132+
# This can be useful when debugging mypyc bugs.
1133+
report_group.add_argument(
1134+
"--skip-c-gen", dest="mypyc_skip_c_generation", action="store_true", help=argparse.SUPPRESS
1135+
)
11311136

11321137
other_group = parser.add_argument_group(title="Miscellaneous")
11331138
other_group.add_argument("--quickstart-file", help=argparse.SUPPRESS)

‎mypy/options.py

+3
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ def __init__(self) -> None:
408408

409409
# Output html file for mypyc -a
410410
self.mypyc_annotation_file: str | None = None
411+
# Skip writing C output files, but perform all other steps of a build (allows
412+
# preserving manual tweaks to generated C file)
413+
self.mypyc_skip_c_generation = False
411414

412415
def use_lowercase_names(self) -> bool:
413416
if self.python_version >= (3, 9):

‎mypyc/build.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,8 @@ def mypyc_build(
452452
cfilenames = []
453453
for cfile, ctext in cfiles:
454454
cfile = os.path.join(compiler_options.target_dir, cfile)
455-
write_file(cfile, ctext)
455+
if not options.mypyc_skip_c_generation:
456+
write_file(cfile, ctext)
456457
if os.path.splitext(cfile)[1] == ".c":
457458
cfilenames.append(cfile)
458459

‎mypyc/doc/dev-intro.md

+19
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,25 @@ Test cases can also have a `[out]` section, which specifies the
386386
expected contents of stdout the test case should produce. New test
387387
cases should prefer assert statements to `[out]` sections.
388388

389+
### Adding Debug Prints and Editing Generated C
390+
391+
Sometimes it's helpful to add some debug prints or other debugging helpers
392+
to the generated C code. You can run mypyc using `--skip-c-gen` to skip the C
393+
generation step, so all manual changes to C files are preserved. Here is
394+
an example of how to use the workflow:
395+
396+
* Compile some file you want to debug: `python -m mypyc foo.py`.
397+
* Add debug prints to the generated C in `build/__native.c`.
398+
* Run the same compilation command line again, but add `--skip-c-gen`:
399+
`python -m mypyc --skip-c-gen foo.py`. This will only rebuild the
400+
binaries.
401+
* Run the compiled code, including your changes: `python -c 'import foo'`.
402+
You should now see the output from the debug prints you added.
403+
404+
This can also be helpful if you want to quickly experiment with different
405+
implementation techniques, without having to first figure out how to
406+
modify mypyc to generate the desired C code.
407+
389408
### Debugging Segfaults
390409

391410
If you experience a segfault, it's recommended to use a debugger that supports

0 commit comments

Comments
 (0)
Please sign in to comment.