blob: eddb3eb796c2a7d045b12deaf75b155be7270b4d [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*":
Fred Drake729eca12000-04-10 18:23:47 +000052 if info.result_refs is None:
53 rc = "Always \NULL{}"
54 else:
55 rc = info.result_refs and "New" or "Borrowed"
56 rc = rc + " reference"
Fred Drakedee86c62000-03-15 14:57:59 +000057 line = r"\begin{cfuncdesc}[%s]{PyObject*}{" % rc \
58 + line[prefix_len:]
59 output.write(line)
60 if infile != "-":
61 input.close()
62 if outfile != "-":
63 output.close()
64
65
66if __name__ == "__main__":
67 main()