Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20601 > unrolled thread
| Started by | Ross Boylan <ross@biostat.ucsf.edu> |
|---|---|
| First post | 2012-02-18 16:54 -0800 |
| Last post | 2012-02-20 11:27 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
#line in python Ross Boylan <ross@biostat.ucsf.edu> - 2012-02-18 16:54 -0800
Re: #line in python Duncan Booth <duncan.booth@invalid.invalid> - 2012-02-20 11:27 +0000
| From | Ross Boylan <ross@biostat.ucsf.edu> |
|---|---|
| Date | 2012-02-18 16:54 -0800 |
| Subject | #line in python |
| Message-ID | <mailman.5949.1329613948.27778.python-list@python.org> |
The ast module shows that elements of the syntax tree have line and column numbers. Would it be sensible to attempt to revise them to achieve effects like the #line directive in C? Context: Using noweb, a literate programming tool, which from a source file foo.nw produces foo.py. The lines in the two files may be in completely different sequenes. For debugging, it is useful to receive error reports that refer to the original line number in foo.nw. I am not sure how such rewriting would interact with debugger commands that set a breakpoint at a file and line number. I'm also not sure it would change the reported line numbers of errors. The lack of a file name could be problematic if multiple sources contributed to the same .py file, but that is an unlikely scenario. As an extension or alternate, could there be a decorator like @source_line(lineno, filename) for classes and methods that could do the conversion on the fly? I don't know if there's a way to go from the function (or class) object the decorator receives to the AST. Comments? Ross Boylan
[toc] | [next] | [standalone]
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Date | 2012-02-20 11:27 +0000 |
| Message-ID | <Xns9FFF74792B724duncanbooth@127.0.0.1> |
| In reply to | #20601 |
Ross Boylan <ross@biostat.ucsf.edu> wrote: > As an extension or alternate, could there be a decorator like > @source_line(lineno, filename) > for classes and methods that could do the conversion on the fly? I > don't know if there's a way to go from the function (or class) object > the decorator receives to the AST. > No [easy] way to go from bytecodes back to AST, but I see no reason why you can't create a new code object with your filename and line numbers and then create a new function using your modified code object. If you don't have a 1:1 correspondence of lines then you'll need to pick out all the existing line numbers from the code object co_lnotab and modify them: see dis.py findlinestarts() for how to do this. Classes would be harder: the decorator doesn't run until after the class body has executed, so you can't change the line numbers that way until it's too late. The only thing I can think would be to put all of the generated code inside a function and fix up that function with a decorator that scans the bytecode to find all contained classes and fix them up. Or you could generate a .pyc file and then fix up line numbers in the whole file: see http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html for some code that shows you what's in a .pyc -- Duncan Booth http://kupuguy.blogspot.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web