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 |
Fred Drake | dee86c6 | 2000-03-15 14:57:59 +0000 | [diff] [blame] | 7 | import sys |
| 8 | |
| 9 | import refcounts |
| 10 | |
| 11 | |
Fred Drake | 3764b6b | 2000-09-22 17:55:32 +0000 | [diff] [blame] | 12 | PREFIX_1 = r"\begin{cfuncdesc}{PyObject*}{" |
| 13 | PREFIX_2 = r"\begin{cfuncdesc}{PyVarObject*}{" |
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 = ["-"] |
Fred Drake | dee86c6 | 2000-03-15 14:57:59 +0000 | [diff] [blame] | 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 |
Fred Drake | 3764b6b | 2000-09-22 17:55:32 +0000 | [diff] [blame] | 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] |
Fred Drake | dee86c6 | 2000-03-15 14:57:59 +0000 | [diff] [blame] | 49 | try: |
| 50 | info = rcdict[s] |
| 51 | except KeyError: |
| 52 | sys.stderr.write("No refcount data for %s\n" % s) |
| 53 | else: |
Fred Drake | 0b25053 | 2000-09-12 20:47:29 +0000 | [diff] [blame] | 54 | if info.result_type in ("PyObject*", "PyVarObject*"): |
Fred Drake | 729eca1 | 2000-04-10 18:23:47 +0000 | [diff] [blame] | 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" |
Fred Drake | 0b25053 | 2000-09-12 20:47:29 +0000 | [diff] [blame] | 60 | line = (r"\begin{cfuncdesc}[%s]{%s}{" |
| 61 | % (rc, info.result_type)) \ |
Fred Drake | 3764b6b | 2000-09-22 17:55:32 +0000 | [diff] [blame] | 62 | + line[len(prefix):] |
Fred Drake | dee86c6 | 2000-03-15 14:57:59 +0000 | [diff] [blame] | 63 | output.write(line) |
| 64 | if infile != "-": |
| 65 | input.close() |
| 66 | if outfile != "-": |
| 67 | output.close() |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
| 71 | main() |