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


Groups > comp.lang.python > #46776 > unrolled thread

Interactive interpreter hooks

Started bySteven D'Aprano <steve+comp.lang.python@pearwood.info>
First post2013-06-03 07:55 +0000
Last post2013-06-03 14:19 +0100
Articles 4 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  Interactive interpreter hooks Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-06-03 07:55 +0000
    Re: Interactive interpreter hooks Fábio Santos <fabiosantosart@gmail.com> - 2013-06-03 10:00 +0100
    Re: Interactive interpreter hooks Terry Jan Reedy <tjreedy@udel.edu> - 2013-06-03 09:03 -0400
    Re: Interactive interpreter hooks Robert Kern <robert.kern@gmail.com> - 2013-06-03 14:19 +0100

#46776 — Interactive interpreter hooks

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-06-03 07:55 +0000
SubjectInteractive interpreter hooks
Message-ID<51ac4bfc$0$11118$c3e8da3@news.astraweb.com>
The sys module defines two hooks that are used in the interactive 
interpreter:

* sys.displayhook(value) gets called with the result of evaluating the 
line when you press ENTER;

* sys.excepthook(type, value, traceback) gets called with the details of 
the exception when your line raises an exception.

Is there a way to hook into the interactive interpreter *before* it is 
evaluated? That is, if I type "len([])" at the prompt and hit ENTER, I 
want a hook that runs before len([]) is evaluated to 0, so that I get the 
string "len([])".



-- 
Steven

[toc] | [next] | [standalone]


#46780

FromFábio Santos <fabiosantosart@gmail.com>
Date2013-06-03 10:00 +0100
Message-ID<mailman.2588.1370250043.3114.python-list@python.org>
In reply to#46776

[Multipart message — attachments visible in raw view] — view raw

On 3 Jun 2013 09:04, "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
wrote:
>
> The sys module defines two hooks that are used in the interactive
> interpreter:
>
> * sys.displayhook(value) gets called with the result of evaluating the
> line when you press ENTER;
>
> * sys.excepthook(type, value, traceback) gets called with the details of
> the exception when your line raises an exception.
>
> Is there a way to hook into the interactive interpreter *before* it is
> evaluated? That is, if I type "len([])" at the prompt and hit ENTER, I
> want a hook that runs before len([]) is evaluated to 0, so that I get the
> string "len([])".
>

I don't know whether that is possible, but you could recreate the repl.
This page seems to have good resources for that:
http://stackoverflow.com/questions/1395913/python-drop-into-repl-read-eval-print-loop

A trace function could also work. See docs for sys.settrace. The source
code for the python GOTO module is a good example of its usage.

[toc] | [prev] | [next] | [standalone]


#46789

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-06-03 09:03 -0400
Message-ID<mailman.2595.1370264612.3114.python-list@python.org>
In reply to#46776
On 6/3/2013 3:55 AM, Steven D'Aprano wrote:
> The sys module defines two hooks that are used in the interactive
> interpreter:
>
> * sys.displayhook(value) gets called with the result of evaluating the
> line when you press ENTER;
>
> * sys.excepthook(type, value, traceback) gets called with the details of
> the exception when your line raises an exception.
>
> Is there a way to hook into the interactive interpreter *before* it is
> evaluated? That is, if I type "len([])" at the prompt and hit ENTER, I
> want a hook that runs before len([]) is evaluated to 0, so that I get the
> string "len([])".

You have not said what you are actually trying to do, but you could 
definitely modify Idle to do something with user input before it sends 
it to the user process.



[toc] | [prev] | [next] | [standalone]


#46790

FromRobert Kern <robert.kern@gmail.com>
Date2013-06-03 14:19 +0100
Message-ID<mailman.2596.1370265589.3114.python-list@python.org>
In reply to#46776
On 2013-06-03 08:55, Steven D'Aprano wrote:
> The sys module defines two hooks that are used in the interactive
> interpreter:
>
> * sys.displayhook(value) gets called with the result of evaluating the
> line when you press ENTER;
>
> * sys.excepthook(type, value, traceback) gets called with the details of
> the exception when your line raises an exception.
>
> Is there a way to hook into the interactive interpreter *before* it is
> evaluated? That is, if I type "len([])" at the prompt and hit ENTER, I
> want a hook that runs before len([]) is evaluated to 0, so that I get the
> string "len([])".

You will need to write your own REPL for this. Use the code.InteractiveConsole 
class:

   http://docs.python.org/2/library/code

I recommend source-diving to see what you need to override, but I suspect you 
can just wrap around the `runsource()` method.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web