blob: 33f85fba9279a7b2073f53aa85f2097423417562 [file] [log] [blame]
Fred Drakedee86c62000-03-15 14:57:59 +00001#! /usr/bin/env python
2"""Add reference count annotations to the Python/C API Reference."""
3__version__ = '$Revision$'
4
5import getopt
6import os
7import string
8import sys
9
10import refcounts
11
12
13PREFIX = r"\begin{cfuncdesc}{PyObject*}{"
14
15
16def main():
17 rcfile = os.path.join(os.path.dirname(refcounts.__file__), os.pardir,
18 "api", "refcounts.dat")
19 outfile = "-"
20 opts, args = getopt.getopt(sys.argv[1:], "o:r:", ["output=", "refcounts="])
21 for opt, arg in opts:
22 if opt in ("-o", "--output"):
23 outfile = arg
24 elif opt in ("-r", "--refcounts"):
25 rcfile = arg
26 rcdict = refcounts.load(rcfile)
27 if outfile == "-":
28 output = sys.stdout
29 else:
30 output = open(outfile, "w")
31 if not args:
32 args = ["-"]
33 prefix = PREFIX
34 prefix_len = len(prefix)
35 for infile in args:
36 if infile == "-":
37 input = sys.stdin
38 else:
39 input = open(infile)
40 while 1:
41 line = input.readline()
42 if not line:
43 break
44 if line[:prefix_len] == prefix:
45 s = string.split(line[prefix_len:], '}', 1)[0]
46 try:
47 info = rcdict[s]
48 except KeyError:
49 sys.stderr.write("No refcount data for %s\n" % s)
50 else:
51 if info.result_type == "PyObject*":
52 rc = info.result_refs and "New" or "Borrowed"
53 line = r"\begin{cfuncdesc}[%s]{PyObject*}{" % rc \
54 + line[prefix_len:]
55 output.write(line)
56 if infile != "-":
57 input.close()
58 if outfile != "-":
59 output.close()
60
61
62if __name__ == "__main__":
63 main()