Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
vscode-python/python_files/pythonrc.py at main · love-be/vscode-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
love-be
vscode-python
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
vscode-python
/
python_files
/
pythonrc.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
137 lines (113 loc) · 5.29 KB
main
Breadcrumbs
vscode-python
/
python_files
/
pythonrc.py
Copy path
Top
File metadata and controls
Code
Blame
137 lines (113 loc) · 5.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
def
_initialize
():
import
platform
import
sys
if
sys
.
platform
!=
"win32"
:
import
readline
# Override regex to support OSC codes
if
sys
.
version_info
>=
(
3
,
13
):
import
re
import
_pyrepl
.
utils
_pyrepl
.
utils
.
ANSI_ESCAPE_SEQUENCE
=
re
.
compile
(
r"\x1b(?:\[[ -@]*[A-~]|\][^\x1b\x07]*(?:\x07|\x1b\\))"
)
original_ps1
=
">>> "
# PYTHONSTARTUP executes this file's code inside the user's __main__
# namespace, so PS1.__str__'s globals are the user's globals. If the
# user later shadows a name we rely on at prompt-render time (e.g.
# `int = 20`, `sys = 1`), a plain lookup would resolve to the user's
# value instead of ours and raise, silently killing the prompt.
#
# Capturing these as locals of _initialize (rather than as names left
# sitting in __main__) means PS1's methods reach them through closure
# cells, not global lookup - so there's no alias name in __main__ for
# user code to reassign and break in the first place.
_int
=
int
_bool
=
bool
_str
=
str
# https://code.visualstudio.com/docs/terminal/shell-integration#_supported-escape-sequences
class
ShellIntegrationSequence
:
soh
=
"
\001
"
stx
=
"
\002
"
template
=
"
\x1b
]633;{}
\x07
"
# Before the prompt (>>>) is displayed
@
classmethod
def
prompt_start
(
cls
)
->
str
:
return
cls
.
template
.
format
(
"A"
)
# After the prompt (>>>) is displayed
@
classmethod
def
prompt_end
(
cls
)
->
str
:
return
cls
.
template
.
format
(
"B"
)
# After the user has typed a command but before it is executed
@
classmethod
def
pre_execution
(
cls
)
->
str
:
return
cls
.
template
.
format
(
"C"
)
@
classmethod
def
execution_finished
(
cls
,
exit_code
:
int
)
->
str
:
"""Mark execution as finished with its exit code."""
return
cls
.
template
.
format
(
f"D;
{
exit_code
}
"
)
@
classmethod
def
command_line
(
cls
,
command
:
object
)
->
str
:
"""Explicitly set the command line interpreted by the shell."""
return
cls
.
template
.
format
(
f"E;
{
command
}
"
)
class
REPLHooks
:
def
__init__
(
self
):
self
.
global_exit
=
None
self
.
last_failure_flag
=
False
self
.
original_excepthook
=
sys
.
excepthook
self
.
original_displayhook
=
sys
.
displayhook
sys
.
excepthook
=
self
.
vscode_excepthook
sys
.
displayhook
=
self
.
vscode_displayhook
def
vscode_displayhook
(
self
,
value
):
if
value
is
None
:
self
.
last_failure_flag
=
False
self
.
original_displayhook
(
value
)
def
vscode_excepthook
(
self
,
type_
,
value
,
traceback
):
self
.
global_exit
=
value
self
.
last_failure_flag
=
True
self
.
original_excepthook
(
type_
,
value
,
traceback
)
def
get_last_command
():
# Get the last history item
last_command
=
""
if
sys
.
platform
!=
"win32"
:
last_command
=
readline
.
get_history_item
(
readline
.
get_current_history_length
())
return
last_command
class
PS1
:
hooks
=
REPLHooks
()
# str will get called for every prompt with exit code to show success/failure
def
__str__
(
self
):
last_exit_code
=
_int
(
_bool
(
self
.
hooks
.
last_failure_flag
))
self
.
hooks
.
last_failure_flag
=
False
# Guide following official VS Code doc for shell integration sequence:
result
=
""
# For non-windows allow recent_command history.
# fmt: off
if
sys
.
platform
!=
"win32"
:
result
=
"{soh}{pre_execution}{command_line}{execution_finished}{prompt_start}{stx}{prompt}{soh}{prompt_end}{stx}"
.
format
(
# noqa: UP032
soh
=
ShellIntegrationSequence
.
soh
,
pre_execution
=
ShellIntegrationSequence
.
pre_execution
(),
command_line
=
ShellIntegrationSequence
.
command_line
(
get_last_command
()),
execution_finished
=
ShellIntegrationSequence
.
execution_finished
(
last_exit_code
),
prompt_start
=
ShellIntegrationSequence
.
prompt_start
(),
stx
=
ShellIntegrationSequence
.
stx
,
prompt
=
original_ps1
,
prompt_end
=
ShellIntegrationSequence
.
prompt_end
(),
)
else
:
result
=
"{execution_finished}{prompt_start}{prompt}{prompt_end}{pre_execution}"
.
format
(
# noqa: UP032
execution_finished
=
ShellIntegrationSequence
.
execution_finished
(
last_exit_code
),
prompt_start
=
ShellIntegrationSequence
.
prompt_start
(),
prompt
=
original_ps1
,
prompt_end
=
ShellIntegrationSequence
.
prompt_end
(),
pre_execution
=
ShellIntegrationSequence
.
pre_execution
(),
)
# fmt: on
return
result
def
__repr__
(
self
):
return
"<Custom PS1 for VS Code Python Shell Integration>"
is_wsl
=
"microsoft-standard-WSL"
in
platform
.
release
()
if
not
is_wsl
and
(
sys
.
platform
!=
"win32"
or
sys
.
version_info
>=
(
3
,
13
)):
sys
.
ps1
=
PS1
()
ctrl_key
=
"Cmd"
if
sys
.
platform
==
"darwin"
else
"Ctrl"
print
(
f"
{
ctrl_key
}
click to launch VS Code Native REPL (https://aka.ms/python-native-repl)"
)
_initialize
()
del
_initialize
You can’t perform that action at this time.