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