blob: b4b7f79995d5e5abf2d7fb7233bcf0254210ab1a [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
Fred Drakedee86c62000-03-15 14:57:59 +00007import sys
8
9import refcounts
10
11
Fred Drake3764b6b2000-09-22 17:55:32 +000012PREFIX_1 = r"\begin{cfuncdesc}{PyObject*}{"
13PREFIX_2 = r"\begin{cfuncdesc}{PyVarObject*}{"
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 = ["-"]
Fred Drakedee86c62000-03-15 14:57:59 +000033 for infile in args:
34 if infile == "-":
35 input = sys.stdin
36 else:
37 input = open(infile)
38 while 1:
39 line = input.readline()
40 if not line:
41 break
Fred Drake3764b6b2000-09-22 17:55:32 +000042 prefix = None
43 if line.startswith(PREFIX_1):
44 prefix = PREFIX_1
45 elif line.startswith(PREFIX_2):
46 prefix = PREFIX_2
47 if prefix:
48 s = line[len(prefix):].split('}', 1)[0]
Fred Drakedee86c62000-03-15 14:57:59 +000049 try:
50 info = rcdict[s]
51 except KeyError:
52 sys.stderr.write("No refcount data for %s\n" % s)
53 else:
Fred Drake0b250532000-09-12 20:47:29 +000054 if info.result_type in ("PyObject*", "PyVarObject*"):
Fred Drake729eca12000-04-10 18:23:47 +000055 if info.result_refs is None:
56 rc = "Always \NULL{}"
57 else:
58 rc = info.result_refs and "New" or "Borrowed"
59 rc = rc + " reference"
Fred Drake0b250532000-09-12 20:47:29 +000060 line = (r"\begin{cfuncdesc}[%s]{%s}{"
61 % (rc, info.result_type)) \
Fred Drake3764b6b2000-09-22 17:55:32 +000062 + line[len(prefix):]
Fred Drakedee86c62000-03-15 14:57:59 +000063 output.write(line)
64 if infile != "-":
65 input.close()
66 if outfile != "-":
67 output.close()
68
69
70if __name__ == "__main__":
71 main()