blob: 420f27133edc3f53a27c6a2c41b74a3a81a23b9d [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
Fred Drake0b250532000-09-12 20:47:29 +000013PREFIX = r"\begin{cfuncdesc}{Py(Var|)Object*}{"
Fred Drakedee86c62000-03-15 14:57:59 +000014
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:
Fred Drake0b250532000-09-12 20:47:29 +000051 if info.result_type in ("PyObject*", "PyVarObject*"):
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 Drake0b250532000-09-12 20:47:29 +000057 line = (r"\begin{cfuncdesc}[%s]{%s}{"
58 % (rc, info.result_type)) \
59 + line[prefix_len:]
Fred Drakedee86c62000-03-15 14:57:59 +000060 output.write(line)
61 if infile != "-":
62 input.close()
63 if outfile != "-":
64 output.close()
65
66
67if __name__ == "__main__":
68 main()