| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,7 +40,7 @@ def test_encoding_error(self): | |||
| 40 | 40 | out, err, rc = run(['-e', 'utf8', f.name]) | |
| 41 | 41 | ||
| 42 | 42 | # Text after : ... can differ between Python versions | |
| 43 | - self.assertRegex(err, '^Unable to decode input: ') | ||
| 43 | + self.assertRegex(err, '^Unable to decode input line ') | ||
| 44 | 44 | self.assertEqual(rc, 1) | |
| 45 | 45 | ||
| 46 | 46 | def test_file_specified_encoding(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,5 +1,6 @@ | |||
| 1 | 1 | # vim:ts=4 sw=4 expandtab softtabstop=4 | |
| 2 | 2 | import argparse | |
| 3 | + import io | ||
| 3 | 4 | import locale | |
| 4 | 5 | import os | |
| 5 | 6 | import sys | |
@@ -30,20 +31,21 @@ def main(): | |||
| 30 | 31 | if args.text: | |
| 31 | 32 | fatal("Can't use both FILE and -c option") | |
| 32 | 33 | else: | |
| 33 | - with open(args.path, 'rb') as f: | ||
| 34 | - stream = f.read() | ||
| 34 | + stream = open(args.path, 'rb') | ||
| 35 | 35 | elif args.text: | |
| 36 | - stream = os.fsencode(args.text) | ||
| 36 | + text = os.fsencode(args.text) | ||
| 37 | 37 | # add a newline to the string if it comes from the | |
| 38 | 38 | # command line so that the result is printed nicely | |
| 39 | 39 | # on the console. | |
| 40 | - stream += b'\n' | ||
| 40 | + stream = io.BytesIO(text + b'\n') | ||
| 41 | 41 | else: | |
| 42 | - stream = sys.stdin.buffer.read() | ||
| 42 | + stream = sys.stdin.buffer | ||
| 43 | 43 | ||
| 44 | - try: | ||
| 45 | - stream = stream.decode(encoding) | ||
| 46 | - except UnicodeDecodeError as e: | ||
| 47 | - fatal('Unable to decode input: %s, start: %d, end: %d' % (e.reason, e.start, e.end)) | ||
| 44 | + for line_nr, line in enumerate(stream): | ||
| 45 | + try: | ||
| 46 | + line = line.decode(encoding) | ||
| 47 | + except UnicodeDecodeError as e: | ||
| 48 | + fatal('Unable to decode input line %s: %s, start: %d, end: %d' % (line_nr, e.reason, e.start, e.end)) | ||
| 48 | 49 | ||
| 49 | - sys.stdout.write(unidecode(stream)) | ||
| 50 | + sys.stdout.write(unidecode(line)) | ||
| 51 | + stream.close() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments