Exceptions raised during renaming in rotating file handlers are now passed to handleE...
[python.git] / Doc / tools / refcounts.py
blobccfc8c609e4acadc06df0502528d37ec4dcf48ad
1 """Support functions for loading the reference count data file."""
2 __version__ = '$Revision$'
4 import os
5 import sys
8 # Determine the expected location of the reference count file:
9 try:
10 p = os.path.dirname(__file__)
11 except NameError:
12 p = os.path.dirname(sys.argv[0])
13 p = os.path.normpath(os.path.join(os.getcwd(), p, os.pardir,
14 "api", "refcounts.dat"))
15 DEFAULT_PATH = p
16 del p
19 def load(path=DEFAULT_PATH):
20 return loadfile(open(path))
23 def loadfile(fp):
24 d = {}
25 while 1:
26 line = fp.readline()
27 if not line:
28 break
29 line = line.strip()
30 if line[:1] in ("", "#"):
31 # blank lines and comments
32 continue
33 parts = line.split(":", 4)
34 if len(parts) != 5:
35 raise ValueError("Not enough fields in %r" % line)
36 function, type, arg, refcount, comment = parts
37 if refcount == "null":
38 refcount = None
39 elif refcount:
40 refcount = int(refcount)
41 else:
42 refcount = None
44 # Get the entry, creating it if needed:
46 try:
47 entry = d[function]
48 except KeyError:
49 entry = d[function] = Entry(function)
51 # Update the entry with the new parameter or the result information.
53 if arg:
54 entry.args.append((arg, type, refcount))
55 else:
56 entry.result_type = type
57 entry.result_refs = refcount
58 return d
61 class Entry:
62 def __init__(self, name):
63 self.name = name
64 self.args = []
65 self.result_type = ''
66 self.result_refs = None
69 def dump(d):
70 """Dump the data in the 'canonical' format, with functions in
71 sorted order."""
72 items = d.items()
73 items.sort()
74 first = 1
75 for k, entry in items:
76 if first:
77 first = 0
78 else:
79 print
80 s = entry.name + ":%s:%s:%s:"
81 if entry.result_refs is None:
82 r = ""
83 else:
84 r = entry.result_refs
85 print s % (entry.result_type, "", r)
86 for t, n, r in entry.args:
87 if r is None:
88 r = ""
89 print s % (t, n, r)
92 def main():
93 d = load()
94 dump(d)
97 if __name__ == "__main__":
98 main()