Create a db_home directory with a unique name so multiple users can
run the test simultaneously.  The simplest thing I found that worked
on both Windows and Unix was to use the PID.  It's unique so should be
sufficient.  This should prevent many of the spurious failures of
the automated tests since they run as different users.

Also cleanup the directory consistenly in the tearDown methods.

It would be nice if someone ensured that the directories are always
created with a consistent name.
diff --git a/Lib/bsddb/test/test_associate.py b/Lib/bsddb/test/test_associate.py
index fd70c5d..88c1f46 100644
--- a/Lib/bsddb/test/test_associate.py
+++ b/Lib/bsddb/test/test_associate.py
@@ -91,7 +91,7 @@
 class AssociateErrorTestCase(unittest.TestCase):
     def setUp(self):
         self.filename = self.__class__.__name__ + '.db'
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         self.homeDir = homeDir
         try:
             os.mkdir(homeDir)
@@ -106,11 +106,8 @@
     def tearDown(self):
         self.env.close()
         self.env = None
-        import glob
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
-
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
     def test00_associateDBError(self):
         if verbose:
@@ -151,7 +148,7 @@
 
     def setUp(self):
         self.filename = self.__class__.__name__ + '.db'
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         self.homeDir = homeDir
         try:
             os.mkdir(homeDir)
diff --git a/Lib/bsddb/test/test_basics.py b/Lib/bsddb/test/test_basics.py
index 6d0fff1..af82cd1 100644
--- a/Lib/bsddb/test/test_basics.py
+++ b/Lib/bsddb/test/test_basics.py
@@ -5,10 +5,10 @@
 
 import os
 import errno
-import shutil
 import string
 import tempfile
 from pprint import pprint
+from test import test_support
 import unittest
 import time
 
@@ -53,13 +53,9 @@
 
     def setUp(self):
         if self.useEnv:
-            homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+            homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
             self.homeDir = homeDir
-            try:
-                shutil.rmtree(homeDir)
-            except OSError, e:
-                # unix returns ENOENT, windows returns ESRCH
-                if e.errno not in (errno.ENOENT, errno.ESRCH): raise
+            test_support.rmtree(homeDir)
             os.mkdir(homeDir)
             try:
                 self.env = db.DBEnv()
@@ -73,7 +69,7 @@
                 tempfile.tempdir = None
             # Yes, a bare except is intended, since we're re-raising the exc.
             except:
-                shutil.rmtree(homeDir)
+                test_support.rmtree(homeDir)
                 raise
         else:
             self.env = None
@@ -97,8 +93,8 @@
     def tearDown(self):
         self.d.close()
         if self.env is not None:
+            test_support.rmtree(self.homeDir)
             self.env.close()
-            shutil.rmtree(self.homeDir)
             ## Make a new DBEnv to remove the env files from the home dir.
             ## (It can't be done while the env is open, nor after it has been
             ## closed, so we make a new one to do it.)
diff --git a/Lib/bsddb/test/test_compare.py b/Lib/bsddb/test/test_compare.py
index 5aa8308..2782e8c 100644
--- a/Lib/bsddb/test/test_compare.py
+++ b/Lib/bsddb/test/test_compare.py
@@ -52,7 +52,7 @@
 
     def setUp (self):
         self.filename = self.__class__.__name__ + '.db'
