jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/436820 )
Change subject: shell.py: call `sys.__interactivehook__()` if available
......................................................................
shell.py: call `sys.__interactivehook__()` if available
This pactch makes shell.py acts more similar to the built-in
interactive shell, where Python native code calls the hook. The
hook may be user-defined, or be the default implementation
defined by the site module of CPython 3.4+ Standard Library. The
site module is automatically imported if -S is not specified to
the Python interpreter.
The default implementation of the hook does all the magic of
loading readline, saving history, and tab-completion.
Unless the user defined their own sys.__interactivehook__, which
may include other features and/or exclude readline, and which we
honor in this patch, by default, this patch only works in CPython
3.4 or newer, after the fix of Python issue #5845 [1], and only on
Python installs where GNU readline is compiled into the CPython
interpreter binary. Other Python implementations, such as
IronPython, may also include their implementations of the hook,
and we try our best to simulate their built-in shell, but it is
not our main concern.
* In Windows, Python uses the ReadConsoleW() Windows API, which
has some cmd-like history functionality built-in. It is able to
use the history of a previous invokation of Python as long as
the terminal window stays open.
* In older versions of Python, sys.__interactivehook__ is not
defined by site.
* In newer versions of Python, but without readline compiled,
interactivehook will attempt to import readline but silently fail.
In these cases, you get the plain old interactive prompt, as if
this patch never existed.
In any case, this patch shall attempt to make the behavior of
shell.py closer to what would be provided if one invokes the
built-in Python interactive shell directly.
[1]
https://bugs.python.org/issue5845
Bug: T196168
Change-Id: I410c9717370cff70fa441f06500625956e8596fc
---
M scripts/shell.py
1 file changed, 9 insertions(+), 3 deletions(-)
Approvals:
jenkins-bot: Verified
Dvorapa: Looks good to me, but someone else must approve
Dalba: Looks good to me, approved
diff --git a/scripts/shell.py b/scripts/shell.py
index b119a6c..5333556 100755
--- a/scripts/shell.py
+++ b/scripts/shell.py
@@ -20,6 +20,9 @@
#
from __future__ import absolute_import, print_function, unicode_literals
+import code
+import sys
+
def main(*args):
"""Script entry point."""
@@ -38,12 +41,16 @@
print('{} arguments: {}\n' # noqa: T001
.format(warn_type, ', '.join(args)))
- import code
+ # Various stuffs in Python 3.4+, such as history file.
+ # This is defined in the site module of the Python Standard Library,
+ # and usually called by the built-in CPython interactive shell.
+ if hasattr(sys, '__interactivehook__'):
+ sys.__interactivehook__()
+
code.interact("""Welcome to the Pywikibot interactive
shell!""", local=env)
if __name__ == "__main__":
- import sys
if sys.platform == 'win32':
import os
os.system('title Python {} Shell'.format(*sys.version.split(' ',
1)))
@@ -51,5 +58,4 @@
args = []
if sys.argv and sys.argv[0].endswith(('shell', 'shell.py')):
args = sys.argv[1:]
- del sys
main(*args)
--
To view, visit
https://gerrit.wikimedia.org/r/436820
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I410c9717370cff70fa441f06500625956e8596fc
Gerrit-Change-Number: 436820
Gerrit-PatchSet: 6
Gerrit-Owner: Zhuyifei1999 <zhuyifei1999(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: Dvorapa <dvorapa(a)seznam.cz>
Gerrit-Reviewer: Framawiki <framawiki(a)tools.wmflabs.org>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zhuyifei1999 <zhuyifei1999(a)gmail.com>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot