@@ -1244,5 +1244,46 @@ def test_assert_not_in_with_arg_in_bytecode(self):
12441244 with self .assertRaises (AssertionError ):
12451245 self .assertNotInBytecode (code , "LOAD_CONST" , 1 )
12461246
1247+
1248+ class TestDisTraceback (unittest .TestCase ):
1249+ def setUp (self ) -> None :
1250+ try : # We need to clean up existing tracebacks
1251+ del sys .last_traceback
1252+ except AttributeError :
1253+ pass
1254+ return super ().setUp ()
1255+
1256+ def get_disassembly (self , tb ):
1257+ output = io .StringIO ()
1258+ with contextlib .redirect_stdout (output ):
1259+ dis .distb (tb )
1260+ return output .getvalue ()
1261+
1262+ def test_distb_empty (self ):
1263+ with self .assertRaises (RuntimeError ):
1264+ dis .distb ()
1265+
1266+ def test_distb_last_traceback (self ):
1267+ # We need to have an existing last traceback in `sys`:
1268+ tb = get_tb ()
1269+ sys .last_traceback = tb
1270+
1271+ self .assertEqual (self .get_disassembly (None ), dis_traceback )
1272+
1273+ def test_distb_explicit_arg (self ):
1274+ tb = get_tb ()
1275+
1276+ self .assertEqual (self .get_disassembly (tb ), dis_traceback )
1277+
1278+
1279+ class TestDisTracebackWithFile (TestDisTraceback ):
1280+ # Run the `distb` tests again, using the file arg instead of print
1281+ def get_disassembly (self , tb ):
1282+ output = io .StringIO ()
1283+ with contextlib .redirect_stdout (output ):
1284+ dis .distb (tb , file = output )
1285+ return output .getvalue ()
1286+
1287+
12471288if __name__ == "__main__" :
12481289 unittest .main ()
0 commit comments