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


Groups > comp.lang.python > #58397

Re: Debugging decorator

References <CAFEUn8YwMwB6m9LGqsXK8iijK_DRnwv88Xxp8VP_x=Hq2TUnhQ@mail.gmail.com> <CANy1k1iqXvTVY261EFc3u-FgzH4-+puMvNxahet9NAG9Bf+F8g@mail.gmail.com>
Date 2013-11-04 00:20 +1100
Subject Re: Debugging decorator
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1984.1383485261.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sun, Nov 3, 2013 at 9:55 PM, Jason Friedman <jsf80238@gmail.com> 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 <module>
>     @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

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


Thread

Re: Debugging decorator Chris Angelico <rosuav@gmail.com> - 2013-11-04 00:20 +1100

csiph-web