diff options
author | Petri Lehtinen <petri@digip.org> | 2012-12-15 22:39:32 +0200 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2012-12-15 22:42:47 +0200 |
commit | 74d6c250e1ce0e5efc03575eaaa1b8cab66241eb (patch) | |
tree | 172bc4bd59fe0d52a877d1811a7bcc3c9d50b624 /Lib/argparse.py | |
parent | 09bb89b8cf24431ef78bbaa227d2832fb852bb18 (diff) | |
download | cpython-git-74d6c250e1ce0e5efc03575eaaa1b8cab66241eb.tar.gz |
#11175: argparse.FileType now accepts encoding and errors arguments.
Patch by Lucas Maystre.
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r-- | Lib/argparse.py | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py index 202f2cb972..3b1a079552 100644 --- a/Lib/argparse.py +++ b/Lib/argparse.py @@ -1140,11 +1140,17 @@ class FileType(object): same values as the builtin open() function. - bufsize -- The file's desired buffer size. Accepts the same values as the builtin open() function. + - encoding -- The file's encoding. Accepts the same values as the + the builtin open() function. + - errors -- A string indicating how encoding and decoding errors are to + be handled. Accepts the same value as the builtin open() function. """ - def __init__(self, mode='r', bufsize=-1): + def __init__(self, mode='r', bufsize=-1, encoding=None, errors=None): self._mode = mode self._bufsize = bufsize + self._encoding = encoding + self._errors = errors def __call__(self, string): # the special argument "-" means sys.std{in,out} @@ -1159,14 +1165,18 @@ class FileType(object): # all other arguments are used as file names try: - return open(string, self._mode, self._bufsize) + return open(string, self._mode, self._bufsize, self._encoding, + self._errors) except IOError as e: message = _("can't open '%s': %s") raise ArgumentTypeError(message % (string, e)) def __repr__(self): args = self._mode, self._bufsize - args_str = ', '.join(repr(arg) for arg in args if arg != -1) + kwargs = [('encoding', self._encoding), ('errors', self._errors)] + args_str = ', '.join([repr(arg) for arg in args if arg != -1] + + ['%s=%r' % (kw, arg) for kw, arg in kwargs + if arg is not None]) return '%s(%s)' % (type(self).__name__, args_str) # =========================== |