diff --git a/python_files/pythonrc.py b/python_files/pythonrc.py index cb2d7f22f429..d016cba4f5c5 100644 --- a/python_files/pythonrc.py +++ b/python_files/pythonrc.py @@ -9,12 +9,13 @@ def _initialize(): if sys.version_info >= (3, 13): import re - import _pyrepl.simple_interact import _pyrepl.utils - _pyrepl.utils.ANSI_ESCAPE_SEQUENCE = re.compile( - r"\x1b(?:\[[ -@]*[A-~]|\][^\x1b\x07]*(?:\x07|\x1b\\))" - ) + _ansi_escape_sequence = re.compile(r"\x1b(?:\[[ -@]*[A-~]|\][^\x1b\x07]*(?:\x07|\x1b\\))") + _pyrepl.utils.ANSI_ESCAPE_SEQUENCE = _ansi_escape_sequence + + # Import again to ensure the module is loaded after patching the ANSI_ESCAPE_SEQUENCE + import _pyrepl.simple_interact # Patch _pyrepl.simple_interact.multiline_input to ensure prompts are strings _original_multiline_input = _pyrepl.simple_interact.multiline_input diff --git a/python_files/tests/test_shell_integration.py b/python_files/tests/test_shell_integration.py index 49646e3e5cb3..a25672531696 100644 --- a/python_files/tests/test_shell_integration.py +++ b/python_files/tests/test_shell_integration.py @@ -115,6 +115,18 @@ def test_replacement_regex_preserves_csi_handling(): ) +def test_replacement_regex_is_imported_by_pyrepl_modules(): + if sys.version_info < (3, 15): + return + + pyrepl_reader = importlib.import_module("_pyrepl.reader") + pyrepl_render = importlib.import_module("_pyrepl.render") + + assert pyrepl_reader.ANSI_ESCAPE_SEQUENCE.sub("", "\x1b]633;A\x07>>> ") == ">>> " + assert pyrepl_render.ANSI_ESCAPE_SEQUENCE.sub("", "\x1b]633;A\x07>>> ") == ">>> " + assert pyrepl_render.RenderLine.from_rendered_text("\x1b]633;A\x07>>> ").width == 4 + + def test_pyrepl_multiline_input_converts_prompts_to_strings(monkeypatch): if sys.version_info < (3, 13): return