From: William Stein Date: Wed, 25 Oct 2006 07:05:47 +0000 (-0500) Subject: Add a new option "-e" or "--embed-positions" to Pyrex. X-Git-Tag: 0.9.6.14~29^2~217 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=5a4ef8d04c11e32110fd57e0962a63b7281480e6;p=cython.git 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. --- diff --git a/Cython/Compiler/CmdLine.py b/Cython/Compiler/CmdLine.py index 9c2269f7..e21a04d4 100644 --- a/Cython/Compiler/CmdLine.py +++ b/Cython/Compiler/CmdLine.py @@ -3,6 +3,7 @@ # import sys +import Options usage = """\ Usage: pyrexc [options] sourcefile... @@ -12,6 +13,8 @@ Options: -I, --include-dir Search for include files in named directory (multiply include directories are allowed). -o, --output-file 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: diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 34e43509..ae42f5a6 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -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 = \ diff --git a/Cython/Compiler/Options.py b/Cython/Compiler/Options.py index 0754fa24..c24fcdf0 100644 --- a/Cython/Compiler/Options.py +++ b/Cython/Compiler/Options.py @@ -3,3 +3,5 @@ # intern_names = 1 # Intern global variable and attribute names + +embed_pos_in_docstring = 0