Skip to content

Commit e27119b

Browse files
authored
Bring back REPL shell integration decoration for Mac and Linux (#22714)
Bring back and enable shell integration decoration for Mac and Linux while fixing Windows pwsh problem. Reverts: #22578
1 parent 97154eb commit e27119b

File tree

2 files changed

+72
-72
lines changed

2 files changed

+72
-72
lines changed

pythonFiles/pythonrc.py

+41-41
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
# import sys
1+
import sys
22

3-
# original_ps1 = ">>> "
3+
original_ps1 = ">>> "
44

55

6-
# class repl_hooks:
7-
# def __init__(self):
8-
# self.global_exit = None
9-
# self.failure_flag = False
10-
# self.original_excepthook = sys.excepthook
11-
# self.original_displayhook = sys.displayhook
12-
# sys.excepthook = self.my_excepthook
13-
# sys.displayhook = self.my_displayhook
6+
class repl_hooks:
7+
def __init__(self):
8+
self.global_exit = None
9+
self.failure_flag = False
10+
self.original_excepthook = sys.excepthook
11+
self.original_displayhook = sys.displayhook
12+
sys.excepthook = self.my_excepthook
13+
sys.displayhook = self.my_displayhook
1414

15-
# def my_displayhook(self, value):
16-
# if value is None:
17-
# self.failure_flag = False
15+
def my_displayhook(self, value):
16+
if value is None:
17+
self.failure_flag = False
1818

19-
# self.original_displayhook(value)
19+
self.original_displayhook(value)
2020

21-
# def my_excepthook(self, type, value, traceback):
22-
# self.global_exit = value
23-
# self.failure_flag = True
21+
def my_excepthook(self, type, value, traceback):
22+
self.global_exit = value
23+
self.failure_flag = True
2424

25-
# self.original_excepthook(type, value, traceback)
25+
self.original_excepthook(type, value, traceback)
2626

2727

28-
# class ps1:
29-
# hooks = repl_hooks()
30-
# sys.excepthook = hooks.my_excepthook
31-
# sys.displayhook = hooks.my_displayhook
28+
class ps1:
29+
hooks = repl_hooks()
30+
sys.excepthook = hooks.my_excepthook
31+
sys.displayhook = hooks.my_displayhook
3232

33-
# # str will get called for every prompt with exit code to show success/failure
34-
# def __str__(self):
35-
# exit_code = 0
36-
# if self.hooks.failure_flag:
37-
# exit_code = 1
38-
# else:
39-
# exit_code = 0
33+
# str will get called for every prompt with exit code to show success/failure
34+
def __str__(self):
35+
exit_code = 0
36+
if self.hooks.failure_flag:
37+
exit_code = 1
38+
else:
39+
exit_code = 0
4040

41-
# # Guide following official VS Code doc for shell integration sequence:
42-
# # result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format(
43-
# # command_finished="\x1b]633;D;" + str(exit_code) + "\x07",
44-
# # prompt_started="\x1b]633;A\x07",
45-
# # prompt=original_ps1,
46-
# # command_start="\x1b]633;B\x07",
47-
# # command_executed="\x1b]633;C\x07",
48-
# # )
49-
# 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)}"
41+
# Guide following official VS Code doc for shell integration sequence:
42+
# result = "{command_finished}{prompt_started}{prompt}{command_start}{command_executed}".format(
43+
# command_finished="\x1b]633;D;" + str(exit_code) + "\x07",
44+
# prompt_started="\x1b]633;A\x07",
45+
# prompt=original_ps1,
46+
# command_start="\x1b]633;B\x07",
47+
# command_executed="\x1b]633;C\x07",
48+
# )
49+
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)}"
5050

51-
# return result
51+
return result
5252

5353

54-
# if sys.platform != "win32":
55-
# sys.ps1 = ps1()
54+
if sys.platform != "win32":
55+
sys.ps1 = ps1()
+31-31
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
# import importlib
2-
# from unittest.mock import Mock
1+
import importlib
2+
from unittest.mock import Mock
33

4-
# import pythonrc
4+
import pythonrc
55

66

7-
# def test_decoration_success():
8-
# importlib.reload(pythonrc)
9-
# ps1 = pythonrc.ps1()
7+
def test_decoration_success():
8+
importlib.reload(pythonrc)
9+
ps1 = pythonrc.ps1()
1010

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

1515

16-
# def test_decoration_failure():
17-
# importlib.reload(pythonrc)
18-
# ps1 = pythonrc.ps1()
16+
def test_decoration_failure():
17+
importlib.reload(pythonrc)
18+
ps1 = pythonrc.ps1()
1919

20-
# ps1.hooks.failure_flag = True
21-
# result = str(ps1)
20+
ps1.hooks.failure_flag = True
21+
result = str(ps1)
2222

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

2525

26-
# def test_displayhook_call():
27-
# importlib.reload(pythonrc)
28-
# pythonrc.ps1()
29-
# mock_displayhook = Mock()
26+
def test_displayhook_call():
27+
importlib.reload(pythonrc)
28+
pythonrc.ps1()
29+
mock_displayhook = Mock()
3030

31-
# hooks = pythonrc.repl_hooks()
32-
# hooks.original_displayhook = mock_displayhook
31+
hooks = pythonrc.repl_hooks()
32+
hooks.original_displayhook = mock_displayhook
3333

34-
# hooks.my_displayhook("mock_value")
34+
hooks.my_displayhook("mock_value")
3535

36-
# mock_displayhook.assert_called_once_with("mock_value")
36+
mock_displayhook.assert_called_once_with("mock_value")
3737

3838

39-
# def test_excepthook_call():
40-
# importlib.reload(pythonrc)
41-
# pythonrc.ps1()
42-
# mock_excepthook = Mock()
39+
def test_excepthook_call():
40+
importlib.reload(pythonrc)
41+
pythonrc.ps1()
42+
mock_excepthook = Mock()
4343

44-
# hooks = pythonrc.repl_hooks()
45-
# hooks.original_excepthook = mock_excepthook
44+
hooks = pythonrc.repl_hooks()
45+
hooks.original_excepthook = mock_excepthook
4646

47-
# hooks.my_excepthook("mock_type", "mock_value", "mock_traceback")
48-
# mock_excepthook.assert_called_once_with("mock_type", "mock_value", "mock_traceback")
47+
hooks.my_excepthook("mock_type", "mock_value", "mock_traceback")
48+
mock_excepthook.assert_called_once_with("mock_type", "mock_value", "mock_traceback")

0 commit comments

Comments
 (0)