Massive changes from SF 589982 (tempfile.py rewrite, by Zack
Weinberg).  This changes all uses of deprecated tempfile functions to
the recommended ones.
diff --git a/Lib/test/pickletester.py b/Lib/test/pickletester.py
index 870a8d6..eb97a9c 100644
--- a/Lib/test/pickletester.py
+++ b/Lib/test/pickletester.py
@@ -1,5 +1,5 @@
 import unittest
-from test.test_support import TestFailed, have_unicode
+from test.test_support import TestFailed, have_unicode, TESTFN
 
 class C:
     def __cmp__(self, other):
@@ -269,17 +269,19 @@
 class AbstractPickleModuleTests(unittest.TestCase):
 
     def test_dump_closed_file(self):
-        import tempfile, os
-        fn = tempfile.mktemp()
-        f = open(fn, "w")
-        f.close()
-        self.assertRaises(ValueError, self.module.dump, 123, f)
-        os.remove(fn)
+        import os
+        f = open(TESTFN, "w")
+        try:
+            f.close()
+            self.assertRaises(ValueError, self.module.dump, 123, f)
+        finally:
+            os.remove(TESTFN)
 
     def test_load_closed_file(self):
-        import tempfile, os
-        fn = tempfile.mktemp()
-        f = open(fn, "w")
-        f.close()
-        self.assertRaises(ValueError, self.module.dump, 123, f)
-        os.remove(fn)
+        import os
+        f = open(TESTFN, "w")
+        try:
+            f.close()
+            self.assertRaises(ValueError, self.module.dump, 123, f)
+        finally:
+            os.remove(TESTFN)
diff --git a/Lib/test/test_anydbm.py b/Lib/test/test_anydbm.py
index 54d2783..0cdc2c3 100644
--- a/Lib/test/test_anydbm.py
+++ b/Lib/test/test_anydbm.py
@@ -6,11 +6,10 @@
 import os
 import unittest
 import anydbm
-import tempfile
 import glob
 from test import test_support
 
-_fname = tempfile.mktemp()
+_fname = test_support.TESTFN
 
 def _delete_files():
     # we don't know the precise name the underlying database uses
diff --git a/Lib/test/test_binhex.py b/Lib/test/test_binhex.py
index 2de35ce..2f89703 100755
--- a/Lib/test/test_binhex.py
+++ b/Lib/test/test_binhex.py
@@ -6,7 +6,6 @@
 """
 import binhex
 import os
-import tempfile
 import unittest
 from test import test_support
 
@@ -14,8 +13,8 @@
 class BinHexTestCase(unittest.TestCase):
 
     def setUp(self):
-        self.fname1 = tempfile.mktemp()
-        self.fname2 = tempfile.mktemp()
+        self.fname1 = test_support.TESTFN + "1"
+        self.fname2 = test_support.TESTFN + "2"
 
     def tearDown(self):
         try: os.unlink(self.fname1)
diff --git a/Lib/test/test_bsddb.py b/Lib/test/test_bsddb.py
index 5cd3958..aa58ef8 100755
--- a/Lib/test/test_bsddb.py
+++ b/Lib/test/test_bsddb.py
@@ -5,8 +5,7 @@
 import os
 import bsddb
 import dbhash # Just so we know it's imported
-import tempfile
-from test.test_support import verbose, verify
+from test.test_support import verbose, verify, TESTFN
 
 def test(openmethod, what, ondisk=1):
 
@@ -14,7 +13,7 @@
         print '\nTesting: ', what, (ondisk and "on disk" or "in memory")
 
     if ondisk:
-        fname = tempfile.mktemp()
+        fname = TESTFN
     else:
         fname = None
     f = openmethod(fname, 'c')
diff --git a/Lib/test/test_commands.py b/Lib/test/test_commands.py
index 5ea08bc..56d4e7c 100644
--- a/Lib/test/test_commands.py
+++ b/Lib/test/test_commands.py
@@ -24,10 +24,17 @@
         self.assertEquals(getoutput('echo xyzzy'), 'xyzzy')
         self.assertEquals(getstatusoutput('echo xyzzy'), (0, 'xyzzy'))
 
-        # we use mktemp in the next line to get a filename which we
-        # _know_ won't exist.  This is guaranteed to fail.
-        status, output = getstatusoutput('cat ' + tempfile.mktemp())
-        self.assertNotEquals(status, 0)
+        # we use mkdtemp in the next line to create an empty directory
+        # under our exclusive control; from that, we can invent a pathname
+        # that we _know_ won't exist.  This is guaranteed to fail.
+        try:
+            dir = tempfile.mkdtemp()
+            name = os.path.join(dir, "foo")
+
+            status, output = getstatusoutput('cat ' + name)
+            self.assertNotEquals(status, 0)
+        finally:
+            os.rmdir(dir)
 
     def test_getstatus(self):
         # This pattern should match 'ls -ld /.' on any posix
diff --git a/Lib/test/test_dumbdbm.py b/Lib/test/test_dumbdbm.py
index 7417f12..4d1bc0e 100644
--- a/Lib/test/test_dumbdbm.py
+++ b/Lib/test/test_dumbdbm.py
@@ -6,10 +6,9 @@
 import os
 import unittest
 import dumbdbm
-import tempfile
 from test import test_support
 
-_fname = tempfile.mktemp()
+_fname = test_support.TESTFN
 
 def _delete_files():
     for ext in [".dir", ".dat", ".bak"]:
diff --git a/Lib/test/test_gzip.py b/Lib/test/test_gzip.py
index 9156d9e..bb4ed49 100644
--- a/Lib/test/test_gzip.py
+++ b/Lib/test/test_gzip.py
@@ -1,8 +1,8 @@
-from test.test_support import verify
+from test.test_support import verify, TESTFN
 import sys, os
-import gzip, tempfile
+import gzip
 
-filename = tempfile.mktemp()
+filename = TESTFN
 
 data1 = """  int length=DEFAULTALLOC, err = Z_OK;
   PyObject *RetVal;
