From: Yuri Khan <yuri.v.khan@gmail.com>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: Emacs developers <emacs-devel@gnu.org>,
Richard Stallman <rms@gnu.org>,
thibaut.verron@gmail.com,
Stefan Monnier <monnier@iro.umontreal.ca>
Subject: Re: Python REPL using standard library functions
Date: Mon, 2 Nov 2020 18:40:14 +0700 [thread overview]
Message-ID: <CAP_d_8UGAW9SP73CxEWi84bTRqYC-5A4CQ+XWuW3BporjRW5Eg@mail.gmail.com> (raw)
In-Reply-To: <0920675e-8470-762d-42aa-c4456c8f7c2c@yandex.ru>
On Mon, 2 Nov 2020 at 16:50, Dmitry Gutov <dgutov@yandex.ru> wrote:
>
> On 02.11.2020 08:14, Yuri Khan wrote:
> > Python has ast.parse(), compile() and exec(), all three exposed in the
> > standard library.
>
> I'd be willing to bet that any given Python REPL doesn't use them, though.
One of the guiding principles of Python is “There must be one — and
preferably only one — obvious way to do it”. Implementing a REPL in
terms of ast.parse, compile() and exec is one obvious way to do it.
There is a catch that compile() with an option argument can be used
instead of ast.parse, so that’s another obvious way to do it. Both are
fully exposed to the user though.
Indeed, my limited experimentation shows I can augment these built-in
functions and they are immediately picked up by at least one REPL:
$ ipython3
In [1]: import ast
In [2]: old_ast_parse = ast.parse
In [3]: def my_parse(*args, **kwargs):
...: print('in ast.parse')
...: return old_ast_parse(*args, **kwargs)
...:
In [4]: ast.parse = my_parse
In [5]: 2+2
Out[5]: 4
# no effect so far
In [7]: old_compile = __builtins__.compile
In [8]: def my_compile(*args, **kwargs):
...: print('in compile')
...: return old_compile(*args, **kwargs)
...:
In [9]: __builtins__.compile = my_compile
In [10]: 2+2
in compile
in compile
in compile
in compile
in compile
in compile
Out[10]: 4
# now we are getting something,
# not sure why so many calls
In [11]: old_exec = __builtins__.exec
in compile
in compile
in compile
in compile
in compile
in compile
In [12]: def my_exec(*args, **kwargs):
...: print('in exec')
...: return old_exec(*args, **kwargs)
...:
in compile
in compile
in compile
in compile
in compile
in compile
In [13]: __builtins__.exec = my_exec
in compile
in compile
in compile
in compile
in compile
in compile
In [14]: 2+2
in compile
in compile
in compile
in compile
in compile
in compile
in exec
Out[14]: 4
next prev parent reply other threads:[~2020-11-02 11:40 UTC|newest]
Thread overview: 85+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-27 9:31 Standardizing more key bindings? Thibaut Verron
2020-09-27 15:57 ` Stefan Monnier
2020-09-28 3:44 ` Richard Stallman
2020-09-28 4:38 ` Thibaut Verron
2020-09-28 7:39 ` Thibaut Verron
2020-09-29 3:30 ` Richard Stallman
2020-09-29 5:07 ` Thibaut Verron
2020-09-29 10:36 ` Vasilij Schneidermann
2020-10-01 4:09 ` Richard Stallman
2020-10-01 5:20 ` Thibaut Verron
2020-09-29 21:56 ` Dmitry Gutov
2020-11-01 4:27 ` Richard Stallman
2020-11-01 6:56 ` References to "REPL" from past Jean Louis
2020-11-01 13:51 ` Standardizing more key bindings? Stefan Monnier
2020-11-02 5:41 ` Richard Stallman
2020-11-02 6:14 ` Yuri Khan
2020-11-02 8:08 ` tomas
2020-11-02 9:50 ` Dmitry Gutov
2020-11-02 11:40 ` Yuri Khan [this message]
2020-11-02 22:46 ` Python REPL using standard library functions Dmitry Gutov
2020-11-01 21:35 ` Standardizing more key bindings? Dmitry Gutov
2020-11-01 22:27 ` Drew Adams
2020-11-02 5:46 ` Richard Stallman
2020-09-29 21:58 ` Dmitry Gutov
2020-09-30 6:08 ` Thibaut Verron
2020-09-30 16:58 ` Opening Up More Keymaps " T.V Raman
2020-09-30 17:29 ` Thibaut Verron
2020-09-30 18:12 ` Robert Pluim
2020-09-30 18:16 ` Stefan Monnier
2020-09-30 18:35 ` T.V Raman
2020-09-30 18:41 ` Robert Pluim
2020-09-30 19:54 ` Stefan Monnier
2020-09-30 19:58 ` T.V Raman
2020-09-30 20:00 ` Noam Postavsky
2020-09-30 20:03 ` T.V Raman
2020-09-30 21:00 ` chad
2020-09-30 21:34 ` T.V Raman
2020-09-30 20:45 ` Stefan Monnier
2020-09-30 20:51 ` T.V Raman
2020-09-30 21:13 ` Gregory Heytings via Emacs development discussions.
2020-09-30 21:19 ` Stefan Monnier
2020-09-30 21:37 ` T.V Raman
2020-09-30 21:44 ` Stefan Monnier
2020-09-30 23:07 ` T.V Raman
2020-10-01 2:35 ` Eli Zaretskii
2020-10-01 3:27 ` Stefan Monnier
2020-10-01 12:38 ` Ergus
2020-10-01 14:17 ` Stefan Kangas
2020-10-01 14:45 ` Caio Henrique
2020-10-02 3:54 ` Richard Stallman
2020-10-02 10:43 ` Ergus
2020-10-04 19:34 ` Juri Linkov
2020-10-02 3:49 ` Richard Stallman
2020-10-02 6:56 ` Eli Zaretskii
2020-10-02 11:34 ` Ergus
2020-10-02 12:26 ` Eli Zaretskii
2020-10-04 3:38 ` Richard Stallman
2020-10-04 10:38 ` Thibaut Verron
2020-10-04 13:46 ` Alfred M. Szmidt
2020-10-04 16:24 ` Thibaut Verron
2020-10-04 17:00 ` Alfred M. Szmidt
2020-10-04 17:32 ` Thibaut Verron
2020-10-04 17:46 ` Alfred M. Szmidt
2020-10-05 3:11 ` Richard Stallman
2020-10-06 8:59 ` Lars Brinkhoff
2020-10-04 17:46 ` Alfred M. Szmidt
2020-10-05 3:13 ` Richard Stallman
2020-10-04 14:10 ` Howard Melman
2020-10-02 3:45 ` Richard Stallman
2020-10-02 6:26 ` Thibaut Verron
2020-10-04 3:39 ` Richard Stallman
2020-10-04 3:39 ` Richard Stallman
2020-10-02 6:52 ` Eli Zaretskii
2020-10-02 14:00 ` Stefan Monnier
2020-10-04 3:38 ` Richard Stallman
2020-10-04 7:16 ` Eli Zaretskii
2020-10-05 3:14 ` Richard Stallman
2020-10-02 13:56 ` Stefan Monnier
2020-10-03 2:57 ` Richard Stallman
2020-10-06 12:53 ` Nikolay Kudryavtsev
2020-10-06 13:27 ` Stefan Monnier
2020-10-06 14:24 ` Nikolay Kudryavtsev
2020-10-06 14:43 ` Stefan Monnier
2020-10-08 9:40 ` Nikolay Kudryavtsev
2020-10-07 4:19 ` Richard Stallman
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAP_d_8UGAW9SP73CxEWi84bTRqYC-5A4CQ+XWuW3BporjRW5Eg@mail.gmail.com \
--to=yuri.v.khan@gmail.com \
--cc=dgutov@yandex.ru \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
--cc=rms@gnu.org \
--cc=thibaut.verron@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).