25a977
diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py
25a977
index ea9d061..a1fba95 100644
25a977
--- a/numpy/core/tests/test_memmap.py
25a977
+++ b/numpy/core/tests/test_memmap.py
25a977
@@ -1,5 +1,5 @@
25a977
 import sys
25a977
-from tempfile import NamedTemporaryFile, mktemp
25a977
+from tempfile import NamedTemporaryFile
25a977
 import os
25a977
 
25a977
 from numpy import memmap
25a977
@@ -31,12 +31,11 @@ class TestMemmap(TestCase):
25a977
         assert_array_equal(self.data, newfp)
25a977
 
25a977
     def test_open_with_filename(self):
25a977
-        tmpname = mktemp('','mmap')
25a977
-        fp = memmap(tmpname, dtype=self.dtype, mode='w+',
25a977
-                       shape=self.shape)
25a977
-        fp[:] = self.data[:]
25a977
-        del fp
25a977
-        os.unlink(tmpname)
25a977
+        with NamedTemporaryFile() as tmp:
25a977
+            fp = memmap(tmp.name, dtype=self.dtype, mode='w+',
25a977
+                        shape=self.shape)
25a977
+            fp[:] = self.data[:]
25a977
+            del fp
25a977
 
25a977
     def test_attributes(self):
25a977
         offset = 1
25a977
@@ -48,17 +47,16 @@ class TestMemmap(TestCase):
25a977
         del fp
25a977
 
25a977
     def test_filename(self):
25a977
-        tmpname = mktemp('','mmap')
25a977
-        fp = memmap(tmpname, dtype=self.dtype, mode='w+',
25a977
-                       shape=self.shape)
25a977
-        abspath = os.path.abspath(tmpname)
25a977
-        fp[:] = self.data[:]
25a977
-        self.assertEqual(abspath, fp.filename)
25a977
-        b = fp[:1]
25a977
-        self.assertEqual(abspath, b.filename)
25a977
-        del b
25a977
-        del fp
25a977
-        os.unlink(tmpname)
25a977
+        with NamedTemporaryFile() as tmp:
25a977
+            fp = memmap(tmp.name, dtype=self.dtype, mode='w+',
25a977
+                        shape=self.shape)
25a977
+            abspath = os.path.abspath(tmp.name)
25a977
+            fp[:] = self.data[:]
25a977
+            self.assertEqual(abspath, fp.filename)
25a977
+            b = fp[:1]
25a977
+            self.assertEqual(abspath, b.filename)
25a977
+            del b
25a977
+            del fp
25a977
 
25a977
     def test_filename_fileobj(self):
25a977
         fp = memmap(self.tmpfp, dtype=self.dtype, mode="w+",
25a977
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
25a977
index db220ec..e845c07 100644
25a977
--- a/numpy/core/tests/test_multiarray.py
25a977
+++ b/numpy/core/tests/test_multiarray.py
25a977
@@ -1587,12 +1587,11 @@ class TestIO(object):
25a977
         self.x = rand(shape) + rand(shape).astype(np.complex)*1j
25a977
         self.x[0,:,1] = [nan, inf, -inf, nan]
25a977
         self.dtype = self.x.dtype
25a977
-        self.filename = tempfile.mktemp()
25a977
+        self.file = tempfile.NamedTemporaryFile()
25a977
+        self.filename = self.file.name
25a977
 
25a977
     def tearDown(self):
25a977
-        if os.path.isfile(self.filename):
25a977
-            os.unlink(self.filename)
25a977
-            #tmp_file.close()
25a977
+        self.file.close()
25a977
 
25a977
     def test_bool_fromstring(self):
25a977
         v = np.array([True,False,True,False], dtype=np.bool_)
25a977
@@ -1620,7 +1619,6 @@ class TestIO(object):
25a977
         y = np.fromfile(f, dtype=self.dtype)
25a977
         f.close()
25a977
         assert_array_equal(y, self.x.flat)
25a977
-        os.unlink(self.filename)
25a977
 
25a977
     def test_roundtrip_filename(self):
25a977
         self.x.tofile(self.filename)
25a977
@@ -1753,7 +1751,6 @@ class TestIO(object):
25a977
         s = f.read()
25a977
         f.close()
25a977
         assert_equal(s, '1.51,2.0,3.51,4.0')
25a977
-        os.unlink(self.filename)
25a977
 
25a977
     def test_tofile_format(self):
25a977
         x = np.array([1.51, 2, 3.51, 4], dtype=float)
25a977
diff --git a/numpy/f2py/__init__.py b/numpy/f2py/__init__.py
25a977
index 220cb3d..d580332 100644
25a977
--- a/numpy/f2py/__init__.py
25a977
+++ b/numpy/f2py/__init__.py
25a977
@@ -27,20 +27,20 @@ def compile(source,
25a977
     from numpy.distutils.exec_command import exec_command
25a977
     import tempfile
25a977
     if source_fn is None:
25a977
-        fname = os.path.join(tempfile.mktemp()+'.f')
25a977
+        f = tempfile.NamedTemporaryFile(suffix='.f')
25a977
     else:
25a977
-        fname = source_fn
25a977
-
25a977
-    f = open(fname,'w')
25a977
-    f.write(source)
25a977
-    f.close()
25a977
-
25a977
-    args = ' -c -m %s %s %s'%(modulename,fname,extra_args)
25a977
-    c = '%s -c "import numpy.f2py as f2py2e;f2py2e.main()" %s' %(sys.executable,args)
25a977
-    s,o = exec_command(c)
25a977
-    if source_fn is None:
25a977
-        try: os.remove(fname)
25a977
-        except OSError: pass
25a977
+        f = open(source_fn, 'w')
25a977
+
25a977
+    try:
25a977
+        f.write(source)
25a977
+        f.flush()
25a977
+
25a977
+        args = ' -c -m %s %s %s'%(modulename, f.name, extra_args)
25a977
+        c = '%s -c "import numpy.f2py as f2py2e;f2py2e.main()" %s' % \
25a977
+                (sys.executable, args)
25a977
+        s, o = exec_command(c)
25a977
+    finally:
25a977
+        f.close()
25a977
     return s
25a977
 
25a977
 from numpy.testing import Tester
25a977
diff --git a/numpy/f2py/f2py2e.py b/numpy/f2py/f2py2e.py
25a977
index 4e6d258..b9b955a 100755
25a977
--- a/numpy/f2py/f2py2e.py
25a977
+++ b/numpy/f2py/f2py2e.py
25a977
@@ -91,7 +91,7 @@ Options:
25a977
                    --lower is assumed with -h key, and --no-lower without -h key.
25a977
 
25a977
   --build-dir <dirname>  All f2py generated files are created in <dirname>.
25a977
-                   Default is tempfile.mktemp().
25a977
+                   Default is tempfile.mkstemp().
25a977
 
25a977
   --overwrite-signature  Overwrite existing signature file.
25a977
 
25a977
@@ -428,7 +428,7 @@ def run_compile():
25a977
         del sys.argv[i]
25a977
     else:
25a977
         remove_build_dir = 1
25a977
-        build_dir = os.path.join(tempfile.mktemp())
25a977
+        build_dir = tempfile.mkdtemp()
25a977
 
25a977
     sysinfo_flags = filter(re.compile(r'[-][-]link[-]').match,sys.argv[1:])
25a977
     sys.argv = filter(lambda a,flags=sysinfo_flags:a not in flags,sys.argv)
25a977
-- 
25a977
1.8.5.3
25a977