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    import string
416    string.letters=[chr(i) for i in xrange(65,91)]
417
418    bsddb._db.DBEnv_orig = bsddb._db.DBEnv
419    bsddb._db.DB_orig = bsddb._db.DB
420    if bsddb.db.version() <= (4, 3) :
421        bsddb._db.DBSequence_orig = None
422    else :
423        bsddb._db.DBSequence_orig = bsddb._db.DBSequence
424
425    def do_proxy_db_py3k(flag) :
426        flag2 = do_proxy_db_py3k.flag
427        do_proxy_db_py3k.flag = flag
428        if flag :
429            bsddb.DBEnv = bsddb.db.DBEnv = bsddb._db.DBEnv = DBEnv_py3k
430            bsddb.DB = bsddb.db.DB = bsddb._db.DB = DB_py3k
431            bsddb._db.DBSequence = DBSequence_py3k
432        else :
433            bsddb.DBEnv = bsddb.db.DBEnv = bsddb._db.DBEnv = bsddb._db.DBEnv_orig
434            bsddb.DB = bsddb.db.DB = bsddb._db.DB = bsddb._db.DB_orig
435            bsddb._db.DBSequence = bsddb._db.DBSequence_orig
436        return flag2
437
438    do_proxy_db_py3k.flag = False
439    do_proxy_db_py3k(True)
440
441try:
442    # For Pythons w/distutils pybsddb
443    from bsddb3 import db, dbtables, dbutils, dbshelve, \
444            hashopen, btopen, rnopen, dbobj
445except ImportError:
446    # For Python 2.3
447    from bsddb import db, dbtables, dbutils, dbshelve, \
448            hashopen, btopen, rnopen, dbobj
449
450try:
451    from bsddb3 import test_support
452except ImportError:
453    if sys.version_info[0] < 3 :
454        from test import test_support
455    else :
456        from test import support as test_support
457
458
459try:
460    if sys.version_info[0] < 3 :
461        from threading import Thread, currentThread
462        del Thread, currentThread
463    else :
464        from threading import Thread, current_thread
465        del Thread, current_thread
466    have_threads = True
467except ImportError:
468    have_threads = False
469
470verbose = 0
471if 'verbose' in sys.argv:
472    verbose = 1
473    sys.argv.remove('verbose')
474
475if 'silent' in sys.argv:  # take care of old flag, just in case
476    verbose = 0
477    sys.argv.remove('silent')
478
479
480def print_versions():
481    print
482    print '-=' * 38
483    print db.DB_VERSION_STRING
484    print 'bsddb.db.version():   %s' % (db.version(), )
485    if db.version() >= (5, 0) :
486        print 'bsddb.db.full_version(): %s' %repr(db.full_version())
487    print 'bsddb.db.__version__: %s' % db.__version__
488    print 'bsddb.db.cvsid:       %s' % db.cvsid
489
490    # Workaround for allowing generating an EGGs as a ZIP files.
491    suffix="__"
492    print 'py module:            %s' % getattr(bsddb, "__file"+suffix)
493    print 'extension module:     %s' % getattr(bsddb, "__file"+suffix)
494
495    print 'python version:       %s' % sys.version
496    print 'My pid:               %s' % os.getpid()
497    print '-=' * 38
498
499
500def get_new_path(name) :
501    get_new_path.mutex.acquire()
502    try :
503        import os
504        path=os.path.join(get_new_path.prefix,
505                name+"_"+str(os.getpid())+"_"+str(get_new_path.num))
506        get_new_path.num+=1
507    finally :
508        get_new_path.mutex.release()
509    return path
510
511def get_new_environment_path() :
512    path=get_new_path("environment")
513    import os
514    try:
515        os.makedirs(path,mode=0700)
516    except os.error:
517        test_support.rmtree(path)
518        os.makedirs(path)
519    return path
520
521def get_new_database_path() :
522    path=get_new_path("database")
523    import os
524    if os.path.exists(path) :
525        os.remove(path)
526    return path
527
528
529# This path can be overriden via "set_test_path_prefix()".
530import os, os.path
531get_new_path.prefix=os.path.join(os.environ.get("TMPDIR",
532    os.path.join(os.sep,"tmp")), "z-Berkeley_DB")
533get_new_path.num=0
534
535def get_test_path_prefix() :
536    return get_new_path.prefix
537
538def set_test_path_prefix(path) :
539    get_new_path.prefix=path
540
541def remove_test_path_directory() :
542    test_support.rmtree(get_new_path.prefix)
543
544if have_threads :
545    import threading
546    get_new_path.mutex=threading.Lock()
547    del threading
548else :
549    class Lock(object) :
550        def acquire(self) :
551            pass
552        def release(self) :
553            pass
554    get_new_path.mutex=Lock()
555    del Lock
556
557
558
559class PrintInfoFakeTest(unittest.TestCase):
560    def testPrintVersions(self):
561        print_versions()
562
563
564# This little hack is for when this module is run as main and all the
565# other modules import it so they will still be able to get the right
566# verbose setting.  It's confusing but it works.
567if sys.version_info[0] < 3 :
568    import test_all
569    test_all.verbose = verbose
570else :
571    import sys
572    print >>sys.stderr, "Work to do!"
573
574
575def suite(module_prefix='', timing_check=None):
576    test_modules = [
577        'test_associate',
578        'test_basics',
579        'test_dbenv',
580        'test_db',
581        'test_compare',
582        'test_compat',
583        'test_cursor_pget_bug',
584        'test_dbobj',
585        'test_dbshelve',
586        'test_dbtables',
587        'test_distributed_transactions',
588        'test_early_close',
589        'test_fileid',
590        'test_get_none',
591        'test_join',
592        'test_lock',
593        'test_misc',
594        'test_pickle',
595        'test_queue',
596        'test_recno',
597        'test_replication',
598        'test_sequence',
599        'test_thread',
600        ]
601
602    alltests = unittest.TestSuite()
603    for name in test_modules:
604        #module = __import__(name)
605        # Do it this way so that suite may be called externally via
606        # python's Lib/test/test_bsddb3.
607        module = __import__(module_prefix+name, globals(), locals(), name)
608
609        alltests.addTest(module.test_suite())
610        if timing_check:
611            alltests.addTest(unittest.makeSuite(timing_check))
612    return alltests
613
614
615def test_suite():
616    suite = unittest.TestSuite()
617    suite.addTest(unittest.makeSuite(PrintInfoFakeTest))
618    return suite
619
620
621if __name__ == '__main__':
622    print_versions()
623    unittest.main(defaultTest='suite')
624