• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / ZODB / 18153960591

01 Oct 2025 06:50AM UTC coverage: 83.781% (-0.03%) from 83.811%
18153960591

Pull #415

github

web-flow
Update docs/articles/old-guide/convert_zodb_guide.py

Co-authored-by: Michael Howitz <icemac@gmx.net>
Pull Request #415: Apply the latest zope.meta templates

2441 of 3542 branches covered (68.92%)

193 of 257 new or added lines in 48 files covered. (75.1%)

12 existing lines in 6 files now uncovered.

13353 of 15938 relevant lines covered (83.78%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/ZODB/scripts/checkbtrees.py
1
#!/usr/bin/env python
2
"""Check the consistency of BTrees in a Data.fs
3

4
usage: checkbtrees.py data.fs
5

6
Try to find all the BTrees in a Data.fs, call their _check() methods,
7
and run them through BTrees.check.check().
8
"""
9

10
from BTrees.check import check
×
11

12
import ZODB
×
13
from ZODB.FileStorage import FileStorage
×
14

15

16
# Set of oids we've already visited.  Since the object structure is
17
# a general graph, this is needed to prevent unbounded paths in the
18
# presence of cycles.  It's also helpful in eliminating redundant
19
# checking when a BTree is pointed to by many objects.
20
oids_seen = {}
×
21

22
# Append (obj, path) to L if and only if obj is a persistent object
23
# and we haven't seen it before.
24

25

26
def add_if_new_persistent(L, obj, path):
×
27
    getattr(obj, '_', None)  # unghostify
×
28
    if hasattr(obj, '_p_oid'):
×
29
        oid = obj._p_oid
×
30
        if oid not in oids_seen:
×
31
            L.append((obj, path))
×
32
            oids_seen[oid] = 1
×
33

34

35
def get_subobjects(obj):
×
36
    getattr(obj, '_', None)  # unghostify
×
37
    sub = []
×
38
    try:
×
39
        attrs = obj.__dict__.items()
×
40
    except AttributeError:
×
41
        attrs = ()
×
42
    for pair in attrs:
×
43
        sub.append(pair)
×
44

45
    # what if it is a mapping?
46
    try:
×
47
        items = obj.items()
×
48
    except AttributeError:
×
49
        items = ()
×
50
    for k, v in items:
×
51
        if not isinstance(k, int):
×
52
            sub.append(("<key>", k))
×
53
        if not isinstance(v, int):
×
54
            sub.append(("[%s]" % repr(k), v))
×
55

56
    # what if it is a sequence?
57
    i = 0
×
UNCOV
58
    while 1:
×
59
        try:
×
60
            elt = obj[i]
×
61
        except:  # noqa: E722 do not use bare 'except'
×
62
            break
×
63
        sub.append(("[%d]" % i, elt))
×
64
        i += 1
×
65

66
    return sub
×
67

68

69
def main(fname=None):
×
70
    if fname is None:
×
71
        import sys
×
72
        try:
×
73
            fname, = sys.argv[1:]
×
74
        except:  # noqa: E722 do not use bare 'except'
×
75
            print(__doc__)
×
76
            sys.exit(2)
×
77

78
    fs = FileStorage(fname, read_only=1)
×
79
    cn = ZODB.DB(fs).open()
×
80
    rt = cn.root()
×
81
    todo = []
×
82
    add_if_new_persistent(todo, rt, '')
×
83

84
    found = 0
×
85
    while todo:
×
86
        obj, path = todo.pop(0)
×
87
        found += 1
×
88
        if not path:
×
89
            print("<root>", repr(obj))
×
90
        else:
91
            print(path, repr(obj))
×
92

93
        mod = str(obj.__class__.__module__)
×
94
        if mod.startswith("BTrees"):
×
95
            if hasattr(obj, "_check"):
×
96
                try:
×
97
                    obj._check()
×
98
                except AssertionError as msg:
×
99
                    print("*" * 60)
×
100
                    print(msg)
×
101
                    print("*" * 60)
×
102

103
                try:
×
104
                    check(obj)
×
105
                except AssertionError as msg:
×
106
                    print("*" * 60)
×
107
                    print(msg)
×
108
                    print("*" * 60)
×
109

110
        if found % 100 == 0:
×
111
            cn.cacheMinimize()
×
112

113
        for k, v in get_subobjects(obj):
×
114
            if k.startswith('['):
×
115
                # getitem
NEW
116
                newpath = f"{path}{k}"
×
117
            else:
NEW
118
                newpath = f"{path}.{k}"
×
119
            add_if_new_persistent(todo, v, newpath)
×
120

121
    print("total", len(fs._index), "found", found)
×
122

123

124
if __name__ == "__main__":
×
125
    main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc