1996-12-09 20:06:24 -04:00
|
|
|
#! /usr/bin/env python
|
|
|
|
"""Test script for the dbm module
|
|
|
|
Roger E. Masse
|
|
|
|
"""
|
2002-11-02 14:25:08 -04:00
|
|
|
import os
|
|
|
|
import random
|
1996-12-09 20:06:24 -04:00
|
|
|
import dbm
|
|
|
|
from dbm import error
|
2006-08-25 20:40:32 -03:00
|
|
|
from test.test_support import verbose, verify, TestSkipped, TESTFN
|
1996-12-20 18:36:52 -04:00
|
|
|
|
2002-11-02 14:25:08 -04:00
|
|
|
# make filename unique to allow multiple concurrent tests
|
|
|
|
# and to minimize the likelihood of a problem from an old file
|
2006-08-25 20:40:32 -03:00
|
|
|
filename = TESTFN
|
1996-12-09 20:06:24 -04:00
|
|
|
|
2002-11-02 14:25:08 -04:00
|
|
|
def cleanup():
|
|
|
|
for suffix in ['', '.pag', '.dir', '.db']:
|
|
|
|
try:
|
|
|
|
os.unlink(filename + suffix)
|
2007-01-10 12:19:56 -04:00
|
|
|
except OSError as e:
|
2007-02-26 20:15:55 -04:00
|
|
|
(errno, strerror) = e.errno, e.strerror
|
2002-11-02 14:25:08 -04:00
|
|
|
# if we can't delete the file because of permissions,
|
|
|
|
# nothing will work, so skip the test
|
|
|
|
if errno == 1:
|
|
|
|
raise TestSkipped, 'unable to remove: ' + filename + suffix
|
2000-09-18 14:56:58 -03:00
|
|
|
|
2002-11-02 14:25:08 -04:00
|
|
|
def test_keys():
|
|
|
|
d = dbm.open(filename, 'c')
|
|
|
|
verify(d.keys() == [])
|
2007-08-14 12:42:45 -03:00
|
|
|
d[b'a'] = b'b'
|
|
|
|
d[b'12345678910'] = b'019237410982340912840198242'
|
2002-11-02 14:25:08 -04:00
|
|
|
d.keys()
|
2007-08-14 12:42:45 -03:00
|
|
|
if b'a' in d:
|
2002-11-02 14:25:08 -04:00
|
|
|
if verbose:
|
2007-02-09 01:37:30 -04:00
|
|
|
print('Test dbm keys: ', d.keys())
|
1996-12-09 20:06:24 -04:00
|
|
|
|
2002-11-02 14:25:08 -04:00
|
|
|
d.close()
|
|
|
|
|
|
|
|
def test_modes():
|
|
|
|
d = dbm.open(filename, 'r')
|
|
|
|
d.close()
|
|
|
|
d = dbm.open(filename, 'rw')
|
|
|
|
d.close()
|
|
|
|
d = dbm.open(filename, 'w')
|
|
|
|
d.close()
|
|
|
|
d = dbm.open(filename, 'n')
|
|
|
|
d.close()
|
|
|
|
|
|
|
|
cleanup()
|
1996-12-09 20:06:24 -04:00
|
|
|
try:
|
2002-11-02 14:25:08 -04:00
|
|
|
test_keys()
|
|
|
|
test_modes()
|
1996-12-09 20:06:24 -04:00
|
|
|
except:
|
2002-11-02 14:25:08 -04:00
|
|
|
cleanup()
|
|
|
|
raise
|
|
|
|
|
|
|
|
cleanup()
|