0001 """TestCases for checking that it does not segfault when a DBEnv object 0002 is closed before its DB objects. 0003 """ 0004 0005 import os 0006 import sys 0007 import tempfile 0008 import glob 0009 import unittest 0010 0011 try: 0012 # For Pythons w/distutils pybsddb 0013 from bsddb3 import db 0014 except ImportError: 0015 # For Python 2.3 0016 from bsddb import db 0017 0018 from test_all import verbose 0019 0020 # We're going to get warnings in this module about trying to close the db when 0021 # its env is already closed. Let's just ignore those. 0022 try: 0023 import warnings 0024 except ImportError: 0025 pass 0026 else: 0027 warnings.filterwarnings('ignore', 0028 message='DB could not be closed in', 0029 category=RuntimeWarning) 0030 0031 0032 #---------------------------------------------------------------------- 0033 0034 class DBEnvClosedEarlyCrash(unittest.TestCase): 0035 def setUp(self): 0036 self.homeDir = os.path.join(os.path.dirname(sys.argv[0]), 'db_home') 0037 try: os.mkdir(self.homeDir) 0038 except os.error: pass 0039 tempfile.tempdir = self.homeDir 0040 self.filename = os.path.split(tempfile.mktemp())[1] 0041 tempfile.tempdir = None 0042 0043 def tearDown(self): 0044 files = glob.glob(os.path.join(self.homeDir, '*')) 0045 for file in files: 0046 os.remove(file) 0047 0048 0049 def test01_close_dbenv_before_db(self): 0050 dbenv = db.DBEnv() 0051 dbenv.open(self.homeDir, 0052 db.DB_INIT_CDB| db.DB_CREATE |db.DB_THREAD|db.DB_INIT_MPOOL, 0053 0666) 0054 0055 d = db.DB(dbenv) 0056 d.open(self.filename, db.DB_BTREE, db.DB_CREATE | db.DB_THREAD, 0666) 0057 0058 try: 0059 dbenv.close() 0060 except db.DBError: 0061 try: 0062 d.close() 0063 except db.DBError: 0064 return 0065 assert 0, \ 0066 "DB close did not raise an exception about its "\ 0067 "DBEnv being trashed" 0068 0069 # XXX This may fail when using older versions of BerkeleyDB. 0070 # E.g. 3.2.9 never raised the exception. 0071 assert 0, "dbenv did not raise an exception about its DB being open" 0072 0073 0074 def test02_close_dbenv_delete_db_success(self): 0075 dbenv = db.DBEnv() 0076 dbenv.open(self.homeDir, 0077 db.DB_INIT_CDB| db.DB_CREATE |db.DB_THREAD|db.DB_INIT_MPOOL, 0078 0666) 0079 0080 d = db.DB(dbenv) 0081 d.open(self.filename, db.DB_BTREE, db.DB_CREATE | db.DB_THREAD, 0666) 0082 0083 try: 0084 dbenv.close() 0085 except db.DBError: 0086 pass # good, it should raise an exception 0087 0088 del d 0089 try: 0090 import gc 0091 except ImportError: 0092 gc = None 0093 if gc: 0094 # force d.__del__ [DB_dealloc] to be called 0095 gc.collect() 0096 0097 0098 #---------------------------------------------------------------------- 0099 0100 def test_suite(): 0101 suite = unittest.TestSuite() 0102 suite.addTest(unittest.makeSuite(DBEnvClosedEarlyCrash)) 0103 return suite 0104 0105 0106 if __name__ == '__main__': 0107 unittest.main(defaultTest='test_suite') 0108
Generated by PyXR 0.9.4