-        homeDir = os.path.join (tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join (tempfile.gettempdir(), 'db_home%d'%os.getpid())
         self.homeDir = homeDir
         try:
             os.mkdir (homeDir)
@@ -70,8 +70,8 @@
         if self.env is not None:
             self.env.close ()
             self.env = None
-        import glob
-        map (os.remove, glob.glob (os.path.join (self.homeDir, '*')))
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
     def addDataToDB (self, data):
         i = 0
diff --git a/Lib/bsddb/test/test_cursor_pget_bug.py b/Lib/bsddb/test/test_cursor_pget_bug.py
index 2e83a6c..95bbe1b 100644
--- a/Lib/bsddb/test/test_cursor_pget_bug.py
+++ b/Lib/bsddb/test/test_cursor_pget_bug.py
@@ -17,7 +17,7 @@
     db_name = 'test-cursor_pget.db'
 
     def setUp(self):
-        self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         try:
             os.mkdir(self.homeDir)
         except os.error:
@@ -42,9 +42,8 @@
         del self.secondary_db
         del self.primary_db
         del self.env
-        for file in glob.glob(os.path.join(self.homeDir, '*')):
-            os.remove(file)
-        os.removedirs(self.homeDir)
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
     def test_pget(self):
         cursor = self.secondary_db.cursor()
diff --git a/Lib/bsddb/test/test_dbobj.py b/Lib/bsddb/test/test_dbobj.py
index dd9909d..071b547 100644
--- a/Lib/bsddb/test/test_dbobj.py
+++ b/Lib/bsddb/test/test_dbobj.py
@@ -1,7 +1,6 @@
 
 import os, string
 import unittest
-import glob
 import tempfile
 
 try:
@@ -20,7 +19,7 @@
     db_name = 'test-dbobj.db'
 
     def setUp(self):
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         self.homeDir = homeDir
         try: os.mkdir(homeDir)
         except os.error: pass
@@ -30,9 +29,8 @@
             del self.db
         if hasattr(self, 'env'):
             del self.env
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
     def test01_both(self):
         class TestDBEnv(dbobj.DBEnv): pass
diff --git a/Lib/bsddb/test/test_dbshelve.py b/Lib/bsddb/test/test_dbshelve.py
index bd399a4..d6c997f 100644
--- a/Lib/bsddb/test/test_dbshelve.py
+++ b/Lib/bsddb/test/test_dbshelve.py
@@ -245,7 +245,7 @@
 class BasicEnvShelveTestCase(DBShelveTestCase):
     def do_open(self):
         self.homeDir = homeDir = os.path.join(
-            tempfile.gettempdir(), 'db_home')
+            tempfile.gettempdir(), 'db_home%d'%os.getpid())
         try: os.mkdir(homeDir)
         except os.error: pass
         self.env = db.DBEnv()
@@ -262,12 +262,9 @@
 
 
     def tearDown(self):
+        from test import test_support
+        test_support.rmtree(self.homeDir)
         self.do_close()
-        import glob
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
-
 
 
 class EnvBTreeShelveTestCase(BasicEnvShelveTestCase):
diff --git a/Lib/bsddb/test/test_dbtables.py b/Lib/bsddb/test/test_dbtables.py
index 0c9cf5f..7acfdd1 100644
--- a/Lib/bsddb/test/test_dbtables.py
+++ b/Lib/bsddb/test/test_dbtables.py
@@ -22,7 +22,6 @@
 
 import os, re
 import tempfile
-import shutil
 try:
     import cPickle
     pickle = cPickle
@@ -58,7 +57,8 @@
 
     def tearDown(self):
         self.tdb.close()
-        shutil.rmtree(self.testHomeDir)
+        from test import test_support
+        test_support.rmtree(self.testHomeDir)
 
     def test01(self):
         tabname = "test01"
diff --git a/Lib/bsddb/test/test_env_close.py b/Lib/bsddb/test/test_env_close.py
index efc3460..409f3dc 100644
--- a/Lib/bsddb/test/test_env_close.py
+++ b/Lib/bsddb/test/test_env_close.py
@@ -4,7 +4,6 @@
 
 import os
 import tempfile
-import glob
 import unittest
 
 try:
@@ -32,7 +31,7 @@
 
 class DBEnvClosedEarlyCrash(unittest.TestCase):
     def setUp(self):
-        self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         try: os.mkdir(self.homeDir)
         except os.error: pass
         tempfile.tempdir = self.homeDir
@@ -40,10 +39,8 @@
         tempfile.tempdir = None
 
     def tearDown(self):
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
-
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
     def test01_close_dbenv_before_db(self):
         dbenv = db.DBEnv()
