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/test/test_importlib/test_main.py at 3.9 · 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
3.9
Breadcrumbs
cpython
/
Lib
/
test
/
test_importlib
/
test_main.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
269 lines (220 loc) · 8.2 KB
3.9
Breadcrumbs
cpython
/
Lib
/
test
/
test_importlib
/
test_main.py
Copy path
Top
File metadata and controls
Code
Blame
269 lines (220 loc) · 8.2 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# coding: utf-8
import
re
import
json
import
pickle
import
textwrap
import
unittest
import
importlib
.
metadata
try
:
import
pyfakefs
.
fake_filesystem_unittest
as
ffs
except
ImportError
:
from
.
stubs
import
fake_filesystem_unittest
as
ffs
from
.
import
fixtures
from
importlib
.
metadata
import
(
Distribution
,
EntryPoint
,
PackageNotFoundError
,
distributions
,
entry_points
,
metadata
,
version
,
)
class
BasicTests
(
fixtures
.
DistInfoPkg
,
unittest
.
TestCase
):
version_pattern
=
r'\d+\.\d+(\.\d)?'
def
test_retrieves_version_of_self
(
self
):
dist
=
Distribution
.
from_name
(
'distinfo-pkg'
)
assert
isinstance
(
dist
.
version
,
str
)
assert
re
.
match
(
self
.
version_pattern
,
dist
.
version
)
def
test_for_name_does_not_exist
(
self
):
with
self
.
assertRaises
(
PackageNotFoundError
):
Distribution
.
from_name
(
'does-not-exist'
)
def
test_new_style_classes
(
self
):
self
.
assertIsInstance
(
Distribution
,
type
)
class
ImportTests
(
fixtures
.
DistInfoPkg
,
unittest
.
TestCase
):
def
test_import_nonexistent_module
(
self
):
# Ensure that the MetadataPathFinder does not crash an import of a
# non-existent module.
with
self
.
assertRaises
(
ImportError
):
importlib
.
import_module
(
'does_not_exist'
)
def
test_resolve
(
self
):
entries
=
dict
(
entry_points
()[
'entries'
])
ep
=
entries
[
'main'
]
self
.
assertEqual
(
ep
.
load
().
__name__
,
"main"
)
def
test_entrypoint_with_colon_in_name
(
self
):
entries
=
dict
(
entry_points
()[
'entries'
])
ep
=
entries
[
'ns:sub'
]
self
.
assertEqual
(
ep
.
value
,
'mod:main'
)
def
test_resolve_without_attr
(
self
):
ep
=
EntryPoint
(
name
=
'ep'
,
value
=
'importlib.metadata'
,
group
=
'grp'
,
)
assert
ep
.
load
()
is
importlib
.
metadata
class
NameNormalizationTests
(
fixtures
.
OnSysPath
,
fixtures
.
SiteDir
,
unittest
.
TestCase
):
@
staticmethod
def
pkg_with_dashes
(
site_dir
):
"""
Create minimal metadata for a package with dashes
in the name (and thus underscores in the filename).
"""
metadata_dir
=
site_dir
/
'my_pkg.dist-info'
metadata_dir
.
mkdir
()
metadata
=
metadata_dir
/
'METADATA'
with
metadata
.
open
(
'w'
)
as
strm
:
strm
.
write
(
'Version: 1.0
\n
'
)
return
'my-pkg'
def
test_dashes_in_dist_name_found_as_underscores
(
self
):
"""
For a package with a dash in the name, the dist-info metadata
uses underscores in the name. Ensure the metadata loads.
"""
pkg_name
=
self
.
pkg_with_dashes
(
self
.
site_dir
)
assert
version
(
pkg_name
)
==
'1.0'
@
staticmethod
def
pkg_with_mixed_case
(
site_dir
):
"""
Create minimal metadata for a package with mixed case
in the name.
"""
metadata_dir
=
site_dir
/
'CherryPy.dist-info'
metadata_dir
.
mkdir
()
metadata
=
metadata_dir
/
'METADATA'
with
metadata
.
open
(
'w'
)
as
strm
:
strm
.
write
(
'Version: 1.0
\n
'
)
return
'CherryPy'
def
test_dist_name_found_as_any_case
(
self
):
"""
Ensure the metadata loads when queried with any case.
"""
pkg_name
=
self
.
pkg_with_mixed_case
(
self
.
site_dir
)
assert
version
(
pkg_name
)
==
'1.0'
assert
version
(
pkg_name
.
lower
())
==
'1.0'
assert
version
(
pkg_name
.
upper
())
==
'1.0'
class
NonASCIITests
(
fixtures
.
OnSysPath
,
fixtures
.
SiteDir
,
unittest
.
TestCase
):
@
staticmethod
def
pkg_with_non_ascii_description
(
site_dir
):
"""
Create minimal metadata for a package with non-ASCII in
the description.
"""
metadata_dir
=
site_dir
/
'portend.dist-info'
metadata_dir
.
mkdir
()
metadata
=
metadata_dir
/
'METADATA'
with
metadata
.
open
(
'w'
,
encoding
=
'utf-8'
)
as
fp
:
fp
.
write
(
'Description: pôrˈtend
\n
'
)
return
'portend'
@
staticmethod
def
pkg_with_non_ascii_description_egg_info
(
site_dir
):
"""
Create minimal metadata for an egg-info package with
non-ASCII in the description.
"""
metadata_dir
=
site_dir
/
'portend.dist-info'
metadata_dir
.
mkdir
()
metadata
=
metadata_dir
/
'METADATA'
with
metadata
.
open
(
'w'
,
encoding
=
'utf-8'
)
as
fp
:
fp
.
write
(
textwrap
.
dedent
(
"""
Name: portend
pôrˈtend
"""
).
lstrip
())
return
'portend'
def
test_metadata_loads
(
self
):
pkg_name
=
self
.
pkg_with_non_ascii_description
(
self
.
site_dir
)
meta
=
metadata
(
pkg_name
)
assert
meta
[
'Description'
]
==
'pôrˈtend'
def
test_metadata_loads_egg_info
(
self
):
pkg_name
=
self
.
pkg_with_non_ascii_description_egg_info
(
self
.
site_dir
)
meta
=
metadata
(
pkg_name
)
assert
meta
.
get_payload
()
==
'pôrˈtend
\n
'
class
DiscoveryTests
(
fixtures
.
EggInfoPkg
,
fixtures
.
DistInfoPkg
,
unittest
.
TestCase
):
def
test_package_discovery
(
self
):
dists
=
list
(
distributions
())
assert
all
(
isinstance
(
dist
,
Distribution
)
for
dist
in
dists
)
assert
any
(
dist
.
metadata
[
'Name'
]
==
'egginfo-pkg'
for
dist
in
dists
)
assert
any
(
dist
.
metadata
[
'Name'
]
==
'distinfo-pkg'
for
dist
in
dists
)
def
test_invalid_usage
(
self
):
with
self
.
assertRaises
(
ValueError
):
list
(
distributions
(
context
=
'something'
,
name
=
'else'
))
class
DirectoryTest
(
fixtures
.
OnSysPath
,
fixtures
.
SiteDir
,
unittest
.
TestCase
):
def
test_egg_info
(
self
):
# make an `EGG-INFO` directory that's unrelated
self
.
site_dir
.
joinpath
(
'EGG-INFO'
).
mkdir
()
# used to crash with `IsADirectoryError`
with
self
.
assertRaises
(
PackageNotFoundError
):
version
(
'unknown-package'
)
def
test_egg
(
self
):
egg
=
self
.
site_dir
.
joinpath
(
'foo-3.6.egg'
)
egg
.
mkdir
()
with
self
.
add_sys_path
(
egg
):
with
self
.
assertRaises
(
PackageNotFoundError
):
version
(
'foo'
)
class
MissingSysPath
(
fixtures
.
OnSysPath
,
unittest
.
TestCase
):
site_dir
=
'/does-not-exist'
def
test_discovery
(
self
):
"""
Discovering distributions should succeed even if
there is an invalid path on sys.path.
"""
importlib
.
metadata
.
distributions
()
class
InaccessibleSysPath
(
fixtures
.
OnSysPath
,
ffs
.
TestCase
):
site_dir
=
'/access-denied'
def
setUp
(
self
):
super
(
InaccessibleSysPath
,
self
).
setUp
()
self
.
setUpPyfakefs
()
self
.
fs
.
create_dir
(
self
.
site_dir
,
perm_bits
=
000
)
def
test_discovery
(
self
):
"""
Discovering distributions should succeed even if
there is an invalid path on sys.path.
"""
list
(
importlib
.
metadata
.
distributions
())
class
TestEntryPoints
(
unittest
.
TestCase
):
def
__init__
(
self
,
*
args
):
super
(
TestEntryPoints
,
self
).
__init__
(
*
args
)
self
.
ep
=
importlib
.
metadata
.
EntryPoint
(
'name'
,
'value'
,
'group'
)
def
test_entry_point_pickleable
(
self
):
revived
=
pickle
.
loads
(
pickle
.
dumps
(
self
.
ep
))
assert
revived
==
self
.
ep
def
test_immutable
(
self
):
"""EntryPoints should be immutable"""
with
self
.
assertRaises
(
AttributeError
):
self
.
ep
.
name
=
'badactor'
def
test_repr
(
self
):
assert
'EntryPoint'
in
repr
(
self
.
ep
)
assert
'name='
in
repr
(
self
.
ep
)
assert
"'name'"
in
repr
(
self
.
ep
)
def
test_hashable
(
self
):
"""EntryPoints should be hashable"""
hash
(
self
.
ep
)
def
test_json_dump
(
self
):
"""
json should not expect to be able to dump an EntryPoint
"""
with
self
.
assertRaises
(
Exception
):
json
.
dumps
(
self
.
ep
)
def
test_module
(
self
):
assert
self
.
ep
.
module
==
'value'
def
test_attr
(
self
):
assert
self
.
ep
.
attr
is
None
class
FileSystem
(
fixtures
.
OnSysPath
,
fixtures
.
SiteDir
,
fixtures
.
FileBuilder
,
unittest
.
TestCase
):
def
test_unicode_dir_on_sys_path
(
self
):
"""
Ensure a Unicode subdirectory of a directory on sys.path
does not crash.
"""
fixtures
.
build_files
(
{
self
.
unicode_filename
(): {}},
prefix
=
self
.
site_dir
,
)
list
(
distributions
())
You can’t perform that action at this time.