summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-02-09 10:21:52 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-02-09 10:21:52 (GMT)
commit33182807f8daefdf4455c3190586f5a2c299820b (patch)
tree9608510297776818ae850fccfd1d4426f3cd5cd6
parent19a5214da47623fa3b8a8e321efe04584e2563c7 (diff)
parentbbbbe8eb600a9efa4c3a13ae49dcdcdbb051f22a (diff)
downloadcpython-33182807f8daefdf4455c3190586f5a2c299820b.zip
cpython-33182807f8daefdf4455c3190586f5a2c299820b.tar.gz
cpython-33182807f8daefdf4455c3190586f5a2c299820b.tar.bz2
Issue #10355: SpooledTemporaryFile properties now work for unrolled files.
Remove obsoleted xreadline method.
-rw-r--r--Lib/tempfile.py30
-rw-r--r--Lib/test/test_tempfile.py39
-rw-r--r--Misc/NEWS4
3 files changed, 63 insertions, 10 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index acd717b..61b0df1 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -537,7 +537,12 @@ class SpooledTemporaryFile:
@property
def encoding(self):
- return self._file.encoding
+ try:
+ return self._file.encoding
+ except AttributeError:
+ if 'b' in self._TemporaryFileArgs['mode']:
+ raise
+ return self._TemporaryFileArgs['encoding']
def fileno(self):
self.rollover()
@@ -551,18 +556,26 @@ class SpooledTemporaryFile:
@property
def mode(self):
- return self._file.mode
+ try:
+ return self._file.mode
+ except AttributeError:
+ return self._TemporaryFileArgs['mode']
@property
def name(self):
- return self._file.name
+ try:
+ return self._file.name
+ except AttributeError:
+ return None
@property
def newlines(self):
- return self._file.newlines
-
- def next(self):
- return self._file.next
+ try:
+ return self._file.newlines
+ except AttributeError:
+ if 'b' in self._TemporaryFileArgs['mode']:
+ raise
+ return self._TemporaryFileArgs['newline']
def read(self, *args):
return self._file.read(*args)
@@ -603,9 +616,6 @@ class SpooledTemporaryFile:
self._check(file)
return rv
- def xreadlines(self, *args):
- return self._file.xreadlines(*args)
-
class TemporaryDirectory(object):
"""Create and return a temporary directory. This has the same
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index d79f319..40e5e6f 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -745,6 +745,26 @@ class TestSpooledTemporaryFile(BaseTestCase):
seek(0, 0)
self.assertEqual(read(70), b'a'*35 + b'b'*35)
+ def test_properties(self):
+ f = tempfile.SpooledTemporaryFile(max_size=10)
+ f.write(b'x' * 10)
+ self.assertFalse(f._rolled)
+ self.assertEqual(f.mode, 'w+b')
+ self.assertIsNone(f.name)
+ with self.assertRaises(AttributeError):
+ f.newlines
+ with self.assertRaises(AttributeError):
+ f.encoding
+
+ f.write(b'x')
+ self.assertTrue(f._rolled)
+ self.assertEqual(f.mode, 'rb+')
+ self.assertIsNotNone(f.name)
+ with self.assertRaises(AttributeError):
+ f.newlines
+ with self.assertRaises(AttributeError):
+ f.encoding
+
def test_text_mode(self):
# Creating a SpooledTemporaryFile with a text mode should produce
# a file object reading and writing (Unicode) text strings.
@@ -755,6 +775,12 @@ class TestSpooledTemporaryFile(BaseTestCase):
f.write("def\n")
f.seek(0)
self.assertEqual(f.read(), "abc\ndef\n")
+ self.assertFalse(f._rolled)
+ self.assertEqual(f.mode, 'w+')
+ self.assertIsNone(f.name)
+ self.assertIsNone(f.newlines)
+ self.assertIsNone(f.encoding)
+
f.write("xyzzy\n")
f.seek(0)
self.assertEqual(f.read(), "abc\ndef\nxyzzy\n")
@@ -762,6 +788,11 @@ class TestSpooledTemporaryFile(BaseTestCase):
f.write("foo\x1abar\n")
f.seek(0)
self.assertEqual(f.read(), "abc\ndef\nxyzzy\nfoo\x1abar\n")
+ self.assertTrue(f._rolled)
+ self.assertEqual(f.mode, 'w+')
+ self.assertIsNotNone(f.name)
+ self.assertEqual(f.newlines, '\n')
+ self.assertIsNotNone(f.encoding)
def test_text_newline_and_encoding(self):
f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10,
@@ -770,11 +801,19 @@ class TestSpooledTemporaryFile(BaseTestCase):
f.seek(0)
self.assertEqual(f.read(), "\u039B\r\n")
self.assertFalse(f._rolled)
+ self.assertEqual(f.mode, 'w+')
+ self.assertIsNone(f.name)
+ self.assertIsNone(f.newlines)
+ self.assertIsNone(f.encoding)
f.write("\u039B" * 20 + "\r\n")
f.seek(0)
self.assertEqual(f.read(), "\u039B\r\n" + ("\u039B" * 20) + "\r\n")
self.assertTrue(f._rolled)
+ self.assertEqual(f.mode, 'w+')
+ self.assertIsNotNone(f.name)
+ self.assertIsNotNone(f.newlines)
+ self.assertEqual(f.encoding, 'utf-8')
def test_context_manager_before_rollover(self):
# A SpooledTemporaryFile can be used as a context manager
diff --git a/Misc/NEWS b/Misc/NEWS
index 57abcde..095d247 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -169,6 +169,10 @@ Core and Builtins
Library
-------
+- Issue #10355: In SpooledTemporaryFile class mode, name, encoding and
+ newlines properties now work for unrolled files. Obsoleted and never
+ working on Python 3 xreadline method now removed.
+
- Issue #16686: Fixed a lot of bugs in audioop module. Fixed crashes in
avgpp(), maxpp() and ratecv(). Fixed an integer overflow in add(), bias(),
and ratecv(). reverse(), lin2lin() and ratecv() no more lose precision for