Merge branch 'master' of https://github.com/xonsh/xonsh into xontrib_sh

This commit is contained in:
a 2020-10-10 00:36:36 +03:00
commit 8439aae833
4 changed files with 43 additions and 3 deletions

23
news/random.rst Normal file
View file

@ -0,0 +1,23 @@
**Added:**
* <news item>
**Changed:**
* <news item>
**Deprecated:**
* <news item>
**Removed:**
* Removed ``import random``.
**Fixed:**
* <news item>
**Security:**
* <news item>

View file

@ -79,6 +79,7 @@ from xonsh.tools import (
balanced_parens,
iglobpath,
all_permutations,
simple_random_choice,
)
from xonsh.environ import Env
@ -94,6 +95,15 @@ ENCODE_ENV_ONLY = {"XONSH_ENCODING_ERRORS": "strict"}
PATHEXT_ENV = {"PATHEXT": [".COM", ".EXE", ".BAT"]}
def test_random_choice():
lst = [1, 2, 3]
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]"
obs = subproc_toks("x", lexer=LEXER, returnline=True)

View file

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
"""The xonsh shell"""
import sys
import random
import time
import difflib
import builtins
@ -14,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, simple_random_choice
from xonsh.events import events
import xonsh.history.main as xhm
@ -158,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()

View file

@ -127,6 +127,14 @@ def _expandpath(path):
return expand_path(path, expand_user=expand_user)
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):
"""Tries to decode the bytes using XONSH_ENCODING if available,
otherwise using sys.getdefaultencoding().