From 71e6dd37c05e151b42ee81c28959b32949fece5d Mon Sep 17 00:00:00 2001 From: a Date: Fri, 9 Oct 2020 18:57:36 +0300 Subject: [PATCH 1/6] random --- news/random.rst | 23 +++++++++++++++++++++++ xonsh/shell.py | 8 ++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 news/random.rst diff --git a/news/random.rst b/news/random.rst new file mode 100644 index 000000000..41bb9d04c --- /dev/null +++ b/news/random.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* Removed `import random`. + +**Fixed:** + +* + +**Security:** + +* diff --git a/xonsh/shell.py b/xonsh/shell.py index 880c57f02..9b5aa10c1 100644 --- a/xonsh/shell.py +++ b/xonsh/shell.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- """The xonsh shell""" import sys -import random import time import difflib import builtins @@ -77,6 +76,11 @@ Fires just after the prompt returns ) +def random_choice(lst): + """Returns random element from the list""" + return lst[int(time.time() % len(lst))] + + def transform_command(src, show_diff=True): """Returns the results of firing the precommand handles.""" i = 0 @@ -158,7 +162,7 @@ class Shell(object): elif env and env.get("TERM", "") == "dumb": shell_type = "dumb" elif shell_type == "random": - shell_type = random.choice(("readline", "prompt_toolkit")) + shell_type = random_choice(("readline", "prompt_toolkit")) if shell_type == "prompt_toolkit": if not has_prompt_toolkit(): use_vended_prompt_toolkit() From ee5b55535b91e124bee96f790eeac033299297f9 Mon Sep 17 00:00:00 2001 From: a Date: Fri, 9 Oct 2020 19:08:02 +0300 Subject: [PATCH 2/6] random_choice --- xonsh/shell.py | 9 ++------- xonsh/tools.py | 5 +++++ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/xonsh/shell.py b/xonsh/shell.py index 9b5aa10c1..f241330e6 100644 --- a/xonsh/shell.py +++ b/xonsh/shell.py @@ -13,7 +13,7 @@ from xonsh.platform import ( has_prompt_toolkit, minimum_required_ptk_version, ) -from xonsh.tools import XonshError, print_exception +from xonsh.tools import XonshError, print_exception, random_choice from xonsh.events import events import xonsh.history.main as xhm @@ -76,11 +76,6 @@ Fires just after the prompt returns ) -def random_choice(lst): - """Returns random element from the list""" - return lst[int(time.time() % len(lst))] - - def transform_command(src, show_diff=True): """Returns the results of firing the precommand handles.""" i = 0 @@ -202,7 +197,7 @@ class Shell(object): env = builtins.__xonsh__.env # build history backend before creating shell builtins.__xonsh__.history = hist = xhm.construct_history( - env=env.detype(), ts=[time.time(), None], locked=True + env=env.detype(), ts=[time(), None], locked=True ) shell_type = self.choose_shell_type(shell_type, env) diff --git a/xonsh/tools.py b/xonsh/tools.py index a73b874c1..028a5cac1 100644 --- a/xonsh/tools.py +++ b/xonsh/tools.py @@ -127,6 +127,11 @@ def _expandpath(path): return expand_path(path, expand_user=expand_user) +def random_choice(lst): + """Returns random element from the list""" + return lst[datetime.datetime.now().microsecond % len(lst)] + + def decode_bytes(b): """Tries to decode the bytes using XONSH_ENCODING if available, otherwise using sys.getdefaultencoding(). From d6ad3a4ae873e913355ef3ca5ce4bed3d42f9b02 Mon Sep 17 00:00:00 2001 From: a Date: Fri, 9 Oct 2020 19:10:23 +0300 Subject: [PATCH 3/6] random_choice --- xonsh/shell.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xonsh/shell.py b/xonsh/shell.py index f241330e6..1d6963ff7 100644 --- a/xonsh/shell.py +++ b/xonsh/shell.py @@ -197,7 +197,7 @@ class Shell(object): env = builtins.__xonsh__.env # build history backend before creating shell builtins.__xonsh__.history = hist = xhm.construct_history( - env=env.detype(), ts=[time(), None], locked=True + env=env.detype(), ts=[time.time(), None], locked=True ) shell_type = self.choose_shell_type(shell_type, env) From 87ce6bfc4d07e3dc113e3b446913216bb6e1b32a Mon Sep 17 00:00:00 2001 From: a Date: Fri, 9 Oct 2020 19:12:48 +0300 Subject: [PATCH 4/6] test --- tests/test_tools.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_tools.py b/tests/test_tools.py index 72766e900..1bbde8346 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -79,6 +79,7 @@ from xonsh.tools import ( balanced_parens, iglobpath, all_permutations, + random_choice, ) from xonsh.environ import Env @@ -94,6 +95,12 @@ ENCODE_ENV_ONLY = {"XONSH_ENCODING_ERRORS": "strict"} PATHEXT_ENV = {"PATHEXT": [".COM", ".EXE", ".BAT"]} +def test_random_choice(): + lst = [1, 2, 3] + r = random_choice(lst) + assert r in lst + + def test_subproc_toks_x(): exp = "![x]" obs = subproc_toks("x", lexer=LEXER, returnline=True) From 238602f5ca75ea9161eb4700cf1f808091e6dcec Mon Sep 17 00:00:00 2001 From: a Date: Fri, 9 Oct 2020 19:16:45 +0300 Subject: [PATCH 5/6] news --- news/random.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/news/random.rst b/news/random.rst index 41bb9d04c..db5d88180 100644 --- a/news/random.rst +++ b/news/random.rst @@ -12,7 +12,7 @@ **Removed:** -* Removed `import random`. +* Removed ``import random``. **Fixed:** From d88fdc6609ade2e7484bb066004489633dfeb129 Mon Sep 17 00:00:00 2001 From: a Date: Fri, 9 Oct 2020 22:08:10 +0300 Subject: [PATCH 6/6] simple_random_choice --- tests/test_tools.py | 7 +++++-- xonsh/shell.py | 4 ++-- xonsh/tools.py | 9 ++++++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/test_tools.py b/tests/test_tools.py index 1bbde8346..2c82267cf 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -79,7 +79,7 @@ from xonsh.tools import ( balanced_parens, iglobpath, all_permutations, - random_choice, + simple_random_choice, ) from xonsh.environ import Env @@ -97,9 +97,12 @@ PATHEXT_ENV = {"PATHEXT": [".COM", ".EXE", ".BAT"]} def test_random_choice(): lst = [1, 2, 3] - r = random_choice(lst) + r = simple_random_choice(lst) assert r in lst + with pytest.raises(ValueError): + simple_random_choice(range(1010101)) + def test_subproc_toks_x(): exp = "![x]" diff --git a/xonsh/shell.py b/xonsh/shell.py index 1d6963ff7..643cfeeac 100644 --- a/xonsh/shell.py +++ b/xonsh/shell.py @@ -13,7 +13,7 @@ from xonsh.platform import ( has_prompt_toolkit, minimum_required_ptk_version, ) -from xonsh.tools import XonshError, print_exception, random_choice +from xonsh.tools import XonshError, print_exception, simple_random_choice from xonsh.events import events import xonsh.history.main as xhm @@ -157,7 +157,7 @@ class Shell(object): elif env and env.get("TERM", "") == "dumb": shell_type = "dumb" elif shell_type == "random": - shell_type = random_choice(("readline", "prompt_toolkit")) + shell_type = simple_random_choice(("readline", "prompt_toolkit")) if shell_type == "prompt_toolkit": if not has_prompt_toolkit(): use_vended_prompt_toolkit() diff --git a/xonsh/tools.py b/xonsh/tools.py index 028a5cac1..18c249094 100644 --- a/xonsh/tools.py +++ b/xonsh/tools.py @@ -127,9 +127,12 @@ def _expandpath(path): return expand_path(path, expand_user=expand_user) -def random_choice(lst): - """Returns random element from the list""" - return lst[datetime.datetime.now().microsecond % len(lst)] +def simple_random_choice(lst): + """Returns random element from the list with length less than 1 million elements.""" + l = len(lst) + if l > 1000000: # microsecond maximum + raise ValueError("The list is too long.") + return lst[datetime.datetime.now().microsecond % l] def decode_bytes(b):