From 5ecb288819e08b22ad0dda4a71c4a52f99ff9793 Mon Sep 17 00:00:00 2001 From: Bolun Thompson Date: Fri, 31 Jul 2020 16:21:51 -0700 Subject: [PATCH] Rewrote dis tests to start a new process to capture stdout (and stderr) This was chaned because stdout is being written to by rust code, which currently can't be redirected by reassigning sys.stdout. This caused the tests to fail because they couldn't see the output of what was being tested. --- Lib/test/test_dis.py | 62 ++++++++++++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py index 7b611ded9..8bbba86a4 100644 --- a/Lib/test/test_dis.py +++ b/Lib/test/test_dis.py @@ -1,29 +1,59 @@ +import subprocess +import sys import unittest -import dis -from test.support import captured_stdout - # This only tests that it prints something in order # to avoid changing this test if the bytecode changes -class TestDis(unittest.TestCase): - def test_dis(self): - test_cases = (self.test_dis, "x = 2; print(x)") - for case in test_cases: - with self.subTest(case=case): - with captured_stdout() as stdout: - dis.dis(case) - self.assertNotEqual("", stdout.getvalue()) +# These tests start a new process instead of redirecting stdout because +# stdout is being written to by rust code, which currently can't be +# redirected by reassigning sys.stdout + + +class TestDis(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.setup = """ +import dis +def tested_func(): pass +""" + cls.command = (sys.executable, "-c") + + def test_dis(self): + test_code = f""" +{self.setup} +dis.dis(tested_func) +dis.dis("x = 2; print(x)") +""" + + result = subprocess.run( + self.command + (test_code,), capture_output=True + ) + self.assertNotEqual("", result.stdout.decode()) + self.assertEqual("", result.stderr.decode()) def test_disassemble(self): + test_code = f""" +{self.setup} +dis.disassemble(tested_func) +""" + result = subprocess.run( + self.command + (test_code,), capture_output=True + ) # In CPython this would raise an AttributeError, not a # TypeError because dis is implemented in python in CPython and # as such the type mismatch wouldn't be caught immeadiately - with self.assertRaises(TypeError): - dis.disassemble(self.test_disassemble) - with captured_stdout() as stdout: - dis.dis(self.test_disassemble.__code__) - self.assertNotEqual("", stdout.getvalue()) + self.assertIn("TypeError", result.stderr.decode()) + + test_code = f""" +{self.setup} +dis.disassemble(tested_func.__code__) +""" + result = subprocess.run( + self.command + (test_code,), capture_output=True + ) + self.assertNotEqual("", result.stdout.decode()) + self.assertEqual("", result.stderr.decode()) if __name__ == "__main__":