Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.05; 'attribute': 0.07; 'debugging': 0.07; 'element': 0.07; 'error:': 0.07; 'function,': 0.09; 'url:github': 0.09; 'python': 0.11; 'def': 0.12; 'wrote': 0.14; '2.7': 0.14; '"from': 0.16; '2.7:': 0.16; '__future__': 0.16; 'ah,': 0.16; 'ast': 0.16; 'expert,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'newline,': 0.16; 'node.': 0.16; 'skip:n 50': 0.16; 'statement.': 0.16; 'there?': 0.16; 'world!")': 0.16; 'wrote:': 0.18; '(not': 0.18; 'obviously': 0.18; 'import': 0.22; 'print': 0.22; 'comparing': 0.24; 'removed.': 0.24; 'skip:" 30': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'getting': 0.31; 'file': 0.32; 'probably': 0.32; 'url:python': 0.33; '(most': 0.33; 'used,': 0.33; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'keyword': 0.36; 'url:org': 0.36; 'jason': 0.38; 'nov': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'most': 0.60; 'url:3': 0.61; "you're": 0.61; 'special': 0.74; 'demand': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=o6MLvDzIFX8MCl4+8AgKEGrE2OOgMRTJpBHdLj736hA=; b=DmutvMnybGhlHVZroHedf1pQ/pyuhV0ycR16XyKk1/pb3R3HBCMHuY1otXZeaVspwj bXSUGvnUbOJj44RVngUq+1eRRzc2gladbZ9kdMesbkiEOK5uI3jYzdLHjbtE9J0NRC+u md/7yidGh2T8vnuiqv55uTFBUie9qS2h2N4a5Bc+VWwq4osQDwHqqgSCvczu68ypGOEY ZfjQ8dfvBihfJ6U3cpWZ8sJ7xTKTQgt8uQEEH93HxUfTbGTw/BvjmqTA4q2unI/aZj5e AzklzrLDR15j5F34dS3DneQEYfxSnv459DzTyeH+e1NReeOoM430RqG3OzmIAizhw2Tg LJ8g== MIME-Version: 1.0 X-Received: by 10.68.234.165 with SMTP id uf5mr12893340pbc.41.1383484804028; Sun, 03 Nov 2013 05:20:04 -0800 (PST) In-Reply-To: References: Date: Mon, 4 Nov 2013 00:20:03 +1100 Subject: Re: Debugging decorator From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1383485261 news.xs4all.nl 15943 [2001:888:2000:d::a6]:36182 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:58397 On Sun, Nov 3, 2013 at 9:55 PM, Jason Friedman wrote: > >> I wrote this decorator: https://gist.github.com/yasar11732/7163528 >> > I ran it with Python 2 and thought it was neat. > Most of my work is Python 3. > I ran 2to3-3.3 against it and I am getting this error: > > $ ./simple.py > Traceback (most recent call last): > File "./simple.py", line 3, in > @debugger.debugging > File "/home/jason/python/debugger.py", line 41, in debugging > new_function_body.append(make_print_node("function %s called" % > func.__name__)) > File "/home/jason/python/debugger.py", line 6, in make_print_node > return ast.Print(dest=None, values=[ast.Str(s=s)], nl=True) > AttributeError: 'module' object has no attribute 'Print' > > Comparing http://docs.python.org/2/library/ast.html#module-ast against > http://docs.python.org/3.3/library/ast.html#module-ast I see that "Print" > has indeed been removed. Ah, that'd be because 'print' is no longer a statement. Check out this function's disassembly: def hello_world(): print("Hello, world!") Python 2.7: 2 0 LOAD_CONST 1 ('Hello, world!') 3 PRINT_ITEM 4 PRINT_NEWLINE 5 LOAD_CONST 0 (None) 8 RETURN_VALUE Python 3.3: 2 0 LOAD_GLOBAL 0 (print) 3 LOAD_CONST 1 ('Hello, world!') 6 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 9 POP_TOP 10 LOAD_CONST 0 (None) 13 RETURN_VALUE As print is now a function, you're going to need to construct a function call element instead of a special 'print' node. I don't know how to do that as I'm not an AST expert, but hopefully you can work it out from there? If you need it to be cross-version, you could probably use sys.stdout.write explicitly (not forgetting to add a newline, which print does and write - obviously - doesn't). Or just demand that "from __future__ import print_function" be used, which will make 2.7 like 3.3. ChrisA