source: trunk/essentials/dev-lang/python/Doc/tools/refcounts.py@ 3315

Last change on this file since 3315 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 2.3 KB
Line 
1"""Support functions for loading the reference count data file."""
2__version__ = '$Revision: 35267 $'
3
4import os
5import sys
6
7
8# Determine the expected location of the reference count file:
9try:
10 p = os.path.dirname(__file__)
11except NameError:
12 p = os.path.dirname(sys.argv[0])
13p = os.path.normpath(os.path.join(os.getcwd(), p, os.pardir,
14 "api", "refcounts.dat"))
15DEFAULT_PATH = p
16del p
17
18
19def load(path=DEFAULT_PATH):
20 return loadfile(open(path))
21
22
23def 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
43 #
44 # Get the entry, creating it if needed:
45 #
46 try:
47 entry = d[function]
48 except KeyError:
49 entry = d[function] = Entry(function)
50 #
51 # Update the entry with the new parameter or the result information.
52 #
53 if arg:
54 entry.args.append((arg, type, refcount))
55 else:
56 entry.result_type = type