Jean-Paul Calderone | 897bc25 | 2008-02-18 20:50:23 -0500 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Add reference count annotations to the Python/C API Reference.""" |
| 3 | __version__ = '$Revision: 1.1.1.1 $' |
| 4 | |
| 5 | import getopt |
| 6 | import os |
| 7 | import sys |
| 8 | |
| 9 | import refcounts |
| 10 | |
| 11 | |
| 12 | PREFIX_1 = r"\begin{cfuncdesc}{PyObject*}{" |
| 13 | PREFIX_2 = r"\begin{cfuncdesc}{PyVarObject*}{" |
| 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 | 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 |
| 42 | 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] |
| 49 | try: |
| 50 | info = rcdict[s] |
| 51 | except KeyError: |
| 52 | sys.stderr.write("No refcount data for %s\n" % s) |
| 53 | else: |
| 54 | if info.result_type in ("PyObject*", "PyVarObject*"): |
| 55 | 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" |
| 60 | line = (r"\begin{cfuncdesc}[%s]{%s}{" |
| 61 | % (rc, info.result_type)) \ |
| 62 | + line[len(prefix):] |
| 63 | output.write(line) |
| 64 | if infile != "-": |
| 65 | input.close() |
| 66 | if outfile != "-": |
| 67 | output.close() |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | main() |