Interactive interpreter hooks

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

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([])".
 
F

Fábio Santos

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.
 
T

Terry Jan Reedy

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.
 
R

Robert Kern

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top