[Python-checkins] r58532 - in python/trunk/Lib/bsddb: dbtables.py test/test_dbtables.py

gregory.p.smith python-checkins at python.org
Thu Oct 18 09:56:55 CEST 2007


Author: gregory.p.smith
Date: Thu Oct 18 09:56:54 2007
New Revision: 58532

Modified:
   python/trunk/Lib/bsddb/dbtables.py
   python/trunk/Lib/bsddb/test/test_dbtables.py
Log:
cleanup test_dbtables to use mkdtemp.  cleanup dbtables to pass txn as a
keyword argument whenever possible to avoid bugs and confusion.  (dbtables.py
line 447 self.db.get using txn as a non-keyword was an actual bug due to this)


Modified: python/trunk/Lib/bsddb/dbtables.py
==============================================================================
--- python/trunk/Lib/bsddb/dbtables.py	(original)
+++ python/trunk/Lib/bsddb/dbtables.py	Thu Oct 18 09:56:54 2007
@@ -20,8 +20,9 @@
 import re
 import sys
 import copy
-import xdrlib
 import random
+import struct
+import base64
 from types import ListType, StringType
 import cPickle as pickle
 
@@ -255,7 +256,7 @@
                                                  flags=DB_RMW))
             tablelist.append(table)
             # delete 1st, in case we opened with DB_DUP
-            self.db.delete(_table_names_key, txn)
+            self.db.delete(_table_names_key, txn=txn)
             self.db.put(_table_names_key, pickle.dumps(tablelist, 1), txn=txn)
 
             txn.commit()
@@ -329,7 +330,7 @@
                 # store the table's new extended column list
                 if newcolumnlist != oldcolumnlist :
                     # delete the old one first since we opened with DB_DUP
-                    self.db.delete(columnlist_key, txn)
+                    self.db.delete(columnlist_key, txn=txn)
                     self.db.put(columnlist_key,
                                 pickle.dumps(newcolumnlist, 1),
                                 txn=txn)
@@ -362,10 +363,9 @@
             # Generate a random 64-bit row ID string
             # (note: this code has <64 bits of randomness
             # but it's plenty for our database id needs!)
-            p = xdrlib.Packer()
-            p.pack_int(int(random.random()*2147483647))
-            p.pack_int(int(random.random()*2147483647))
-            newid = p.get_buffer()
+            newid = struct.pack('ll',
+                                random.randint(0, 2147483647),
+                                random.randint(0, 2147483647))
 
             # Guarantee uniqueness by adding this key to the database
             try:
@@ -444,10 +444,10 @@
                         try:
                             dataitem = self.db.get(
                                 _data_key(table, column, rowid),
-                                txn)
+                                txn=txn)
                             self.db.delete(
                                 _data_key(table, column, rowid),
-                                txn)
+                                txn=txn)
                         except DBNotFoundError:
                              # XXXXXXX row key somehow didn't exist, assume no
                              # error
@@ -490,13 +490,13 @@
                         # delete the data key
                         try:
                             self.db.delete(_data_key(table, column, rowid),
-                                           txn)
+                                           txn=txn)
                         except DBNotFoundError:
                             # XXXXXXX column may not exist, assume no error
                             pass
 
                     try:
-                        self.db.delete(_rowid_key(table, rowid), txn)
+                        self.db.delete(_rowid_key(table, rowid), txn=txn)
                     except DBNotFoundError:
                         # XXXXXXX row key somehow didn't exist, assume no error
                         pass
@@ -652,7 +652,7 @@
             txn = self.env.txn_begin()
 
             # delete the column list
-            self.db.delete(_columns_key(table), txn)
+            self.db.delete(_columns_key(table), txn=txn)
 
             cur = self.db.cursor(txn)
 
@@ -691,7 +691,7 @@
                 # hmm, it wasn't there, oh well, that's what we want.
                 pass
             # delete 1st, incase we opened with DB_DUP
-            self.db.delete(_table_names_key, txn)
+            self.db.delete(_table_names_key, txn=txn)
             self.db.put(_table_names_key, pickle.dumps(tablelist, 1), txn=txn)
 
             txn.commit()

Modified: python/trunk/Lib/bsddb/test/test_dbtables.py
==============================================================================
--- python/trunk/Lib/bsddb/test/test_dbtables.py	(original)
+++ python/trunk/Lib/bsddb/test/test_dbtables.py	Thu Oct 18 09:56:54 2007
@@ -21,6 +21,8 @@
 # $Id$
 
 import sys, os, re
+import tempfile
+import shutil
 try:
     import cPickle
     pickle = cPickle
@@ -47,8 +49,8 @@
     db_name = 'test-table.db'
 
     def setUp(self):
-        homeDir = os.path.join(tempfile.gettempdir(), 'db_home')
-        self.homeDir = homeDir
+        homeDir = tempfile.mkdtemp()
+        self.testHomeDir = homeDir
         try: os.mkdir(homeDir)
         except os.error: pass
         self.tdb = dbtables.bsdTableDB(
@@ -56,10 +58,7 @@
 
     def tearDown(self):
         self.tdb.close()
-        import glob
-        files = glob.glob(os.path.join(self.homeDir, '*'))
-        for file in files:
-            os.remove(file)
+        shutil.rmtree(self.testHomeDir)
 
     def test01(self):
         tabname = "test01"


More information about the Python-checkins mailing list