Commit 5a4ef8d0 authored by William Stein's avatar William Stein

Add a new option "-e" or "--embed-positions" to Pyrex.

  -p, --embed-positions          If specified, the positions in Pyrex files of each
                                 function definition is embedded in its docstring.

This is very useful to support interactive viewing of *Pyrex* source
code in, e.g, IPython.
parent 4eca3f15
......@@ -3,6 +3,7 @@
#
import sys
import Options
usage = """\
Usage: pyrexc [options] sourcefile...
......@@ -12,6 +13,8 @@ Options:
-I, --include-dir <directory> Search for include files in named directory
(multiply include directories are allowed).
-o, --output-file <filename> Specify name of generated C file
-p, --embed-positions If specified, the positions in Pyrex files of each
function definition is embedded in its docstring.
"""
#The following experimental options are supported only on MacOSX:
# -C, --compile Compile generated .c file to .o file
......@@ -62,6 +65,8 @@ def parse_command_line(args):
options.include_path.append(pop_arg())
elif option in ("-o", "--output-file"):
options.output_file = pop_arg()
elif option in ("-p", "--embed-positions"):
Options.embed_pos_in_docstring = 1
else:
bad_usage()
else:
......
......@@ -18,6 +18,27 @@ import Options
from DebugFlags import debug_disposal_code
absolute_path_length = len(os.path.abspath('.'))
def relative_position(pos):
"""
We embed the relative filename in the generated C file, since we
don't want to have to regnerate and compile all the source code
whenever the Python install directory moves (which could happen,
e.g,. when distributing binaries.)
INPUT:
a position tuple -- (absolute filename, line number column position)
OUTPUT:
relative filename
line number
AUTHOR: William Stein
"""
return (pos[0][absolute_path_length+1:], pos[1])
class Node:
# pos (string, int, int) Source file position
# is_name boolean Is a NameNode
......@@ -2004,7 +2025,12 @@ class DefNode(FuncDefNode):
def declare_pyfunction(self, env):
self.entry = env.declare_pyfunction(self.name, self.pos)
self.entry.doc = self.doc
if Options.embed_pos_in_docstring:
self.entry.doc = 'File: %s (starting at line %s)'%relative_position(self.pos)
if not self.doc is None:
self.entry.doc = self.entry.doc + '\\n' + self.doc
else:
self.entry.doc = self.doc
self.entry.func_cname = \
Naming.func_prefix + env.scope_prefix + self.name
self.entry.doc_cname = \
......
......@@ -3,3 +3,5 @@
#
intern_names = 1 # Intern global variable and attribute names
embed_pos_in_docstring = 0
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment