Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f0cb692

Browse files
committed
Issue #18415: Normalize what type of quotes are used with string
constants in importlib._bootstrap. Along the way clean up from string interpolation to use the repr explicitly. Initial patch by Madison May.
1 parent d917dcb commit f0cb692

2 files changed

Lines changed: 36 additions & 36 deletions

File tree

Lib/importlib/_bootstrap.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def acquire(self):
175175
self.count += 1
176176
return True
177177
if self.has_deadlock():
178-
raise _DeadlockError("deadlock detected by %r" % self)
178+
raise _DeadlockError('deadlock detected by %r' % self)
179179
if self.wakeup.acquire(False):
180180
self.waiters += 1
181181
# Wait for a release() call
@@ -188,7 +188,7 @@ def release(self):
188188
tid = _thread.get_ident()
189189
with self.lock:
190190
if self.owner != tid:
191-
raise RuntimeError("cannot release un-acquired lock")
191+
raise RuntimeError('cannot release un-acquired lock')
192192
assert self.count > 0
193193
self.count -= 1
194194
if self.count == 0:
@@ -198,7 +198,7 @@ def release(self):
198198
self.wakeup.release()
199199

200200
def __repr__(self):
201-
return "_ModuleLock({!r}) at {}".format(self.name, id(self))
201+
return '_ModuleLock({!r}) at {}'.format(self.name, id(self))
202202

203203

204204
class _DummyModuleLock:
@@ -215,11 +215,11 @@ def acquire(self):
215215

216216
def release(self):
217217
if self.count == 0:
218-
raise RuntimeError("cannot release un-acquired lock")
218+
raise RuntimeError('cannot release un-acquired lock')
219219
self.count -= 1
220220

221221
def __repr__(self):
222-
return "_DummyModuleLock({!r}) at {}".format(self.name, id(self))
222+
return '_DummyModuleLock({!r}) at {}'.format(self.name, id(self))
223223

224224

225225
# The following two functions are for consumption by Python/import.c.
@@ -603,7 +603,7 @@ def _check_name_wrapper(self, name=None, *args, **kwargs):
603603
if name is None:
604604
name = self.name
605605
elif self.name != name:
606-
raise ImportError("loader cannot handle %s" % name, name=name)
606+
raise ImportError('loader cannot handle %s' % name, name=name)
607607
return method(self, name, *args, **kwargs)
608608
_wrap(_check_name_wrapper, method)
609609
return _check_name_wrapper
@@ -613,7 +613,7 @@ def _requires_builtin(fxn):
613613
"""Decorator to verify the named module is built-in."""
614614
def _requires_builtin_wrapper(self, fullname):
615615
if fullname not in sys.builtin_module_names:
616-
raise ImportError("{} is not a built-in module".format(fullname),
616+
raise ImportError('{} is not a built-in module'.format(fullname),
617617
name=fullname)
618618
return fxn(self, fullname)
619619
_wrap(_requires_builtin_wrapper, fxn)
@@ -624,7 +624,7 @@ def _requires_frozen(fxn):
624624
"""Decorator to verify the named module is frozen."""
625625
def _requires_frozen_wrapper(self, fullname):
626626
if not _imp.is_frozen(fullname):
627-
raise ImportError("{} is not a frozen module".format(fullname),
627+
raise ImportError('{} is not a frozen module'.format(fullname),
628628
name=fullname)
629629
return fxn(self, fullname)
630630
_wrap(_requires_frozen_wrapper, fxn)
@@ -639,7 +639,7 @@ def _find_module_shim(self, fullname):
639639
# return None.
640640
loader, portions = self.find_loader(fullname)
641641
if loader is None and len(portions):
642-
msg = "Not importing directory {}: missing __init__"
642+
msg = 'Not importing directory {}: missing __init__'
643643
_warnings.warn(msg.format(portions[0]), ImportWarning)
644644
return loader
645645

@@ -694,7 +694,7 @@ def _validate_bytecode_header(data, source_stats=None, name=None, path=None):
694694
pass
695695
else:
696696
if _r_long(raw_size) != source_size:
697-
raise ImportError("bytecode is stale for {!r}".format(name),
697+
raise ImportError('bytecode is stale for {!r}'.format(name),
698698
**exc_details)
699699
return data[12:]
700700

@@ -708,7 +708,7 @@ def _compile_bytecode(data, name=None, bytecode_path=None, source_path=None):
708708
_imp._fix_co_filename(code, source_path)
709709
return code
710710
else:
711-
raise ImportError("Non-code object in {!r}".format(bytecode_path),
711+
raise ImportError('Non-code object in {!r}'.format(bytecode_path),
712712
name=name, path=bytecode_path)
713713

714714
def _code_to_bytecode(code, mtime=0, source_size=0):
@@ -746,7 +746,7 @@ class BuiltinImporter:
746746

747747
@classmethod
748748
def module_repr(cls, module):
749-
return "<module '{}' (built-in)>".format(module.__name__)
749+
return '<module {!r} (built-in)>'.format(module.__name__)
750750

751751
@classmethod
752752
def find_module(cls, fullname, path=None):
@@ -798,7 +798,7 @@ class FrozenImporter:
798798

799799
@classmethod
800800
def module_repr(cls, m):
801-
return "<module '{}' (frozen)>".format(m.__name__)
801+
return '<module {!r} (frozen)>'.format(m.__name__)
802802

803803
@classmethod
804804
def find_module(cls, fullname, path=None):
@@ -842,11 +842,11 @@ class WindowsRegistryFinder:
842842
"""
843843

844844
REGISTRY_KEY = (
845-
"Software\\Python\\PythonCore\\{sys_version}"
846-
"\\Modules\\{fullname}")
845+
'Software\\Python\\PythonCore\\{sys_version}'
846+
'\\Modules\\{fullname}')
847847
REGISTRY_KEY_DEBUG = (
848-
"Software\\Python\\PythonCore\\{sys_version}"
849-
"\\Modules\\{fullname}\\Debug")
848+
'Software\\Python\\PythonCore\\{sys_version}'
849+
'\\Modules\\{fullname}\\Debug')
850850
DEBUG_BUILD = False # Changed in _setup()
851851

852852
@classmethod
@@ -866,7 +866,7 @@ def _search_registry(cls, fullname):
866866
sys_version=sys.version[:3])
867867
try:
868868
with cls._open_registry(key) as hkey:
869-
filepath = _winreg.QueryValue(hkey, "")
869+
filepath = _winreg.QueryValue(hkey, '')
870870
except OSError:
871871
return None
872872
return filepath
@@ -973,7 +973,7 @@ def get_source(self, fullname):
973973
try:
974974
source_bytes = self.get_data(path)
975975
except OSError as exc:
976-
raise ImportError("source not available through get_data()",
976+
raise ImportError('source not available through get_data()',
977977
name=fullname) from exc
978978
return decode_source(source_bytes)
979979

@@ -1218,7 +1218,7 @@ def __len__(self):
12181218
return len(self._recalculate())
12191219

12201220
def __repr__(self):
1221-
return "_NamespacePath({!r})".format(self._path)
1221+
return '_NamespacePath({!r})'.format(self._path)
12221222

12231223
def __contains__(self, item):
12241224
return item in self._recalculate()
@@ -1233,7 +1233,7 @@ def __init__(self, name, path, path_finder):
12331233

12341234
@classmethod
12351235
def module_repr(cls, module):
1236-
return "<module '{}' (namespace)>".format(module.__name__)
1236+
return '<module {!r} (namespace)>'.format(module.__name__)
12371237

12381238
def is_package(self, fullname):
12391239
return True
@@ -1467,13 +1467,13 @@ def path_hook(cls, *loader_details):
14671467
def path_hook_for_FileFinder(path):
14681468
"""Path hook for importlib.machinery.FileFinder."""
14691469
if not _path_isdir(path):
1470-
raise ImportError("only directories are supported", path=path)
1470+
raise ImportError('only directories are supported', path=path)
14711471
return cls(path, *loader_details)
14721472

14731473
return path_hook_for_FileFinder
14741474

14751475
def __repr__(self):
1476-
return "FileFinder({!r})".format(self.path)
1476+
return 'FileFinder({!r})'.format(self.path)
14771477

14781478

14791479
# Import itself ###############################################################
@@ -1520,18 +1520,18 @@ def _find_module(name, path):
15201520
def _sanity_check(name, package, level):
15211521
"""Verify arguments are "sane"."""
15221522
if not isinstance(name, str):
1523-
raise TypeError("module name must be str, not {}".format(type(name)))
1523+
raise TypeError('module name must be str, not {}'.format(type(name)))
15241524
if level < 0:
15251525
raise ValueError('level must be >= 0')
15261526
if package:
15271527
if not isinstance(package, str):
1528-
raise TypeError("__package__ not set to a string")
1528+
raise TypeError('__package__ not set to a string')
15291529
elif package not in sys.modules:
1530-
msg = ("Parent module {!r} not loaded, cannot perform relative "
1531-
"import")
1530+
msg = ('Parent module {!r} not loaded, cannot perform relative '
1531+
'import')
15321532
raise SystemError(msg.format(package))
15331533
if not name and level == 0:
1534-
raise ValueError("Empty module name")
1534+
raise ValueError('Empty module name')
15351535

15361536

15371537
_ERR_MSG_PREFIX = 'No module named '
@@ -1614,8 +1614,8 @@ def _gcd_import(name, package=None, level=0):
16141614
module = sys.modules[name]
16151615
if module is None:
16161616
_imp.release_lock()
1617-
message = ("import of {} halted; "
1618-
"None in sys.modules".format(name))
1617+
message = ('import of {} halted; '
1618+
'None in sys.modules'.format(name))
16191619
raise ImportError(message, name=name)
16201620
_lock_unlock_module(name)
16211621
return module

Python/importlib.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,8 +1434,8 @@ const unsigned char _Py_M__importlib[] = {
14341434
99,2,0,0,0,0,0,0,0,2,0,0,0,2,0,0,
14351435
0,67,0,0,0,115,16,0,0,0,100,1,0,106,0,0,
14361436
124,1,0,106,1,0,131,1,0,83,40,2,0,0,0,78,
1437-
117,24,0,0,0,60,109,111,100,117,108,101,32,39,123,125,
1438-
39,32,40,98,117,105,108,116,45,105,110,41,62,40,2,0,
1437+
117,24,0,0,0,60,109,111,100,117,108,101,32,123,33,114,
1438+
125,32,40,98,117,105,108,116,45,105,110,41,62,40,2,0,
14391439
0,0,114,46,0,0,0,114,56,0,0,0,40,2,0,0,
14401440
0,244,3,0,0,0,99,108,115,114,161,0,0,0,114,4,
14411441
0,0,0,114,4,0,0,0,114,5,0,0,0,244,11,0,
@@ -1546,8 +1546,8 @@ const unsigned char _Py_M__importlib[] = {
15461546
32,99,2,0,0,0,0,0,0,0,2,0,0,0,2,0,
15471547
0,0,67,0,0,0,115,16,0,0,0,100,1,0,106,0,
15481548
0,124,1,0,106,1,0,131,1,0,83,40,2,0,0,0,
1549-
78,117,22,0,0,0,60,109,111,100,117,108,101,32,39,123,
1550-
125,39,32,40,102,114,111,122,101,110,41,62,40,2,0,0,
1549+
78,117,22,0,0,0,60,109,111,100,117,108,101,32,123,33,
1550+
114,125,32,40,102,114,111,122,101,110,41,62,40,2,0,0,
15511551
0,114,46,0,0,0,114,56,0,0,0,40,2,0,0,0,
15521552
114,215,0,0,0,244,1,0,0,0,109,114,4,0,0,0,
15531553
114,4,0,0,0,114,5,0,0,0,114,216,0,0,0,31,
@@ -2565,8 +2565,8 @@ const unsigned char _Py_M__importlib[] = {
25652565
95,95,99,2,0,0,0,0,0,0,0,2,0,0,0,2,
25662566
0,0,0,67,0,0,0,115,16,0,0,0,100,1,0,106,
25672567
0,0,124,1,0,106,1,0,131,1,0,83,40,2,0,0,
2568-
0,78,117,25,0,0,0,60,109,111,100,117,108,101,32,39,
2569-
123,125,39,32,40,110,97,109,101,115,112,97,99,101,41,62,
2568+
0,78,117,25,0,0,0,60,109,111,100,117,108,101,32,123,
2569+
33,114,125,32,40,110,97,109,101,115,112,97,99,101,41,62,
25702570
40,2,0,0,0,114,46,0,0,0,114,56,0,0,0,40,
25712571
2,0,0,0,114,215,0,0,0,114,161,0,0,0,114,4,
25722572
0,0,0,114,4,0,0,0,114,5,0,0,0,114,216,0,

0 commit comments

Comments
 (0)