| 1 |
"""Core implementation of import. |
| 2 |
|
| 3 |
This module is NOT meant to be directly imported! It has been designed such |
| 4 |
that it can be bootstrapped into Python as the implementation of import. As |
| 5 |
such it requires the injection of specific modules and attributes in order to |
| 6 |
work. One should use importlib as the public-facing version of this module. |
| 7 |
|
| 8 |
""" |
| 9 |
# |
| 10 |
# IMPORTANT: Whenever making changes to this module, be sure to run a top-level |
| 11 |
# `make regen-importlib` followed by `make` in order to get the frozen version |
| 12 |
# of the module updated. Not doing so will result in the Makefile to fail for |
| 13 |
# all others who don't have a ./python around to freeze the module |
| 14 |
# in the early stages of compilation. |
| 15 |
# |
| 16 |
|
| 17 |
# See importlib._setup() for what is injected into the global namespace. |
| 18 |
|
| 19 |
# When editing this code be aware that code executed at import time CANNOT |
| 20 |
# reference any injected objects! This includes not only global code but also |
| 21 |
# anything specified at the class level. |
| 22 |
|
| 23 |
def _object_name(obj): |
| 24 |
try: |
| 25 |
return obj.__qualname__ |
| 26 |
except AttributeError: |
| 27 |
return type(obj).__qualname__ |
| 28 |
|
| 29 |
# Bootstrap-related code ###################################################### |
| 30 |
|
| 31 |
# Modules injected manually by _setup() |
| 32 |
_thread = None |
| 33 |
_warnings = None |
| 34 |
_weakref = None |
| 35 |
|
| 36 |
# Import done by _install_external_importers() |
| 37 |
_bootstrap_external = None |
| 38 |
|
| 39 |
|
| 40 |
def _wrap(new, old): |
| 41 |
"""Simple substitute for functools.update_wrapper.""" |
| 42 |
for replace in ['__module__', '__name__', '__qualname__', '__doc__']: |
| 43 |
if hasattr(old, replace): |
| 44 |
setattr(new, replace, getattr(old, replace)) |
| 45 |
new.__dict__.update(old.__dict__) |
| 46 |
|
| 47 |
|
| 48 |
def _new_module(name): |
| 49 |
return type(sys)(name) |
| 50 |
|
| 51 |
|
| 52 |
# Module-level locking ######################################################## |
| 53 |
|
| 54 |
# For a list that can have a weakref to it. |
| 55 |
class _List(list): |
| 56 |
__slots__ = ("__weakref__",) |
| 57 |
|
| 58 |
|
| 59 |
# Copied from weakref.py with some simplifications and modifications unique to |
| 60 |
# bootstrapping importlib. Many methods were simply deleting for simplicity, so if they |
| 61 |
# are needed in the future they may work if simply copied back in. |
| 62 |
class _WeakValueDictionary: |
| 63 |
|
| 64 |
def __init__(self): |
| 65 |
self_weakref = _weakref.ref(self) |
| 66 |
|
| 67 |
# Inlined to avoid issues with inheriting from _weakref.ref before _weakref is |
| 68 |
# set by _setup(). Since there's only one instance of this class, this is |
| 69 |
# not expensive. |
| 70 |
class KeyedRef(_weakref.ref): |
| 71 |
|
| 72 |
__slots__ = "key", |
| 73 |
|
| 74 |
def __new__(type, ob, key): |
| 75 |
self = super().__new__(type, ob, type.remove) |
| 76 |
self.key = key |
| 77 |
return self |
| 78 |
|
| 79 |
def __init__(self, ob, key): |
| 80 |
super().__init__(ob, self.remove) |
| 81 |
|
| 82 |
@staticmethod |
| 83 |
def remove(wr): |
| 84 |
nonlocal self_weakref |
| 85 |
|
| 86 |
self = self_weakref() |
| 87 |
if self is not None: |
| 88 |
if self._iterating: |
| 89 |
self._pending_removals.append(wr.key) |
| 90 |
else: |
| 91 |
_weakref._remove_dead_weakref(self.data, wr.key) |
| 92 |
|
| 93 |
self._KeyedRef = KeyedRef |
| 94 |
self.clear() |
| 95 |
|
| 96 |
def clear(self): |
| 97 |
self._pending_removals = [] |
| 98 |
self._iterating = set() |
| 99 |
self.data = {} |
| 100 |
|
| 101 |
def _commit_removals(self): |
| 102 |
pop = self._pending_removals.pop |
| 103 |
d = self.data |
| 104 |
while True: |
| 105 |
try: |
| 106 |
key = pop() |
| 107 |
except IndexError: |
| 108 |
return |
| 109 |
_weakref._remove_dead_weakref(d, key) |
| 110 |
|
| 111 |
def get(self, key, default=None): |
| 112 |
if self._pending_removals: |
| 113 |
self._commit_removals() |
| 114 |
try: |
| 115 |
wr = self.data[key] |
| 116 |
except KeyError: |
| 117 |
return default |
| 118 |
else: |
| 119 |
if (o := wr()) is None: |
| 120 |
return default |
| 121 |
else: |
| 122 |
return o |
| 123 |
|
| 124 |
def setdefault(self, key, default=None): |
| 125 |
try: |
| 126 |
o = self.data[key]() |
| 127 |
except KeyError: |
| 128 |
o = None |
| 129 |
if o is None: |
| 130 |
if self._pending_removals: |
| 131 |
self._commit_removals() |
| 132 |
self.data[key] = self._KeyedRef(default, key) |
| 133 |
return default |
| 134 |
else: |
| 135 |
return o |
| 136 |
|
| 137 |
|
| 138 |
# A dict mapping module names to weakrefs of _ModuleLock instances. |
| 139 |
# Dictionary protected by the global import lock. |
| 140 |
_module_locks = {} |
| 141 |
|
| 142 |
# A dict mapping thread IDs to weakref'ed lists of _ModuleLock instances. |
| 143 |
# This maps a thread to the module locks it is blocking on acquiring. The |
| 144 |
# values are lists because a single thread could perform a re-entrant import |
| 145 |
# and be "in the process" of blocking on locks for more than one module. A |
| 146 |
# thread can be "in the process" because a thread cannot actually block on |
| 147 |
# acquiring more than one lock but it can have set up bookkeeping that reflects |
| 148 |
# that it intends to block on acquiring more than one lock. |
| 149 |
# |
| 150 |
# The dictionary uses a WeakValueDictionary to avoid keeping unnecessary |
| 151 |
# lists around, regardless of GC runs. This way there's no memory leak if |
| 152 |
# the list is no longer needed (GH-106176). |
| 153 |
_blocking_on = None |
| 154 |
|
| 155 |
|
| 156 |
class _BlockingOnManager: |
| 157 |
"""A context manager responsible to updating ``_blocking_on``.""" |
| 158 |
def __init__(self, thread_id, lock): |
| 159 |
self.thread_id = thread_id |
| 160 |
self.lock = lock |
| 161 |
|
| 162 |
def __enter__(self): |
| 163 |
"""Mark the running thread as waiting for self.lock. via _blocking_on.""" |
| 164 |
# Interactions with _blocking_on are *not* protected by the global |
| 165 |
# import lock here because each thread only touches the state that it |
| 166 |
# owns (state keyed on its thread id). The global import lock is |
| 167 |
# re-entrant (i.e., a single thread may take it more than once) so it |
| 168 |
# wouldn't help us be correct in the face of re-entrancy either. |
| 169 |
|
| 170 |
self.blocked_on = _blocking_on.setdefault(self.thread_id, _List()) |
| 171 |
self.blocked_on.append(self.lock) |
| 172 |
|
| 173 |
def __exit__(self, *args, **kwargs): |
| 174 |
"""Remove self.lock from this thread's _blocking_on list.""" |
| 175 |
self.blocked_on.remove(self.lock) |
| 176 |
|
| 177 |
|
| 178 |
class _DeadlockError(RuntimeError): |
| 179 |
pass |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
def _has_deadlocked(target_id, *, seen_ids, candidate_ids, blocking_on): |
| 184 |
"""Check if 'target_id' is holding the same lock as another thread(s). |
| 185 |
|
| 186 |
The search within 'blocking_on' starts with the threads listed in |
| 187 |
'candidate_ids'. 'seen_ids' contains any threads that are considered |
| 188 |
already traversed in the search. |
| 189 |
|
| 190 |
Keyword arguments: |
| 191 |
target_id -- The thread id to try to reach. |
| 192 |
seen_ids -- A set of threads that have already been visited. |
| 193 |
candidate_ids -- The thread ids from which to begin. |
| 194 |
blocking_on -- A dict representing the thread/blocking-on graph. This may |
| 195 |
be the same object as the global '_blocking_on' but it is |
| 196 |
a parameter to reduce the impact that global mutable |
| 197 |
state has on the result of this function. |
| 198 |
""" |
| 199 |
if target_id in candidate_ids: |
| 200 |
# If we have already reached the target_id, we're done - signal that it |
| 201 |
# is reachable. |
| 202 |
return True |
| 203 |
|
| 204 |
# Otherwise, try to reach the target_id from each of the given candidate_ids. |
| 205 |
for tid in candidate_ids: |
| 206 |
if not (candidate_blocking_on := blocking_on.get(tid)): |
| 207 |
# There are no edges out from this node, skip it. |
| 208 |
continue |
| 209 |
elif tid in seen_ids: |
| 210 |
# bpo 38091: the chain of tid's we encounter here eventually leads |
| 211 |
# to a fixed point or a cycle, but does not reach target_id. |
| 212 |
# This means we would not actually deadlock. This can happen if |
| 213 |
# other threads are at the beginning of acquire() below. |
| 214 |
return False |
| 215 |
seen_ids.add(tid) |
| 216 |
|
| 217 |
# Follow the edges out from this thread. |
| 218 |
edges = [lock.owner for lock in candidate_blocking_on] |
| 219 |
if _has_deadlocked(target_id, seen_ids=seen_ids, candidate_ids=edges, |
| 220 |
blocking_on=blocking_on): |
| 221 |
return True |
| 222 |
|
| 223 |
return False |
| 224 |
|
| 225 |
|
| 226 |
class _ModuleLock: |
| 227 |
"""A recursive lock implementation which is able to detect deadlocks |
| 228 |
(e.g. thread 1 trying to take locks A then B, and thread 2 trying to |
| 229 |
take locks B then A). |
| 230 |
""" |
| 231 |
|
| 232 |
def __init__(self, name): |
| 233 |
# Create an RLock for protecting the import process for the |
| 234 |
# corresponding module. Since it is an RLock, a single thread will be |
| 235 |
# able to take it more than once. This is necessary to support |
| 236 |
# re-entrancy in the import system that arises from (at least) signal |
| 237 |
# handlers and the garbage collector. Consider the case of: |
| 238 |
# |
| 239 |
# import foo |
| 240 |
# -> ... |
| 241 |
# -> importlib._bootstrap._ModuleLock.acquire |
| 242 |
# -> ... |
| 243 |
# -> <garbage collector> |
| 244 |
# -> __del__ |
| 245 |
# -> import foo |
| 246 |
# -> ... |
| 247 |
# -> importlib._bootstrap._ModuleLock.acquire |
| 248 |
# -> _BlockingOnManager.__enter__ |
| 249 |
# |
| 250 |
# If a different thread than the running one holds the lock then the |
| 251 |
# thread will have to block on taking the lock, which is what we want |
| 252 |
# for thread safety. |
| 253 |
self.lock = _thread.RLock() |
| 254 |
self.wakeup = _thread.allocate_lock() |
| 255 |
|
| 256 |
# The name of the module for which this is a lock. |
| 257 |
self.name = name |
| 258 |
|
| 259 |
# Can end up being set to None if this lock is not owned by any thread |
| 260 |
# or the thread identifier for the owning thread. |
| 261 |
self.owner = None |
| 262 |
|
| 263 |
# Represent the number of times the owning thread has acquired this lock |
| 264 |
# via a list of True. This supports RLock-like ("re-entrant lock") |
| 265 |
# behavior, necessary in case a single thread is following a circular |
| 266 |
# import dependency and needs to take the lock for a single module |
| 267 |
# more than once. |
| 268 |
# |
| 269 |
# Counts are represented as a list of True because list.append(True) |
| 270 |
# and list.pop() are both atomic and thread-safe in CPython and it's hard |
| 271 |
# to find another primitive with the same properties. |
| 272 |
self.count = [] |
| 273 |
|
| 274 |
# This is a count of the number of threads that are blocking on |
| 275 |
# self.wakeup.acquire() awaiting to get their turn holding this module |
| 276 |
# lock. When the module lock is released, if this is greater than |
| 277 |
# zero, it is decremented and `self.wakeup` is released one time. The |
| 278 |
# intent is that this will let one other thread make more progress on |
| 279 |
# acquiring this module lock. This repeats until all the threads have |
| 280 |
# gotten a turn. |
| 281 |
# |
| 282 |
# This is incremented in self.acquire() when a thread notices it is |
| 283 |
# going to have to wait for another thread to finish. |
| 284 |
# |
| 285 |
# See the comment above count for explanation of the representation. |
| 286 |
self.waiters = [] |
| 287 |
|
| 288 |
def has_deadlock(self): |
| 289 |
# To avoid deadlocks for concurrent or re-entrant circular imports, |
| 290 |
# look at _blocking_on to see if any threads are blocking |
| 291 |
# on getting the import lock for any module for which the import lock |
| 292 |
# is held by this thread. |
| 293 |
return _has_deadlocked( |
| 294 |
# Try to find this thread. |
| 295 |
target_id=_thread.get_ident(), |
| 296 |
seen_ids=set(), |
| 297 |
# Start from the thread that holds the import lock for this |
| 298 |
# module. |
| 299 |
candidate_ids=[self.owner], |
| 300 |
# Use the global "blocking on" state. |
| 301 |
blocking_on=_blocking_on, |
| 302 |
) |
| 303 |
|
| 304 |
def acquire(self): |
| 305 |
""" |
| 306 |
Acquire the module lock. If a potential deadlock is detected, |
| 307 |
a _DeadlockError is raised. |
| 308 |
Otherwise, the lock is always acquired and True is returned. |
| 309 |
""" |
| 310 |
tid = _thread.get_ident() |
| 311 |
with _BlockingOnManager(tid, self): |
| 312 |
while True: |
| 313 |
# Protect interaction with state on self with a per-module |
| 314 |
# lock. This makes it safe for more than one thread to try to |
| 315 |
# acquire the lock for a single module at the same time. |
| 316 |
with self.lock: |
| 317 |
if self.count == [] or self.owner == tid: |
| 318 |
# If the lock for this module is unowned then we can |
| 319 |
# take the lock immediately and succeed. If the lock |
| 320 |
# for this module is owned by the running thread then |
| 321 |
# we can also allow the acquire to succeed. This |
| 322 |
# supports circular imports (thread T imports module A |
| 323 |
# which imports module B which imports module A). |
| 324 |
self.owner = tid |
| 325 |
self.count.append(True) |
| 326 |
return True |
| 327 |
|
| 328 |
# At this point we know the lock is held (because count != |
| 329 |
# 0) by another thread (because owner != tid). We'll have |
| 330 |
# to get in line to take the module lock. |
| 331 |
|
| 332 |
# But first, check to see if this thread would create a |
| 333 |
# deadlock by acquiring this module lock. If it would |
| 334 |
# then just stop with an error. |
| 335 |
# |
| 336 |
# It's not clear who is expected to handle this error. |
| 337 |
# There is one handler in _lock_unlock_module but many |
| 338 |
# times this method is called when entering the context |
| 339 |
# manager _ModuleLockManager instead - so _DeadlockError |
| 340 |
# will just propagate up to application code. |
| 341 |
# |
| 342 |
# This seems to be more than just a hypothetical - |
| 343 |
# https://stackoverflow.com/questions/59509154 |
| 344 |
# https://github.com/encode/django-rest-framework/issues/7078 |
| 345 |
if self.has_deadlock(): |
| 346 |
raise _DeadlockError(f'deadlock detected by {self!r}') |
| 347 |
|
| 348 |
# Check to see if we're going to be able to acquire the |
| 349 |
# lock. If we are going to have to wait then increment |
| 350 |
# the waiters so `self.release` will know to unblock us |
| 351 |
# later on. We do this part non-blockingly so we don't |
| 352 |
# get stuck here before we increment waiters. We have |
| 353 |
# this extra acquire call (in addition to the one below, |
| 354 |
# outside the self.lock context manager) to make sure |
| 355 |
# self.wakeup is held when the next acquire is called (so |
| 356 |
# we block). This is probably needlessly complex and we |
| 357 |
# should just take self.wakeup in the return codepath |
| 358 |
# above. |
| 359 |
if self.wakeup.acquire(False): |
| 360 |
self.waiters.append(None) |
| 361 |
|
| 362 |
# Now take the lock in a blocking fashion. This won't |
| 363 |
# complete until the thread holding this lock |
| 364 |
# (self.owner) calls self.release. |
| 365 |
self.wakeup.acquire() |
| 366 |
|
| 367 |
# Taking the lock has served its purpose (making us wait), so we can |
| 368 |
# give it up now. We'll take it w/o blocking again on the |
| 369 |
# next iteration around this 'while' loop. |
| 370 |
self.wakeup.release() |
| 371 |
|
| 372 |
def release(self): |
| 373 |
tid = _thread.get_ident() |
| 374 |
with self.lock: |
| 375 |
if self.owner != tid: |
| 376 |
raise RuntimeError('cannot release un-acquired lock') |
| 377 |
assert len(self.count) > 0 |
| 378 |
self.count.pop() |
| 379 |
if not len(self.count): |
| 380 |
self.owner = None |
| 381 |
if len(self.waiters) > 0: |
| 382 |
self.waiters.pop() |
| 383 |
self.wakeup.release() |
| 384 |
|
| 385 |
def locked(self): |
| 386 |
return bool(self.count) |
| 387 |
|
| 388 |
def __repr__(self): |
| 389 |
return f'_ModuleLock({self.name!r}) at {id(self)}' |
| 390 |
|
| 391 |
|
| 392 |
class _DummyModuleLock: |
| 393 |
"""A simple _ModuleLock equivalent for Python builds without |
| 394 |
multi-threading support.""" |
| 395 |
|
| 396 |
def __init__(self, name): |
| 397 |
self.name = name |
| 398 |
self.count = 0 |
| 399 |
|
| 400 |
def acquire(self): |
| 401 |
self.count += 1 |
| 402 |
return True |
| 403 |
|
| 404 |
def release(self): |
| 405 |
if self.count == 0: |
| 406 |
raise RuntimeError('cannot release un-acquired lock') |
| 407 |
self.count -= 1 |
| 408 |
|
| 409 |
def __repr__(self): |
| 410 |
return f'_DummyModuleLock({self.name!r}) at {id(self)}' |
| 411 |
|
| 412 |
|
| 413 |
class _ModuleLockManager: |
| 414 |
|
| 415 |
def __init__(self, name): |
| 416 |
self._name = name |
| 417 |
self._lock = None |
| 418 |
|
| 419 |
def __enter__(self): |
| 420 |
self._lock = _get_module_lock(self._name) |
| 421 |
self._lock.acquire() |
| 422 |
|
| 423 |
def __exit__(self, *args, **kwargs): |
| 424 |
self._lock.release() |
| 425 |
|
| 426 |
|
| 427 |
# The following two functions are for consumption by Python/import.c. |
| 428 |
|
| 429 |
def _get_module_lock(name): |
| 430 |
"""Get or create the module lock for a given module name. |
| 431 |
|
| 432 |
Acquire/release internally the global import lock to protect |
| 433 |
_module_locks.""" |
| 434 |
|
| 435 |
_imp.acquire_lock() |
| 436 |
try: |
| 437 |
try: |
| 438 |
lock = _module_locks[name]() |
| 439 |
except KeyError: |
| 440 |
lock = None |
| 441 |
|
| 442 |
if lock is None: |
| 443 |
if _thread is None: |
| 444 |
lock = _DummyModuleLock(name) |
| 445 |
else: |
| 446 |
lock = _ModuleLock(name) |
| 447 |
|
| 448 |
def cb(ref, name=name): |
| 449 |
_imp.acquire_lock() |
| 450 |
try: |
| 451 |
# bpo-31070: Check if another thread created a new lock |
| 452 |
# after the previous lock was destroyed |
| 453 |
# but before the weakref callback was called. |
| 454 |
if _module_locks.get(name) is ref: |
| 455 |
del _module_locks[name] |
| 456 |
finally: |
| 457 |
_imp.release_lock() |
| 458 |
|
| 459 |
_module_locks[name] = _weakref.ref(lock, cb) |
| 460 |
finally: |
| 461 |
_imp.release_lock() |
| 462 |
|
| 463 |
return lock |
| 464 |
|
| 465 |
|
| 466 |
def _lock_unlock_module(name): |
| 467 |
"""Acquires then releases the module lock for a given module name. |
| 468 |
|
| 469 |
This is used to ensure a module is completely initialized, in the |
| 470 |
event it is being imported by another thread. |
| 471 |
""" |
| 472 |
lock = _get_module_lock(name) |
| 473 |
try: |
| 474 |
lock.acquire() |
| 475 |
except _DeadlockError: |
| 476 |
# Concurrent circular import, we'll accept a partially initialized |
| 477 |
# module object. |
| 478 |
pass |
| 479 |
else: |
| 480 |
lock.release() |
| 481 |
|
| 482 |
# Frame stripping magic ############################################### |
| 483 |
def _call_with_frames_removed(f, *args, **kwds): |
| 484 |
"""remove_importlib_frames in import.c will always remove sequences |
| 485 |
of importlib frames that end with a call to this function |
| 486 |
|
| 487 |
Use it instead of a normal call in places where including the importlib |
| 488 |
frames introduces unwanted noise into the traceback (e.g. when executing |
| 489 |
module code) |
| 490 |
""" |
| 491 |
return f(*args, **kwds) |
| 492 |
|
| 493 |
|
| 494 |
def _verbose_message(message, *args, verbosity=1): |
| 495 |
"""Print the message to stderr if -v/PYTHONVERBOSE is turned on.""" |
| 496 |
if sys.flags.verbose >= verbosity: |
| 497 |
if not message.startswith(('#', 'import ')): |
| 498 |
message = '# ' + message |
| 499 |
print(message.format(*args), file=sys.stderr) |
| 500 |
|
| 501 |
|
| 502 |
def _requires_builtin(fxn): |
| 503 |
"""Decorator to verify the named module is built-in.""" |
| 504 |
def _requires_builtin_wrapper(self, fullname): |
| 505 |
if fullname not in sys.builtin_module_names: |
| 506 |
raise ImportError(f'{fullname!r} is not a built-in module', |
| 507 |
name=fullname) |
| 508 |
return fxn(self, fullname) |
| 509 |
_wrap(_requires_builtin_wrapper, fxn) |
| 510 |
return _requires_builtin_wrapper |
| 511 |
|
| 512 |
|
| 513 |
def _requires_frozen(fxn): |
| 514 |
"""Decorator to verify the named module is frozen.""" |
| 515 |
def _requires_frozen_wrapper(self, fullname): |
| 516 |
if not _imp.is_frozen(fullname): |
| 517 |
raise ImportError(f'{fullname!r} is not a frozen module', |
| 518 |
name=fullname) |
| 519 |
return fxn(self, fullname) |
| 520 |
_wrap(_requires_frozen_wrapper, fxn) |
| 521 |
return _requires_frozen_wrapper |
| 522 |
|
| 523 |
|
| 524 |
# Typically used by loader classes as a method replacement. |
| 525 |
def _load_module_shim(self, fullname): |
| 526 |
"""Load the specified module into sys.modules and return it. |
| 527 |
|
| 528 |
This method is deprecated. Use loader.exec_module() instead. |
| 529 |
|
| 530 |
""" |
| 531 |
msg = ("the load_module() method is deprecated and slated for removal in " |
| 532 |
"Python 3.15; use exec_module() instead") |
| 533 |
_warnings.warn(msg, DeprecationWarning) |
| 534 |
spec = spec_from_loader(fullname, self) |
| 535 |
if fullname in sys.modules: |
| 536 |
module = sys.modules[fullname] |
| 537 |
_exec(spec, module) |
| 538 |
return sys.modules[fullname] |
| 539 |
else: |
| 540 |
return _load(spec) |
| 541 |
|
| 542 |
# Module specifications ####################################################### |
| 543 |
|
| 544 |
def _module_repr(module): |
| 545 |
"""The implementation of ModuleType.__repr__().""" |
| 546 |
loader = getattr(module, '__loader__', None) |
| 547 |
if spec := getattr(module, "__spec__", None): |
| 548 |
return _module_repr_from_spec(spec) |
| 549 |
# Fall through to a catch-all which always succeeds. |
| 550 |
try: |
| 551 |
name = module.__name__ |
| 552 |
except AttributeError: |
| 553 |
name = '?' |
| 554 |
try: |
| 555 |
filename = module.__file__ |
| 556 |
except AttributeError: |
| 557 |
if loader is None: |
| 558 |
return f'<module {name!r}>' |
| 559 |
else: |
| 560 |
return f'<module {name!r} ({loader!r})>' |
| 561 |
else: |
| 562 |
return f'<module {name!r} from {filename!r}>' |
| 563 |
|
| 564 |
|
| 565 |
class ModuleSpec: |
| 566 |
"""The specification for a module, used for loading. |
| 567 |
|
| 568 |
A module's spec is the source for information about the module. For |
| 569 |
data associated with the module, including source, use the spec's |
| 570 |
loader. |
| 571 |
|
| 572 |
`name` is the absolute name of the module. `loader` is the loader |
| 573 |
to use when loading the module. `parent` is the name of the |
| 574 |
package the module is in. The parent is derived from the name. |
| 575 |
|
| 576 |
`is_package` determines if the module is considered a package or |
| 577 |
not. On modules this is reflected by the `__path__` attribute. |
| 578 |
|
| 579 |
`origin` is the specific location used by the loader from which to |
| 580 |
load the module, if that information is available. When filename is |
| 581 |
set, origin will match. |
| 582 |
|
| 583 |
`has_location` indicates that a spec's "origin" reflects a location. |
| 584 |
When this is True, `__file__` attribute of the module is set. |
| 585 |
|
| 586 |
`cached` is the location of the cached bytecode file, if any. It |
| 587 |
corresponds to the `__cached__` attribute. |
| 588 |
|
| 589 |
`submodule_search_locations` is the sequence of path entries to |
| 590 |
search when importing submodules. If set, is_package should be |
| 591 |
True--and False otherwise. |
| 592 |
|
| 593 |
Packages are simply modules that (may) have submodules. If a spec |
| 594 |
has a non-None value in `submodule_search_locations`, the import |
| 595 |
system will consider modules loaded from the spec as packages. |
| 596 |
|
| 597 |
Only finders (see importlib.abc.MetaPathFinder and |
| 598 |
importlib.abc.PathEntryFinder) should modify ModuleSpec instances. |
| 599 |
|
| 600 |
""" |
| 601 |
|
| 602 |
def __init__(self, name, loader, *, origin=None, loader_state=None, |
| 603 |
is_package=None): |
| 604 |
self.name = name |
| 605 |
self.loader = loader |
| 606 |
self.origin = origin |
| 607 |
self.loader_state = loader_state |
| 608 |
self.submodule_search_locations = [] if is_package else None |
| 609 |
self._uninitialized_submodules = [] |
| 610 |
|
| 611 |
# file-location attributes |
| 612 |
self._set_fileattr = False |
| 613 |
self._cached = None |
| 614 |
|
| 615 |
def __repr__(self): |
| 616 |
args = [f'name={self.name!r}', f'loader={self.loader!r}'] |
| 617 |
if self.origin is not None: |
| 618 |
args.append(f'origin={self.origin!r}') |
| 619 |
if self.submodule_search_locations is not None: |
| 620 |
args.append(f'submodule_search_locations={self.submodule_search_locations}') |
| 621 |
return f'{self.__class__.__name__}({", ".join(args)})' |
| 622 |
|
| 623 |
def __eq__(self, other): |
| 624 |
smsl = self.submodule_search_locations |
| 625 |
try: |
| 626 |
return (self.name == other.name and |
| 627 |
self.loader == other.loader and |
| 628 |
self.origin == other.origin and |
| 629 |
smsl == other.submodule_search_locations and |
| 630 |
self.cached == other.cached and |
| 631 |
self.has_location == other.has_location) |
| 632 |
except AttributeError: |
| 633 |
return NotImplemented |
| 634 |
|
| 635 |
@property |
| 636 |
def cached(self): |
| 637 |
if self._cached is None: |
| 638 |
if self.origin is not None and self._set_fileattr: |
| 639 |
if _bootstrap_external is None: |
| 640 |
raise NotImplementedError |
| 641 |
self._cached = _bootstrap_external._get_cached(self.origin) |
| 642 |
return self._cached |
| 643 |
|
| 644 |
@cached.setter |
| 645 |
def cached(self, cached): |
| 646 |
self._cached = cached |
| 647 |
|
| 648 |
@property |
| 649 |
def parent(self): |
| 650 |
"""The name of the module's parent.""" |
| 651 |
if self.submodule_search_locations is None: |
| 652 |
return self.name.rpartition('.')[0] |
| 653 |
else: |
| 654 |
return self.name |
| 655 |
|
| 656 |
@property |
| 657 |
def has_location(self): |
| 658 |
return self._set_fileattr |
| 659 |
|
| 660 |
@has_location.setter |
| 661 |
def has_location(self, value): |
| 662 |
self._set_fileattr = bool(value) |
| 663 |
|
| 664 |
|
| 665 |
def spec_from_loader(name, loader, *, origin=None, is_package=None): |
| 666 |
"""Return a module spec based on various loader methods.""" |
| 667 |
if origin is None: |
| 668 |
origin = getattr(loader, '_ORIGIN', None) |
| 669 |
|
| 670 |
if not origin and hasattr(loader, 'get_filename'): |
| 671 |
if _bootstrap_external is None: |
| 672 |
raise NotImplementedError |
| 673 |
spec_from_file_location = _bootstrap_external.spec_from_file_location |
| 674 |
|
| 675 |
if is_package is None: |
| 676 |
return spec_from_file_location(name, loader=loader) |
| 677 |
search = [] if is_package else None |
| 678 |
return spec_from_file_location(name, loader=loader, |
| 679 |
submodule_search_locations=search) |
| 680 |
|
| 681 |
if is_package is None: |
| 682 |
if hasattr(loader, 'is_package'): |
| 683 |
try: |
| 684 |
is_package = loader.is_package(name) |
| 685 |
except ImportError: |
| 686 |
is_package = None # aka, undefined |
| 687 |
else: |
| 688 |
# the default |
| 689 |
is_package = False |
| 690 |
|
| 691 |
return ModuleSpec(name, loader, origin=origin, is_package=is_package) |
| 692 |
|
| 693 |
|
| 694 |
def _spec_from_module(module, loader=None, origin=None): |
| 695 |
# This function is meant for use in _setup(). |
| 696 |
try: |
| 697 |
spec = module.__spec__ |
| 698 |
except AttributeError: |
| 699 |
pass |
| 700 |
else: |
| 701 |
if spec is not None: |
| 702 |
return spec |
| 703 |
|
| 704 |
name = module.__name__ |
| 705 |
if loader is None: |
| 706 |
try: |
| 707 |
loader = module.__loader__ |
| 708 |
except AttributeError: |
| 709 |
# loader will stay None. |
| 710 |
pass |
| 711 |
try: |
| 712 |
location = module.__file__ |
| 713 |
except AttributeError: |
| 714 |
location = None |
| 715 |
if origin is None: |
| 716 |
if loader is not None: |
| 717 |
origin = getattr(loader, '_ORIGIN', None) |
| 718 |
if not origin and location is not None: |
| 719 |
origin = location |
| 720 |
try: |
| 721 |
cached = module.__cached__ |
| 722 |
except AttributeError: |
| 723 |
cached = None |
| 724 |
try: |
| 725 |
submodule_search_locations = list(module.__path__) |
| 726 |
except AttributeError: |
| 727 |
submodule_search_locations = None |
| 728 |
|
| 729 |
spec = ModuleSpec(name, loader, origin=origin) |
| 730 |
spec._set_fileattr = False if location is None else (origin == location) |
| 731 |
spec.cached = cached |
| 732 |
spec.submodule_search_locations = submodule_search_locations |
| 733 |
return spec |
| 734 |
|
| 735 |
|
| 736 |
def _init_module_attrs(spec, module, *, override=False): |
| 737 |
# The passed-in module may be not support attribute assignment, |
| 738 |
# in which case we simply don't set the attributes. |
| 739 |
# __name__ |
| 740 |
if (override or getattr(module, '__name__', None) is None): |
| 741 |
try: |
| 742 |
module.__name__ = spec.name |
| 743 |
except AttributeError: |
| 744 |
pass |
| 745 |
# __loader__ |
| 746 |
if override or getattr(module, '__loader__', None) is None: |
| 747 |
loader = spec.loader |
| 748 |
if loader is None: |
| 749 |
# A backward compatibility hack. |
| 750 |
if spec.submodule_search_locations is not None: |
| 751 |
if _bootstrap_external is None: |
| 752 |
raise NotImplementedError |
| 753 |
NamespaceLoader = _bootstrap_external.NamespaceLoader |
| 754 |
|
| 755 |
loader = NamespaceLoader.__new__(NamespaceLoader) |
| 756 |
loader._path = spec.submodule_search_locations |
| 757 |
spec.loader = loader |
| 758 |
# While the docs say that module.__file__ is not set for |
| 759 |
# built-in modules, and the code below will avoid setting it if |
| 760 |
# spec.has_location is false, this is incorrect for namespace |
| 761 |
# packages. Namespace packages have no location, but their |
| 762 |
# __spec__.origin is None, and thus their module.__file__ |
| 763 |
# should also be None for consistency. While a bit of a hack, |
| 764 |
# this is the best place to ensure this consistency. |
| 765 |
# |
| 766 |
# See # https://docs.python.org/3/library/importlib.html#importlib.abc.Loader.load_module |
| 767 |
# and bpo-32305 |
| 768 |
module.__file__ = None |
| 769 |
try: |
| 770 |
module.__loader__ = loader |
| 771 |
except AttributeError: |
| 772 |
pass |
| 773 |
# __package__ |
| 774 |
if override or getattr(module, '__package__', None) is None: |
| 775 |
try: |
| 776 |
module.__package__ = spec.parent |
| 777 |
except AttributeError: |
| 778 |
pass |
| 779 |
# __spec__ |
| 780 |
try: |
| 781 |
module.__spec__ = spec |
| 782 |
except AttributeError: |
| 783 |
pass |
| 784 |
# __path__ |
| 785 |
if override or getattr(module, '__path__', None) is None: |
| 786 |
if spec.submodule_search_locations is not None: |
| 787 |
# XXX We should extend __path__ if it's already a list. |
| 788 |
try: |
| 789 |
module.__path__ = spec.submodule_search_locations |
| 790 |
except AttributeError: |
| 791 |
pass |
| 792 |
# __file__/__cached__ |
| 793 |
if spec.has_location: |
| 794 |
if override or getattr(module, '__file__', None) is None: |
| 795 |
try: |
| 796 |
module.__file__ = spec.origin |
| 797 |
except AttributeError: |
| 798 |
pass |
| 799 |
|
| 800 |
if override or getattr(module, '__cached__', None) is None: |
| 801 |
if spec.cached is not None: |
| 802 |
try: |
| 803 |
module.__cached__ = spec.cached |
| 804 |
except AttributeError: |
| 805 |
pass |
| 806 |
return module |
| 807 |
|
| 808 |
|
| 809 |
def module_from_spec(spec): |
| 810 |
"""Create a module based on the provided spec.""" |
| 811 |
# Typically loaders will not implement create_module(). |
| 812 |
module = None |
| 813 |
if hasattr(spec.loader, 'create_module'): |
| 814 |
# If create_module() returns `None` then it means default |
| 815 |
# module creation should be used. |
| 816 |
module = spec.loader.create_module(spec) |
| 817 |
elif hasattr(spec.loader, 'exec_module'): |
| 818 |
raise ImportError('loaders that define exec_module() ' |
| 819 |
'must also define create_module()') |
| 820 |
if module is None: |
| 821 |
module = _new_module(spec.name) |
| 822 |
_init_module_attrs(spec, module) |
| 823 |
return module |
| 824 |
|
| 825 |
|
| 826 |
def _module_repr_from_spec(spec): |
| 827 |
"""Return the repr to use for the module.""" |
| 828 |
name = '?' if spec.name is None else spec.name |
| 829 |
if spec.origin is None: |
| 830 |
loader = spec.loader |
| 831 |
if loader is None: |
| 832 |
return f'<module {name!r}>' |
| 833 |
elif ( |
| 834 |
_bootstrap_external is not None |
| 835 |
and isinstance(loader, _bootstrap_external.NamespaceLoader) |
| 836 |
): |
| 837 |
return f'<module {name!r} (namespace) from {list(loader._path)}>' |
| 838 |
else: |
| 839 |
return f'<module {name!r} ({loader!r})>' |
| 840 |
else: |
| 841 |
if spec.has_location: |
| 842 |
return f'<module {name!r} from {spec.origin!r}>' |
| 843 |
else: |
| 844 |
return f'<module {spec.name!r} ({spec.origin})>' |
| 845 |
|
| 846 |
|
| 847 |
# Used by importlib.reload() and _load_module_shim(). |
| 848 |
def _exec(spec, module): |
| 849 |
"""Execute the spec's specified module in an existing module's namespace.""" |
| 850 |
name = spec.name |
| 851 |
with _ModuleLockManager(name): |
| 852 |
if sys.modules.get(name) is not module: |
| 853 |
msg = f'module {name!r} not in sys.modules' |
| 854 |
raise ImportError(msg, name=name) |
| 855 |
try: |
| 856 |
if spec.loader is None: |
| 857 |
if spec.submodule_search_locations is None: |
| 858 |
raise ImportError('missing loader', name=spec.name) |
| 859 |
# Namespace package. |
| 860 |
_init_module_attrs(spec, module, override=True) |
| 861 |
else: |
| 862 |
_init_module_attrs(spec, module, override=True) |
| 863 |
if not hasattr(spec.loader, 'exec_module'): |
| 864 |
msg = (f"{_object_name(spec.loader)}.exec_module() not found; " |
| 865 |
"falling back to load_module()") |
| 866 |
_warnings.warn(msg, ImportWarning) |
| 867 |
spec.loader.load_module(name) |
| 868 |
else: |
| 869 |
spec.loader.exec_module(module) |
| 870 |
finally: |
| 871 |
# Update the order of insertion into sys.modules for module |
| 872 |
# clean-up at shutdown. |
| 873 |
module = sys.modules.pop(spec.name) |
| 874 |
sys.modules[spec.name] = module |
| 875 |
return module |
| 876 |
|
| 877 |
|
| 878 |
def _load_backward_compatible(spec): |
| 879 |
# It is assumed that all callers have been warned about using load_module() |
| 880 |
# appropriately before calling this function. |
| 881 |
try: |
| 882 |
spec.loader.load_module(spec.name) |
| 883 |
except: |
| 884 |
if spec.name in sys.modules: |
| 885 |
module = sys.modules.pop(spec.name) |
| 886 |
sys.modules[spec.name] = module |
| 887 |
raise |
| 888 |
# The module must be in sys.modules at this point! |
| 889 |
# Move it to the end of sys.modules. |
| 890 |
module = sys.modules.pop(spec.name) |
| 891 |
sys.modules[spec.name] = module |
| 892 |
if getattr(module, '__loader__', None) is None: |
| 893 |
try: |
| 894 |
module.__loader__ = spec.loader |
| 895 |
except AttributeError: |
| 896 |
pass |
| 897 |
if getattr(module, '__package__', None) is None: |
| 898 |
try: |
| 899 |
# Since module.__path__ may not line up with |
| 900 |
# spec.submodule_search_paths, we can't necessarily rely |
| 901 |
# on spec.parent here. |
| 902 |
module.__package__ = module.__name__ |
| 903 |
if not hasattr(module, '__path__'): |
| 904 |
module.__package__ = spec.name.rpartition('.')[0] |
| 905 |
except AttributeError: |
| 906 |
pass |
| 907 |
if getattr(module, '__spec__', None) is None: |
| 908 |
try: |
| 909 |
module.__spec__ = spec |
| 910 |
except AttributeError: |
| 911 |
pass |
| 912 |
return module |
| 913 |
|
| 914 |
def _load_unlocked(spec): |
| 915 |
# A helper for direct use by the import system. |
| 916 |
if spec.loader is not None: |
| 917 |
# Not a namespace package. |
| 918 |
if not hasattr(spec.loader, 'exec_module'): |
| 919 |
msg = (f"{_object_name(spec.loader)}.exec_module() not found; " |
| 920 |
"falling back to load_module()") |
| 921 |
_warnings.warn(msg, ImportWarning) |
| 922 |
return _load_backward_compatible(spec) |
| 923 |
|
| 924 |
module = module_from_spec(spec) |
| 925 |
|
| 926 |
# This must be done before putting the module in sys.modules |
| 927 |
# (otherwise an optimization shortcut in import.c becomes |
| 928 |
# wrong). |
| 929 |
spec._initializing = True |
| 930 |
try: |
| 931 |
sys.modules[spec.name] = module |
| 932 |
try: |
| 933 |
if spec.loader is None: |
| 934 |
if spec.submodule_search_locations is None: |
| 935 |
raise ImportError('missing loader', name=spec.name) |
| 936 |
# A namespace package so do nothing. |
| 937 |
else: |
| 938 |
spec.loader.exec_module(module) |
| 939 |
except: |
| 940 |
try: |
| 941 |
del sys.modules[spec.name] |
| 942 |
except KeyError: |
| 943 |
pass |
| 944 |
raise |
| 945 |
# Move the module to the end of sys.modules. |
| 946 |
# We don't ensure that the import-related module attributes get |
| 947 |
# set in the sys.modules replacement case. Such modules are on |
| 948 |
# their own. |
| 949 |
module = sys.modules.pop(spec.name) |
| 950 |
sys.modules[spec.name] = module |
| 951 |
_verbose_message('import {!r} # {!r}', spec.name, spec.loader) |
| 952 |
finally: |
| 953 |
spec._initializing = False |
| 954 |
|
| 955 |
return module |
| 956 |
|
| 957 |
# A method used during testing of _load_unlocked() and by |
| 958 |
# _load_module_shim(). |
| 959 |
def _load(spec): |
| 960 |
"""Return a new module object, loaded by the spec's loader. |
| 961 |
|
| 962 |
The module is not added to its parent. |
| 963 |
|
| 964 |
If a module is already in sys.modules, that existing module gets |
| 965 |
clobbered. |
| 966 |
|
| 967 |
""" |
| 968 |
with _ModuleLockManager(spec.name): |
| 969 |
return _load_unlocked(spec) |
| 970 |
|
| 971 |
|
| 972 |
# Loaders ##################################################################### |
| 973 |
|
| 974 |
class BuiltinImporter: |
| 975 |
|
| 976 |
"""Meta path import for built-in modules. |
| 977 |
|
| 978 |
All methods are either class or static methods to avoid the need to |
| 979 |
instantiate the class. |
| 980 |
|
| 981 |
""" |
| 982 |
|
| 983 |
_ORIGIN = "built-in" |
| 984 |
|
| 985 |
@classmethod |
| 986 |
def find_spec(cls, fullname, path=None, target=None): |
| 987 |
if _imp.is_builtin(fullname): |
| 988 |
return spec_from_loader(fullname, cls, origin=cls._ORIGIN) |
| 989 |
else: |
| 990 |
return None |
| 991 |
|
| 992 |
@staticmethod |
| 993 |
def create_module(spec): |
| 994 |
"""Create a built-in module""" |
| 995 |
if spec.name not in sys.builtin_module_names: |
| 996 |
raise ImportError(f'{spec.name!r} is not a built-in module', |
| 997 |
name=spec.name) |
| 998 |
return _call_with_frames_removed(_imp.create_builtin, spec) |
| 999 |
|
| 1000 |
@staticmethod |
| 1001 |
def exec_module(module): |
| 1002 |
"""Exec a built-in module""" |
| 1003 |
_call_with_frames_removed(_imp.exec_builtin, module) |
| 1004 |
|
| 1005 |
@classmethod |
| 1006 |
@_requires_builtin |
| 1007 |
def get_code(cls, fullname): |
| 1008 |
"""Return None as built-in modules do not have code objects.""" |
| 1009 |
return None |
| 1010 |
|
| 1011 |
@classmethod |
| 1012 |
@_requires_builtin |
| 1013 |
def get_source(cls, fullname): |
| 1014 |
"""Return None as built-in modules do not have source code.""" |
| 1015 |
return None |
| 1016 |
|
| 1017 |
@classmethod |
| 1018 |
@_requires_builtin |
| 1019 |
def is_package(cls, fullname): |
| 1020 |
"""Return False as built-in modules are never packages.""" |
| 1021 |
return False |
| 1022 |
|
| 1023 |
load_module = classmethod(_load_module_shim) |
| 1024 |
|
| 1025 |
|
| 1026 |
class FrozenImporter: |
| 1027 |
|
| 1028 |
"""Meta path import for frozen modules. |
| 1029 |
|
| 1030 |
All methods are either class or static methods to avoid the need to |
| 1031 |
instantiate the class. |
| 1032 |
|
| 1033 |
""" |
| 1034 |
|
| 1035 |
_ORIGIN = "frozen" |
| 1036 |
|
| 1037 |
@classmethod |
| 1038 |
def _fix_up_module(cls, module): |
| 1039 |
spec = module.__spec__ |
| 1040 |
state = spec.loader_state |
| 1041 |
if state is None: |
| 1042 |
# The module is missing FrozenImporter-specific values. |
| 1043 |
|
| 1044 |
# Fix up the spec attrs. |
| 1045 |
origname = vars(module).pop('__origname__', None) |
| 1046 |
assert origname, 'see PyImport_ImportFrozenModuleObject()' |
| 1047 |
ispkg = hasattr(module, '__path__') |
| 1048 |
assert _imp.is_frozen_package(module.__name__) == ispkg, ispkg |
| 1049 |
filename, pkgdir = cls._resolve_filename(origname, spec.name, ispkg) |
| 1050 |
spec.loader_state = type(sys.implementation)( |
| 1051 |
filename=filename, |
| 1052 |
origname=origname, |
| 1053 |
) |
| 1054 |
__path__ = spec.submodule_search_locations |
| 1055 |
if ispkg: |
| 1056 |
assert __path__ == [], __path__ |
| 1057 |
if pkgdir: |
| 1058 |
spec.submodule_search_locations.insert(0, pkgdir) |
| 1059 |
else: |
| 1060 |
assert __path__ is None, __path__ |
| 1061 |
|
| 1062 |
# Fix up the module attrs (the bare minimum). |
| 1063 |
assert not hasattr(module, '__file__'), module.__file__ |
| 1064 |
if filename: |
| 1065 |
try: |
| 1066 |
module.__file__ = filename |
| 1067 |
except AttributeError: |
| 1068 |
pass |
| 1069 |
if ispkg: |
| 1070 |
if module.__path__ != __path__: |
| 1071 |
assert module.__path__ == [], module.__path__ |
| 1072 |
module.__path__.extend(__path__) |
| 1073 |
else: |
| 1074 |
# These checks ensure that _fix_up_module() is only called |
| 1075 |
# in the right places. |
| 1076 |
__path__ = spec.submodule_search_locations |
| 1077 |
ispkg = __path__ is not None |
| 1078 |
# Check the loader state. |
| 1079 |
assert sorted(vars(state)) == ['filename', 'origname'], state |
| 1080 |
if state.origname: |
| 1081 |
# The only frozen modules with "origname" set are stdlib modules. |
| 1082 |
(__file__, pkgdir, |
| 1083 |
) = cls._resolve_filename(state.origname, spec.name, ispkg) |
| 1084 |
assert state.filename == __file__, (state.filename, __file__) |
| 1085 |
if pkgdir: |
| 1086 |
assert __path__ == [pkgdir], (__path__, pkgdir) |
| 1087 |
else: |
| 1088 |
assert __path__ == ([] if ispkg else None), __path__ |
| 1089 |
else: |
| 1090 |
__file__ = None |
| 1091 |
assert state.filename is None, state.filename |
| 1092 |
assert __path__ == ([] if ispkg else None), __path__ |
| 1093 |
# Check the file attrs. |
| 1094 |
if __file__: |
| 1095 |
assert hasattr(module, '__file__') |
| 1096 |
assert module.__file__ == __file__, (module.__file__, __file__) |
| 1097 |
else: |
| 1098 |
assert not hasattr(module, '__file__'), module.__file__ |
| 1099 |
if ispkg: |
| 1100 |
assert hasattr(module, '__path__') |
| 1101 |
assert module.__path__ == __path__, (module.__path__, __path__) |
| 1102 |
else: |
| 1103 |
assert not hasattr(module, '__path__'), module.__path__ |
| 1104 |
assert not spec.has_location |
| 1105 |
|
| 1106 |
@classmethod |
| 1107 |
def _resolve_filename(cls, fullname, alias=None, ispkg=False): |
| 1108 |
if not fullname or not getattr(sys, '_stdlib_dir', None): |
| 1109 |
return None, None |
| 1110 |
try: |
| 1111 |
sep = cls._SEP |
| 1112 |
except AttributeError: |
| 1113 |
sep = cls._SEP = '\\' if sys.platform == 'win32' else '/' |
| 1114 |
|
| 1115 |
if fullname != alias: |
| 1116 |
if fullname.startswith('<'): |
| 1117 |
fullname = fullname[1:] |
| 1118 |
if not ispkg: |
| 1119 |
fullname = f'{fullname}.__init__' |
| 1120 |
else: |
| 1121 |
ispkg = False |
| 1122 |
relfile = fullname.replace('.', sep) |
| 1123 |
if ispkg: |
| 1124 |
pkgdir = f'{sys._stdlib_dir}{sep}{relfile}' |
| 1125 |
filename = f'{pkgdir}{sep}__init__.py' |
| 1126 |
else: |
| 1127 |
pkgdir = None |
| 1128 |
filename = f'{sys._stdlib_dir}{sep}{relfile}.py' |
| 1129 |
return filename, pkgdir |
| 1130 |
|
| 1131 |
@classmethod |
| 1132 |
def find_spec(cls, fullname, path=None, target=None): |
| 1133 |
info = _call_with_frames_removed(_imp.find_frozen, fullname) |
| 1134 |
if info is None: |
| 1135 |
return None |
| 1136 |
# We get the marshaled data in exec_module() (the loader |
| 1137 |
# part of the importer), instead of here (the finder part). |
| 1138 |
# The loader is the usual place to get the data that will |
| 1139 |
# be loaded into the module. (For example, see _LoaderBasics |
| 1140 |
# in _bootstrap_external.py.) Most importantly, this importer |
| 1141 |
# is simpler if we wait to get the data. |
| 1142 |
# However, getting as much data in the finder as possible |
| 1143 |
# to later load the module is okay, and sometimes important. |
| 1144 |
# (That's why ModuleSpec.loader_state exists.) This is |
| 1145 |
# especially true if it avoids throwing away expensive data |
| 1146 |
# the loader would otherwise duplicate later and can be done |
| 1147 |
# efficiently. In this case it isn't worth it. |
| 1148 |
_, ispkg, origname = info |
| 1149 |
spec = spec_from_loader(fullname, cls, |
| 1150 |
origin=cls._ORIGIN, |
| 1151 |
is_package=ispkg) |
| 1152 |
filename, pkgdir = cls._resolve_filename(origname, fullname, ispkg) |
| 1153 |
spec.loader_state = type(sys.implementation)( |
| 1154 |
filename=filename, |
| 1155 |
origname=origname, |
| 1156 |
) |
| 1157 |
if pkgdir: |
| 1158 |
spec.submodule_search_locations.insert(0, pkgdir) |
| 1159 |
return spec |
| 1160 |
|
| 1161 |
@staticmethod |
| 1162 |
def create_module(spec): |
| 1163 |
"""Set __file__, if able.""" |
| 1164 |
module = _new_module(spec.name) |
| 1165 |
try: |
| 1166 |
filename = spec.loader_state.filename |
| 1167 |
except AttributeError: |
| 1168 |
pass |
| 1169 |
else: |
| 1170 |
if filename: |
| 1171 |
module.__file__ = filename |
| 1172 |
return module |
| 1173 |
|
| 1174 |
@staticmethod |
| 1175 |
def exec_module(module): |
| 1176 |
spec = module.__spec__ |
| 1177 |
name = spec.name |
| 1178 |
code = _call_with_frames_removed(_imp.get_frozen_object, name) |
| 1179 |
exec(code, module.__dict__) |
| 1180 |
|
| 1181 |
@classmethod |
| 1182 |
def load_module(cls, fullname): |
| 1183 |
"""Load a frozen module. |
| 1184 |
|
| 1185 |
This method is deprecated. Use exec_module() instead. |
| 1186 |
|
| 1187 |
""" |
| 1188 |
# Warning about deprecation implemented in _load_module_shim(). |
| 1189 |
module = _load_module_shim(cls, fullname) |
| 1190 |
info = _imp.find_frozen(fullname) |
| 1191 |
assert info is not None |
| 1192 |
_, ispkg, origname = info |
| 1193 |
module.__origname__ = origname |
| 1194 |
vars(module).pop('__file__', None) |
| 1195 |
if ispkg: |
| 1196 |
module.__path__ = [] |
| 1197 |
cls._fix_up_module(module) |
| 1198 |
return module |
| 1199 |
|
| 1200 |
@classmethod |
| 1201 |
@_requires_frozen |
| 1202 |
def get_code(cls, fullname): |
| 1203 |
"""Return the code object for the frozen module.""" |
| 1204 |
return _imp.get_frozen_object(fullname) |
| 1205 |
|
| 1206 |
@classmethod |
| 1207 |
@_requires_frozen |
| 1208 |
def get_source(cls, fullname): |
| 1209 |
"""Return None as frozen modules do not have source code.""" |
| 1210 |
return None |
| 1211 |
|
| 1212 |
@classmethod |
| 1213 |
@_requires_frozen |
| 1214 |
def is_package(cls, fullname): |
| 1215 |
"""Return True if the frozen module is a package.""" |
| 1216 |
return _imp.is_frozen_package(fullname) |
| 1217 |
|
| 1218 |
|
| 1219 |
# Import itself ############################################################### |
| 1220 |
|
| 1221 |
class _ImportLockContext: |
| 1222 |
|
| 1223 |
"""Context manager for the import lock.""" |
| 1224 |
|
| 1225 |
def __enter__(self): |
| 1226 |
"""Acquire the import lock.""" |
| 1227 |
_imp.acquire_lock() |
| 1228 |
|
| 1229 |
def __exit__(self, exc_type, exc_value, exc_traceback): |
| 1230 |
"""Release the import lock regardless of any raised exceptions.""" |
| 1231 |
_imp.release_lock() |
| 1232 |
|
| 1233 |
|
| 1234 |
def _resolve_name(name, package, level): |
| 1235 |
"""Resolve a relative module name to an absolute one.""" |
| 1236 |
bits = package.rsplit('.', level - 1) |
| 1237 |
if len(bits) < level: |
| 1238 |
raise ImportError('attempted relative import beyond top-level package') |
| 1239 |
base = bits[0] |
| 1240 |
return f'{base}.{name}' if name else base |
| 1241 |
|
| 1242 |
|
| 1243 |
def _find_spec(name, path, target=None): |
| 1244 |
"""Find a module's spec.""" |
| 1245 |
meta_path = sys.meta_path |
| 1246 |
if meta_path is None: |
| 1247 |
raise ImportError("sys.meta_path is None, Python is likely " |
| 1248 |
"shutting down") |
| 1249 |
|
| 1250 |
# gh-130094: Copy sys.meta_path so that we have a consistent view of the |
| 1251 |
# list while iterating over it. |
| 1252 |
meta_path = list(meta_path) |
| 1253 |
if not meta_path: |
| 1254 |
_warnings.warn('sys.meta_path is empty', ImportWarning) |
| 1255 |
|
| 1256 |
# We check sys.modules here for the reload case. While a passed-in |
| 1257 |
# target will usually indicate a reload there is no guarantee, whereas |
| 1258 |
# sys.modules provides one. |
| 1259 |
is_reload = name in sys.modules |
| 1260 |
for finder in meta_path: |
| 1261 |
with _ImportLockContext(): |
| 1262 |
try: |
| 1263 |
find_spec = finder.find_spec |
| 1264 |
except AttributeError: |
| 1265 |
continue |
| 1266 |
else: |
| 1267 |
spec = find_spec(name, path, target) |
| 1268 |
if spec is not None: |
| 1269 |
# The parent import may have already imported this module. |
| 1270 |
if not is_reload and name in sys.modules: |
| 1271 |
module = sys.modules[name] |
| 1272 |
try: |
| 1273 |
__spec__ = module.__spec__ |
| 1274 |
except AttributeError: |
| 1275 |
# We use the found spec since that is the one that |
| 1276 |
# we would have used if the parent module hadn't |
| 1277 |
# beaten us to the punch. |
| 1278 |
return spec |
| 1279 |
else: |
| 1280 |
if __spec__ is None: |
| 1281 |
return spec |
| 1282 |
else: |
| 1283 |
return __spec__ |
| 1284 |
else: |
| 1285 |
return spec |
| 1286 |
else: |
| 1287 |
return None |
| 1288 |
|
| 1289 |
|
| 1290 |
def _sanity_check(name, package, level): |
| 1291 |
"""Verify arguments are "sane".""" |
| 1292 |
if not isinstance(name, str): |
| 1293 |
raise TypeError(f'module name must be str, not {type(name)}') |
| 1294 |
if level < 0: |
| 1295 |
raise ValueError('level must be >= 0') |
| 1296 |
if level > 0: |
| 1297 |
if not isinstance(package, str): |
| 1298 |
raise TypeError('__package__ not set to a string') |
| 1299 |
elif not package: |
| 1300 |
raise ImportError('attempted relative import with no known parent ' |
| 1301 |
'package') |
| 1302 |
if not name and level == 0: |
| 1303 |
raise ValueError('Empty module name') |
| 1304 |
|
| 1305 |
|
| 1306 |
_ERR_MSG_PREFIX = 'No module named ' |
| 1307 |
|
| 1308 |
def _find_and_load_unlocked(name, import_): |
| 1309 |
path = None |
| 1310 |
parent = name.rpartition('.')[0] |
| 1311 |
parent_spec = None |
| 1312 |
if parent: |
| 1313 |
if parent not in sys.modules: |
| 1314 |
_call_with_frames_removed(import_, parent) |
| 1315 |
# Crazy side-effects! |
| 1316 |
module = sys.modules.get(name) |
| 1317 |
if module is not None: |
| 1318 |
return module |
| 1319 |
parent_module = sys.modules[parent] |
| 1320 |
try: |
| 1321 |
path = parent_module.__path__ |
| 1322 |
except AttributeError: |
| 1323 |
msg = f'{_ERR_MSG_PREFIX}{name!r}; {parent!r} is not a package' |
| 1324 |
raise ModuleNotFoundError(msg, name=name) from None |
| 1325 |
parent_spec = parent_module.__spec__ |
| 1326 |
if getattr(parent_spec, '_initializing', False): |
| 1327 |
_call_with_frames_removed(import_, parent) |
| 1328 |
# Crazy side-effects (again)! |
| 1329 |
module = sys.modules.get(name) |
| 1330 |
if module is not None: |
| 1331 |
return module |
| 1332 |
child = name.rpartition('.')[2] |
| 1333 |
spec = _find_spec(name, path) |
| 1334 |
if spec is None: |
| 1335 |
raise ModuleNotFoundError(f'{_ERR_MSG_PREFIX}{name!r}', name=name) |
| 1336 |
else: |
| 1337 |
if parent_spec: |
| 1338 |
# Temporarily add child we are currently importing to parent's |
| 1339 |
# _uninitialized_submodules for circular import tracking. |
| 1340 |
parent_spec._uninitialized_submodules.append(child) |
| 1341 |
try: |
| 1342 |
module = _load_unlocked(spec) |
| 1343 |
finally: |
| 1344 |
if parent_spec: |
| 1345 |
parent_spec._uninitialized_submodules.pop() |
| 1346 |
if parent: |
| 1347 |
# Set the module as an attribute on its parent. |
| 1348 |
parent_module = sys.modules[parent] |
| 1349 |
try: |
| 1350 |
setattr(parent_module, child, module) |
| 1351 |
except AttributeError: |
| 1352 |
msg = f"Cannot set an attribute on {parent!r} for child module {child!r}" |
| 1353 |
_warnings.warn(msg, ImportWarning) |
| 1354 |
return module |
| 1355 |
|
| 1356 |
|
| 1357 |
_NEEDS_LOADING = object() |
| 1358 |
|
| 1359 |
|
| 1360 |
def _find_and_load(name, import_): |
| 1361 |
"""Find and load the module.""" |
| 1362 |
|
| 1363 |
# Optimization: we avoid unneeded module locking if the module |
| 1364 |
# already exists in sys.modules and is fully initialized. |
| 1365 |
module = sys.modules.get(name, _NEEDS_LOADING) |
| 1366 |
if (module is _NEEDS_LOADING or |
| 1367 |
getattr(getattr(module, "__spec__", None), "_initializing", False)): |
| 1368 |
with _ModuleLockManager(name): |
| 1369 |
module = sys.modules.get(name, _NEEDS_LOADING) |
| 1370 |
if module is _NEEDS_LOADING: |
| 1371 |
return _find_and_load_unlocked(name, import_) |
| 1372 |
|
| 1373 |
# Optimization: only call _bootstrap._lock_unlock_module() if |
| 1374 |
# module.__spec__._initializing is True. |
| 1375 |
# NOTE: because of this, initializing must be set *before* |
| 1376 |
# putting the new module in sys.modules. |
| 1377 |
_lock_unlock_module(name) |
| 1378 |
else: |
| 1379 |
# Verify the module is still in sys.modules. Another thread may have |
| 1380 |
# removed it (due to import failure) between our sys.modules.get() |
| 1381 |
# above and the _initializing check. If removed, we retry the import |
| 1382 |
# to preserve normal semantics: the caller gets the exception from |
| 1383 |
# the actual import failure rather than a synthetic error. |
| 1384 |
if sys.modules.get(name) is not module: |
| 1385 |
return _find_and_load(name, import_) |
| 1386 |
|
| 1387 |
if module is None: |
| 1388 |
message = f'import of {name} halted; None in sys.modules' |
| 1389 |
raise ModuleNotFoundError(message, name=name) |
| 1390 |
|
| 1391 |
return module |
| 1392 |
|
| 1393 |
|
| 1394 |
def _gcd_import(name, package=None, level=0): |
| 1395 |
"""Import and return the module based on its name, the package the call is |
| 1396 |
being made from, and the level adjustment. |
| 1397 |
|
| 1398 |
This function represents the greatest common denominator of functionality |
| 1399 |
between import_module and __import__. This includes setting __package__ if |
| 1400 |
the loader did not. |
| 1401 |
|
| 1402 |
""" |
| 1403 |
_sanity_check(name, package, level) |
| 1404 |
if level > 0: |
| 1405 |
name = _resolve_name(name, package, level) |
| 1406 |
return _find_and_load(name, _gcd_import) |
| 1407 |
|
| 1408 |
|
| 1409 |
def _handle_fromlist(module, fromlist, import_, *, recursive=False): |
| 1410 |
"""Figure out what __import__ should return. |
| 1411 |
|
| 1412 |
The import_ parameter is a callable which takes the name of module to |
| 1413 |
import. It is required to decouple the function from assuming importlib's |
| 1414 |
import implementation is desired. |
| 1415 |
|
| 1416 |
""" |
| 1417 |
# The hell that is fromlist ... |
| 1418 |
# If a package was imported, try to import stuff from fromlist. |
| 1419 |
for x in fromlist: |
| 1420 |
if not isinstance(x, str): |
| 1421 |
if recursive: |
| 1422 |
where = module.__name__ + '.__all__' |
| 1423 |
else: |
| 1424 |
where = "``from list''" |
| 1425 |
raise TypeError(f"Item in {where} must be str, " |
| 1426 |
f"not {type(x).__name__}") |
| 1427 |
elif x == '*': |
| 1428 |
if not recursive and hasattr(module, '__all__'): |
| 1429 |
_handle_fromlist(module, module.__all__, import_, |
| 1430 |
recursive=True) |
| 1431 |
elif not hasattr(module, x): |
| 1432 |
from_name = f'{module.__name__}.{x}' |
| 1433 |
try: |
| 1434 |
_call_with_frames_removed(import_, from_name) |
| 1435 |
except ModuleNotFoundError as exc: |
| 1436 |
# Backwards-compatibility dictates we ignore failed |
| 1437 |
# imports triggered by fromlist for modules that don't |
| 1438 |
# exist. |
| 1439 |
if (exc.name == from_name and |
| 1440 |
sys.modules.get(from_name, _NEEDS_LOADING) is not None): |
| 1441 |
continue |
| 1442 |
raise |
| 1443 |
return module |
| 1444 |
|
| 1445 |
|
| 1446 |
def _calc___package__(globals): |
| 1447 |
"""Calculate what __package__ should be. |
| 1448 |
|
| 1449 |
__package__ is not guaranteed to be defined or could be set to None |
| 1450 |
to represent that its proper value is unknown. |
| 1451 |
|
| 1452 |
""" |
| 1453 |
package = globals.get('__package__') |
| 1454 |
spec = globals.get('__spec__') |
| 1455 |
if package is not None: |
| 1456 |
if spec is not None and package != spec.parent: |
| 1457 |
_warnings.warn("__package__ != __spec__.parent " |
| 1458 |
f"({package!r} != {spec.parent!r})", |
| 1459 |
DeprecationWarning, stacklevel=3) |
| 1460 |
return package |
| 1461 |
elif spec is not None: |
| 1462 |
return spec.parent |
| 1463 |
else: |
| 1464 |
_warnings.warn("can't resolve package from __spec__ or __package__, " |
| 1465 |
"falling back on __name__ and __path__", |
| 1466 |
ImportWarning, stacklevel=3) |
| 1467 |
package = globals['__name__'] |
| 1468 |
if '__path__' not in globals: |
| 1469 |
package = package.rpartition('.')[0] |
| 1470 |
return package |
| 1471 |
|
| 1472 |
|
| 1473 |
def __import__(name, globals=None, locals=None, fromlist=(), level=0): |
| 1474 |
"""Import a module. |
| 1475 |
|
| 1476 |
The 'globals' argument is used to infer where the import is occurring from |
| 1477 |
to handle relative imports. The 'locals' argument is ignored. The |
| 1478 |
'fromlist' argument specifies what should exist as attributes on the module |
| 1479 |
being imported (e.g. ``from module import <fromlist>``). The 'level' |
| 1480 |
argument represents the package location to import from in a relative |
| 1481 |
import (e.g. ``from ..pkg import mod`` would have a 'level' of 2). |
| 1482 |
|
| 1483 |
""" |
| 1484 |
if level == 0: |
| 1485 |
module = _gcd_import(name) |
| 1486 |
else: |
| 1487 |
globals_ = globals if globals is not None else {} |
| 1488 |
package = _calc___package__(globals_) |
| 1489 |
module = _gcd_import(name, package, level) |
| 1490 |
if not fromlist: |
| 1491 |
# Return up to the first dot in 'name'. This is complicated by the fact |
| 1492 |
# that 'name' may be relative. |
| 1493 |
if level == 0: |
| 1494 |
return _gcd_import(name.partition('.')[0]) |
| 1495 |
elif not name: |
| 1496 |
return module |
| 1497 |
else: |
| 1498 |
# Figure out where to slice the module's name up to the first dot |
| 1499 |
# in 'name'. |
| 1500 |
cut_off = len(name) - len(name.partition('.')[0]) |
| 1501 |
# Slice end needs to be positive to alleviate need to special-case |
| 1502 |
# when ``'.' not in name``. |
| 1503 |
return sys.modules[module.__name__[:len(module.__name__)-cut_off]] |
| 1504 |
elif hasattr(module, '__path__'): |
| 1505 |
return _handle_fromlist(module, fromlist, _gcd_import) |
| 1506 |
else: |
| 1507 |
return module |
| 1508 |
|
| 1509 |
|
| 1510 |
def _builtin_from_name(name): |
| 1511 |
spec = BuiltinImporter.find_spec(name) |
| 1512 |
if spec is None: |
| 1513 |
raise ImportError('no built-in module named ' + name) |
| 1514 |
return _load_unlocked(spec) |
| 1515 |
|
| 1516 |
|
| 1517 |
def _setup(sys_module, _imp_module): |
| 1518 |
"""Setup importlib by importing needed built-in modules and injecting them |
| 1519 |
into the global namespace. |
| 1520 |
|
| 1521 |
As sys is needed for sys.modules access and _imp is needed to load built-in |
| 1522 |
modules, those two modules must be explicitly passed in. |
| 1523 |
|
| 1524 |
""" |
| 1525 |
global _imp, sys, _blocking_on |
| 1526 |
_imp = _imp_module |
| 1527 |
sys = sys_module |
| 1528 |
|
| 1529 |
# Set up the spec for existing builtin/frozen modules. |
| 1530 |
module_type = type(sys) |
| 1531 |
for name, module in sys.modules.items(): |
| 1532 |
if isinstance(module, module_type): |
| 1533 |
if name in sys.builtin_module_names: |
| 1534 |
loader = BuiltinImporter |
| 1535 |
elif _imp.is_frozen(name): |
| 1536 |
loader = FrozenImporter |
| 1537 |
else: |
| 1538 |
continue |
| 1539 |
spec = _spec_from_module(module, loader) |
| 1540 |
_init_module_attrs(spec, module) |
| 1541 |
if loader is FrozenImporter: |
| 1542 |
loader._fix_up_module(module) |
| 1543 |
|
| 1544 |
# Directly load built-in modules needed during bootstrap. |
| 1545 |
self_module = sys.modules[__name__] |
| 1546 |
for builtin_name in ('_thread', '_warnings', '_weakref'): |
| 1547 |
if builtin_name not in sys.modules: |
| 1548 |
builtin_module = _builtin_from_name(builtin_name) |
| 1549 |
else: |
| 1550 |
builtin_module = sys.modules[builtin_name] |
| 1551 |
setattr(self_module, builtin_name, builtin_module) |
| 1552 |
|
| 1553 |
# Instantiation requires _weakref to have been set. |
| 1554 |
_blocking_on = _WeakValueDictionary() |
| 1555 |
|
| 1556 |
|
| 1557 |
def _install(sys_module, _imp_module): |
| 1558 |
"""Install importers for builtin and frozen modules""" |
| 1559 |
_setup(sys_module, _imp_module) |
| 1560 |
|
| 1561 |
sys.meta_path.append(BuiltinImporter) |
| 1562 |
sys.meta_path.append(FrozenImporter) |
| 1563 |
|
| 1564 |
|
| 1565 |
def _install_external_importers(): |
| 1566 |
"""Install importers that require external filesystem access""" |
| 1567 |
global _bootstrap_external |
| 1568 |
import _frozen_importlib_external |
| 1569 |
_bootstrap_external = _frozen_importlib_external |
| 1570 |
_frozen_importlib_external._install(sys.modules[__name__]) |