|
|
|
@ -12,28 +12,33 @@ Text = type(u'')
|
|
|
|
|
def main():
|
|
|
|
|
"""Main program entry point."""
|
|
|
|
|
for path in sys.argv[1:] or sys.stdin.read().splitlines():
|
|
|
|
|
with open(path, 'rb') as source_fd:
|
|
|
|
|
source = source_fd.read()
|
|
|
|
|
compile_source(path)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
compile(source, path, 'exec', dont_inherit=True)
|
|
|
|
|
except SyntaxError as ex:
|
|
|
|
|
extype, message, lineno, offset = type(ex), ex.text, ex.lineno, ex.offset
|
|
|
|
|
except BaseException as ex: # pylint: disable=broad-except
|
|
|
|
|
extype, message, lineno, offset = type(ex), str(ex), 0, 0
|
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# In some situations offset can be None. This can happen for syntax errors on Python 2.6
|
|
|
|
|
# (__future__ import following after a regular import).
|
|
|
|
|
offset = offset or 0
|
|
|
|
|
def compile_source(path):
|
|
|
|
|
"""Compile the specified source file, printing an error if one occurs."""
|
|
|
|
|
with open(path, 'rb') as source_fd:
|
|
|
|
|
source = source_fd.read()
|
|
|
|
|
|
|
|
|
|
result = "%s:%d:%d: %s: %s" % (path, lineno, offset, extype.__name__, safe_message(message))
|
|
|
|
|
try:
|
|
|
|
|
compile(source, path, 'exec', dont_inherit=True)
|
|
|
|
|
except SyntaxError as ex:
|
|
|
|
|
extype, message, lineno, offset = type(ex), ex.text, ex.lineno, ex.offset
|
|
|
|
|
except BaseException as ex: # pylint: disable=broad-except
|
|
|
|
|
extype, message, lineno, offset = type(ex), str(ex), 0, 0
|
|
|
|
|
else:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if sys.version_info <= (3,):
|
|
|
|
|
result = result.encode(ENCODING, ERRORS)
|
|
|
|
|
# In some situations offset can be None. This can happen for syntax errors on Python 2.6
|
|
|
|
|
# (__future__ import following after a regular import).
|
|
|
|
|
offset = offset or 0
|
|
|
|
|
|
|
|
|
|
print(result)
|
|
|
|
|
result = "%s:%d:%d: %s: %s" % (path, lineno, offset, extype.__name__, safe_message(message))
|
|
|
|
|
|
|
|
|
|
if sys.version_info <= (3,):
|
|
|
|
|
result = result.encode(ENCODING, ERRORS)
|
|
|
|
|
|
|
|
|
|
print(result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def safe_message(value):
|
|
|
|
|