Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Tools/patchcheck/patchcheck.py at main · python/cpython · 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 }}
Uh oh!
There was an error while loading.
Please reload this page
.
python
/
cpython
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
35.4k
Star
77.2k
Code
Issues
5k+
Pull requests
2.6k
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
main
Breadcrumbs
cpython
/
Tools
/
patchcheck
/
patchcheck.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
230 lines (196 loc) · 7.86 KB
main
Breadcrumbs
cpython
/
Tools
/
patchcheck
/
patchcheck.py
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
230 lines (196 loc) · 7.86 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
"""Check proposed changes for common issues."""
import
sys
import
os
.
path
import
subprocess
import
sysconfig
def
get_python_source_dir
():
src_dir
=
sysconfig
.
get_config_var
(
'abs_srcdir'
)
if
not
src_dir
:
src_dir
=
sysconfig
.
get_config_var
(
'srcdir'
)
return
os
.
path
.
abspath
(
src_dir
)
SRCDIR
=
get_python_source_dir
()
def
n_files_str
(
count
):
"""Return 'N file(s)' with the proper plurality on 'file'."""
s
=
"s"
if
count
!=
1
else
""
return
f"
{
count
}
file
{
s
}
"
def
status
(
message
,
modal
=
False
,
info
=
None
):
"""Decorator to output status info to stdout."""
def
decorated_fxn
(
fxn
):
def
call_fxn
(
*
args
,
**
kwargs
):
sys
.
stdout
.
write
(
message
+
' ... '
)
sys
.
stdout
.
flush
()
result
=
fxn
(
*
args
,
**
kwargs
)
if
not
modal
and
not
info
:
print
(
"done"
)
elif
info
:
print
(
info
(
result
))
else
:
print
(
"yes"
if
result
else
"NO"
)
return
result
return
call_fxn
return
decorated_fxn
def
get_git_branch
():
"""Get the symbolic name for the current git branch"""
cmd
=
"git rev-parse --abbrev-ref HEAD"
.
split
()
try
:
return
subprocess
.
check_output
(
cmd
,
stderr
=
subprocess
.
DEVNULL
,
cwd
=
SRCDIR
,
encoding
=
'UTF-8'
)
except
subprocess
.
CalledProcessError
:
return
None
def
get_git_upstream_remote
():
"""
Get the remote name to use for upstream branches
Check for presence of "https://github.com/python/cpython" remote URL.
If only one is found, return that remote name. If multiple are found,
check for and return "upstream", "origin", or "python", in that
order. Raise an error if no valid matches are found.
"""
cmd
=
"git remote -v"
.
split
()
output
=
subprocess
.
check_output
(
cmd
,
stderr
=
subprocess
.
DEVNULL
,
cwd
=
SRCDIR
,
encoding
=
"UTF-8"
)
# Filter to desired remotes, accounting for potential uppercasing
filtered_remotes
=
{
remote
.
split
(
"
\t
"
)[
0
].
lower
()
for
remote
in
output
.
split
(
'
\n
'
)
if
"python/cpython"
in
remote
.
lower
()
and
remote
.
endswith
(
"(fetch)"
)
}
if
len
(
filtered_remotes
)
==
1
:
[
remote
]
=
filtered_remotes
return
remote
for
remote_name
in
[
"upstream"
,
"origin"
,
"python"
]:
if
remote_name
in
filtered_remotes
:
return
remote_name
remotes_found
=
"
\n
"
.
join
(
{
remote
for
remote
in
output
.
split
(
'
\n
'
)
if
remote
.
endswith
(
"(fetch)"
)}
)
raise
ValueError
(
f"Patchcheck was unable to find an unambiguous upstream remote, "
f"with URL matching 'https://github.com/python/cpython'. "
f"For help creating an upstream remote, see Dev Guide: "
f"https://devguide.python.org/getting-started/"
f"git-boot-camp/#cloning-a-forked-cpython-repository "
f"
\n
Remotes found:
\n
{
remotes_found
}
"
)
def
get_git_remote_default_branch
(
remote_name
):
"""Get the name of the default branch for the given remote
It is typically called 'main', but may differ
"""
cmd
=
f"git remote show
{
remote_name
}
"
.
split
()
env
=
os
.
environ
.
copy
()
env
[
'LANG'
]
=
'C'
try
:
remote_info
=
subprocess
.
check_output
(
cmd
,
stderr
=
subprocess
.
DEVNULL
,
cwd
=
SRCDIR
,
encoding
=
'UTF-8'
,
env
=
env
)
except
subprocess
.
CalledProcessError
:
return
None
for
line
in
remote_info
.
splitlines
():
if
"HEAD branch:"
in
line
:
base_branch
=
line
.
split
(
":"
)[
1
].
strip
()
return
base_branch
return
None
@
status
(
"Getting base branch for PR"
,
info
=
lambda
x
:
x
if
x
is
not
None
else
"not a PR branch"
)
def
get_base_branch
():
if
not
os
.
path
.
exists
(
os
.
path
.
join
(
SRCDIR
,
'.git'
)):
# Not a git checkout, so there's no base branch
return
None
upstream_remote
=
get_git_upstream_remote
()
version
=
sys
.
version_info
if
version
.
releaselevel
==
'alpha'
:
base_branch
=
get_git_remote_default_branch
(
upstream_remote
)
else
:
base_branch
=
"{0.major}.{0.minor}"
.
format
(
version
)
this_branch
=
get_git_branch
()
if
this_branch
is
None
or
this_branch
==
base_branch
:
# Not on a git PR branch, so there's no base branch
return
None
return
upstream_remote
+
"/"
+
base_branch
@
status
(
"Getting the list of files that have been added/changed"
,
info
=
lambda
x
:
n_files_str
(
len
(
x
)))
def
changed_files
(
base_branch
=
None
):
"""Get the list of changed or added files from git."""
if
os
.
path
.
exists
(
os
.
path
.
join
(
SRCDIR
,
'.git'
)):
# We just use an existence check here as:
# directory = normal git checkout/clone
# file = git worktree directory
if
base_branch
:
cmd
=
'git diff --name-status '
+
base_branch
else
:
cmd
=
'git status --porcelain'
filenames
=
[]
with
subprocess
.
Popen
(
cmd
.
split
(),
stdout
=
subprocess
.
PIPE
,
cwd
=
SRCDIR
)
as
st
:
git_file_status
,
_
=
st
.
communicate
()
if
st
.
returncode
!=
0
:
sys
.
exit
(
f'error running
{
cmd
}
'
)
for
line
in
git_file_status
.
splitlines
():
line
=
line
.
decode
().
rstrip
()
status_text
,
filename
=
line
.
split
(
maxsplit
=
1
)
status
=
set
(
status_text
)
# modified, added or unmerged files
if
not
status
.
intersection
(
'MAU'
):
continue
if
' -> '
in
filename
:
# file is renamed
filename
=
filename
.
split
(
' -> '
,
2
)[
1
].
strip
()
filenames
.
append
(
filename
)
else
:
sys
.
exit
(
'need a git checkout to get modified files'
)
return
list
(
map
(
os
.
path
.
normpath
,
filenames
))
@
status
(
"Docs modified"
,
modal
=
True
)
def
docs_modified
(
file_paths
):
"""Report if any file in the Doc directory has been changed."""
return
bool
(
file_paths
)
@
status
(
"Misc/NEWS.d updated with `blurb`"
,
modal
=
True
)
def
reported_news
(
file_paths
):
"""Check if Misc/NEWS.d has been changed."""
return
any
(
p
.
startswith
(
os
.
path
.
join
(
'Misc'
,
'NEWS.d'
,
'next'
))
for
p
in
file_paths
)
@
status
(
"configure regenerated"
,
modal
=
True
,
info
=
str
)
def
regenerated_configure
(
file_paths
):
"""Check if configure has been regenerated."""
if
'configure.ac'
in
file_paths
:
return
"yes"
if
'configure'
in
file_paths
else
"no"
else
:
return
"not needed"
@
status
(
"pyconfig.h.in regenerated"
,
modal
=
True
,
info
=
str
)
def
regenerated_pyconfig_h_in
(
file_paths
):
"""Check if pyconfig.h.in has been regenerated."""
if
'configure.ac'
in
file_paths
:
return
"yes"
if
'pyconfig.h.in'
in
file_paths
else
"no"
else
:
return
"not needed"
def
main
():
base_branch
=
get_base_branch
()
file_paths
=
changed_files
(
base_branch
)
has_doc_files
=
any
(
fn
for
fn
in
file_paths
if
fn
.
startswith
(
'Doc'
)
and
fn
.
endswith
((
'.rst'
,
'.inc'
)))
misc_files
=
{
p
for
p
in
file_paths
if
p
.
startswith
(
'Misc'
)}
# Docs updated.
docs_modified
(
has_doc_files
)
# Misc/NEWS changed.
reported_news
(
misc_files
)
# Regenerated configure, if necessary.
regenerated_configure
(
file_paths
)
# Regenerated pyconfig.h.in, if necessary.
regenerated_pyconfig_h_in
(
file_paths
)
# Test suite run and passed.
has_c_files
=
any
(
fn
for
fn
in
file_paths
if
fn
.
endswith
((
'.c'
,
'.h'
)))
has_python_files
=
any
(
fn
for
fn
in
file_paths
if
fn
.
endswith
(
'.py'
))
print
()
if
has_c_files
:
print
(
"Did you run the test suite and check for refleaks?"
)
elif
has_python_files
:
print
(
"Did you run the test suite?"
)
if
__name__
==
'__main__'
:
main
()
You can’t perform that action at this time.