diff options
author | sth <sth.dev@tejp.de> | 2018-05-23 05:07:01 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2018-05-23 05:07:01 (GMT) |
commit | 825aab95fde959541859383f8ea7e7854ebfd49f (patch) | |
tree | 727aafb15e3d3054ccd279f732b715aae74f899d /Lib | |
parent | 0e61dffdbaf39ac5916ce431ff3b37db8faa1d2d (diff) | |
download | cpython-825aab95fde959541859383f8ea7e7854ebfd49f.zip cpython-825aab95fde959541859383f8ea7e7854ebfd49f.tar.gz cpython-825aab95fde959541859383f8ea7e7854ebfd49f.tar.bz2 |
bpo-27300: Add the errors parameter to tempfile classes. (GH-6696)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/tempfile.py | 35 | ||||
-rw-r--r-- | Lib/test/test_tempfile.py | 11 |
2 files changed, 26 insertions, 20 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 2143224..e6fb3c8 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -519,7 +519,7 @@ class _TemporaryFileWrapper: def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, - dir=None, delete=True): + dir=None, delete=True, *, errors=None): """Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. @@ -528,6 +528,7 @@ def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 'delete' -- whether the file is deleted on close (default True). + 'errors' -- the errors argument to io.open (default None) The file is created as mkstemp() would do it. Returns an object with a file-like interface; the name of the file @@ -547,7 +548,7 @@ def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None, (fd, name) = _mkstemp_inner(dir, prefix, suffix, flags, output_type) try: file = _io.open(fd, mode, buffering=buffering, - newline=newline, encoding=encoding) + newline=newline, encoding=encoding, errors=errors) return _TemporaryFileWrapper(file, name, delete) except BaseException: @@ -568,7 +569,7 @@ else: def TemporaryFile(mode='w+b', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, - dir=None): + dir=None, *, errors=None): """Create and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. @@ -576,6 +577,7 @@ else: 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) + 'errors' -- the errors argument to io.open (default None) The file is created as mkstemp() would do it. Returns an object with a file-like interface. The file has no @@ -609,7 +611,8 @@ else: else: try: return _io.open(fd, mode, buffering=buffering, - newline=newline, encoding=encoding) + newline=newline, encoding=encoding, + errors=errors) except: _os.close(fd) raise @@ -619,7 +622,7 @@ else: try: _os.unlink(name) return _io.open(fd, mode, buffering=buffering, - newline=newline, encoding=encoding) + newline=newline, encoding=encoding, errors=errors) except: _os.close(fd) raise @@ -633,7 +636,7 @@ class SpooledTemporaryFile: def __init__(self, max_size=0, mode='w+b', buffering=-1, encoding=None, newline=None, - suffix=None, prefix=None, dir=None): + suffix=None, prefix=None, dir=None, *, errors=None): if 'b' in mode: self._file = _io.BytesIO() else: @@ -646,7 +649,7 @@ class SpooledTemporaryFile: self._TemporaryFileArgs = {'mode': mode, 'buffering': buffering, 'suffix': suffix, 'prefix': prefix, 'encoding': encoding, 'newline': newline, - 'dir': dir} + 'dir': dir, 'errors': errors} def _check(self, file): if self._rolled: return @@ -692,12 +695,11 @@ class SpooledTemporaryFile: @property def encoding(self): - try: - return self._file.encoding - except AttributeError: - if 'b' in self._TemporaryFileArgs['mode']: - raise - return self._TemporaryFileArgs['encoding'] + return self._file.encoding + + @property + def errors(self): + return self._file.errors def fileno(self): self.rollover() @@ -725,12 +727,7 @@ class SpooledTemporaryFile: @property def newlines(self): - try: - return self._file.newlines - except AttributeError: - if 'b' in self._TemporaryFileArgs['mode']: - raise - return self._TemporaryFileArgs['newline'] + return self._file.newlines def read(self, *args): return self._file.read(*args) diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 710756b..e5098d2 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -1094,6 +1094,8 @@ class TestSpooledTemporaryFile(BaseTestCase): f.newlines with self.assertRaises(AttributeError): f.encoding + with self.assertRaises(AttributeError): + f.errors f.write(b'x') self.assertTrue(f._rolled) @@ -1103,6 +1105,8 @@ class TestSpooledTemporaryFile(BaseTestCase): f.newlines with self.assertRaises(AttributeError): f.encoding + with self.assertRaises(AttributeError): + f.errors def test_text_mode(self): # Creating a SpooledTemporaryFile with a text mode should produce @@ -1119,6 +1123,7 @@ class TestSpooledTemporaryFile(BaseTestCase): self.assertIsNone(f.name) self.assertIsNone(f.newlines) self.assertIsNone(f.encoding) + self.assertIsNone(f.errors) f.write("xyzzy\n") f.seek(0) @@ -1132,10 +1137,12 @@ class TestSpooledTemporaryFile(BaseTestCase): self.assertIsNotNone(f.name) self.assertEqual(f.newlines, os.linesep) self.assertIsNotNone(f.encoding) + self.assertIsNotNone(f.errors) def test_text_newline_and_encoding(self): f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10, - newline='', encoding='utf-8') + newline='', encoding='utf-8', + errors='ignore') f.write("\u039B\r\n") f.seek(0) self.assertEqual(f.read(), "\u039B\r\n") @@ -1144,6 +1151,7 @@ class TestSpooledTemporaryFile(BaseTestCase): self.assertIsNone(f.name) self.assertIsNone(f.newlines) self.assertIsNone(f.encoding) + self.assertIsNone(f.errors) f.write("\u039B" * 20 + "\r\n") f.seek(0) @@ -1153,6 +1161,7 @@ class TestSpooledTemporaryFile(BaseTestCase): self.assertIsNotNone(f.name) self.assertIsNotNone(f.newlines) self.assertEqual(f.encoding, 'utf-8') + self.assertEqual(f.errors, 'ignore') def test_context_manager_before_rollover(self): # A SpooledTemporaryFile can be used as a context manager |