Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20623

Re: #line in python (dirty tricks)

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ross@biostat.ucsf.edu>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"if': 0.04; 'suppose': 0.05; 'exec': 0.07; 'space.': 0.07; 'decorator': 0.09; 'filename': 0.09; 'subject:python': 0.10; 'def': 0.13; '-----------': 0.15; 'ast': 0.16; 'ast,': 0.16; 'bytecode': 0.16; 'class)': 0.16; 'columns': 0.16; 'directive': 0.16; 'executed,': 0.16; 'filename)': 0.16; 'naive': 0.16; 'numbering': 0.16; 'prepend': 0.16; 'rewrites': 0.16; 'x-mailer:evolution 2.22.3.1': 0.16; 'syntax': 0.16; 'extension': 0.17; 'url:blog': 0.17; 'wrote:': 0.18; 'modified': 0.18; 'rewrite': 0.18; 'appears': 0.19; 'cc:no real name:2**0': 0.21; 'wrote': 0.21; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; '(or': 0.22; 'string': 0.24; 'receives': 0.24; 'fix': 0.25; 'modify': 0.25; 'cc:2**0': 0.26; 'classes': 0.26; 'code': 0.26; 'up.': 0.26; 'function': 0.27; 'import': 0.27; 'putting': 0.28; 'bit': 0.28; 'second': 0.28; 'pass': 0.29; 'class': 0.29; 'lines': 0.30; 'skip:_ 70': 0.30; 'booth': 0.30; 'object.': 0.30; 'changes': 0.30; 'file.': 0.31; 'there': 0.33; 'skip:- 30': 0.33; 'this.': 0.33; "can't": 0.33; 'it.': 0.33; 'too': 0.33; 'object': 0.33; 'file': 0.34; '---------': 0.34; 'numbers.': 0.34; 'succeeded': 0.34; 'to:addr:python-list': 0.35; 'run': 0.37; 'but': 0.37; 'shows': 0.37; 'using': 0.37; 'could': 0.38; 'some': 0.38; 'think': 0.38; 'itself.': 0.39; 'goes': 0.39; 'subject: (': 0.40; 'point': 0.40; 'put': 0.40; 'change': 0.40; 'might': 0.40; 'to:addr:python.org': 0.40; 'back': 0.60; "you'll": 0.61; 'body': 0.61; 'your': 0.61; 'header:Message-Id:1': 0.62; 'leading': 0.62; 'here': 0.64; '(after': 0.67; 'concept': 0.74; 'received:38': 0.76; 'protect': 0.79; 'received:10.0.2': 0.84; 'them:': 0.84; 'received:38.99': 0.91; 'subject:: #': 0.91; 'subject:tricks': 0.93
Subject Re: #line in python (dirty tricks)
From Ross Boylan <ross@biostat.ucsf.edu>
To python-list@python.org
In-Reply-To <1329612858.9780.13.camel@corn.betterworld.us>
References <1329612858.9780.13.camel@corn.betterworld.us>
Content-Type text/plain
Organization UCSF
Date Mon, 20 Feb 2012 07:08:55 -0800
Mime-Version 1.0
X-Mailer Evolution 2.22.3.1
Content-Transfer-Encoding 7bit
X-Spam_score -4.4
X-Spam_score_int -43
X-Spam_bar ----
X-Spam_report (-4.4 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -1.8 ALL_TRUSTED Passed through trusted hosts only via SMTP -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1% [score: 0.0000] 0.0 AWL AWL: From: address is in the auto white-list
Cc ross@biostat.ucsf.edu
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11.1329750566.3037.python-list@python.org> (permalink)
Lines 74
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1329750566 news.xs4all.nl 6972 [2001:888:2000:d::a6]:58863
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20623

Show key headers only | View raw


Duncan Booth wrote
________________________________________________________________________
> Ross Boylan <ross at 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.
Could you elaborate?  I don't understand what you are suggesting.
> 
> 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
> 
My latest concept is to produce code that rewrites itself.  Suppose the
naive file would be
----------- mycode.py (naive) --------------------------------
class SomeClass:
    "class comment"

    def some_function(self, bar):
        pass
--------- end -------------------------------

Protect that code by putting  an "if 0:" in front of it and indenting
each line one space.  Than prepend a bit of code to do the rewriting,
and add indicators of the original line numbers.

----------- mycode.py (after wrapping) -------------
from detangle import detangle
detangle("mycode.py", "mycode.nw")
if 0:
 # original code goes here
 class SomeClass:
    "class comment"
 #and when line numbering changes
 #line 35
    def some_function(self, bar):
       pass
------------- end -------------------
I would write detangle so that it scans through the file in which it
appears (named in the first argument), rewriting so that it appears to
come from the original file (mycode.nw) given in the second argument.

The scanning would look for the "if 0:" in the file.  At that point it
would accumulate code by reading lines and stripping the leading space.
If it found a #line directive it would remember it and then remove it
from the string it was accumulating.  Finally, detangle would would
pass the string of code to ast.compile, catching any syntax errors and
rewriting the file and line number (I might rewrite columns too with an
extension) and then rethrowing them.

If compilation succeeded detangle could rewrite the AST and then exec
it.

Ross

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: #line in python (dirty tricks) Ross Boylan <ross@biostat.ucsf.edu> - 2012-02-20 07:08 -0800
  Re: #line in python (dirty tricks) Duncan Booth <duncan.booth@invalid.invalid> - 2012-02-20 18:54 +0000

csiph-web