Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'interpreter': 0.05; 'method.': 0.07; 'skip:` 10': 0.07; 'sys': 0.07; 'string': 0.09; 'defines': 0.09; 'exception.': 0.09; 'hooks': 0.09; 'raises': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runs': 0.10; '*before*': 0.16; 'class:': 0.16; 'evaluating': 0.16; 'kern': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'repl': 0.16; 'underlying': 0.16; 'exception': 0.16; 'wrote:': 0.18; 'module': 0.19; 'header:User-Agent:1': 0.23; 'interpret': 0.24; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'url:code': 0.29; 'robert': 0.30; "d'aprano": 0.31; 'steven': 0.31; 'this.': 0.32; 'url:python': 0.33; 'but': 0.35; 'there': 0.35; 'url:org': 0.36; 'two': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'our': 0.64; 'details': 0.65; 'world': 0.66; 'believe': 0.68; 'prompt': 0.68; 'press': 0.70; 'eco': 0.84; 'terrible': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Robert Kern Subject: Re: Interactive interpreter hooks Date: Mon, 03 Jun 2013 14:19:33 +0100 References: <51ac4bfc$0$11118$c3e8da3@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: 213.1.240.226 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130509 Thunderbird/17.0.6 In-Reply-To: <51ac4bfc$0$11118$c3e8da3@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370265589 news.xs4all.nl 16010 [2001:888:2000:d::a6]:41404 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46790 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