summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorR. David Murray <rdmurray@bitdance.com>2010-12-26 19:54:29 (GMT)
committerR. David Murray <rdmurray@bitdance.com>2010-12-26 19:54:29 (GMT)
commitb4ca59b783d6d2047c34eb242d97272d81b94688 (patch)
treeb2195b8992551247c8075615e139af818068bdc7 /Lib/test
parente6f1e435d4e01d2fdb349610565e99b792ace1a8 (diff)
downloadcpython-b4ca59b783d6d2047c34eb242d97272d81b94688.zip
cpython-b4ca59b783d6d2047c34eb242d97272d81b94688.tar.gz
cpython-b4ca59b783d6d2047c34eb242d97272d81b94688.tar.bz2
#5258/#10642: print fn, line, traceback and continue when .pth file is broken
If a .pth file contained an error, it could cause a traceback in site.py, terminating its processing. In 2.7 and 3.2, the interpreter will then not start. Previously, a message would print saying to use -v to get the traceback. In either case, the traceback generated for a failed .pth file did not include the .pth filename, making it difficult to debug the problem. Now site.py reports not only the .pth filename but also the line number causing the error, and just skips the remainder of the file.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_site.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index 94d6699..0ed4706 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -6,6 +6,7 @@ executing have not been removed.
"""
import unittest
from test.support import run_unittest, TESTFN, EnvironmentVarGuard
+from test.support import captured_stderr
import builtins
import os
import sys
@@ -90,6 +91,53 @@ class HelperFunctionsTests(unittest.TestCase):
finally:
pth_file.cleanup()
+ def make_pth(self, contents, pth_dir='.', pth_name=TESTFN):
+ # Create a .pth file and return its (abspath, basename).
+ pth_dir = os.path.abspath(pth_dir)
+ pth_basename = pth_name + '.pth'
+ pth_fn = os.path.join(pth_dir, pth_basename)
+ pth_file = open(pth_fn, 'w', encoding='utf-8')
+ self.addCleanup(lambda: os.remove(pth_fn))
+ pth_file.write(contents)
+ pth_file.close()
+ return pth_dir, pth_basename
+
+ def test_addpackage_import_bad_syntax(self):
+ # Issue 10642
+ pth_dir, pth_fn = self.make_pth("import bad)syntax\n")
+ with captured_stderr() as err_out:
+ site.addpackage(pth_dir, pth_fn, set())
+ self.assertRegex(err_out.getvalue(), "line 1")
+ self.assertRegex(err_out.getvalue(), os.path.join(pth_dir, pth_fn))
+ # XXX: the previous two should be independent checks so that the
+ # order doesn't matter. The next three could be a single check
+ # but my regex foo isn't good enough to write it.
+ self.assertRegex(err_out.getvalue(), 'Traceback')
+ self.assertRegex(err_out.getvalue(), r'import bad\)syntax')
+ self.assertRegex(err_out.getvalue(), 'SyntaxError')
+
+ def test_addpackage_import_bad_exec(self):
+ # Issue 10642
+ pth_dir, pth_fn = self.make_pth("randompath\nimport nosuchmodule\n")
+ with captured_stderr() as err_out:
+ site.addpackage(pth_dir, pth_fn, set())
+ self.assertRegex(err_out.getvalue(), "line 2")
+ self.assertRegex(err_out.getvalue(), os.path.join(pth_dir, pth_fn))
+ # XXX: ditto previous XXX comment.
+ self.assertRegex(err_out.getvalue(), 'Traceback')
+ self.assertRegex(err_out.getvalue(), 'ImportError')
+
+ def test_addpackage_import_bad_pth_file(self):
+ # Issue 5258
+ pth_dir, pth_fn = self.make_pth("abc\x00def\n")
+ with captured_stderr() as err_out:
+ site.addpackage(pth_dir, pth_fn, set())
+ self.assertRegex(err_out.getvalue(), "line 1")
+ self.assertRegex(err_out.getvalue(), os.path.join(pth_dir, pth_fn))
+ # XXX: ditto previous XXX comment.
+ self.assertRegex(err_out.getvalue(), 'Traceback')
+ self.assertRegex(err_out.getvalue(), 'TypeError')
+
def test_addsitedir(self):
# Same tests for test_addpackage since addsitedir() essentially just
# calls addpackage() for every .pth file in the directory