Commits

Armin Rigo committed ef71652

Implement os.tmpnam() and os.tempnam() at app-level,
by calling the 'tempfile' module.

Comments (0)

Files changed (3)

pypy/module/posix/__init__.py

     'fdopen'     : 'app_posix.fdopen',
     'tmpfile'    : 'app_posix.tmpfile',
     'popen'      : 'app_posix.popen',
+    'tmpnam'     : 'app_posix.tmpnam',
+    'tempnam'    : 'app_posix.tempnam',
     }
     if os.name == 'nt':
         appleveldefs.update({

pypy/module/posix/app_posix.py

     return f
 
 
+def tmpnam():
+    """Return an absolute pathname of a file that did not exist at the
+    time the call is made."""
+    import tempfile
+    return tempfile.mktemp()
+
+def tempnam(dir=None, prefix=None):
+    """Return an absolute pathname of a file that did not exist at the
+    time the call is made.  The directory and a prefix may be specified
+    as strings; they may be omitted or None if not needed."""
+    import tempfile
+    return tempfile.mktemp('', prefix or 'tmp', dir)
+
+
 # Implement popen() for platforms which have os.fork()
 if osname == 'posix':
 

pypy/module/posix/test/test_posix2.py

         if hasattr(os, 'major'):
             cls.w_expected_major_12345 = space.wrap(os.major(12345))
             cls.w_expected_minor_12345 = space.wrap(os.minor(12345))
+        cls.w_udir = space.wrap(str(udir))
 
     def setup_method(self, meth):
         if getattr(meth, 'need_sparse_files', False):
             assert os.WIFEXITED(status1)
             assert os.WEXITSTATUS(status1) == myprio + 3
 
+    def test_tmpfile(self):
+        os = self.posix
+        f = os.tmpfile()
+        f.write("xxx")
+        f.flush()
+        f.seek(0, 0)
+        assert isinstance(f, file)
+        assert f.read() == 'xxx'
+
+    def test_tmpnam(self):
+        import stat, os
+        s1 = os.tmpnam()
+        s2 = os.tmpnam()
+        assert s1 != s2
+        def isdir(s):
+            try:
+                return stat.S_ISDIR(os.stat(s).st_mode)
+            except OSError:
+                return -1
+        assert isdir(s1) == -1
+        assert isdir(s2) == -1
+        assert isdir(os.path.dirname(s1)) == 1
+        assert isdir(os.path.dirname(s2)) == 1
+
+    def test_tempnam(self):
+        import stat, os
+        for dir in [None, self.udir]:
+            for prefix in [None, 'foobar']:
+                s1 = os.tempnam(dir, prefix)
+                s2 = os.tempnam(dir, prefix)
+                assert s1 != s2
+                def isdir(s):
+                    try:
+                        return stat.S_ISDIR(os.stat(s).st_mode)
+                    except OSError:
+                        return -1
+                assert isdir(s1) == -1
+                assert isdir(s2) == -1
+                assert isdir(os.path.dirname(s1)) == 1
+                assert isdir(os.path.dirname(s2)) == 1
+                if dir:
+                    assert os.path.dirname(s1) == dir
+                    assert os.path.dirname(s2) == dir
+                assert os.path.basename(s1).startswith(prefix or 'tmp')
+                assert os.path.basename(s2).startswith(prefix or 'tmp')
+
 
 class AppTestEnvironment(object):
     def setup_class(cls): 
             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 AppTestPosixUnicode:
 
     def setup_class(cls):