1*4882a593SmuzhiyunFrom e2ea659eac1849db471d3c01a0d0af9d6fca2e9a Mon Sep 17 00:00:00 2001 2*4882a593SmuzhiyunFrom: Christophe Vu-Brugier <cvubrugier@fastmail.fm> 3*4882a593SmuzhiyunDate: Wed, 22 Feb 2017 16:48:49 -0800 4*4882a593SmuzhiyunSubject: [PATCH] Add importlib fix for PEP 3147 issue 5*4882a593Smuzhiyun 6*4882a593SmuzhiyunPython 3 has a new standard for installing .pyc file, called PEP 7*4882a593Smuzhiyun3147. Unfortunately, this standard requires both the .py and .pyc 8*4882a593Smuzhiyunfiles to be installed for a Python module to be found. This is quite 9*4882a593Smuzhiyunannoying on space-constrained embedded systems, since the .py file is 10*4882a593Smuzhiyuntechnically not required for execution. 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunThis patch changes cache_from_source() and source_from_cache() in 13*4882a593Smuzhiyunimportlib to get rid of the "__pycache__" directory. 14*4882a593SmuzhiyunThis effectively disables PEP 3147 for: 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun* The python standard library 17*4882a593Smuzhiyun* Packages built with distutils or setuptools 18*4882a593Smuzhiyun* Packages built with automake that use the `py-compile` helper 19*4882a593Smuzhiyun 20*4882a593SmuzhiyunSigned-off-by: Christophe Vu-Brugier <cvubrugier@fastmail.fm> 21*4882a593Smuzhiyun[ Andrey Smirnov: ported to Python 3.6 ] 22*4882a593SmuzhiyunSigned-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> 23*4882a593Smuzhiyun--- 24*4882a593Smuzhiyun Lib/importlib/_bootstrap_external.py | 44 ++++------------------------ 25*4882a593Smuzhiyun 1 file changed, 5 insertions(+), 39 deletions(-) 26*4882a593Smuzhiyun 27*4882a593Smuzhiyundiff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py 28*4882a593Smuzhiyunindex 25a3f8c0e0..2cb9a9aa52 100644 29*4882a593Smuzhiyun--- a/Lib/importlib/_bootstrap_external.py 30*4882a593Smuzhiyun+++ b/Lib/importlib/_bootstrap_external.py 31*4882a593Smuzhiyun@@ -392,8 +392,6 @@ def cache_from_source(path, debug_override=None, *, optimization=None): 32*4882a593Smuzhiyun a True value is the same as setting 'optimization' to the empty string 33*4882a593Smuzhiyun while a False value is equivalent to setting 'optimization' to '1'. 34*4882a593Smuzhiyun 35*4882a593Smuzhiyun- If sys.implementation.cache_tag is None then NotImplementedError is raised. 36*4882a593Smuzhiyun- 37*4882a593Smuzhiyun """ 38*4882a593Smuzhiyun if debug_override is not None: 39*4882a593Smuzhiyun _warnings.warn('the debug_override parameter is deprecated; use ' 40*4882a593Smuzhiyun@@ -405,10 +403,7 @@ def cache_from_source(path, debug_override=None, *, optimization=None): 41*4882a593Smuzhiyun path = _os.fspath(path) 42*4882a593Smuzhiyun head, tail = _path_split(path) 43*4882a593Smuzhiyun base, sep, rest = tail.rpartition('.') 44*4882a593Smuzhiyun- tag = sys.implementation.cache_tag 45*4882a593Smuzhiyun- if tag is None: 46*4882a593Smuzhiyun- raise NotImplementedError('sys.implementation.cache_tag is None') 47*4882a593Smuzhiyun- almost_filename = ''.join([(base if base else rest), sep, tag]) 48*4882a593Smuzhiyun+ almost_filename = ''.join([(base if base else rest)]) 49*4882a593Smuzhiyun if optimization is None: 50*4882a593Smuzhiyun if sys.flags.optimize == 0: 51*4882a593Smuzhiyun optimization = '' 52*4882a593Smuzhiyun@@ -445,46 +440,17 @@ def cache_from_source(path, debug_override=None, *, optimization=None): 53*4882a593Smuzhiyun head.lstrip(path_separators), 54*4882a593Smuzhiyun filename, 55*4882a593Smuzhiyun ) 56*4882a593Smuzhiyun- return _path_join(head, _PYCACHE, filename) 57*4882a593Smuzhiyun+ return _path_join(head, filename) 58*4882a593Smuzhiyun 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun def source_from_cache(path): 61*4882a593Smuzhiyun """Given the path to a .pyc. file, return the path to its .py file. 62*4882a593Smuzhiyun 63*4882a593Smuzhiyun The .pyc file does not need to exist; this simply returns the path to 64*4882a593Smuzhiyun- the .py file calculated to correspond to the .pyc file. If path does 65*4882a593Smuzhiyun- not conform to PEP 3147/488 format, ValueError will be raised. If 66*4882a593Smuzhiyun- sys.implementation.cache_tag is None then NotImplementedError is raised. 67*4882a593Smuzhiyun- 68*4882a593Smuzhiyun+ the .py file calculated to correspond to the .pyc file. 69*4882a593Smuzhiyun """ 70*4882a593Smuzhiyun- if sys.implementation.cache_tag is None: 71*4882a593Smuzhiyun- raise NotImplementedError('sys.implementation.cache_tag is None') 72*4882a593Smuzhiyun- path = _os.fspath(path) 73*4882a593Smuzhiyun- head, pycache_filename = _path_split(path) 74*4882a593Smuzhiyun- found_in_pycache_prefix = False 75*4882a593Smuzhiyun- if sys.pycache_prefix is not None: 76*4882a593Smuzhiyun- stripped_path = sys.pycache_prefix.rstrip(path_separators) 77*4882a593Smuzhiyun- if head.startswith(stripped_path + path_sep): 78*4882a593Smuzhiyun- head = head[len(stripped_path):] 79*4882a593Smuzhiyun- found_in_pycache_prefix = True 80*4882a593Smuzhiyun- if not found_in_pycache_prefix: 81*4882a593Smuzhiyun- head, pycache = _path_split(head) 82*4882a593Smuzhiyun- if pycache != _PYCACHE: 83*4882a593Smuzhiyun- raise ValueError(f'{_PYCACHE} not bottom-level directory in ' 84*4882a593Smuzhiyun- f'{path!r}') 85*4882a593Smuzhiyun- dot_count = pycache_filename.count('.') 86*4882a593Smuzhiyun- if dot_count not in {2, 3}: 87*4882a593Smuzhiyun- raise ValueError(f'expected only 2 or 3 dots in {pycache_filename!r}') 88*4882a593Smuzhiyun- elif dot_count == 3: 89*4882a593Smuzhiyun- optimization = pycache_filename.rsplit('.', 2)[-2] 90*4882a593Smuzhiyun- if not optimization.startswith(_OPT): 91*4882a593Smuzhiyun- raise ValueError("optimization portion of filename does not start " 92*4882a593Smuzhiyun- f"with {_OPT!r}") 93*4882a593Smuzhiyun- opt_level = optimization[len(_OPT):] 94*4882a593Smuzhiyun- if not opt_level.isalnum(): 95*4882a593Smuzhiyun- raise ValueError(f"optimization level {optimization!r} is not an " 96*4882a593Smuzhiyun- "alphanumeric value") 97*4882a593Smuzhiyun- base_filename = pycache_filename.partition('.')[0] 98*4882a593Smuzhiyun+ head, filename = _path_split(path) 99*4882a593Smuzhiyun+ base_filename = filename.partition('.')[0] 100*4882a593Smuzhiyun return _path_join(head, base_filename + SOURCE_SUFFIXES[0]) 101*4882a593Smuzhiyun 102*4882a593Smuzhiyun 103*4882a593Smuzhiyun-- 104*4882a593Smuzhiyun2.25.1 105*4882a593Smuzhiyun 106