top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Interactive interpreter hooks in python

0 votes

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

posted Jun 3, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

2 Answers

+1 vote
Best answer

I dont know whether that is possible, but you could recreate the repl. This page seems to have good resources for that:

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.

answer Jun 3, 2013 by anonymous
0 votes

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

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

answer Jun 3, 2013 by anonymous
Similar Questions
+1 vote

I'm trying to work through Skienna's algorithms handbook, and note that the author often uses graphical representations of the diagrams to help understand (and even debug) the algorithms. I'd like to reproduce this in python.

How would you go about this? pyQt, pygame and pyglet immediately come to mind, but if I go that route the number of people that I can share my work with becomes quite limited, as compared to the portability of javascript projects.

I guess my question really is: has anyone had success creating an interactive graphical project in the browser using python?

+3 votes

I'm looking for a simple Address Book program written in Python, I've looked at addressbook 1.0.1 from PyPi but it doesn't quite do what I want. I may have to start with this and modify it but maybe there's something better out there.

In particular I want some sort of import/export ability and I'd really like some more configurability.

Any ideas anyone?

+2 votes

I have a pile of C code that I wrote that I want to interface to via the ctypes module ( ).

The C code uses the Boehm-Demers-Weiser garbage collector ( ) for all of its memory management. What I want to know is, who owns allocated memory? That is, if my C code allocates memory via GC_MALLOC() (the standard call for allocating memory in the garbage collector), and I access some object via ctypes in python, will the python garbage collector assume that it owns it and attempt to dispose of it when it goes out of scope?

Ideally, the memory is owned by the side that created it, with the other side simply referencing it, but I want to be sure before I invest a lot of time interfacing the two sides together.

Useful Links with Similar Problem
Contact Us
+91 9880187415
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.