mirror of
https://github.com/python/cpython.git
synced 2025-11-25 21:11:09 +00:00
#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.
This commit is contained in:
parent
e6f1e435d4
commit
b4ca59b783
3 changed files with 71 additions and 9 deletions
28
Lib/site.py
28
Lib/site.py
|
|
@ -55,6 +55,7 @@ ImportError exception, it is silently ignored.
|
|||
import sys
|
||||
import os
|
||||
import builtins
|
||||
import traceback
|
||||
|
||||
# Prefixes for site-packages; add additional prefixes like /usr/local here
|
||||
PREFIXES = [sys.prefix, sys.exec_prefix]
|
||||
|
|
@ -141,17 +142,26 @@ def addpackage(sitedir, name, known_paths):
|
|||
except IOError:
|
||||
return
|
||||
with f:
|
||||
for line in f:
|
||||
for n, line in enumerate(f):
|
||||
if line.startswith("#"):
|
||||
continue
|
||||
if line.startswith(("import ", "import\t")):
|
||||
exec(line)
|
||||
continue
|
||||
line = line.rstrip()
|
||||
dir, dircase = makepath(sitedir, line)
|
||||
if not dircase in known_paths and os.path.exists(dir):
|
||||
sys.path.append(dir)
|
||||
known_paths.add(dircase)
|
||||
try:
|
||||
if line.startswith(("import ", "import\t")):
|
||||
exec(line)
|
||||
continue
|
||||
line = line.rstrip()
|
||||
dir, dircase = makepath(sitedir, line)
|
||||
if not dircase in known_paths and os.path.exists(dir):
|
||||
sys.path.append(dir)
|
||||
known_paths.add(dircase)
|
||||
except Exception as err:
|
||||
print("Error processing line {:d} of {}:\n".format(n+1, fullname),
|
||||
file=sys.stderr)
|
||||
for record in traceback.format_exception(*sys.exc_info()):
|
||||
for line in record.splitlines():
|
||||
print(' '+line, file=sys.stderr)
|
||||
print("\nRemainder of file ignored", file=sys.stderr)
|
||||
break
|
||||
if reset:
|
||||
known_paths = None
|
||||
return known_paths
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue