Skip to content

Commit 894242f

Browse files
authored
Avoid exceptions when loading importlib (#1594)
* Avoid exceptions when loading importlib * Use Ubuntu 20.04 for CI * Fix build * Avoid a few more KeyError exceptions
1 parent b81818d commit 894242f

3 files changed

Lines changed: 14 additions & 15 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ jobs:
1010
strategy:
1111
fail-fast: false
1212
matrix:
13-
os: [windows-2022, ubuntu-latest, macos-latest]
13+
os: [windows-latest, ubuntu-20.04, macos-latest]
1414

1515
steps:
1616
- name: Install tools
17-
if: matrix.os == 'ubuntu-latest'
17+
if: matrix.os == 'ubuntu-20.04'
1818
run: sudo apt-get -yq install mono-vbnc dos2unix
1919
- uses: actions/checkout@v2
2020
with:

.vsts-ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ jobs:
66
displayName: Windows
77
timeoutInMinutes: 180
88
pool:
9-
vmImage: windows-2022
9+
vmImage: windows-latest
1010
steps:
1111
- template: Build/steps.yml
1212
parameters:
@@ -18,7 +18,7 @@ jobs:
1818
displayName: Linux (Ubuntu)
1919
timeoutInMinutes: 180
2020
pool:
21-
vmImage: ubuntu-latest
21+
vmImage: ubuntu-20.04
2222
steps:
2323
- template: Build/steps.yml
2424
parameters:

Src/StdLib/Lib/importlib/_bootstrap.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
_CASE_INSENSITIVE_PLATFORMS = 'win', 'cygwin', 'darwin'
2626

27+
_unspecified = object() # ironpython: default value for dict.get
2728

2829
def _make_relax_case():
2930
if sys.platform.startswith(_CASE_INSENSITIVE_PLATFORMS):
@@ -276,10 +277,10 @@ def _get_module_lock(name):
276277
277278
Should only be called with the import lock taken."""
278279
lock = None
279-
try:
280-
lock = _module_locks[name]()
281-
except KeyError:
282-
pass
280+
# ironpython: optimization to avoid KeyError exception
281+
lock_fn = _module_locks.get(name, _unspecified)
282+
if lock_fn is not _unspecified:
283+
lock = lock_fn()
283284
if lock is None:
284285
if _thread is None:
285286
lock = _DummyModuleLock(name)
@@ -958,10 +959,7 @@ def _spec_from_module(module, loader=None, origin=None):
958959
location = None
959960
if origin is None:
960961
if location is None:
961-
try:
962-
origin = loader._ORIGIN
963-
except AttributeError:
964-
origin = None
962+
origin = getattr(loader, '_ORIGIN', None) # ironpython: optimization to avoid KeyError exception
965963
else:
966964
origin = location
967965
try:
@@ -1830,9 +1828,9 @@ def _path_importer_cache(cls, path):
18301828
"""
18311829
if path == '':
18321830
path = _os.getcwd()
1833-
try:
1834-
finder = sys.path_importer_cache[path]
1835-
except KeyError:
1831+
# ironpython: optimization to avoid KeyError exception
1832+
finder = sys.path_importer_cache.get(path, _unspecified)
1833+
if finder is _unspecified:
18361834
finder = cls._path_hooks(path)
18371835
sys.path_importer_cache[path] = finder
18381836
return finder
@@ -2355,6 +2353,7 @@ def _setup(sys_module, _imp_module):
23552353

23562354
# Directly load the os module (needed during bootstrap).
23572355
os_details = ('posix', ['/']), ('nt', ['\\', '/'])
2356+
if sys.platform == 'win32': os_details = reversed(os_details) # ironpython: optimization to avoid ImportError exception
23582357
for builtin_os, path_separators in os_details:
23592358
# Assumption made in _path_join()
23602359
assert all(len(sep) == 1 for sep in path_separators)

0 commit comments

Comments
 (0)