diff --git a/Lib/bsddb/test/test_join.py b/Lib/bsddb/test/test_join.py
index 26c2912..f9e7f00 100644
--- a/Lib/bsddb/test/test_join.py
+++ b/Lib/bsddb/test/test_join.py
@@ -47,7 +47,7 @@
 
     def setUp(self):
         self.filename = self.__class__.__name__ + '.db'
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         self.homeDir = homeDir
         try: os.mkdir(homeDir)
         except os.error: pass
@@ -56,10 +56,8 @@
 
     def tearDown(self):
         self.env.close()
-        import glob
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
     def test01_join(self):
         if verbose:
diff --git a/Lib/bsddb/test/test_lock.py b/Lib/bsddb/test/test_lock.py
index 18469a1..2862079 100644
--- a/Lib/bsddb/test/test_lock.py
+++ b/Lib/bsddb/test/test_lock.py
@@ -2,7 +2,6 @@
 TestCases for testing the locking sub-system.
 """
 
-import shutil
 import tempfile
 import time
 
@@ -37,7 +36,8 @@
 
     def tearDown(self):
         self.env.close()
-        shutil.rmtree(self.homeDir)
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
 
     def test01_simple(self):
diff --git a/Lib/bsddb/test/test_misc.py b/Lib/bsddb/test/test_misc.py
index 3d566f1..a9228f9 100644
--- a/Lib/bsddb/test/test_misc.py
+++ b/Lib/bsddb/test/test_misc.py
@@ -17,7 +17,7 @@
 class MiscTestCase(unittest.TestCase):
     def setUp(self):
         self.filename = self.__class__.__name__ + '.db'
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         self.homeDir = homeDir
         try:
             os.mkdir(homeDir)
@@ -25,12 +25,9 @@
             pass
 
     def tearDown(self):
-        try:
-            os.remove(self.filename)
-        except OSError:
-            pass
-        import shutil
-        shutil.rmtree(self.homeDir)
+        from test import test_support
+        test_support.unlink(self.filename)
+        test_support.rmtree(self.homeDir)
 
     def test01_badpointer(self):
         dbs = dbshelve.open(self.filename)
diff --git a/Lib/bsddb/test/test_pickle.py b/Lib/bsddb/test/test_pickle.py
index b87cc68..10ce685 100644
--- a/Lib/bsddb/test/test_pickle.py
+++ b/Lib/bsddb/test/test_pickle.py
@@ -7,7 +7,6 @@
     cPickle = None
 import unittest
 import tempfile
-import glob
 
 try:
     # For Pythons w/distutils pybsddb
@@ -25,7 +24,7 @@
     db_name = 'test-dbobj.db'
 
     def setUp(self):
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         self.homeDir = homeDir
         try: os.mkdir(homeDir)
         except os.error: pass
@@ -35,9 +34,8 @@
             del self.db
         if hasattr(self, 'env'):
             del self.env
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
     def _base_test_pickle_DBError(self, pickle):
         self.env = db.DBEnv()
diff --git a/Lib/bsddb/test/test_recno.py b/Lib/bsddb/test/test_recno.py
index 9630db1..1074a76 100644
--- a/Lib/bsddb/test/test_recno.py
+++ b/Lib/bsddb/test/test_recno.py
@@ -24,12 +24,13 @@
 class SimpleRecnoTestCase(unittest.TestCase):
     def setUp(self):
         self.filename = tempfile.mktemp()
+        self.homeDir = None
 
     def tearDown(self):
-        try:
-            os.remove(self.filename)
-        except OSError, e:
-            if e.errno <> errno.EEXIST: raise
+        from test import test_support
+        test_support.unlink(self.filename)
+        if self.homeDir:
+            test_support.rmtree(self.homeDir)
 
     def test01_basic(self):
         d = db.DB()
@@ -202,7 +203,8 @@
         just a line in the file, but you can set a different record delimiter
         if needed.
         """
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
+        self.homeDir = homeDir
         source = os.path.join(homeDir, 'test_recno.txt')
         if not os.path.isdir(homeDir):
             os.mkdir(homeDir)
