FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Read input line by line · risclog-solution/unidecode@fd6f0bd · GitHub

Repository navigation

Commit fd6f0bd

Browse files
committed
Read input line by line
1 parent 1c74053 commit fd6f0bd

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

‎tests/test_utility.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_encoding_error(self):
4040
out, err, rc = run(['-e', 'utf8', f.name])
4141

4242
# Text after : ... can differ between Python versions
43-
self.assertRegex(err, '^Unable to decode input: ')
43+
self.assertRegex(err, '^Unable to decode input line ')
4444
self.assertEqual(rc, 1)
4545

4646
def test_file_specified_encoding(self):

‎unidecode/util.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# vim:ts=4 sw=4 expandtab softtabstop=4
22
import argparse
3+
import io
34
import locale
45
import os
56
import sys
@@ -30,20 +31,21 @@ def main():
3031
if args.text:
3132
fatal("Can't use both FILE and -c option")
3233
else:
33-
with open(args.path, 'rb') as f:
34-
stream = f.read()
34+
stream = open(args.path, 'rb')
3535
elif args.text:
36-
stream = os.fsencode(args.text)
36+
text = os.fsencode(args.text)
3737
# add a newline to the string if it comes from the
3838
# command line so that the result is printed nicely
3939
# on the console.
40-
stream += b'\n'
40+
stream = io.BytesIO(text + b'\n')
4141
else:
42-
stream = sys.stdin.buffer.read()
42+
stream = sys.stdin.buffer
4343

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))
4849

49-
sys.stdout.write(unidecode(stream))
50+
sys.stdout.write(unidecode(line))
51+
stream.close()

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL