PageRenderTime 47ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Lib/test/test_importlib/test_util.py

https://gitlab.com/unofficial-mirrors/cpython
Python | 761 lines | 687 code | 48 blank | 26 comment | 22 complexity | 35f3f5ee53ec05c600e64bb4d2623184 MD5 | raw file
  1. from . import util
  2. abc = util.import_importlib('importlib.abc')
  3. init = util.import_importlib('importlib')
  4. machinery = util.import_importlib('importlib.machinery')
  5. importlib_util = util.import_importlib('importlib.util')
  6. import os
  7. import pathlib
  8. import string
  9. import sys
  10. from test import support
  11. import types
  12. import unittest
  13. import warnings
  14. class DecodeSourceBytesTests:
  15. source = "string ='ΓΌ'"
  16. def test_ut8_default(self):
  17. source_bytes = self.source.encode('utf-8')
  18. self.assertEqual(self.util.decode_source(source_bytes), self.source)
  19. def test_specified_encoding(self):
  20. source = '# coding=latin-1\n' + self.source
  21. source_bytes = source.encode('latin-1')
  22. assert source_bytes != source.encode('utf-8')
  23. self.assertEqual(self.util.decode_source(source_bytes), source)
  24. def test_universal_newlines(self):
  25. source = '\r\n'.join([self.source, self.source])
  26. source_bytes = source.encode('utf-8')
  27. self.assertEqual(self.util.decode_source(source_bytes),
  28. '\n'.join([self.source, self.source]))
  29. (Frozen_DecodeSourceBytesTests,
  30. Source_DecodeSourceBytesTests
  31. ) = util.test_both(DecodeSourceBytesTests, util=importlib_util)
  32. class ModuleFromSpecTests:
  33. def test_no_create_module(self):
  34. class Loader:
  35. def exec_module(self, module):
  36. pass
  37. spec = self.machinery.ModuleSpec('test', Loader())
  38. with self.assertRaises(ImportError):
  39. module = self.util.module_from_spec(spec)
  40. def test_create_module_returns_None(self):
  41. class Loader(self.abc.Loader):
  42. def create_module(self, spec):
  43. return None
  44. spec = self.machinery.ModuleSpec('test', Loader())
  45. module = self.util.module_from_spec(spec)
  46. self.assertIsInstance(module, types.ModuleType)
  47. self.assertEqual(module.__name__, spec.name)
  48. def test_create_module(self):
  49. name = 'already set'
  50. class CustomModule(types.ModuleType):
  51. pass
  52. class Loader(self.abc.Loader):
  53. def create_module(self, spec):
  54. module = CustomModule(spec.name)
  55. module.__name__ = name
  56. return module
  57. spec = self.machinery.ModuleSpec('test', Loader())
  58. module = self.util.module_from_spec(spec)
  59. self.assertIsInstance(module, CustomModule)
  60. self.assertEqual(module.__name__, name)
  61. def test___name__(self):
  62. spec = self.machinery.ModuleSpec('test', object())
  63. module = self.util.module_from_spec(spec)
  64. self.assertEqual(module.__name__, spec.name)
  65. def test___spec__(self):
  66. spec = self.machinery.ModuleSpec('test', object())
  67. module = self.util.module_from_spec(spec)
  68. self.assertEqual(module.__spec__, spec)
  69. def test___loader__(self):
  70. loader = object()
  71. spec = self.machinery.ModuleSpec('test', loader)
  72. module = self.util.module_from_spec(spec)
  73. self.assertIs(module.__loader__, loader)
  74. def test___package__(self):
  75. spec = self.machinery.ModuleSpec('test.pkg', object())
  76. module = self.util.module_from_spec(spec)
  77. self.assertEqual(module.__package__, spec.parent)
  78. def test___path__(self):
  79. spec = self.machinery.ModuleSpec('test', object(), is_package=True)
  80. module = self.util.module_from_spec(spec)
  81. self.assertEqual(module.__path__, spec.submodule_search_locations)
  82. def test___file__(self):
  83. spec = self.machinery.ModuleSpec('test', object(), origin='some/path')
  84. spec.has_location = True
  85. module = self.util.module_from_spec(spec)
  86. self.assertEqual(module.__file__, spec.origin)
  87. def test___cached__(self):
  88. spec = self.machinery.ModuleSpec('test', object())
  89. spec.cached = 'some/path'
  90. spec.has_location = True
  91. module = self.util.module_from_spec(spec)
  92. self.assertEqual(module.__cached__, spec.cached)
  93. (Frozen_ModuleFromSpecTests,
  94. Source_ModuleFromSpecTests
  95. ) = util.test_both(ModuleFromSpecTests, abc=abc, machinery=machinery,
  96. util=importlib_util)
  97. class ModuleForLoaderTests:
  98. """Tests for importlib.util.module_for_loader."""
  99. @classmethod
  100. def module_for_loader(cls, func):
  101. with warnings.catch_warnings():
  102. warnings.simplefilter('ignore', DeprecationWarning)
  103. return cls.util.module_for_loader(func)
  104. def test_warning(self):
  105. # Should raise a PendingDeprecationWarning when used.
  106. with warnings.catch_warnings():
  107. warnings.simplefilter('error', DeprecationWarning)
  108. with self.assertRaises(DeprecationWarning):
  109. func = self.util.module_for_loader(lambda x: x)
  110. def return_module(self, name):
  111. fxn = self.module_for_loader(lambda self, module: module)
  112. return fxn(self, name)
  113. def raise_exception(self, name):
  114. def to_wrap(self, module):
  115. raise ImportError
  116. fxn = self.module_for_loader(to_wrap)
  117. try:
  118. fxn(self, name)
  119. except ImportError:
  120. pass
  121. def test_new_module(self):
  122. # Test that when no module exists in sys.modules a new module is
  123. # created.
  124. module_name = 'a.b.c'
  125. with util.uncache(module_name):
  126. module = self.return_module(module_name)
  127. self.assertIn(module_name, sys.modules)
  128. self.assertIsInstance(module, types.ModuleType)
  129. self.assertEqual(module.__name__, module_name)
  130. def test_reload(self):
  131. # Test that a module is reused if already in sys.modules.
  132. class FakeLoader:
  133. def is_package(self, name):
  134. return True
  135. @self.module_for_loader
  136. def load_module(self, module):
  137. return module
  138. name = 'a.b.c'
  139. module = types.ModuleType('a.b.c')
  140. module.__loader__ = 42
  141. module.__package__ = 42
  142. with util.uncache(name):
  143. sys.modules[name] = module
  144. loader = FakeLoader()
  145. returned_module = loader.load_module(name)
  146. self.assertIs(returned_module, sys.modules[name])
  147. self.assertEqual(module.__loader__, loader)
  148. self.assertEqual(module.__package__, name)
  149. def test_new_module_failure(self):
  150. # Test that a module is removed from sys.modules if added but an
  151. # exception is raised.
  152. name = 'a.b.c'
  153. with util.uncache(name):
  154. self.raise_exception(name)
  155. self.assertNotIn(name, sys.modules)
  156. def test_reload_failure(self):
  157. # Test that a failure on reload leaves the module in-place.
  158. name = 'a.b.c'
  159. module = types.ModuleType(name)
  160. with util.uncache(name):
  161. sys.modules[name] = module
  162. self.raise_exception(name)
  163. self.assertIs(module, sys.modules[name])
  164. def test_decorator_attrs(self):
  165. def fxn(self, module): pass
  166. wrapped = self.module_for_loader(fxn)
  167. self.assertEqual(wrapped.__name__, fxn.__name__)
  168. self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
  169. def test_false_module(self):
  170. # If for some odd reason a module is considered false, still return it
  171. # from sys.modules.
  172. class FalseModule(types.ModuleType):
  173. def __bool__(self): return False
  174. name = 'mod'
  175. module = FalseModule(name)
  176. with util.uncache(name):
  177. self.assertFalse(module)
  178. sys.modules[name] = module
  179. given = self.return_module(name)
  180. self.assertIs(given, module)
  181. def test_attributes_set(self):
  182. # __name__, __loader__, and __package__ should be set (when
  183. # is_package() is defined; undefined implicitly tested elsewhere).
  184. class FakeLoader:
  185. def __init__(self, is_package):
  186. self._pkg = is_package
  187. def is_package(self, name):
  188. return self._pkg
  189. @self.module_for_loader
  190. def load_module(self, module):
  191. return module
  192. name = 'pkg.mod'
  193. with util.uncache(name):
  194. loader = FakeLoader(False)
  195. module = loader.load_module(name)
  196. self.assertEqual(module.__name__, name)
  197. self.assertIs(module.__loader__, loader)
  198. self.assertEqual(module.__package__, 'pkg')
  199. name = 'pkg.sub'
  200. with util.uncache(name):
  201. loader = FakeLoader(True)
  202. module = loader.load_module(name)
  203. self.assertEqual(module.__name__, name)
  204. self.assertIs(module.__loader__, loader)
  205. self.assertEqual(module.__package__, name)
  206. (Frozen_ModuleForLoaderTests,
  207. Source_ModuleForLoaderTests
  208. ) = util.test_both(ModuleForLoaderTests, util=importlib_util)
  209. class SetPackageTests:
  210. """Tests for importlib.util.set_package."""
  211. def verify(self, module, expect):
  212. """Verify the module has the expected value for __package__ after
  213. passing through set_package."""
  214. fxn = lambda: module
  215. wrapped = self.util.set_package(fxn)
  216. with warnings.catch_warnings():
  217. warnings.simplefilter('ignore', DeprecationWarning)
  218. wrapped()
  219. self.assertTrue(hasattr(module, '__package__'))
  220. self.assertEqual(expect, module.__package__)
  221. def test_top_level(self):
  222. # __package__ should be set to the empty string if a top-level module.
  223. # Implicitly tests when package is set to None.
  224. module = types.ModuleType('module')
  225. module.__package__ = None
  226. self.verify(module, '')
  227. def test_package(self):
  228. # Test setting __package__ for a package.
  229. module = types.ModuleType('pkg')
  230. module.__path__ = ['<path>']
  231. module.__package__ = None
  232. self.verify(module, 'pkg')
  233. def test_submodule(self):
  234. # Test __package__ for a module in a package.
  235. module = types.ModuleType('pkg.mod')
  236. module.__package__ = None
  237. self.verify(module, 'pkg')
  238. def test_setting_if_missing(self):
  239. # __package__ should be set if it is missing.
  240. module = types.ModuleType('mod')
  241. if hasattr(module, '__package__'):
  242. delattr(module, '__package__')
  243. self.verify(module, '')
  244. def test_leaving_alone(self):
  245. # If __package__ is set and not None then leave it alone.
  246. for value in (True, False):
  247. module = types.ModuleType('mod')
  248. module.__package__ = value
  249. self.verify(module, value)
  250. def test_decorator_attrs(self):
  251. def fxn(module): pass
  252. with warnings.catch_warnings():
  253. warnings.simplefilter('ignore', DeprecationWarning)
  254. wrapped = self.util.set_package(fxn)
  255. self.assertEqual(wrapped.__name__, fxn.__name__)
  256. self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
  257. (Frozen_SetPackageTests,
  258. Source_SetPackageTests
  259. ) = util.test_both(SetPackageTests, util=importlib_util)
  260. class SetLoaderTests:
  261. """Tests importlib.util.set_loader()."""
  262. @property
  263. def DummyLoader(self):
  264. # Set DummyLoader on the class lazily.
  265. class DummyLoader:
  266. @self.util.set_loader
  267. def load_module(self, module):
  268. return self.module
  269. self.__class__.DummyLoader = DummyLoader
  270. return DummyLoader
  271. def test_no_attribute(self):
  272. loader = self.DummyLoader()
  273. loader.module = types.ModuleType('blah')
  274. try:
  275. del loader.module.__loader__
  276. except AttributeError:
  277. pass
  278. with warnings.catch_warnings():
  279. warnings.simplefilter('ignore', DeprecationWarning)
  280. self.assertEqual(loader, loader.load_module('blah').__loader__)
  281. def test_attribute_is_None(self):
  282. loader = self.DummyLoader()
  283. loader.module = types.ModuleType('blah')
  284. loader.module.__loader__ = None
  285. with warnings.catch_warnings():
  286. warnings.simplefilter('ignore', DeprecationWarning)
  287. self.assertEqual(loader, loader.load_module('blah').__loader__)
  288. def test_not_reset(self):
  289. loader = self.DummyLoader()
  290. loader.module = types.ModuleType('blah')
  291. loader.module.__loader__ = 42
  292. with warnings.catch_warnings():
  293. warnings.simplefilter('ignore', DeprecationWarning)
  294. self.assertEqual(42, loader.load_module('blah').__loader__)
  295. (Frozen_SetLoaderTests,
  296. Source_SetLoaderTests
  297. ) = util.test_both(SetLoaderTests, util=importlib_util)
  298. class ResolveNameTests:
  299. """Tests importlib.util.resolve_name()."""
  300. def test_absolute(self):
  301. # bacon
  302. self.assertEqual('bacon', self.util.resolve_name('bacon', None))
  303. def test_absolute_within_package(self):
  304. # bacon in spam
  305. self.assertEqual('bacon', self.util.resolve_name('bacon', 'spam'))
  306. def test_no_package(self):
  307. # .bacon in ''
  308. with self.assertRaises(ValueError):
  309. self.util.resolve_name('.bacon', '')
  310. def test_in_package(self):
  311. # .bacon in spam
  312. self.assertEqual('spam.eggs.bacon',
  313. self.util.resolve_name('.bacon', 'spam.eggs'))
  314. def test_other_package(self):
  315. # ..bacon in spam.bacon
  316. self.assertEqual('spam.bacon',
  317. self.util.resolve_name('..bacon', 'spam.eggs'))
  318. def test_escape(self):
  319. # ..bacon in spam
  320. with self.assertRaises(ValueError):
  321. self.util.resolve_name('..bacon', 'spam')
  322. (Frozen_ResolveNameTests,
  323. Source_ResolveNameTests
  324. ) = util.test_both(ResolveNameTests, util=importlib_util)
  325. class FindSpecTests:
  326. class FakeMetaFinder:
  327. @staticmethod
  328. def find_spec(name, path=None, target=None): return name, path, target
  329. def test_sys_modules(self):
  330. name = 'some_mod'
  331. with util.uncache(name):
  332. module = types.ModuleType(name)
  333. loader = 'a loader!'
  334. spec = self.machinery.ModuleSpec(name, loader)
  335. module.__loader__ = loader
  336. module.__spec__ = spec
  337. sys.modules[name] = module
  338. found = self.util.find_spec(name)
  339. self.assertEqual(found, spec)
  340. def test_sys_modules_without___loader__(self):
  341. name = 'some_mod'
  342. with util.uncache(name):
  343. module = types.ModuleType(name)
  344. del module.__loader__
  345. loader = 'a loader!'
  346. spec = self.machinery.ModuleSpec(name, loader)
  347. module.__spec__ = spec
  348. sys.modules[name] = module
  349. found = self.util.find_spec(name)
  350. self.assertEqual(found, spec)
  351. def test_sys_modules_spec_is_None(self):
  352. name = 'some_mod'
  353. with util.uncache(name):
  354. module = types.ModuleType(name)
  355. module.__spec__ = None
  356. sys.modules[name] = module
  357. with self.assertRaises(ValueError):
  358. self.util.find_spec(name)
  359. def test_sys_modules_loader_is_None(self):
  360. name = 'some_mod'
  361. with util.uncache(name):
  362. module = types.ModuleType(name)
  363. spec = self.machinery.ModuleSpec(name, None)
  364. module.__spec__ = spec
  365. sys.modules[name] = module
  366. found = self.util.find_spec(name)
  367. self.assertEqual(found, spec)
  368. def test_sys_modules_spec_is_not_set(self):
  369. name = 'some_mod'
  370. with util.uncache(name):
  371. module = types.ModuleType(name)
  372. try:
  373. del module.__spec__
  374. except AttributeError:
  375. pass
  376. sys.modules[name] = module
  377. with self.assertRaises(ValueError):
  378. self.util.find_spec(name)
  379. def test_success(self):
  380. name = 'some_mod'
  381. with util.uncache(name):
  382. with util.import_state(meta_path=[self.FakeMetaFinder]):
  383. self.assertEqual((name, None, None),
  384. self.util.find_spec(name))
  385. def test_nothing(self):
  386. # None is returned upon failure to find a loader.
  387. self.assertIsNone(self.util.find_spec('nevergoingtofindthismodule'))
  388. def test_find_submodule(self):
  389. name = 'spam'
  390. subname = 'ham'
  391. with util.temp_module(name, pkg=True) as pkg_dir:
  392. fullname, _ = util.submodule(name, subname, pkg_dir)
  393. spec = self.util.find_spec(fullname)
  394. self.assertIsNot(spec, None)
  395. self.assertIn(name, sorted(sys.modules))
  396. self.assertNotIn(fullname, sorted(sys.modules))
  397. # Ensure successive calls behave the same.
  398. spec_again = self.util.find_spec(fullname)
  399. self.assertEqual(spec_again, spec)
  400. def test_find_submodule_parent_already_imported(self):
  401. name = 'spam'
  402. subname = 'ham'
  403. with util.temp_module(name, pkg=True) as pkg_dir:
  404. self.init.import_module(name)
  405. fullname, _ = util.submodule(name, subname, pkg_dir)
  406. spec = self.util.find_spec(fullname)
  407. self.assertIsNot(spec, None)
  408. self.assertIn(name, sorted(sys.modules))
  409. self.assertNotIn(fullname, sorted(sys.modules))
  410. # Ensure successive calls behave the same.
  411. spec_again = self.util.find_spec(fullname)
  412. self.assertEqual(spec_again, spec)
  413. def test_find_relative_module(self):
  414. name = 'spam'
  415. subname = 'ham'
  416. with util.temp_module(name, pkg=True) as pkg_dir:
  417. fullname, _ = util.submodule(name, subname, pkg_dir)
  418. relname = '.' + subname
  419. spec = self.util.find_spec(relname, name)
  420. self.assertIsNot(spec, None)
  421. self.assertIn(name, sorted(sys.modules))
  422. self.assertNotIn(fullname, sorted(sys.modules))
  423. # Ensure successive calls behave the same.
  424. spec_again = self.util.find_spec(fullname)
  425. self.assertEqual(spec_again, spec)
  426. def test_find_relative_module_missing_package(self):
  427. name = 'spam'
  428. subname = 'ham'
  429. with util.temp_module(name, pkg=True) as pkg_dir:
  430. fullname, _ = util.submodule(name, subname, pkg_dir)
  431. relname = '.' + subname
  432. with self.assertRaises(ValueError):
  433. self.util.find_spec(relname)
  434. self.assertNotIn(name, sorted(sys.modules))
  435. self.assertNotIn(fullname, sorted(sys.modules))
  436. (Frozen_FindSpecTests,
  437. Source_FindSpecTests
  438. ) = util.test_both(FindSpecTests, init=init, util=importlib_util,
  439. machinery=machinery)
  440. class MagicNumberTests:
  441. def test_length(self):
  442. # Should be 4 bytes.
  443. self.assertEqual(len(self.util.MAGIC_NUMBER), 4)
  444. def test_incorporates_rn(self):
  445. # The magic number uses \r\n to come out wrong when splitting on lines.
  446. self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n'))
  447. (Frozen_MagicNumberTests,
  448. Source_MagicNumberTests
  449. ) = util.test_both(MagicNumberTests, util=importlib_util)
  450. class PEP3147Tests:
  451. """Tests of PEP 3147-related functions: cache_from_source and source_from_cache."""
  452. tag = sys.implementation.cache_tag
  453. @unittest.skipUnless(sys.implementation.cache_tag is not None,
  454. 'requires sys.implementation.cache_tag not be None')
  455. def test_cache_from_source(self):
  456. # Given the path to a .py file, return the path to its PEP 3147
  457. # defined .pyc file (i.e. under __pycache__).
  458. path = os.path.join('foo', 'bar', 'baz', 'qux.py')
  459. expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
  460. 'qux.{}.pyc'.format(self.tag))
  461. self.assertEqual(self.util.cache_from_source(path, optimization=''),
  462. expect)
  463. def test_cache_from_source_no_cache_tag(self):
  464. # No cache tag means NotImplementedError.
  465. with support.swap_attr(sys.implementation, 'cache_tag', None):
  466. with self.assertRaises(NotImplementedError):
  467. self.util.cache_from_source('whatever.py')
  468. def test_cache_from_source_no_dot(self):
  469. # Directory with a dot, filename without dot.
  470. path = os.path.join('foo.bar', 'file')
  471. expect = os.path.join('foo.bar', '__pycache__',
  472. 'file{}.pyc'.format(self.tag))
  473. self.assertEqual(self.util.cache_from_source(path, optimization=''),
  474. expect)
  475. def test_cache_from_source_debug_override(self):
  476. # Given the path to a .py file, return the path to its PEP 3147/PEP 488
  477. # defined .pyc file (i.e. under __pycache__).
  478. path = os.path.join('foo', 'bar', 'baz', 'qux.py')
  479. with warnings.catch_warnings():
  480. warnings.simplefilter('ignore')
  481. self.assertEqual(self.util.cache_from_source(path, False),
  482. self.util.cache_from_source(path, optimization=1))
  483. self.assertEqual(self.util.cache_from_source(path, True),
  484. self.util.cache_from_source(path, optimization=''))
  485. with warnings.catch_warnings():
  486. warnings.simplefilter('error')
  487. with self.assertRaises(DeprecationWarning):
  488. self.util.cache_from_source(path, False)
  489. with self.assertRaises(DeprecationWarning):
  490. self.util.cache_from_source(path, True)
  491. def test_cache_from_source_cwd(self):
  492. path = 'foo.py'
  493. expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
  494. self.assertEqual(self.util.cache_from_source(path, optimization=''),
  495. expect)
  496. def test_cache_from_source_override(self):
  497. # When debug_override is not None, it can be any true-ish or false-ish
  498. # value.
  499. path = os.path.join('foo', 'bar', 'baz.py')
  500. # However if the bool-ishness can't be determined, the exception
  501. # propagates.
  502. class Bearish:
  503. def __bool__(self): raise RuntimeError
  504. with warnings.catch_warnings():
  505. warnings.simplefilter('ignore')
  506. self.assertEqual(self.util.cache_from_source(path, []),
  507. self.util.cache_from_source(path, optimization=1))
  508. self.assertEqual(self.util.cache_from_source(path, [17]),
  509. self.util.cache_from_source(path, optimization=''))
  510. with self.assertRaises(RuntimeError):
  511. self.util.cache_from_source('/foo/bar/baz.py', Bearish())
  512. def test_cache_from_source_optimization_empty_string(self):
  513. # Setting 'optimization' to '' leads to no optimization tag (PEP 488).
  514. path = 'foo.py'
  515. expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
  516. self.assertEqual(self.util.cache_from_source(path, optimization=''),
  517. expect)
  518. def test_cache_from_source_optimization_None(self):
  519. # Setting 'optimization' to None uses the interpreter's optimization.
  520. # (PEP 488)
  521. path = 'foo.py'
  522. optimization_level = sys.flags.optimize
  523. almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag))
  524. if optimization_level == 0:
  525. expect = almost_expect + '.pyc'
  526. elif optimization_level <= 2:
  527. expect = almost_expect + '.opt-{}.pyc'.format(optimization_level)
  528. else:
  529. msg = '{!r} is a non-standard optimization level'.format(optimization_level)
  530. self.skipTest(msg)
  531. self.assertEqual(self.util.cache_from_source(path, optimization=None),
  532. expect)
  533. def test_cache_from_source_optimization_set(self):
  534. # The 'optimization' parameter accepts anything that has a string repr
  535. # that passes str.alnum().
  536. path = 'foo.py'
  537. valid_characters = string.ascii_letters + string.digits
  538. almost_expect = os.path.join('__pycache__', 'foo.{}'.format(self.tag))
  539. got = self.util.cache_from_source(path, optimization=valid_characters)
  540. # Test all valid characters are accepted.
  541. self.assertEqual(got,
  542. almost_expect + '.opt-{}.pyc'.format(valid_characters))
  543. # str() should be called on argument.
  544. self.assertEqual(self.util.cache_from_source(path, optimization=42),
  545. almost_expect + '.opt-42.pyc')
  546. # Invalid characters raise ValueError.
  547. with self.assertRaises(ValueError):
  548. self.util.cache_from_source(path, optimization='path/is/bad')
  549. def test_cache_from_source_debug_override_optimization_both_set(self):
  550. # Can only set one of the optimization-related parameters.
  551. with warnings.catch_warnings():
  552. warnings.simplefilter('ignore')
  553. with self.assertRaises(TypeError):
  554. self.util.cache_from_source('foo.py', False, optimization='')
  555. @unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
  556. 'test meaningful only where os.altsep is defined')
  557. def test_sep_altsep_and_sep_cache_from_source(self):
  558. # Windows path and PEP 3147 where sep is right of altsep.
  559. self.assertEqual(
  560. self.util.cache_from_source('\\foo\\bar\\baz/qux.py', optimization=''),
  561. '\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
  562. @unittest.skipUnless(sys.implementation.cache_tag is not None,
  563. 'requires sys.implementation.cache_tag not be None')
  564. def test_source_from_cache_path_like_arg(self):
  565. path = pathlib.PurePath('foo', 'bar', 'baz', 'qux.py')
  566. expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
  567. 'qux.{}.pyc'.format(self.tag))
  568. self.assertEqual(self.util.cache_from_source(path, optimization=''),
  569. expect)
  570. @unittest.skipUnless(sys.implementation.cache_tag is not None,
  571. 'requires sys.implementation.cache_tag to not be '
  572. 'None')
  573. def test_source_from_cache(self):
  574. # Given the path to a PEP 3147 defined .pyc file, return the path to
  575. # its source. This tests the good path.
  576. path = os.path.join('foo', 'bar', 'baz', '__pycache__',
  577. 'qux.{}.pyc'.format(self.tag))
  578. expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
  579. self.assertEqual(self.util.source_from_cache(path), expect)
  580. def test_source_from_cache_no_cache_tag(self):
  581. # If sys.implementation.cache_tag is None, raise NotImplementedError.
  582. path = os.path.join('blah', '__pycache__', 'whatever.pyc')
  583. with support.swap_attr(sys.implementation, 'cache_tag', None):
  584. with self.assertRaises(NotImplementedError):
  585. self.util.source_from_cache(path)
  586. def test_source_from_cache_bad_path(self):
  587. # When the path to a pyc file is not in PEP 3147 format, a ValueError
  588. # is raised.
  589. self.assertRaises(
  590. ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc')
  591. def test_source_from_cache_no_slash(self):
  592. # No slashes at all in path -> ValueError
  593. self.assertRaises(
  594. ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc')
  595. def test_source_from_cache_too_few_dots(self):
  596. # Too few dots in final path component -> ValueError
  597. self.assertRaises(
  598. ValueError, self.util.source_from_cache, '__pycache__/foo.pyc')
  599. def test_source_from_cache_too_many_dots(self):
  600. with self.assertRaises(ValueError):
  601. self.util.source_from_cache(
  602. '__pycache__/foo.cpython-32.opt-1.foo.pyc')
  603. def test_source_from_cache_not_opt(self):
  604. # Non-`opt-` path component -> ValueError
  605. self.assertRaises(
  606. ValueError, self.util.source_from_cache,
  607. '__pycache__/foo.cpython-32.foo.pyc')
  608. def test_source_from_cache_no__pycache__(self):
  609. # Another problem with the path -> ValueError
  610. self.assertRaises(
  611. ValueError, self.util.source_from_cache,
  612. '/foo/bar/foo.cpython-32.foo.pyc')
  613. def test_source_from_cache_optimized_bytecode(self):
  614. # Optimized bytecode is not an issue.
  615. path = os.path.join('__pycache__', 'foo.{}.opt-1.pyc'.format(self.tag))
  616. self.assertEqual(self.util.source_from_cache(path), 'foo.py')
  617. def test_source_from_cache_missing_optimization(self):
  618. # An empty optimization level is a no-no.
  619. path = os.path.join('__pycache__', 'foo.{}.opt-.pyc'.format(self.tag))
  620. with self.assertRaises(ValueError):
  621. self.util.source_from_cache(path)
  622. @unittest.skipUnless(sys.implementation.cache_tag is not None,
  623. 'requires sys.implementation.cache_tag to not be '
  624. 'None')
  625. def test_source_from_cache_path_like_arg(self):
  626. path = pathlib.PurePath('foo', 'bar', 'baz', '__pycache__',
  627. 'qux.{}.pyc'.format(self.tag))
  628. expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
  629. self.assertEqual(self.util.source_from_cache(path), expect)
  630. (Frozen_PEP3147Tests,
  631. Source_PEP3147Tests
  632. ) = util.test_both(PEP3147Tests, util=importlib_util)
  633. if __name__ == '__main__':
  634. unittest.main()