Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit dee86c6

Browse files
committed
Script to annotate api.tex with reference count information.
1 parent def77e5 commit dee86c6

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

Doc/tools/anno-api.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
13+
PREFIX = r"\begin{cfuncdesc}{PyObject*}{"
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:
51+
if info.result_type == "PyObject*":
52+
rc = info.result_refs and "New" or "Borrowed"
53+
line = r"\begin{cfuncdesc}[%s]{PyObject*}{" % rc \
54+
+ line[prefix_len:]
55+
output.write(line)
56+
if infile != "-":
57+
input.close()
58+
if outfile != "-":
59+
output.close()
60+
61+
62+
if __name__ == "__main__":
63+
main()

0 commit comments

Comments
 (0)