@@ -1202,5 +1202,46 @@ def test_from_traceback_dis(self):
12021202 b = dis .Bytecode .from_traceback (tb )
12031203 self .assertEqual (b .dis (), dis_traceback )
12041204
1205+
1206+ class TestDisTraceback (unittest .TestCase ):
1207+ def setUp (self ) -> None :
1208+ try : # We need to clean up existing tracebacks
1209+ del sys .last_traceback
1210+ except AttributeError :
1211+ pass
1212+ return super ().setUp ()
1213+
1214+ def get_disassembly (self , tb ):
1215+ output = io .StringIO ()
1216+ with contextlib .redirect_stdout (output ):
1217+ dis .distb (tb )
1218+ return output .getvalue ()
1219+
1220+ def test_distb_empty (self ):
1221+ with self .assertRaises (RuntimeError ):
1222+ dis .distb ()
1223+
1224+ def test_distb_last_traceback (self ):
1225+ # We need to have an existing last traceback in `sys`:
1226+ tb = get_tb ()
1227+ sys .last_traceback = tb
1228+
1229+ self .assertEqual (self .get_disassembly (None ), dis_traceback )
1230+
1231+ def test_distb_explicit_arg (self ):
1232+ tb = get_tb ()
1233+
1234+ self .assertEqual (self .get_disassembly (tb ), dis_traceback )
1235+
1236+
1237+ class TestDisTracebackWithFile (TestDisTraceback ):
1238+ # Run the `distb` tests again, using the file arg instead of print
1239+ def get_disassembly (self , tb ):
1240+ output = io .StringIO ()
1241+ with contextlib .redirect_stdout (output ):
1242+ dis .distb (tb , file = output )
1243+ return output .getvalue ()
1244+
1245+
12051246if __name__ == "__main__" :
12061247 unittest .main ()
0 commit comments