diff options
author | Brett Cannon <brett@python.org> | 2012-04-20 17:23:54 (GMT) |
---|---|---|
committer | Brett Cannon <brett@python.org> | 2012-04-20 17:23:54 (GMT) |
commit | c33f3f2339fd3217a0c6fe3df916616abab2fab4 (patch) | |
tree | 45b2793ea426faee0669f4a6d34387cd236aff06 /Lib/tokenize.py | |
parent | dd9a56953e561076b5573d53f6e4fdd7f42b208c (diff) | |
download | cpython-c33f3f2339fd3217a0c6fe3df916616abab2fab4.zip cpython-c33f3f2339fd3217a0c6fe3df916616abab2fab4.tar.gz cpython-c33f3f2339fd3217a0c6fe3df916616abab2fab4.tar.bz2 |
Issue #14629: Mention the filename in SyntaxError exceptions from
tokenizer.detect_encoding() (when available).
Diffstat (limited to 'Lib/tokenize.py')
-rw-r--r-- | Lib/tokenize.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/Lib/tokenize.py b/Lib/tokenize.py index c05f764..e4c9d3c 100644 --- a/Lib/tokenize.py +++ b/Lib/tokenize.py @@ -353,6 +353,10 @@ def detect_encoding(readline): If no encoding is specified, then the default of 'utf-8' will be returned. """ + try: + filename = readline.__self__.name + except AttributeError: + filename = None bom_found = False encoding = None default = 'utf-8' @@ -369,7 +373,10 @@ def detect_encoding(readline): # per default encoding. line_string = line.decode('utf-8') except UnicodeDecodeError: - raise SyntaxError("invalid or missing encoding declaration") + msg = "invalid or missing encoding declaration" + if filename is not None: + msg = '{} for {!r}'.format(msg, filename) + raise SyntaxError(msg) matches = cookie_re.findall(line_string) if not matches: @@ -379,12 +386,21 @@ def detect_encoding(readline): codec = lookup(encoding) except LookupError: # This behaviour mimics the Python interpreter - raise SyntaxError("unknown encoding: " + encoding) + if filename is None: + msg = "unknown encoding: " + encoding + else: + msg = "unknown encoding for {!r}: {}".format(filename, + encoding) + raise SyntaxError(msg) if bom_found: if codec.name != 'utf-8': # This behaviour mimics the Python interpreter - raise SyntaxError('encoding problem: utf-8') + if filename is None: + msg = 'encoding problem: utf-8' + else: + msg = 'encoding problem for {!r}: utf-8'.format(filename) + raise SyntaxError(msg) encoding += '-sig' return encoding |