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


Groups > comp.lang.python > #96288

Re: Context-aware return

Date 2015-09-10 20:56 +0200
From "Sven R. Kunze" <srkunze@mail.de>
Subject Re: Context-aware return
References <55f1c3c6$0$1659$c3e8da3$5496439d@news.astraweb.com> <55F1C5D6.7020202@mail.de> <85r3m66t0m.fsf@benfinney.id.au> <55F1CD23.5060009@mail.de> <55F1CFC0.6070505@mail.de>
Newsgroups comp.lang.python
Message-ID <mailman.336.1441911387.8327.python-list@python.org> (permalink)

Show all headers | View raw


Oops, missing print:

On 10.09.2015 20:45, Sven R. Kunze wrote:
> On 10.09.2015 20:34, Sven R. Kunze wrote:
>> You are right. I turned out to me harder that I first thought.
>>
>> My initial guess was like: use AST. But now I see, it would be hard 
>> to get the source code.
>>
>> So, what actually could work, would be faking the interactive 
>> interpreter wrapping it up and thus have control over the source code 
>> typed in.
>
> Ha, got it:  sys.settrace
>
> >>> def traceit(frame, event, arg):
...    print(frame, event, arg)
> ... return traceit
>
> >>> sys.settrace(traceit)
>
> >>> a=1
> (<frame object at 0x7ff9016785c0>, 'call', None)
> (<frame object at 0x7ff9016785c0>, 'line', None)
> (<frame object at 0x7ff9016785c0>, 'return', (u'a=1\n', 4))
> (<frame object at 0x7ff90168a1f8>, 'call', None)
> (<frame object at 0x7ff90168a1f8>, 'line', None)
> (<frame object at 0x7ff90168a1f8>, 'return', None)
>
>
> There you got the source. The use an AST to find out whether the line 
> fits your definition of 'interactive'.
>
> That should only be necessary when the top frame is interactive.
>
>
> Best,
> Sven

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


Thread

Context-aware return Steven D'Aprano <steve@pearwood.info> - 2015-09-11 03:54 +1000
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:03 +0200
  Re: Context-aware return Ben Finney <ben+python@benfinney.id.au> - 2015-09-11 04:12 +1000
  Re: Context-aware return Ben Finney <ben+python@benfinney.id.au> - 2015-09-11 04:14 +1000
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:21 +0200
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:24 +0200
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:34 +0200
  Re: Context-aware return Chris Angelico <rosuav@gmail.com> - 2015-09-11 04:39 +1000
  Re: Context-aware return Laura Creighton <lac@openend.se> - 2015-09-10 20:40 +0200
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:45 +0200
  Re: Context-aware return "Sven R. Kunze" <srkunze@mail.de> - 2015-09-10 20:56 +0200
  Re: Context-aware return Paul Rubin <no.email@nospam.invalid> - 2015-09-10 12:19 -0700
  Re: Context-aware return Grant Edwards <invalid@invalid.invalid> - 2015-09-10 19:23 +0000
    Re: Context-aware return Akira Li <4kir4.1i@gmail.com> - 2015-09-10 23:15 +0300
      Re: Context-aware return Grant Edwards <invalid@invalid.invalid> - 2015-09-10 20:27 +0000
    Re: Context-aware return random832@fastmail.us - 2015-09-10 16:42 -0400
    Re: Context-aware return Rustom Mody <rustompmody@gmail.com> - 2015-09-11 08:55 -0700
    Re: Context-aware return Rustom Mody <rustompmody@gmail.com> - 2015-09-11 08:55 -0700
  Re: Context-aware return Denis McMahon <denismfmcmahon@gmail.com> - 2015-09-11 00:41 +0000
    Re: Context-aware return Ned Batchelder <ned@nedbatchelder.com> - 2015-09-12 16:27 -0700
      Re: Context-aware return Steven D'Aprano <steve@pearwood.info> - 2015-09-17 12:31 +1000
  Re: Context-aware return Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-09-12 03:11 +0100

csiph-web