0012-Add-importlib-fix-for-PEP-3147-issue.patch 4.5 KB

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