def test_bug737473(self): import os, tempfile, time savedpath = sys.path[:] testdir = tempfile.mkdtemp() try: sys.path.insert(0, testdir) testfile = os.path.join(testdir, 'test_bug737473.py') with open(testfile, 'w') as f: print >> f, """ def test(): raise ValueError""" if 'test_bug737473' in sys.modules: del sys.modules['test_bug737473'] import test_bug737473 try: test_bug737473.test() except ValueError: # this loads source code to linecache traceback.extract_tb(sys.exc_traceback) # If this test runs too quickly, test_bug737473.py's mtime # attribute will remain unchanged even if the file is rewritten. # Consequently, the file would not reload. So, added a sleep() # delay to assure that a new, distinct timestamp is written. # Since WinME with FAT32 has multisecond resolution, more than # three seconds are needed for this test to pass reliably :-( time.sleep(4) with open(testfile, 'w') as f: print >> f, """ def test(): raise NotImplementedError""" reload(test_bug737473) try: test_bug737473.test() except NotImplementedError: src = traceback.extract_tb(sys.exc_traceback)[-1][-1] self.assertEqual(src, 'raise NotImplementedError') finally: sys.path[:] = savedpath for f in os.listdir(testdir): os.unlink(os.path.join(testdir, f)) os.rmdir(testdir) err = self.get_exception_format(self.syntax_error_bad_indentation2, IndentationError) self.assertEqual(len(err), 4) self.assertEqual(err[1].strip(), "print(2)") self.assertIn("^", err[2]) if check_impl_detail(): self.assertEqual(err[1].find("p"), err[2].find("^")) if check_impl_detail(pypy=True): self.assertEqual(err[1].find("2)") + 1, err[2].find("^"))
def test_bug737473(self): import sys, os, tempfile, time savedpath = sys.path[:] testdir = tempfile.mkdtemp() try: sys.path.insert(0, testdir) testfile = os.path.join(testdir, 'test_bug737473.py') f = open(testfile, 'w') print >> f, """ def test(): raise ValueError""" f.close() if 'test_bug737473' in sys.modules: del sys.modules['test_bug737473'] import test_bug737473 try: test_bug737473.test() except ValueError: # this loads source code to linecache traceback.extract_tb(sys.exc_traceback) # If this test runs too quickly, test_bug737473.py's mtime # attribute will remain unchanged even if the file is rewritten. # Consequently, the file would not reload. So, added a sleep() # delay to assure that a new, distinct timestamp is written. # Since WinME with FAT32 has multisecond resolution, more than # three seconds are needed for this test to pass reliably :-( time.sleep(4) f = open(testfile, 'w') print >> f, """ def test(): raise NotImplementedError""" f.close() reload(test_bug737473) try: test_bug737473.test() except NotImplementedError: src = traceback.extract_tb(sys.exc_traceback)[-1][-1] self.failUnlessEqual(src, 'raise NotImplementedError') finally: sys.path[:] = savedpath for f in os.listdir(testdir): os.unlink(os.path.join(testdir, f)) os.rmdir(testdir)