Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
cpython/Lib/unittest/util.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
/
Lib
/
unittest
/
util.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
187 lines (164 loc) · 5.49 KB
main
Breadcrumbs
cpython
/
Lib
/
unittest
/
util.py
Copy path
Top
File metadata and controls
Code
Blame
187 lines (164 loc) · 5.49 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
"""Various utility functions."""
from
collections
import
namedtuple
,
Counter
__unittest
=
True
_MAX_LENGTH
=
80
_PLACEHOLDER_LEN
=
12
_MIN_BEGIN_LEN
=
5
_MIN_END_LEN
=
5
_MIN_COMMON_LEN
=
5
_MIN_DIFF_LEN
=
_MAX_LENGTH
-
\
(
_MIN_BEGIN_LEN
+
_PLACEHOLDER_LEN
+
_MIN_COMMON_LEN
+
_PLACEHOLDER_LEN
+
_MIN_END_LEN
)
assert
_MIN_DIFF_LEN
>=
0
def
_shorten
(
s
,
prefixlen
,
suffixlen
):
skip
=
len
(
s
)
-
prefixlen
-
suffixlen
if
skip
>
_PLACEHOLDER_LEN
:
s
=
'%s[%d chars]%s'
%
(
s
[:
prefixlen
],
skip
,
s
[
len
(
s
)
-
suffixlen
:])
return
s
def
_common_prefix
(
m
):
if
not
m
:
return
""
s1
=
min
(
m
)
s2
=
max
(
m
)
for
i
,
c
in
enumerate
(
s1
):
if
c
!=
s2
[
i
]:
return
s1
[:
i
]
return
s1
def
_common_shorten_repr
(
*
args
):
args
=
tuple
(
map
(
safe_repr
,
args
))
maxlen
=
max
(
map
(
len
,
args
))
if
maxlen
<=
_MAX_LENGTH
:
return
args
prefix
=
_common_prefix
(
args
)
prefixlen
=
len
(
prefix
)
common_len
=
_MAX_LENGTH
-
\
(
maxlen
-
prefixlen
+
_MIN_BEGIN_LEN
+
_PLACEHOLDER_LEN
)
if
common_len
>
_MIN_COMMON_LEN
:
assert
_MIN_BEGIN_LEN
+
_PLACEHOLDER_LEN
+
_MIN_COMMON_LEN
+
\
(
maxlen
-
prefixlen
)
<
_MAX_LENGTH
prefix
=
_shorten
(
prefix
,
_MIN_BEGIN_LEN
,
common_len
)
return
tuple
(
prefix
+
s
[
prefixlen
:]
for
s
in
args
)
prefix
=
_shorten
(
prefix
,
_MIN_BEGIN_LEN
,
_MIN_COMMON_LEN
)
return
tuple
(
prefix
+
_shorten
(
s
[
prefixlen
:],
_MIN_DIFF_LEN
,
_MIN_END_LEN
)
for
s
in
args
)
def
safe_repr
(
obj
,
short
=
False
):
try
:
result
=
repr
(
obj
)
except
Exception
:
result
=
object
.
__repr__
(
obj
)
if
not
short
or
len
(
result
)
<
_MAX_LENGTH
:
return
result
return
result
[:
_MAX_LENGTH
]
+
' [truncated]...'
def
strclass
(
cls
):
return
"%s.%s"
%
(
cls
.
__module__
,
cls
.
__qualname__
)
def
_dedupe_sorted
(
lst
):
"""Remove consecutive duplicate elements from a sorted list."""
result
=
[]
for
item
in
lst
:
if
not
result
or
result
[
-
1
]
!=
item
:
result
.
append
(
item
)
return
result
def
sorted_list_difference
(
expected
,
actual
):
"""Finds elements in only one or the other of two, sorted input lists.
Returns a two-element tuple of lists. The first list contains those
elements in the "expected" list but not in the "actual" list, and the
second contains those elements in the "actual" list but not in the
"expected" list. Duplicate elements in either input list are ignored.
"""
i
=
j
=
0
missing
=
[]
unexpected
=
[]
while
True
:
try
:
e
=
expected
[
i
]
a
=
actual
[
j
]
if
e
<
a
:
missing
.
append
(
e
)
i
+=
1
while
expected
[
i
]
==
e
:
i
+=
1
elif
e
>
a
:
unexpected
.
append
(
a
)
j
+=
1
while
actual
[
j
]
==
a
:
j
+=
1
else
:
i
+=
1
try
:
while
expected
[
i
]
==
e
:
i
+=
1
finally
:
j
+=
1
while
actual
[
j
]
==
a
:
j
+=
1
except
IndexError
:
missing
.
extend
(
_dedupe_sorted
(
expected
[
i
:]))
unexpected
.
extend
(
_dedupe_sorted
(
actual
[
j
:]))
break
return
missing
,
unexpected
def
unorderable_list_difference
(
expected
,
actual
):
"""Same behavior as sorted_list_difference but
for lists of unorderable items (like dicts).
As it does a linear search per item (remove) it
has O(n*n) performance."""
missing
=
[]
while
expected
:
item
=
expected
.
pop
()
try
:
actual
.
remove
(
item
)
except
ValueError
:
missing
.
append
(
item
)
# anything left in actual is unexpected
return
missing
,
actual
def
three_way_cmp
(
x
,
y
):
"""Return -1 if x < y, 0 if x == y and 1 if x > y"""
return
(
x
>
y
)
-
(
x
<
y
)
_Mismatch
=
namedtuple
(
'Mismatch'
,
'actual expected value'
)
def
_count_diff_all_purpose
(
actual
,
expected
):
'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
# elements need not be hashable
s
,
t
=
list
(
actual
),
list
(
expected
)
m
,
n
=
len
(
s
),
len
(
t
)
NULL
=
object
()
result
=
[]
for
i
,
elem
in
enumerate
(
s
):
if
elem
is
NULL
:
continue
cnt_s
=
cnt_t
=
0
for
j
in
range
(
i
,
m
):
if
s
[
j
]
==
elem
:
cnt_s
+=
1
s
[
j
]
=
NULL
for
j
,
other_elem
in
enumerate
(
t
):
if
other_elem
==
elem
:
cnt_t
+=
1
t
[
j
]
=
NULL
if
cnt_s
!=
cnt_t
:
diff
=
_Mismatch
(
cnt_s
,
cnt_t
,
elem
)
result
.
append
(
diff
)
for
i
,
elem
in
enumerate
(
t
):
if
elem
is
NULL
:
continue
cnt_t
=
0
for
j
in
range
(
i
,
n
):
if
t
[
j
]
==
elem
:
cnt_t
+=
1
t
[
j
]
=
NULL
diff
=
_Mismatch
(
0
,
cnt_t
,
elem
)
result
.
append
(
diff
)
return
result
def
_count_diff_hashable
(
actual
,
expected
):
'Returns list of (cnt_act, cnt_exp, elem) triples where the counts differ'
# elements must be hashable
s
,
t
=
Counter
(
actual
),
Counter
(
expected
)
result
=
[]
for
elem
,
cnt_s
in
s
.
items
():
cnt_t
=
t
.
get
(
elem
,
0
)
if
cnt_s
!=
cnt_t
:
diff
=
_Mismatch
(
cnt_s
,
cnt_t
,
elem
)
result
.
append
(
diff
)
for
elem
,
cnt_t
in
t
.
items
():
if
elem
not
in
s
:
diff
=
_Mismatch
(
0
,
cnt_t
,
elem
)
result
.
append
(
diff
)
return
result
You can’t perform that action at this time.