diff --git a/Lib/test/test_netrc.py b/Lib/test/test_netrc.py
index 034b105..15e7e68 100644
--- a/Lib/test/test_netrc.py
+++ b/Lib/test/test_netrc.py
@@ -1,5 +1,5 @@
 
-import netrc, os, tempfile, unittest
+import netrc, os, unittest
 from test import test_support
 
 TEST_NETRC = """
@@ -17,7 +17,7 @@
 
 """
 
-temp_filename = tempfile.mktemp()
+temp_filename = test_support.TESTFN
 
 class NetrcTestCase(unittest.TestCase):
 
diff --git a/Lib/test/test_pkg.py b/Lib/test/test_pkg.py
index 224cefa..7dec2e9 100644
--- a/Lib/test/test_pkg.py
+++ b/Lib/test/test_pkg.py
@@ -8,7 +8,8 @@
 # Helpers to create and destroy hierarchies.
 
 def mkhier(root, descr):
-    mkdir(root)
+    if not os.path.isdir(root):
+        mkdir(root)
     for name, contents in descr:
         comps = name.split()
         fullname = root
@@ -52,18 +53,17 @@
 # Helper to run a test
 
 def runtest(hier, code):
-    root = tempfile.mktemp()
+    root = tempfile.mkdtemp()
     mkhier(root, hier)
     savepath = sys.path[:]
-    codefile = tempfile.mktemp()
-    f = open(codefile, "w")
-    f.write(code)
-    f.close()
+    codefile = tempfile.NamedTemporaryFile()
+    codefile.write(code)
+    codefile.flush()
     try:
         sys.path.insert(0, root)
         if verbose: print "sys.path =", sys.path
         try:
-            execfile(codefile, globals(), {})
+            execfile(codefile.name, globals(), {})
         except:
             traceback.print_exc(file=sys.stdout)
     finally:
@@ -72,7 +72,6 @@
             cleanout(root)
         except (os.error, IOError):
             pass
-        os.remove(codefile)
 
 # Test descriptions
 
diff --git a/Lib/test/test_pkgimport.py b/Lib/test/test_pkgimport.py
index 523ba5a..1343173 100644
--- a/Lib/test/test_pkgimport.py
+++ b/Lib/test/test_pkgimport.py
@@ -17,8 +17,7 @@
                 del sys.modules[module_name]
 
     def setUp(self):
-        self.test_dir = tempfile.mktemp()
-        os.mkdir(self.test_dir)
+        self.test_dir = tempfile.mkdtemp()
         sys.path.append(self.test_dir)
         self.package_dir = os.path.join(self.test_dir,
                                         self.package_name)
diff --git a/Lib/test/test_uu.py b/Lib/test/test_uu.py
index 75d1108..bb553a1 100644
--- a/Lib/test/test_uu.py
+++ b/Lib/test/test_uu.py
@@ -124,8 +124,7 @@
     verify(str(e) == 'No valid begin line found in input file')
 
 # Test to verify that decode() will refuse to overwrite an existing file
-import tempfile
-outfile = tempfile.mktemp()
+outfile = TESTFN + "out"
 inp = StringIO('Here is a message to be uuencoded')
 out = StringIO()
 uu.encode(inp, out, outfile)
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
index a0e913c..1a6ff5c 100644
--- a/Lib/test/test_wave.py
+++ b/Lib/test/test_wave.py
@@ -1,5 +1,5 @@
-from test.test_support import TestFailed
-import os, tempfile
+from test.test_support import TestFailed, TESTFN
+import os
 import wave
 
 def check(t, msg=None):
@@ -11,9 +11,7 @@
 framerate = 8000
 nframes = 100
 
-testfile = tempfile.mktemp()
-
-f = wave.open(testfile, 'wb')
+f = wave.open(TESTFN, 'wb')
 f.setnchannels(nchannels)
 f.setsampwidth(sampwidth)
 f.setframerate(framerate)
@@ -22,7 +20,7 @@
 f.writeframes(output)
 f.close()
 
-f = wave.open(testfile, 'rb')
+f = wave.open(TESTFN, 'rb')
 check(nchannels == f.getnchannels(), "nchannels")
 check(sampwidth == f.getsampwidth(), "sampwidth")
 check(framerate == f.getframerate(), "framerate")
@@ -31,4 +29,4 @@
 check(input == output, "data")
 f.close()
 
-os.remove(testfile)
+os.remove(TESTFN)
diff --git a/Lib/test/test_whichdb.py b/Lib/test/test_whichdb.py
index 21f1588..2f1a3a2 100644
--- a/Lib/test/test_whichdb.py
+++ b/Lib/test/test_whichdb.py
@@ -11,7 +11,7 @@
 import tempfile
 import glob
 
-_fname = tempfile.mktemp()
+_fname = test.test_support.TESTFN
 
 def _delete_files():
     # we don't know the precise name the underlying database uses