diff --git a/Lib/bsddb/test/test_sequence.py b/Lib/bsddb/test/test_sequence.py
index d7029e5..86c58b0 100644
--- a/Lib/bsddb/test/test_sequence.py
+++ b/Lib/bsddb/test/test_sequence.py
@@ -1,7 +1,6 @@
 import unittest
 import os
 import tempfile
-import glob
 
 try:
     # For Pythons w/distutils pybsddb
@@ -13,7 +12,7 @@
 class DBSequenceTest(unittest.TestCase):
     def setUp(self):
         self.int_32_max = 0x100000000
-        self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        self.homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         try:
             os.mkdir(self.homeDir)
         except os.error:
@@ -38,9 +37,8 @@
             self.dbenv.close()
             del self.dbenv
 
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
+        from test import test_support
+        test_support.rmtree(self.homeDir)
 
     def test_get(self):
         self.seq = db.DBSequence(self.d, flags=0)
diff --git a/Lib/bsddb/test/test_thread.py b/Lib/bsddb/test/test_thread.py
index d14f6da..8a8b313 100644
--- a/Lib/bsddb/test/test_thread.py
+++ b/Lib/bsddb/test/test_thread.py
@@ -5,7 +5,6 @@
 import sys
 import time
 import errno
-import shutil
 import tempfile
 from random import random
 
@@ -52,7 +51,7 @@
         if verbose:
             dbutils._deadlock_VerboseFile = sys.stdout
 
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
+        homeDir = os.path.join(tempfile.gettempdir(), 'db_home%d'%os.getpid())
         self.homeDir = homeDir
         try:
             os.mkdir(homeDir)
@@ -69,12 +68,10 @@
         self.d.open(self.filename, self.dbtype, self.dbopenflags|db.DB_CREATE)
 
     def tearDown(self):
+        from test import test_support
+        test_support.rmtree(self.homeDir)
         self.d.close()
         self.env.close()
-        try:
-            shutil.rmtree(self.homeDir)
-        except OSError, e:
-            if e.errno != errno.EEXIST: raise
 
     def setEnvOpts(self):
         pass
diff --git a/Lib/test/test_bsddb3.py b/Lib/test/test_bsddb3.py
index 5a4258e..27a0900 100644
--- a/Lib/test/test_bsddb3.py
+++ b/Lib/test/test_bsddb3.py
@@ -2,10 +2,12 @@
 """
 Run all test cases.
 """
+import os
 import sys
+import tempfile
 import time
 import unittest
-from test.test_support import requires, verbose, run_unittest, unlink
+from test.test_support import requires, verbose, run_unittest, unlink, rmtree
 
 # When running as a script instead of within the regrtest framework, skip the
 # requires test, since it's obvious we want to run them.
@@ -85,6 +87,15 @@
 # For invocation through regrtest
 def test_main():
     run_unittest(suite())
+    db_home = os.path.join(tempfile.gettempdir(), 'db_home')
+    # The only reason to remove db_home is in case if there is an old
+    # one lying around.  This might be by a different user, so just
+    # ignore errors.  We should always make a unique name now.
+    try:
+        rmtree(db_home)
+    except:
+        pass
+    rmtree('db_home%d' % os.getpid())
 
 # For invocation as a script
 if __name__ == '__main__':
diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py
index d36d81c..2071606 100644
--- a/Lib/test/test_support.py
+++ b/Lib/test/test_support.py
@@ -9,6 +9,7 @@
 import sys
 import os
 import os.path
+import shutil
 import warnings
 import unittest
 
@@ -64,6 +65,14 @@
     except OSError:
         pass
 
+def rmtree(path):
+    try:
+        shutil.rmtree(path)
+    except OSError, e:
+        # Unix returns ENOENT, Windows returns ESRCH.
+        if e.errno not in (errno.ENOENT, errno.ESRCH):
+            raise
+
 def forget(modname):
     '''"Forget" a module was ever imported by removing it from sys.modules and
     deleting any .pyc and .pyo files.'''