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