Add a new option "-e" or "--embed-positions" to Pyrex.
authorWilliam Stein <wstein@gmail.com>
Wed, 25 Oct 2006 07:05:47 +0000 (02:05 -0500)
committerWilliam Stein <wstein@gmail.com>
Wed, 25 Oct 2006 07:05:47 +0000 (02:05 -0500)
  -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.

Cython/Compiler/CmdLine.py
Cython/Compiler/Nodes.py
Cython/Compiler/Options.py

index 9c2269f749a382f4018d09373aa2da20f962be9a..e21a04d4423d7797090f249b30f9da8dfb7b27c2 100644 (file)
@@ -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:
index 34e43509520602109553e123d7ab73df4856daa5..ae42f5a6c67601354c48ee555548930d9e953e50 100644 (file)
@@ -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 = \
index 0754fa24e7f8fb490b22e2e0883cabce3843112a..c24fcdf0a18127dc3926bb14d415faa5840287be 100644 (file)
@@ -3,3 +3,5 @@
 #
 
 intern_names = 1   #  Intern global variable and attribute names
+
+embed_pos_in_docstring = 0