| 
									
										
										
										
											2000-03-15 14:57:59 +00:00
										 |  |  | #! /usr/bin/env python | 
					
						
							|  |  |  | """Add reference count annotations to the Python/C API Reference.""" | 
					
						
							|  |  |  | __version__ = '$Revision$' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import getopt | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import refcounts | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-09-22 17:55:32 +00:00
										 |  |  | PREFIX_1 = r"\begin{cfuncdesc}{PyObject*}{" | 
					
						
							|  |  |  | PREFIX_2 = r"\begin{cfuncdesc}{PyVarObject*}{" | 
					
						
							| 
									
										
										
										
											2000-03-15 14:57:59 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     rcfile = os.path.join(os.path.dirname(refcounts.__file__), os.pardir, | 
					
						
							|  |  |  |                           "api", "refcounts.dat") | 
					
						
							|  |  |  |     outfile = "-" | 
					
						
							|  |  |  |     opts, args = getopt.getopt(sys.argv[1:], "o:r:", ["output=", "refcounts="]) | 
					
						
							|  |  |  |     for opt, arg in opts: | 
					
						
							|  |  |  |         if opt in ("-o", "--output"): | 
					
						
							|  |  |  |             outfile = arg | 
					
						
							|  |  |  |         elif opt in ("-r", "--refcounts"): | 
					
						
							|  |  |  |             rcfile = arg | 
					
						
							|  |  |  |     rcdict = refcounts.load(rcfile) | 
					
						
							|  |  |  |     if outfile == "-": | 
					
						
							|  |  |  |         output = sys.stdout | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         output = open(outfile, "w") | 
					
						
							|  |  |  |     if not args: | 
					
						
							|  |  |  |         args = ["-"] | 
					
						
							|  |  |  |     for infile in args: | 
					
						
							|  |  |  |         if infile == "-": | 
					
						
							|  |  |  |             input = sys.stdin | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             input = open(infile) | 
					
						
							|  |  |  |         while 1: | 
					
						
							|  |  |  |             line = input.readline() | 
					
						
							|  |  |  |             if not line: | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2000-09-22 17:55:32 +00:00
										 |  |  |             prefix = None | 
					
						
							|  |  |  |             if line.startswith(PREFIX_1): | 
					
						
							|  |  |  |                 prefix = PREFIX_1 | 
					
						
							|  |  |  |             elif line.startswith(PREFIX_2): | 
					
						
							|  |  |  |                 prefix = PREFIX_2 | 
					
						
							|  |  |  |             if prefix: | 
					
						
							|  |  |  |                 s = line[len(prefix):].split('}', 1)[0] | 
					
						
							| 
									
										
										
										
											2000-03-15 14:57:59 +00:00
										 |  |  |                 try: | 
					
						
							|  |  |  |                     info = rcdict[s] | 
					
						
							|  |  |  |                 except KeyError: | 
					
						
							|  |  |  |                     sys.stderr.write("No refcount data for %s\n" % s) | 
					
						
							|  |  |  |                 else: | 
					
						
							| 
									
										
										
										
											2000-09-12 20:47:29 +00:00
										 |  |  |                     if info.result_type in ("PyObject*", "PyVarObject*"): | 
					
						
							| 
									
										
										
										
											2000-04-10 18:23:47 +00:00
										 |  |  |                         if info.result_refs is None: | 
					
						
							|  |  |  |                             rc = "Always \NULL{}" | 
					
						
							|  |  |  |                         else: | 
					
						
							|  |  |  |                             rc = info.result_refs and "New" or "Borrowed" | 
					
						
							|  |  |  |                             rc = rc + " reference" | 
					
						
							| 
									
										
										
										
											2000-09-12 20:47:29 +00:00
										 |  |  |                         line = (r"\begin{cfuncdesc}[%s]{%s}{" | 
					
						
							|  |  |  |                                 % (rc, info.result_type)) \ | 
					
						
							| 
									
										
										
										
											2000-09-22 17:55:32 +00:00
										 |  |  |                                 + line[len(prefix):] | 
					
						
							| 
									
										
										
										
											2000-03-15 14:57:59 +00:00
										 |  |  |             output.write(line) | 
					
						
							|  |  |  |         if infile != "-": | 
					
						
							|  |  |  |             input.close() | 
					
						
							|  |  |  |     if outfile != "-": | 
					
						
							|  |  |  |         output.close() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     main() |