Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring back REPL shell integration decoration for Mac and Linux #22714

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions pythonFiles/pythonrc.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
# import sys
import sys

# original_ps1 = ">>> "
original_ps1 = ">>> "


# class repl_hooks:
# def __init__(self):
# self.global_exit = None
# self.failure_flag = False
# self.original_excepthook = sys.excepthook
# self.original_displayhook = sys.displayhook
# sys.excepthook = self.my_excepthook
# sys.displayhook = self.my_displayhook
class repl_hooks:
def __init__(self):
self.global_exit = None
self.failure_flag = False
self.original_excepthook = sys.excepthook
self.original_displayhook = sys.displayhook
sys.excepthook = self.my_excepthook
sys.displayhook = self.my_displayhook

# def my_displayhook(self, value):
# if value is None:
# self.failure_flag = False
def my_displayhook(self, value):
if value is None:
self.failure_flag = False

# self.original_displayhook(value)
self.original_displayhook(value)

# def my_excepthook(self, type, value, traceback):
# self.global_exit = value
# self.failure_flag = True
def my_excepthook(self, type, value, traceback):
self.global_exit = value
self.failure_flag = True

# self.original_excepthook(type, value, traceback)
self.original_excepthook(type, value, traceback)


# class ps1:
# hooks = repl_hooks()
# sys.excepthook = hooks.my_excepthook
# sys.displayhook = hooks.my_displayhook
class ps1:
hooks = repl_hooks()
sys.excepthook = hooks.my_excepthook
sys.displayhook = hooks.my_displayhook

# # str will get called for every prompt with exit code to show success/failure
# def __str__(self):
# exit_code = 0
# if self.hooks.failure_flag:
# exit_code = 1
# else:
# exit_code = 0
# str will get called for every prompt with exit code to show success/failure
def __str__(self):
exit_code = 0
if self.hooks.failure_flag:
exit_code = 1
else:
exit_code = 0

# # Guide following official VS Code doc for shell integration sequence:
# # result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format(
# # command_finished="\x1b]633;D;" + str(exit_code) + "\x07",
# # prompt_started="\x1b]633;A\x07",
# # prompt=original_ps1,
# # command_start="\x1b]633;B\x07",
# # command_executed="\x1b]633;C\x07",
# # )
# result = f"{chr(27)}]633;D;{exit_code}{chr(7)}{chr(27)}]633;A{chr(7)}{original_ps1}{chr(27)}]633;B{chr(7)}{chr(27)}]633;C{chr(7)}"
# Guide following official VS Code doc for shell integration sequence:
# result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format(
# command_finished="\x1b]633;D;" + str(exit_code) + "\x07",
# prompt_started="\x1b]633;A\x07",
# prompt=original_ps1,
# command_start="\x1b]633;B\x07",
# command_executed="\x1b]633;C\x07",
# )
result = f"{chr(27)}]633;D;{exit_code}{chr(7)}{chr(27)}]633;A{chr(7)}{original_ps1}{chr(27)}]633;B{chr(7)}{chr(27)}]633;C{chr(7)}"

# return result
return result


# if sys.platform != "win32":
# sys.ps1 = ps1()
if sys.platform != "win32":
sys.ps1 = ps1()
62 changes: 31 additions & 31 deletions pythonFiles/tests/test_shell_integration.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
# import importlib
# from unittest.mock import Mock
import importlib
from unittest.mock import Mock

# import pythonrc
import pythonrc


# def test_decoration_success():
# importlib.reload(pythonrc)
# ps1 = pythonrc.ps1()
def test_decoration_success():
importlib.reload(pythonrc)
ps1 = pythonrc.ps1()

# ps1.hooks.failure_flag = False
# result = str(ps1)
# assert result == "\x1b]633;D;0\x07\x1b]633;A\x07>>> \x1b]633;B\x07\x1b]633;C\x07"
ps1.hooks.failure_flag = False
result = str(ps1)
assert result == "\x1b]633;D;0\x07\x1b]633;A\x07>>> \x1b]633;B\x07\x1b]633;C\x07"


# def test_decoration_failure():
# importlib.reload(pythonrc)
# ps1 = pythonrc.ps1()
def test_decoration_failure():
importlib.reload(pythonrc)
ps1 = pythonrc.ps1()

# ps1.hooks.failure_flag = True
# result = str(ps1)
ps1.hooks.failure_flag = True
result = str(ps1)

# assert result == "\x1b]633;D;1\x07\x1b]633;A\x07>>> \x1b]633;B\x07\x1b]633;C\x07"
assert result == "\x1b]633;D;1\x07\x1b]633;A\x07>>> \x1b]633;B\x07\x1b]633;C\x07"


# def test_displayhook_call():
# importlib.reload(pythonrc)
# pythonrc.ps1()
# mock_displayhook = Mock()
def test_displayhook_call():
importlib.reload(pythonrc)
pythonrc.ps1()
mock_displayhook = Mock()

# hooks = pythonrc.repl_hooks()
# hooks.original_displayhook = mock_displayhook
hooks = pythonrc.repl_hooks()
hooks.original_displayhook = mock_displayhook

# hooks.my_displayhook("mock_value")
hooks.my_displayhook("mock_value")

# mock_displayhook.assert_called_once_with("mock_value")
mock_displayhook.assert_called_once_with("mock_value")


# def test_excepthook_call():
# importlib.reload(pythonrc)
# pythonrc.ps1()
# mock_excepthook = Mock()
def test_excepthook_call():
importlib.reload(pythonrc)
pythonrc.ps1()
mock_excepthook = Mock()

# hooks = pythonrc.repl_hooks()
# hooks.original_excepthook = mock_excepthook
hooks = pythonrc.repl_hooks()
hooks.original_excepthook = mock_excepthook

# hooks.my_excepthook("mock_type", "mock_value", "mock_traceback")
# mock_excepthook.assert_called_once_with("mock_type", "mock_value", "mock_traceback")
hooks.my_excepthook("mock_type", "mock_value", "mock_traceback")
mock_excepthook.assert_called_once_with("mock_type", "mock_value", "mock_traceback")
Loading