From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuri Khan Newsgroups: gmane.emacs.devel Subject: Re: Python REPL using standard library functions Date: Mon, 2 Nov 2020 18:40:14 +0700 Message-ID: References: <0920675e-8470-762d-42aa-c4456c8f7c2c@yandex.ru> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22257"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Emacs developers , Richard Stallman , thibaut.verron@gmail.com, Stefan Monnier To: Dmitry Gutov Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Mon Nov 02 12:41:08 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kZYCy-0005gQ-47 for ged-emacs-devel@m.gmane-mx.org; Mon, 02 Nov 2020 12:41:08 +0100 Original-Received: from localhost ([::1]:34546 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kZYCx-0004iB-5I for ged-emacs-devel@m.gmane-mx.org; Mon, 02 Nov 2020 06:41:07 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:48538) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZYCN-0004HN-Ht for emacs-devel@gnu.org; Mon, 02 Nov 2020 06:40:31 -0500 Original-Received: from mail-vs1-xe34.google.com ([2607:f8b0:4864:20::e34]:36498) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kZYCK-0005O1-DC; Mon, 02 Nov 2020 06:40:31 -0500 Original-Received: by mail-vs1-xe34.google.com with SMTP id h5so7288097vsp.3; Mon, 02 Nov 2020 03:40:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Z57rOvAIkK9P9wVYASbhGpOIT16uh8++I+OdNhKpl5c=; b=Ao4v/NH0Sp5wc95caYaD2Kt41wLVQHiyk0s+XhqZowHajC1qthGQwlrvj7bXYoU+kq /nTwLfepmFeXqllUR6y6X93Yf0n+kWSBFw2DNIQ5s0AqRhpfTwvxfEgCZWw5M7gF3u4Z hD/0Uqe3EEFflkN/eZ7RcyGt76vYQv6FPD3daoA6e6hjrIOmD5WsUDBvjW2QhO8CxlhX 76aFGkVDYtUjn4uVpZrcgMLZM9WTA7Z9m5+L2fheOoq3cmUhDTImMHSii2UlXYBNkAwC kbL1xUAUBgle4Lla6AUHLvtMHl9B3VX3SRr6BW6e9XG8xAF2DMncA868wiLADQnqtFwa 8geQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Z57rOvAIkK9P9wVYASbhGpOIT16uh8++I+OdNhKpl5c=; b=fNQhKO1DjOcxLsMsUdCliwLDgtN6rVyV7ILc54SLixGtH5N5KSy9/3cOnrhoGr/d7R J1xzeawrs1Cf/5sLvgySdyzLkIaDP1TFvyH46K9hRii3gz/l8KxZ0x7OmkCUZDPmoZbY bI/SCV5NtSSACfCGzxN94ZXj8tNt23C6cThUkCH8DJMYXOQnQd7VuDDUhb6iiQr7Y24X ptVDkyxyKa3lf/i1BIkrWqWSTYosu0qjbCPbiSslkSc6QR+r1EQTHTL/kESrTS5S7MJR 5w14/Pyluwj9rdqqTi8iSbJcHVzFbuMLZPboC7Jdv06bXvm2YpbK26BzL7mLCdshFwOs 1nJQ== X-Gm-Message-State: AOAM532v1rT1kGZOGYMcUPxHuCF4XT9sP7M6IOYZ5iPrd5CciNse6Z4L 83nl7cuBVADFhNv6RRNqpaGnDYTlW142mFyTLMvQ9Qk3 X-Google-Smtp-Source: ABdhPJzMV9K0CWRfOCDbhsMpXWXDDvuotqgPC4MjBtusSUwP43PHw69RF8Z7rYBU0wFDTgKQqqX8OqrhLWzLINwtE/o= X-Received: by 2002:a67:2c53:: with SMTP id s80mr2378618vss.12.1604317226044; Mon, 02 Nov 2020 03:40:26 -0800 (PST) In-Reply-To: <0920675e-8470-762d-42aa-c4456c8f7c2c@yandex.ru> Received-SPF: pass client-ip=2607:f8b0:4864:20::e34; envelope-from=yurivkhan@gmail.com; helo=mail-vs1-xe34.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:258626 Archived-At: On Mon, 2 Nov 2020 at 16:50, Dmitry Gutov 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 =E2=80=9CThere must be one =E2= =80=94 and preferably only one =E2=80=94 obvious way to do it=E2=80=9D. 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=E2=80=99s 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 =3D ast.parse In [3]: def my_parse(*args, **kwargs): ...: print('in ast.parse') ...: return old_ast_parse(*args, **kwargs) ...: In [4]: ast.parse =3D my_parse In [5]: 2+2 Out[5]: 4 # no effect so far In [7]: old_compile =3D __builtins__.compile In [8]: def my_compile(*args, **kwargs): ...: print('in compile') ...: return old_compile(*args, **kwargs) ...: In [9]: __builtins__.compile =3D 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 =3D __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 =3D 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