Sven R. Kunze
srkunze at mail.de
Thu Sep 10 20:56:08 CEST 2015
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.
More information about the Python-list