Skip to content

Commit 303ba58

Browse files
authored
Pythonrc: Don't register PyREPL hooks twice (#26145)
This pull request refactors the naming and usage of exception and display hooks in the `pythonrc.py` file to better reflect their association with VS Code integration. It also simplifies the REPL launch message and updates related tests to use the new hook names. **Refactoring and renaming for clarity:** * Renamed `my_excepthook` and `my_displayhook` methods in `REPLHooks` to `vscode_excepthook` and `vscode_displayhook`, and updated all references and assignments accordingly in `pythonrc.py`. * Updated test cases in `test_shell_integration.py` to use the new hook method names (`vscode_displayhook` and `vscode_excepthook`). [[1]](diffhunk://#diff-f900183871669133df435002b57b4e9e7b8d57779718d41d65427fccc833172bL49-R49) [[2]](diffhunk://#diff-f900183871669133df435002b57b4e9e7b8d57779718d41d65427fccc833172bL62-R62) **Prompt message simplification:** * Simplified the REPL launch message to dynamically use "Cmd" or "Ctrl" based on the platform, reducing duplicated code. Fixes #26146
1 parent 9edd2b3 commit 303ba58

2 files changed

Lines changed: 9 additions & 15 deletions

File tree

‎python_files/pythonrc.py‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ def __init__(self):
2929
self.failure_flag = False
3030
self.original_excepthook = sys.excepthook
3131
self.original_displayhook = sys.displayhook
32-
sys.excepthook = self.my_excepthook
33-
sys.displayhook = self.my_displayhook
32+
sys.excepthook = self.vscode_excepthook
33+
sys.displayhook = self.vscode_displayhook
3434

35-
def my_displayhook(self, value):
35+
def vscode_displayhook(self, value):
3636
if value is None:
3737
self.failure_flag = False
38-
3938
self.original_displayhook(value)
4039

41-
def my_excepthook(self, type_, value, traceback):
40+
def vscode_excepthook(self, type_, value, traceback):
4241
self.global_exit = value
4342
self.failure_flag = True
44-
4543
self.original_excepthook(type_, value, traceback)
4644

4745

@@ -50,14 +48,11 @@ def get_last_command():
5048
last_command = ""
5149
if sys.platform != "win32":
5250
last_command = readline.get_history_item(readline.get_current_history_length())
53-
5451
return last_command
5552

5653

5754
class PS1:
5855
hooks = REPLHooks()
59-
sys.excepthook = hooks.my_excepthook
60-
sys.displayhook = hooks.my_displayhook
6156

6257
# str will get called for every prompt with exit code to show success/failure
6358
def __str__(self):
@@ -101,7 +96,6 @@ def __repr__(self):
10196
if sys.platform != "win32" and (not is_wsl):
10297
sys.ps1 = PS1()
10398

104-
if sys.platform == "darwin":
105-
print("Cmd click to launch VS Code Native REPL (https://aka.ms/python-native-repl)")
106-
else:
107-
print("Ctrl click to launch VS Code Native REPL (https://aka.ms/python-native-repl)")
99+
ctrl_key = "Cmd" if sys.platform == "darwin" else "Ctrl"
100+
101+
print(f"{ctrl_key} click to launch VS Code Native REPL (https://aka.ms/python-native-repl)")

‎python_files/tests/test_shell_integration.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def test_displayhook_call():
4646
hooks = pythonrc.REPLHooks()
4747
hooks.original_displayhook = mock_displayhook
4848

49-
hooks.my_displayhook("mock_value")
49+
hooks.vscode_displayhook("mock_value")
5050

5151
mock_displayhook.assert_called_once_with("mock_value")
5252

@@ -59,7 +59,7 @@ def test_excepthook_call():
5959
hooks = pythonrc.REPLHooks()
6060
hooks.original_excepthook = mock_excepthook
6161

62-
hooks.my_excepthook("mock_type", "mock_value", "mock_traceback")
62+
hooks.vscode_excepthook("mock_type", "mock_value", "mock_traceback")
6363
mock_excepthook.assert_called_once_with("mock_type", "mock_value", "mock_traceback")
6464

6565

0 commit comments

Comments
 (0)