[Python-3000-checkins] r56210 - in python/branches/py3k-struni/Lib: tempfile.py test/test_tempfile.py

guido.van.rossum python-3000-checkins at python.org
Mon Jul 9 12:24:45 CEST 2007


Author: guido.van.rossum
Date: Mon Jul  9 12:24:45 2007
New Revision: 56210

Modified:
   python/branches/py3k-struni/Lib/tempfile.py
   python/branches/py3k-struni/Lib/test/test_tempfile.py
Log:
Make test_tempfile.py work.  Make SpooledTempFile work in text and binary mode.


Modified: python/branches/py3k-struni/Lib/tempfile.py
==============================================================================
--- python/branches/py3k-struni/Lib/tempfile.py	(original)
+++ python/branches/py3k-struni/Lib/tempfile.py	Mon Jul  9 12:24:45 2007
@@ -29,6 +29,7 @@
 
 # Imports.
 
+import io as _io
 import os as _os
 import errno as _errno
 from random import Random as _Random
@@ -37,8 +38,6 @@
     import Carbon.Folder as _Folder
     import Carbon.Folders as _Folders
 
-from io import StringIO as _StringIO
-
 try:
     import fcntl as _fcntl
 except ImportError:
@@ -486,7 +485,10 @@
 
     def __init__(self, max_size=0, mode='w+b', bufsize=-1,
                  suffix="", prefix=template, dir=None):
-        self._file = _StringIO()
+        if 'b' in mode:
+            self._file = _io.BytesIO()
+        else:
+            self._file = _io.StringIO()
         self._max_size = max_size
         self._rolled = False
         self._TemporaryFileArgs = (mode, bufsize, suffix, prefix, dir)

Modified: python/branches/py3k-struni/Lib/test/test_tempfile.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_tempfile.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_tempfile.py	Mon Jul  9 12:24:45 2007
@@ -664,7 +664,7 @@
             self.failUnless(f._rolled)
             filename = f.name
             f.close()
-            self.failIf(os.path.exists(filename),
+            self.failIf(isinstance(filename, str) and os.path.exists(filename),
                         "SpooledTemporaryFile %s exists after close" % filename)
         finally:
             os.rmdir(dir)
@@ -730,7 +730,22 @@
         write("a" * 35)
         write("b" * 35)
         seek(0, 0)
-        self.assertEqual(read(70), 'a'*35 + 'b'*35)
+        self.assertEqual(read(70), b'a'*35 + b'b'*35)
+
+    def test_text_mode(self):
+        # Creating a SpooledTemporaryFile with a text mode should produce
+        # a file object reading and writing (Unicode) text strings.
+        f = tempfile.SpooledTemporaryFile(mode='w+', max_size=10)
+        f.write("abc\n")
+        f.seek(0)
+        self.assertEqual(f.read(), "abc\n")
+        f.write("def\n")
+        f.seek(0)
+        self.assertEqual(f.read(), "abc\ndef\n")
+        f.write("xyzzy\n")
+        f.seek(0)
+        self.assertEqual(f.read(), "abc\ndef\nxyzzy\n")
+
 
 test_classes.append(test_SpooledTemporaryFile)
 


More information about the Python-3000-checkins mailing list