Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'interpreter': 0.05; 'sys': 0.07; 'string': 0.09; 'defines': 0.09; 'exception.': 0.09; 'hooks': 0.09; 'raises': 0.09; 'runs': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; '*before*': 0.16; 'evaluating': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'exception': 0.16; 'wrote:': 0.18; 'module': 0.19; 'possible,': 0.19; 'seems': 0.21; 'example': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'source': 0.25; '>': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'message- id:@mail.gmail.com': 0.30; 'work.': 0.31; 'code': 0.31; 'trace': 0.31; 'could': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'two': 0.37; 'skip:& 10': 0.38; 'skip:& 20': 0.39; 'called': 0.40; 'details': 0.65; 'prompt': 0.68; 'press': 0.70; 'goto': 0.84; 'usage.': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=P8rUkGNcsEF/K7axAuyvbTMotYx968G+iEGEp1jQEvw=; b=P3pfu/BqFmquJreQDg9oOwi3ZzrruzjEFiWXhF3ls5nyRqPL6s333KJ8BGGFaK2Zsp ejln2yszxuTCzwDZt2COtEx5TNevWzyZxcK7KlXJvTFv8KSCQbGE2pMIZjyOHrHQiihT R2f+nCfrOH1kr72mRdkZscvK/aWk8TQ4gnUq7Me9Z3Yh+bkDr7eBSXhLY75A3m1uTr5P DZ6NHb3ba9wPdQqUDf8wFeTDaJOWIk3PuoJbTxGWlXTIQBtSlZVRyBaXIQfKNGbWtHAZ 64YFgjlK70dOVG4U9MsSEUTet7YE+XNH69acnI3OJAtjvMm9M8U/2WlLxHaQto+cRysg Cd1g== MIME-Version: 1.0 X-Received: by 10.224.174.6 with SMTP id r6mr18006338qaz.87.1370250035402; Mon, 03 Jun 2013 02:00:35 -0700 (PDT) In-Reply-To: <51ac4bfc$0$11118$c3e8da3@news.astraweb.com> References: <51ac4bfc$0$11118$c3e8da3@news.astraweb.com> Date: Mon, 3 Jun 2013 10:00:35 +0100 Subject: Re: Interactive interpreter hooks From: =?ISO-8859-1?Q?F=E1bio_Santos?= To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=485b397dd2059d386404de3c3221 Cc: python-list@python.org 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370250043 news.xs4all.nl 16007 [2001:888:2000:d::a6]:52300 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:46780 --485b397dd2059d386404de3c3221 Content-Type: text/plain; charset=ISO-8859-1 On 3 Jun 2013 09:04, "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([])". > 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. --485b397dd2059d386404de3c3221 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On 3 Jun 2013 09:04, "Steven D'Aprano" <steve+comp.lang.python@pearwood.inf= o> 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 hi= t 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 rec= reate the repl. This page seems to have good resources for that: http://stackoverflow.com/questions/1395913/python-drop-into-re= pl-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.=

--485b397dd2059d386404de3c3221--