Fred Drake | dee86c6 | 2000-03-15 14:57:59 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Add reference count annotations to the Python/C API Reference.""" |
| 3 | __version__ = '$Revision$' |
| 4 | |
| 5 | import getopt |
| 6 | import os |
| 7 | import string |
| 8 | import sys |
| 9 | |
| 10 | import refcounts |
| 11 | |
| 12 | |
Fred Drake | 0b25053 | 2000-09-12 20:47:29 +0000 | [diff] [blame] | 13 | PREFIX = r"\begin{cfuncdesc}{Py(Var|)Object*}{" |
Fred Drake | dee86c6 | 2000-03-15 14:57:59 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def 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 Drake | 0b25053 | 2000-09-12 20:47:29 +0000 | [diff] [blame] | 51 | if info.result_type in ("PyObject*", "PyVarObject*"): |
Fred Drake | 729eca1 | 2000-04-10 18:23:47 +0000 | [diff] [blame] | 52 | 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 Drake | 0b25053 | 2000-09-12 20:47:29 +0000 | [diff] [blame] | 57 | line = (r"\begin{cfuncdesc}[%s]{%s}{" |
| 58 | % (rc, info.result_type)) \ |
| 59 | + line[prefix_len:] |
Fred Drake | dee86c6 | 2000-03-15 14:57:59 +0000 | [diff] [blame] | 60 | output.write(line) |
| 61 | if infile != "-": |
| 62 | input.close() |
| 63 | if outfile != "-": |
| 64 | output.close() |
| 65 | |
| 66 | |
| 67 | if __name__ == "__main__": |
| 68 | main() |