• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Run all test cases.
2"""
3
4import sys
5import os
6import unittest
7try:
8    # For Pythons w/distutils pybsddb
9    import bsddb3 as bsddb
10except ImportError:
11    # For Python 2.3
12    import bsddb
13
14
15if sys.version_info[0] >= 3 :
16    charset = "iso8859-1"  # Full 8 bit
17
18    class logcursor_py3k(object) :
19        def __init__(self, env) :
20            self._logcursor = env.log_cursor()
21
22        def __getattr__(self, v) :
23            return getattr(self._logcursor, v)
24
25        def __next__(self) :
26            v = getattr(self._logcursor, "next")()
27            if v is not None :
28                v = (v[0], v[1].decode(charset))
29            return v
30
31        next = __next__
32
33        def first(self) :
34            v = self._logcursor.first()
35            if v is not None :
36                v = (v[0], v[1].decode(charset))
37            return v
38
39        def last(self) :
40            v = self._logcursor.last()
41            if v is not None :
42                v = (v[0], v[1].decode(charset))
43            return v
44
45        def prev(self) :
46            v = self._logcursor.prev()
47            if v is not None :
48                v = (v[0], v[1].decode(charset))
49            return v
50
51        def current(self) :
52            v = self._logcursor.current()
53            if v is not None :
54                v = (v[0], v[1].decode(charset))
55            return v
56
57        def set(self, lsn) :
58            v = self._logcursor.set(lsn)
59            if v is not None :
60                v = (v[0], v[1].decode(charset))
61            return v
62
63    class cursor_py3k(object) :
64        def __init__(self, db, *args, **kwargs) :
65            self._dbcursor = db.cursor(*args, **kwargs)
66
67        def __getattr__(self, v) :
68            return getattr(self._dbcursor, v)
69
70        def _fix(self, v) :
71            if v is None : return None
72            key, value = v
73            if isinstance(key, bytes) :
74                key = key.decode(charset)
75            return (key, value.decode(charset))
76
77        def __next__(self) :
78            v = getattr(self._dbcursor, "next")()
79            return self._fix(v)
80
81        next = __next__
82
83        def previous(self) :
84            v = self._dbcursor.previous()
85            return self._fix(v)
86
87        def last(self) :
88            v = self._dbcursor.last()
89            return self._fix(v)
90
91        def set(self, k) :
92            if isinstance(k, str) :
93                k = bytes(k, charset)
94            v = self._dbcursor.set(k)
95            return self._fix(v)
96
97        def set_recno(self, num) :
98            v = self._dbcursor.set_recno(num)
99            return self._fix(v)
100
101        def set_range(self, k, dlen=-1, doff=-1) :
102            if isinstance(k, str) :
103                k = bytes(k, charset)
104            v = self._dbcursor.set_range(k, dlen=dlen, doff=doff)
105            return self._fix(v)
106
107        def dup(self, flags=0) :
108            cursor = self._dbcursor.dup(flags)
109            return dup_cursor_py3k(cursor)
110
111        def next_dup(self) :
112            v = self._dbcursor.next_dup()
113            return self._fix(v)
114
115        def next_nodup(self) :
116            v = self._dbcursor.next_nodup()
117            return self._fix(v)
118
119        def put(self, key, data, flags=0, dlen=-1, doff=-1) :
120            if isinstance(key, str) :
121                key = bytes(key, charset)
122            if isinstance(data, str) :
123                value = bytes(data, charset)
124            return self._dbcursor.put(key, data, flags=flags, dlen=dlen,
125                    doff=doff)
126
127        def current(self, flags=0, dlen=-1, doff=-1) :
128            v = self._dbcursor.current(flags=flags, dlen=dlen, doff=doff)
129            return self._fix(v)
130
131        def first(self) :
132            v = self._dbcursor.first()
133            return self._fix(v)
134
135        def pget(self, key=None, data=None, flags=0) :
136            # Incorrect because key can be a bare number,
137            # but enough to pass testsuite
138            if isinstance(key, int) and (data is None) and (flags == 0) :
139                flags = key
140                key = None
141            if isinstance(key, str) :
142                key = bytes(key, charset)
143            if isinstance(data, int) and (flags==0) :
144                flags = data
145                data = None
146            if isinstance(data, str) :
147                data = bytes(data, charset)
148            v=self._dbcursor.pget(key=key, data=data, flags=flags)
149            if v is not None :
150                v1, v2, v3 = v
151                if isinstance(v1, bytes) :
152                    v1 = v1.decode(charset)
153                if isinstance(v2, bytes) :
154                    v2 = v2.decode(charset)
155
156                v = (v1, v2, v3.decode(charset))
157
158            return v
159
160        def join_item(self) :
161            v = self._dbcursor.join_item()
162            if v is not None :
163                v = v.decode(charset)
164            return v
165
166        def get(self, *args, **kwargs) :
167            l = len(args)
168            if l == 2 :
169                k, f = args
170                if isinstance(k, str) :
171                    k = bytes(k, "iso8859-1")
172                args = (k, f)
173            elif l == 3 :
174                k, d, f = args
175                if isinstance(k, str) :
176                    k = bytes(k, charset)
177                if isinstance(d, str) :
178                    d = bytes(d, charset)
179                args =(k, d, f)
180
181            v = self._dbcursor.get(*args, **kwargs)
182            if v is not None :
183                k, v = v
184                if isinstance(k, bytes) :
185                    k = k.decode(charset)
186                v = (k, v.decode(charset))
187            return v
188
189        def get_both(self, key, value) :
190            if isinstance(key, str) :
191                key = bytes(key, charset)
192            if isinstance(value, str) :
193                value = bytes(value, charset)
194            v=self._dbcursor.get_both(key, value)
195            return self._fix(v)
196
197    class dup_cursor_py3k(cursor_py3k) :
198        def __init__(self, dbcursor) :
199            self._dbcursor = dbcursor
200
201    class DB_py3k(object) :
202        def __init__(self, *args, **kwargs) :
203            args2=[]
204            for i in args :
205                if isinstance(i, DBEnv_py3k) :
206                    i = i._dbenv
207                args2.append(i)
208            args = tuple(args2)
209            for k, v in kwargs.items() :
210                if isinstance(v, DBEnv_py3k) :
211                    kwargs[k] = v._dbenv
212
213            self._db = bsddb._db.DB_orig(*args, **kwargs)
214
215        def __contains__(self, k) :
216            if isinstance(k, str) :
217                k = bytes(k, charset)
218            return getattr(self._db, "has_key")(k)
219
220        def __getitem__(self, k) :
221            if isinstance(k, str) :
222                k = bytes(k, charset)
223            v = self._db[k]
224            if v is not None :
225                v = v.decode(charset)
226            return v
227
228        def __setitem__(self, k, v) :
229            if isinstance(k, str) :
230                k = bytes(k, charset)
231            if isinstance(v, str) :
232                v = bytes(v, charset)
233            self._db[k] = v
234
235        def __delitem__(self, k) :
236            if isinstance(k, str) :
237                k = bytes(k, charset)
238            del self._db[k]
239
240        def __getattr__(self, v) :
241            return getattr(self._db, v)
242
243        def __len__(self) :
244            return len(self._db)
245
246        def has_key(self, k, txn=None) :
247            if isinstance(k, str) :
248                k = bytes(k, charset)
249            return self._db.has_key(k, txn=txn)
250
251        def set_re_delim(self, c) :
252            if isinstance(c, str) :  # We can use a numeric value byte too
253                c = bytes(c, charset)
254            return self._db.set_re_delim(c)
255
256        def set_re_pad(self, c) :
257            if isinstance(c, str) :  # We can use a numeric value byte too
258                c = bytes(c, charset)
259            return self._db.set_re_pad(c)
260
261        def get_re_source(self) :
262            source = self._db.get_re_source()
263            return source.decode(charset)
264
265        def put(self, key, data, txn=None, flags=0, dlen=-1, doff=-1) :
266            if isinstance(key, str) :
267                key = bytes(key, charset)
268            if isinstance(data, str) :
269                value = bytes(data, charset)
270            return self._db.put(key, data, flags=flags, txn=txn, dlen=dlen,
271                    doff=doff)
272
273        def append(self, value, txn=None) :
274            if isinstance(value, str) :
275                value = bytes(value, charset)
276            return self._db.append(value, txn=txn)
277
278        def get_size(self, key) :
279            if isinstance(key, str) :
280                key = bytes(key, charset)
281            return self._db.get_size(key)
282
283        def exists(self, key, *args, **kwargs) :
284            if isinstance(key, str) :
285                key = bytes(key, charset)
286            return self._db.exists(key, *args, **kwargs)
287
288        def get(self, key, default="MagicCookie", txn=None, flags=0, dlen=-1, doff=-1) :
289            if isinstance(key, str) :
290                key = bytes(key, charset)
291            if default != "MagicCookie" :  # Magic for 'test_get_none.py'
292                v=self._db.get(key, default=default, txn=txn, flags=flags,
293                        dlen=dlen, doff=doff)
294            else :
295                v=self._db.get(key, txn=txn, flags=flags,
296                        dlen=dlen, doff=doff)
297            if (v is not None) and isinstance(v, bytes) :
298                v = v.decode(charset)
299            return v
300
301        def pget(self, key, txn=None) :
302            if isinstance(key, str) :
303                key = bytes(key, charset)
304            v=self._db.pget(key, txn=txn)
305            if v is not None :
306                v1, v2 = v
307                if isinstance(v1, bytes) :
308                    v1 = v1.decode(charset)
309
310                v = (v1, v2.decode(charset))
311            return v
312
313        def get_both(self, key, value, txn=None, flags=0) :
314            if isinstance(key, str) :
315                key = bytes(key, charset)
316            if isinstance(value, str) :
317                value = bytes(value, charset)
318            v=self._db.get_both(key, value, txn=txn, flags=flags)
319            if v is not None :
320                v = v.decode(charset)
321            return v
322
323        def delete(self, key, txn=None) :
324            if isinstance(key, str) :
325                key = bytes(key, charset)
326            return self._db.delete(key, txn=txn)
327
328        def keys(self) :
329            k = self._db.keys()
330            if len(k) and isinstance(k[0], bytes) :
331                return [i.decode(charset) for i in self._db.keys()]
332            else :
333                return k
334
335        def items(self) :
336            data = self._db.items()
337            if not len(data) : return data
338            data2 = []
339            for k, v in data :
340                if isinstance(k, bytes) :
341                    k = k.decode(charset)
342                data2.append((k, v.decode(charset)))
343            return data2
344
345        def associate(self, secondarydb, callback, flags=0, txn=None) :
346            class associate_callback(object) :
347                def __init__(self, callback) :
348                    self._callback = callback
349
350                def callback(self, key, data) :
351                    if isinstance(key, str) :
352                        key = key.decode(charset)
353                    data = data.decode(charset)
354                    key = self._callback(key, data)
355                    if (key != bsddb._db.DB_DONOTINDEX) :
356                        if isinstance(key, str) :
357                            key = bytes(key, charset)
358                        elif isinstance(key, list) :
359                            key2 = []
360                            for i in key :
361                                if isinstance(i, str) :
362                                    i = bytes(i, charset)
363                                key2.append(i)
364                            key = key2
365                    return key
366
367            return self._db.associate(secondarydb._db,
368                    associate_callback(callback).callback, flags=flags,
369                    txn=txn)
370
371        def cursor(self, txn=None, flags=0) :
372            return cursor_py3k(self._db, txn=txn, flags=flags)
373
374        def join(self, cursor_list) :
375            cursor_list = [i._dbcursor for i in cursor_list]
376            return dup_cursor_py3k(self._db.join(cursor_list))
377
378    class DBEnv_py3k(object) :
379        def __init__(self, *args, **kwargs) :
380            self._dbenv = bsddb._db.DBEnv_orig(*args, **kwargs)
381
382        def __getattr__(self, v) :
383            return getattr(self._dbenv, v)
384
385        def log_cursor(self, flags=0) :
386            return logcursor_py3k(self._dbenv)
387
388        def get_lg_dir(self) :
389            return self._dbenv.get_lg_dir().decode(charset)
390
391        def get_tmp_dir(self) :
392            return self._dbenv.get_tmp_dir().decode(charset)
393
394        def get_data_dirs(self) :
395            return tuple(
396                (i.decode(charset) for i in self._dbenv.get_data_dirs()))
397
398    class DBSequence_py3k(object) :
399        def __init__(self, db, *args, **kwargs) :
400            self._db=db
401            self._dbsequence = bsddb._db.DBSequence_orig(db._db, *args, **kwargs)
402
403        def __getattr__(self, v) :
404            return getattr(self._dbsequence, v)
405
406        def open(self, key, *args, **kwargs) :
407            return self._dbsequence.open(bytes(key, charset), *args, **kwargs)
408
409        def get_key(self) :
410            return  self._dbsequence.get_key().decode(charset)
411
412        def get_dbp(self) :
413            return self._db
414
415    bsddb._db.DBEnv_orig = bsddb._db.DBEnv
416    bsddb._db.DB_orig = bsddb._db.DB
417    if bsddb.db.version() <= (4, 3) :
418        bsddb._db.DBSequence_orig = None
419    else :
420        bsddb._db.DBSequence_orig = bsddb._db.DBSequence
421
422    def do_proxy_db_py3k(flag) :
423        flag2 = do_proxy_db_py3k.flag
424        do_proxy_db_py3k.flag = flag
425        if flag :
426            bsddb.DBEnv = bsddb.db.DBEnv = bsddb._db.DBEnv = DBEnv_py3k
427            bsddb.DB = bsddb.db.DB = bsddb._db.DB = DB_py3k
428            bsddb._db.DBSequence = DBSequence_py3k
429        else :
430            bsddb.DBEnv = bsddb.db.DBEnv = bsddb._db.DBEnv = bsddb._db.DBEnv_orig
431            bsddb.DB = bsddb.db.DB = bsddb._db.DB = bsddb._db.DB_orig
432            bsddb._db.DBSequence = bsddb._db.DBSequence_orig
433        return flag2
434
435    do_proxy_db_py3k.flag = False
436    do_proxy_db_py3k(True)
437
438try:
439    # For Pythons w/distutils pybsddb
440    from bsddb3 import db, dbtables, dbutils, dbshelve, \
441            hashopen, btopen, rnopen, dbobj
442except ImportError:
443    # For Python 2.3
444    from bsddb import db, dbtables, dbutils, dbshelve, \
445            hashopen, btopen, rnopen, dbobj
446
447try:
448    from bsddb3 import test_support
449except ImportError:
450    if sys.version_info[0] < 3 :
451        from test import test_support
452    else :
453        from test import support as test_support
454
455
456try:
457    if sys.version_info[0] < 3 :
458        from threading import Thread, currentThread
459        del Thread, currentThread
460    else :
461        from threading import Thread, current_thread
462        del Thread, current_thread
463    have_threads = True
464except ImportError:
465    have_threads = False
466
467verbose = 0
468if 'verbose' in sys.argv:
469    verbose = 1
470    sys.argv.remove('verbose')
471
472if 'silent' in sys.argv:  # take care of old flag, just in case
473    verbose = 0
474    sys.argv.remove('silent')
475
476
477def print_versions():
478    print
479    print '-=' * 38
480    print db.DB_VERSION_STRING
481    print 'bsddb.db.version():   %s' % (db.version(), )
482    if db.version() >= (5, 0) :
483        print 'bsddb.db.full_version(): %s' %repr(db.full_version())
484    print 'bsddb.db.__version__: %s' % db.__version__
485    print 'bsddb.db.cvsid:       %s' % db.cvsid
486
487    # Workaround for allowing generating an EGGs as a ZIP files.
488    suffix="__"
489    print 'py module:            %s' % getattr(bsddb, "__file"+suffix)
490    print 'extension module:     %s' % getattr(bsddb, "__file"+suffix)
491
492    print 'python version:       %s' % sys.version
493    print 'My pid:               %s' % os.getpid()
494    print '-=' * 38
495
496
497def get_new_path(name) :
498    get_new_path.mutex.acquire()
499    try :
500        import os
501        path=os.path.join(get_new_path.prefix,
502                name+"_"+str(os.getpid())+"_"+str(get_new_path.num))
503        get_new_path.num+=1
504    finally :
505        get_new_path.mutex.release()
506    return path
507
508def get_new_environment_path() :
509    path=get_new_path("environment")
510    import os
511    try:
512        os.makedirs(path,mode=0700)
513    except os.error:
514        test_support.rmtree(path)
515        os.makedirs(path)
516    return path
517
518def get_new_database_path() :
519    path=get_new_path("database")
520    import os
521    if os.path.exists(path) :
522        os.remove(path)
523    return path
524
525
526# This path can be overridden via "set_test_path_prefix()".
527import os, os.path
528get_new_path.prefix=os.path.join(os.environ.get("TMPDIR",
529    os.path.join(os.sep,"tmp")), "z-Berkeley_DB")
530get_new_path.num=0
531
532def get_test_path_prefix() :
533    return get_new_path.prefix
534
535def set_test_path_prefix(path) :
536    get_new_path.prefix=path
537
538def remove_test_path_directory() :
539    test_support.rmtree(get_new_path.prefix)
540
541if have_threads :
542    import threading
543    get_new_path.mutex=threading.Lock()
544    del threading
545else :
546    class Lock(object) :
547        def acquire(self) :
548            pass
549        def release(self) :
550            pass
551    get_new_path.mutex=Lock()
552    del Lock
553
554
555
556class PrintInfoFakeTest(unittest.TestCase):
557    def testPrintVersions(self):
558        print_versions()
559
560
561# This little hack is for when this module is run as main and all the
562# other modules import it so they will still be able to get the right
563# verbose setting.  It's confusing but it works.
564if sys.version_info[0] < 3 :
565    import test_all
566    test_all.verbose = verbose
567else :
568    import sys
569    print >>sys.stderr, "Work to do!"
570
571
572def suite(module_prefix='', timing_check=None):
573    test_modules = [
574        'test_associate',
575        'test_basics',
576        'test_dbenv',
577        'test_db',
578        'test_compare',
579        'test_compat',
580        'test_cursor_pget_bug',
581        'test_dbobj',
582        'test_dbshelve',
583        'test_dbtables',
584        'test_distributed_transactions',
585        'test_early_close',
586        'test_fileid',
587        'test_get_none',
588        'test_join',
589        'test_lock',
590        'test_misc',
591        'test_pickle',
592        'test_queue',
593        'test_recno',
594        'test_replication',
595        'test_sequence',
596        'test_thread',
597        ]
598
599    alltests = unittest.TestSuite()
600    for name in test_modules:
601        #module = __import__(name)
602        # Do it this way so that suite may be called externally via
603        # python's Lib/test/test_bsddb3.
604        module = __import__(module_prefix+name, globals(), locals(), name)
605
606        alltests.addTest(module.test_suite())
607        if timing_check:
608            alltests.addTest(unittest.makeSuite(timing_check))
609    return alltests
610
611
612def test_suite():
613    suite = unittest.TestSuite()
614    suite.addTest(unittest.makeSuite(PrintInfoFakeTest))
615    return suite
616
617
618if __name__ == '__main__':
619    print_versions()
620    unittest.main(defaultTest='suite')
621