summaryrefslogtreecommitdiffstats
path: root/Lib/argparse.py
diff options
context:
space:
mode:
authorSteven Bethard <steven.bethard@gmail.com>2011-01-24 21:02:50 (GMT)
committerSteven Bethard <steven.bethard@gmail.com>2011-01-24 21:02:50 (GMT)
commitb02701101b94bda88179652b0133557baa801368 (patch)
tree4e39189e5645d3c9e8bf68472e89998dbf5e83a4 /Lib/argparse.py
parent2a6ac15f263c58842c5be9398da9cc78d3516988 (diff)
downloadcpython-b02701101b94bda88179652b0133557baa801368.zip
cpython-b02701101b94bda88179652b0133557baa801368.tar.gz
cpython-b02701101b94bda88179652b0133557baa801368.tar.bz2
Issue #9509: make argarse properly handle IOErrors raised by argparse.FileType. Approved by Georg in the tracker.
Diffstat (limited to 'Lib/argparse.py')
-rw-r--r--Lib/argparse.py13
1 files changed, 7 insertions, 6 deletions
diff --git a/Lib/argparse.py b/Lib/argparse.py
index 57eaaad..5fd82da 100644
--- a/Lib/argparse.py
+++ b/Lib/argparse.py
@@ -1121,7 +1121,7 @@ class FileType(object):
the builtin open() function.
"""
- def __init__(self, mode='r', bufsize=None):
+ def __init__(self, mode='r', bufsize=-1):
self._mode = mode
self._bufsize = bufsize
@@ -1137,14 +1137,15 @@ class FileType(object):
raise ValueError(msg)
# all other arguments are used as file names
- if self._bufsize:
+ try:
return open(string, self._mode, self._bufsize)
- else:
- return open(string, self._mode)
+ 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 is not None])
+ args = self._mode, self._bufsize
+ args_str = ', '.join(repr(arg) for arg in args if arg != -1)
return '%s(%s)' % (type(self).__name__, args_str)
# ===========================