8000 tools/gen-cpydiff.py: Improve stdout vs stderr interleaving. · andrewleech/micropython@605eda1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 605eda1

Browse files
jeplerdpgeorge
authored andcommitted
tools/gen-cpydiff.py: Improve stdout vs stderr interleaving.
In the syntax_space cpydiff, all the warnings were shown after the other output. This is because the output always showed all of stdout first and all of stdout second. By running Python in unbuffered mode and using `stderr=STDOUT`, the two streams are interleaved in exactly the order they're printed, so the SyntaxWarnings are interleaved with the other output. By using the `encoding=` argument of Popen, the need to explicitly convert to utf-8 is avoided. The encoding of the input also becomes utf-8 in this case, which all the test cases are (well, they're all ASCII, I think). As in `run-tests.py`, setting PYTHONIOENCODING ensures the Python interpreter's input and output are in utf-8, which is not always the case, especially on Windows systems. I spot-checked the generated doc pages and they all seemed to make sense still. Signed-off-by: Jeff Epler <jepler@gmail.com>
1 parent 2f97d1d commit 605eda1

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

tools/gen-cpydiff.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
CPYTHON3 = os.getenv("MICROPY_CPYTHON3", "python3")
4646
MICROPYTHON = os.getenv("MICROPY_MICROPYTHON", "../ports/unix/build-standard/micropython")
4747

48+
# Set PYTHONIOENCODING so that CPython will use utf-8 on systems which set another encoding in the locale
49+
os.environ["PYTHONIOENCODING"] = "utf-8"
50+
51+
# Set PYTHONUNBUFFERED so that CPython will interleave stdout & stderr without buffering
52+
os.environ["PYTHONUNBUFFERED"] = "a non-empty string"
53+
4854
TESTPATH = "../tests/cpydiff"
4955
DOCPATH = "../docs/genrst"
5056
SRCDIR = "../docs/differences"
@@ -111,28 +117,30 @@ def run_tests(tests):
111117
results = []
112118
for test in tests:
113119
test_fullpath = os.path.join(TESTPATH, test.name)
114-
with open(test_fullpath, "rb") as f:
120+
with open(test_fullpath, "r") as f:
115121
input_py = f.read()
116122

117123
process = subprocess.Popen(
118124
CPYTHON3,
119125
shell=True,
120126
stdout=subprocess.PIPE,
121127
stdin=subprocess.PIPE,
122-
stderr=subprocess.PIPE,
128+
stderr=subprocess.STDOUT,
129+
encoding="utf-8",
123130
)
124-
output_cpy = [com.decode("utf8") for com in process.communicate(input_py)]
131+
output_cpy = process.communicate(input_py)[0]
125132

126133
process = subprocess.Popen(
127134
MICROPYTHON,
128135
shell=True,
129136
stdout=subprocess.PIPE,
130137
stdin=subprocess.PIPE,
131-
stderr=subprocess.PIPE,
138+
stderr=subprocess.STDOUT,
139+
encoding="utf-8",
132140
)
133-
output_upy = [com.decode("utf8") for com in process.communicate(input_py)]
141+
output_upy = process.communicate(input_py)[0]
134142

135-
if output_cpy[0] == output_upy[0] and output_cpy[1] == output_upy[1]:
143+
if output_cpy == output_upy:
136144
print("Error: Test has same output in CPython vs MicroPython: " + test_fullpath)
137145
same_results = True
138146
else:
@@ -246,9 +254,9 @@ def gen_rst(results):
246254
rst.write("**Workaround:** " + output.workaround + "\n\n")
247255

248256
rst.write("Sample code::\n\n" + indent(output.code, TAB) + "\n")
249-
output_cpy = indent("".join(output.output_cpy[0:2]), TAB).rstrip()
257+
output_cpy = indent(output.output_cpy, TAB).rstrip()
250258
output_cpy = ("::\n\n" if output_cpy != "" else "") + output_cpy
251-
output_upy = indent("".join(output.output_upy[0:2]), TAB).rstrip()
259+
output_upy = indent(output.output_upy, TAB).rstrip()
252260
output_upy = ("::\n\n" if output_upy != "" else "") + output_upy
253261
table = gen_table([["CPy output:", output_cpy], ["uPy output:", output_upy]])
254262
rst.write(table)

0 commit comments

Comments
 (0)
0