[pypy-svn] r46347 - in pypy/dist/pypy/module/posix: . test

arigo at codespeak.net arigo at codespeak.net
Wed Sep 5 16:07:02 CEST 2007


Author: arigo
Date: Wed Sep  5 16:07:01 2007
New Revision: 46347

Modified:
   pypy/dist/pypy/module/posix/__init__.py
   pypy/dist/pypy/module/posix/app_posix.py
   pypy/dist/pypy/module/posix/test/test_posix2.py
Log:
This is a replacement for the revision 45516 and 45517:
a pure app-level implementation of os.tmpfile() based
on the 'tempfile' module.  The idea is that it nicely
avoids the hacks that were needed in 45516 and 45517.


Modified: pypy/dist/pypy/module/posix/__init__.py
==============================================================================
--- pypy/dist/pypy/module/posix/__init__.py	(original)
+++ pypy/dist/pypy/module/posix/__init__.py	Wed Sep  5 16:07:01 2007
@@ -17,6 +17,7 @@
     'error'      : 'app_posix.error',
     'stat_result': 'app_posix.stat_result',
     'fdopen'     : 'app_posix.fdopen',
+    'tmpfile'    : 'app_posix.tmpfile',
     }
     
     interpleveldefs = {

Modified: pypy/dist/pypy/module/posix/app_posix.py
==============================================================================
--- pypy/dist/pypy/module/posix/app_posix.py	(original)
+++ pypy/dist/pypy/module/posix/app_posix.py	Wed Sep  5 16:07:01 2007
@@ -6,8 +6,10 @@
 import sys
 if 'posix' in sys.builtin_module_names:
     import posix
+    osname = 'posix'
 elif 'nt' in sys.builtin_module_names:
     import nt as posix
+    osname = 'nt'
 else:
     raise ImportError("XXX")
 
@@ -56,6 +58,19 @@
     return file.fdopen(fd, mode, buffering)
 
 
+def tmpfile():
+    """Create a temporary file.
+
+    The data in the file is freed when you
+    close the file, or automatically by the OS when the program ends."""
+    import tempfile
+    f = tempfile.TemporaryFile()
+    if osname == 'nt':
+        f = f.file     # on NT, with the 2.4 stdlib of CPython,
+                       # we get a _TemporaryFileWrapper for no good reason
+    return f
+
+
 # __________ only if we have os.fork() __________
 
 class popenfile(file):

Modified: pypy/dist/pypy/module/posix/test/test_posix2.py
==============================================================================
--- pypy/dist/pypy/module/posix/test/test_posix2.py	(original)
+++ pypy/dist/pypy/module/posix/test/test_posix2.py	Wed Sep  5 16:07:01 2007
@@ -328,6 +328,15 @@
             res = os.system(cmd)
             assert res == 0
 
+    def test_tmpfile(self):
+        os = self.os
+        f = os.tmpfile()
+        f.write("xxx")
+        f.flush()
+        f.seek(0, 0)
+        assert isinstance(f, file)
+        assert f.read() == 'xxx'
+
 class TestPexpect(object):
     # XXX replace with AppExpectTest class as soon as possible
     def setup_class(cls):


More information about the pypy-svn mailing list