summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSteven Bethard <steven.bethard@gmail.com>2011-01-24 20:40:15 (GMT)
committerSteven Bethard <steven.bethard@gmail.com>2011-01-24 20:40:15 (GMT)
commitf8583acb534432097671e79eb4110b9861dd2e17 (patch)
tree1287cc0567d1e65b5b6edfc868a5fa0f577f7cff /Lib/test
parentcdb8388cad74e0f2910d5b43531daf6271467292 (diff)
downloadcpython-f8583acb534432097671e79eb4110b9861dd2e17.zip
cpython-f8583acb534432097671e79eb4110b9861dd2e17.tar.gz
cpython-f8583acb534432097671e79eb4110b9861dd2e17.tar.bz2
Issue #9509: make argarse properly handle IOErrors raised by argparse.FileType. Approved by Georg in the tracker.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_argparse.py23
1 files changed, 15 insertions, 8 deletions
diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 3e71f57..50a6d49 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -4,6 +4,7 @@ import codecs
import inspect
import os
import shutil
+import stat
import sys
import textwrap
import tempfile
@@ -46,14 +47,13 @@ class TempDirMixin(object):
def tearDown(self):
os.chdir(self.old_dir)
- while True:
- try:
- shutil.rmtree(self.temp_dir)
- except WindowsError:
- continue
- else:
- break
+ shutil.rmtree(self.temp_dir, True)
+ def create_readonly_file(self, filename):
+ file_path = os.path.join(self.temp_dir, filename)
+ with open(file_path, 'w') as file:
+ file.write(filename)
+ os.chmod(file_path, stat.S_IREAD)
class Sig(object):
@@ -1451,17 +1451,19 @@ class TestFileTypeR(TempDirMixin, ParserTestCase):
file = open(os.path.join(self.temp_dir, file_name), 'w')
file.write(file_name)
file.close()
+ self.create_readonly_file('readonly')
argument_signatures = [
Sig('-x', type=argparse.FileType()),
Sig('spam', type=argparse.FileType('r')),
]
- failures = ['-x', '-x bar']
+ failures = ['-x', '-x bar', 'non-existent-file.txt']
successes = [
('foo', NS(x=None, spam=RFile('foo'))),
('-x foo bar', NS(x=RFile('foo'), spam=RFile('bar'))),
('bar -x foo', NS(x=RFile('foo'), spam=RFile('bar'))),
('-x - -', NS(x=sys.stdin, spam=sys.stdin)),
+ ('readonly', NS(x=None, spam=RFile('readonly'))),
]
@@ -1510,11 +1512,16 @@ class WFile(object):
class TestFileTypeW(TempDirMixin, ParserTestCase):
"""Test the FileType option/argument type for writing files"""
+ def setUp(self):
+ super(TestFileTypeW, self).setUp()
+ self.create_readonly_file('readonly')
+
argument_signatures = [
Sig('-x', type=argparse.FileType('w')),
Sig('spam', type=argparse.FileType('w')),
]
failures = ['-x', '-x bar']
+ failures = ['-x', '-x bar', 'readonly']
successes = [
('foo', NS(x=None, spam=WFile('foo'))),
('-x foo bar', NS(x=WFile('foo'), spam=WFile('bar'))),