diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2023-05-06 09:42:06 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2023-05-06 10:08:09 -0400 |
commit | abb38b8eb7718eab1c01f7e3e1f32588201da5c2 (patch) | |
tree | e29658a6ed20c3ab35a79430cc9c4cfa19b291f0 | |
parent | 4cbe7f8514a0f7d4a7a2dbf21371476410ddf23d (diff) | |
download | python-coveragepy-git-abb38b8eb7718eab1c01f7e3e1f32588201da5c2.tar.gz |
test: a helper to get disassembly of test files
-rw-r--r-- | tests/helpers.py | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/tests/helpers.py b/tests/helpers.py index 83d0cb0c..cb1b1b50 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -7,6 +7,7 @@ from __future__ import annotations import collections import contextlib +import dis import os import os.path import re @@ -94,14 +95,27 @@ def make_file( data = text.encode("utf-8") # Make sure the directories are available. - dirs, _ = os.path.split(filename) - if dirs and not os.path.exists(dirs): - os.makedirs(dirs) + dirs, basename = os.path.split(filename) + if dirs: + os.makedirs(dirs, exist_ok=True) # Create the file. with open(filename, 'wb') as f: f.write(data) + # $set_env.py: COVERAGE_DIS - Disassemble test code to /tmp/dis + show_dis = bool(int(os.environ.get("COVERAGE_DIS", 0))) + if basename.endswith(".py") and show_dis: # pragma: debugging + os.makedirs("/tmp/dis", exist_ok=True) + with open(f"/tmp/dis/{basename}.dis", "w") as fdis: + print(f"# {os.path.abspath(filename)}", file=fdis) + try: + dis.dis(textwrap.dedent(text), file=fdis) + except Exception as exc: + # Some tests make .py files that aren't Python, so dis will + # fail, which is expected. + print(f"#! {exc!r}") + # For debugging, enable this to show the contents of files created. if 0: # pragma: debugging print(f" ───┬──┤ {filename} ├───────────────────────") # type: ignore[unreachable] |