Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(62073)

Delta Between Two Patch Sets: Lib/fileinput.py

Issue 25788: fileinput.hook_encoded has no way to pass arguments to codecs
Left Patch Set: Created 3 years, 8 months ago
Right Patch Set: Created 3 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 """Helper class to quickly write a loop over all standard input files. 1 """Helper class to quickly write a loop over all standard input files.
2 2
3 Typical use is: 3 Typical use is:
4 4
5 import fileinput 5 import fileinput
6 for line in fileinput.input(): 6 for line in fileinput.input():
7 process(line) 7 process(line)
8 8
9 This iterates over the lines of all files listed in sys.argv[1:], 9 This iterates over the lines of all files listed in sys.argv[1:],
10 defaulting to sys.stdin if the list is empty. If a filename is '-' it 10 defaulting to sys.stdin if the list is empty. If a filename is '-' it
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 if ext == '.gz': 405 if ext == '.gz':
406 import gzip 406 import gzip
407 return gzip.open(filename, mode) 407 return gzip.open(filename, mode)
408 elif ext == '.bz2': 408 elif ext == '.bz2':
409 import bz2 409 import bz2
410 return bz2.BZ2File(filename, mode) 410 return bz2.BZ2File(filename, mode)
411 else: 411 else:
412 return open(filename, mode) 412 return open(filename, mode)
413 413
414 414
415 def hook_encoded(encoding, errors='strict'): 415 def hook_encoded(encoding, errors=None):
storchaka 2016/04/25 21:03:01 Default "errors" value for open() is None.
416 def openhook(filename, mode): 416 def openhook(filename, mode):
417 return open(filename, mode, encoding=encoding, errors=errors) 417 return open(filename, mode, encoding=encoding, errors=errors)
418 return openhook 418 return openhook
419 419
420 420
421 def _test(): 421 def _test():
422 import getopt 422 import getopt
423 inplace = False 423 inplace = False
424 backup = False 424 backup = False
425 opts, args = getopt.getopt(sys.argv[1:], "ib:") 425 opts, args = getopt.getopt(sys.argv[1:], "ib:")
426 for o, a in opts: 426 for o, a in opts:
427 if o == '-i': inplace = True 427 if o == '-i': inplace = True
428 if o == '-b': backup = a 428 if o == '-b': backup = a
429 for line in input(args, inplace=inplace, backup=backup): 429 for line in input(args, inplace=inplace, backup=backup):
430 if line[-1:] == '\n': line = line[:-1] 430 if line[-1:] == '\n': line = line[:-1]
431 if line[-1:] == '\r': line = line[:-1] 431 if line[-1:] == '\r': line = line[:-1]
432 print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(), 432 print("%d: %s[%d]%s %s" % (lineno(), filename(), filelineno(),
433 isfirstline() and "*" or "", line)) 433 isfirstline() and "*" or "", line))
434 print("%d: %s[%d]" % (lineno(), filename(), filelineno())) 434 print("%d: %s[%d]" % (lineno(), filename(), filelineno()))
435 435
436 if __name__ == '__main__': 436 if __name__ == '__main__':
437 _test() 437 _test()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+