2009-02-16 22:45:03 -04:00
|
|
|
from importlib import util
|
|
|
|
from . import util as test_util
|
2013-10-18 16:40:11 -03:00
|
|
|
frozen_util, source_util = test_util.import_importlib('importlib.util')
|
2013-06-15 19:39:21 -03:00
|
|
|
|
2013-06-14 23:26:30 -03:00
|
|
|
import os
|
2009-02-16 22:45:03 -04:00
|
|
|
import sys
|
2013-05-28 18:29:34 -03:00
|
|
|
from test import support
|
2009-02-16 22:45:03 -04:00
|
|
|
import types
|
|
|
|
import unittest
|
2013-05-31 19:56:47 -03:00
|
|
|
import warnings
|
2009-02-16 22:45:03 -04:00
|
|
|
|
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
class DecodeSourceBytesTests:
|
2013-06-16 19:37:53 -03:00
|
|
|
|
|
|
|
source = "string ='ü'"
|
|
|
|
|
|
|
|
def test_ut8_default(self):
|
|
|
|
source_bytes = self.source.encode('utf-8')
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.decode_source(source_bytes), self.source)
|
2013-06-16 19:37:53 -03:00
|
|
|
|
|
|
|
def test_specified_encoding(self):
|
|
|
|
source = '# coding=latin-1\n' + self.source
|
|
|
|
source_bytes = source.encode('latin-1')
|
|
|
|
assert source_bytes != source.encode('utf-8')
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.decode_source(source_bytes), source)
|
2013-06-16 19:37:53 -03:00
|
|
|
|
|
|
|
def test_universal_newlines(self):
|
|
|
|
source = '\r\n'.join([self.source, self.source])
|
|
|
|
source_bytes = source.encode('utf-8')
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.decode_source(source_bytes),
|
2013-06-16 19:37:53 -03:00
|
|
|
'\n'.join([self.source, self.source]))
|
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
Frozen_DecodeSourceBytesTests, Source_DecodeSourceBytesTests = test_util.test_both(
|
|
|
|
DecodeSourceBytesTests, util=[frozen_util, source_util])
|
2013-06-16 19:37:53 -03:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
|
|
|
|
class ModuleForLoaderTests:
|
2009-02-16 22:45:03 -04:00
|
|
|
|
|
|
|
"""Tests for importlib.util.module_for_loader."""
|
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
@classmethod
|
|
|
|
def module_for_loader(cls, func):
|
2013-05-31 19:56:47 -03:00
|
|
|
with warnings.catch_warnings():
|
2014-01-06 23:49:04 -04:00
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
2013-10-18 16:40:11 -03:00
|
|
|
return cls.util.module_for_loader(func)
|
2013-05-31 19:56:47 -03:00
|
|
|
|
|
|
|
def test_warning(self):
|
|
|
|
# Should raise a PendingDeprecationWarning when used.
|
|
|
|
with warnings.catch_warnings():
|
2014-01-06 23:49:04 -04:00
|
|
|
warnings.simplefilter('error', DeprecationWarning)
|
|
|
|
with self.assertRaises(DeprecationWarning):
|
2013-10-18 16:40:11 -03:00
|
|
|
func = self.util.module_for_loader(lambda x: x)
|
2013-05-31 19:56:47 -03:00
|
|
|
|
2009-02-16 22:45:03 -04:00
|
|
|
def return_module(self, name):
|
2013-05-31 19:56:47 -03:00
|
|
|
fxn = self.module_for_loader(lambda self, module: module)
|
2009-02-16 22:45:03 -04:00
|
|
|
return fxn(self, name)
|
|
|
|
|
|
|
|
def raise_exception(self, name):
|
|
|
|
def to_wrap(self, module):
|
|
|
|
raise ImportError
|
2013-05-31 19:56:47 -03:00
|
|
|
fxn = self.module_for_loader(to_wrap)
|
2009-02-16 22:45:03 -04:00
|
|
|
try:
|
|
|
|
fxn(self, name)
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_new_module(self):
|
|
|
|
# Test that when no module exists in sys.modules a new module is
|
|
|
|
# created.
|
|
|
|
module_name = 'a.b.c'
|
|
|
|
with test_util.uncache(module_name):
|
|
|
|
module = self.return_module(module_name)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIn(module_name, sys.modules)
|
|
|
|
self.assertIsInstance(module, types.ModuleType)
|
2009-02-16 22:45:03 -04:00
|
|
|
self.assertEqual(module.__name__, module_name)
|
|
|
|
|
|
|
|
def test_reload(self):
|
|
|
|
# Test that a module is reused if already in sys.modules.
|
2013-05-28 19:35:54 -03:00
|
|
|
class FakeLoader:
|
|
|
|
def is_package(self, name):
|
|
|
|
return True
|
2013-05-31 19:56:47 -03:00
|
|
|
@self.module_for_loader
|
2013-05-28 19:35:54 -03:00
|
|
|
def load_module(self, module):
|
|
|
|
return module
|
2009-02-16 22:45:03 -04:00
|
|
|
name = 'a.b.c'
|
2013-06-15 19:39:21 -03:00
|
|
|
module = types.ModuleType('a.b.c')
|
2013-05-28 19:35:54 -03:00
|
|
|
module.__loader__ = 42
|
|
|
|
module.__package__ = 42
|
2009-02-16 22:45:03 -04:00
|
|
|
with test_util.uncache(name):
|
|
|
|
sys.modules[name] = module
|
2013-05-28 19:35:54 -03:00
|
|
|
loader = FakeLoader()
|
|
|
|
returned_module = loader.load_module(name)
|
2010-06-20 23:49:35 -03:00
|
|
|
self.assertIs(returned_module, sys.modules[name])
|
2013-05-28 19:35:54 -03:00
|
|
|
self.assertEqual(module.__loader__, loader)
|
|
|
|
self.assertEqual(module.__package__, name)
|
2009-02-16 22:45:03 -04:00
|
|
|
|
|
|
|
def test_new_module_failure(self):
|
|
|
|
# Test that a module is removed from sys.modules if added but an
|
|
|
|
# exception is raised.
|
|
|
|
name = 'a.b.c'
|
|
|
|
with test_util.uncache(name):
|
|
|
|
self.raise_exception(name)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertNotIn(name, sys.modules)
|
2009-02-16 22:45:03 -04:00
|
|
|
|
|
|
|
def test_reload_failure(self):
|
|
|
|
# Test that a failure on reload leaves the module in-place.
|
|
|
|
name = 'a.b.c'
|
2013-06-15 19:39:21 -03:00
|
|
|
module = types.ModuleType(name)
|
2009-02-16 22:45:03 -04:00
|
|
|
with test_util.uncache(name):
|
|
|
|
sys.modules[name] = module
|
|
|
|
self.raise_exception(name)
|
2010-06-20 23:49:35 -03:00
|
|
|
self.assertIs(module, sys.modules[name])
|
2009-02-16 22:45:03 -04:00
|
|
|
|
2011-12-15 00:53:13 -04:00
|
|
|
def test_decorator_attrs(self):
|
|
|
|
def fxn(self, module): pass
|
2013-05-31 19:56:47 -03:00
|
|
|
wrapped = self.module_for_loader(fxn)
|
2011-12-15 00:53:13 -04:00
|
|
|
self.assertEqual(wrapped.__name__, fxn.__name__)
|
|
|
|
self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
|
2009-02-16 22:45:03 -04:00
|
|
|
|
2012-04-17 22:41:35 -03:00
|
|
|
def test_false_module(self):
|
|
|
|
# If for some odd reason a module is considered false, still return it
|
|
|
|
# from sys.modules.
|
|
|
|
class FalseModule(types.ModuleType):
|
|
|
|
def __bool__(self): return False
|
|
|
|
|
|
|
|
name = 'mod'
|
|
|
|
module = FalseModule(name)
|
|
|
|
with test_util.uncache(name):
|
|
|
|
self.assertFalse(module)
|
|
|
|
sys.modules[name] = module
|
|
|
|
given = self.return_module(name)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIs(given, module)
|
2009-03-02 10:38:26 -04:00
|
|
|
|
2012-04-27 18:27:14 -03:00
|
|
|
def test_attributes_set(self):
|
|
|
|
# __name__, __loader__, and __package__ should be set (when
|
|
|
|
# is_package() is defined; undefined implicitly tested elsewhere).
|
|
|
|
class FakeLoader:
|
|
|
|
def __init__(self, is_package):
|
|
|
|
self._pkg = is_package
|
|
|
|
def is_package(self, name):
|
|
|
|
return self._pkg
|
2013-05-31 19:56:47 -03:00
|
|
|
@self.module_for_loader
|
2012-04-27 18:27:14 -03:00
|
|
|
def load_module(self, module):
|
|
|
|
return module
|
|
|
|
|
|
|
|
name = 'pkg.mod'
|
|
|
|
with test_util.uncache(name):
|
|
|
|
loader = FakeLoader(False)
|
|
|
|
module = loader.load_module(name)
|
|
|
|
self.assertEqual(module.__name__, name)
|
|
|
|
self.assertIs(module.__loader__, loader)
|
|
|
|
self.assertEqual(module.__package__, 'pkg')
|
|
|
|
|
|
|
|
name = 'pkg.sub'
|
|
|
|
with test_util.uncache(name):
|
|
|
|
loader = FakeLoader(True)
|
|
|
|
module = loader.load_module(name)
|
|
|
|
self.assertEqual(module.__name__, name)
|
|
|
|
self.assertIs(module.__loader__, loader)
|
|
|
|
self.assertEqual(module.__package__, name)
|
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
Frozen_ModuleForLoaderTests, Source_ModuleForLoaderTests = test_util.test_both(
|
|
|
|
ModuleForLoaderTests, util=[frozen_util, source_util])
|
2009-03-02 10:38:26 -04:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
|
|
|
|
class SetPackageTests:
|
2012-04-17 22:41:35 -03:00
|
|
|
|
2009-03-04 12:07:00 -04:00
|
|
|
"""Tests for importlib.util.set_package."""
|
2009-03-02 10:38:26 -04:00
|
|
|
|
|
|
|
def verify(self, module, expect):
|
|
|
|
"""Verify the module has the expected value for __package__ after
|
2009-03-04 12:07:00 -04:00
|
|
|
passing through set_package."""
|
2009-03-02 10:38:26 -04:00
|
|
|
fxn = lambda: module
|
2013-10-18 16:40:11 -03:00
|
|
|
wrapped = self.util.set_package(fxn)
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
wrapped()
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(hasattr(module, '__package__'))
|
2009-03-02 10:38:26 -04:00
|
|
|
self.assertEqual(expect, module.__package__)
|
|
|
|
|
|
|
|
def test_top_level(self):
|
|
|
|
# __package__ should be set to the empty string if a top-level module.
|
|
|
|
# Implicitly tests when package is set to None.
|
2013-06-15 19:39:21 -03:00
|
|
|
module = types.ModuleType('module')
|
2009-03-02 10:38:26 -04:00
|
|
|
module.__package__ = None
|
|
|
|
self.verify(module, '')
|
|
|
|
|
|
|
|
def test_package(self):
|
|
|
|
# Test setting __package__ for a package.
|
2013-06-15 19:39:21 -03:00
|
|
|
module = types.ModuleType('pkg')
|
2009-03-02 10:38:26 -04:00
|
|
|
module.__path__ = ['<path>']
|
|
|
|
module.__package__ = None
|
|
|
|
self.verify(module, 'pkg')
|
|
|
|
|
|
|
|
def test_submodule(self):
|
|
|
|
# Test __package__ for a module in a package.
|
2013-06-15 19:39:21 -03:00
|
|
|
module = types.ModuleType('pkg.mod')
|
2009-03-02 10:38:26 -04:00
|
|
|
module.__package__ = None
|
|
|
|
self.verify(module, 'pkg')
|
|
|
|
|
|
|
|
def test_setting_if_missing(self):
|
|
|
|
# __package__ should be set if it is missing.
|
2013-06-15 19:39:21 -03:00
|
|
|
module = types.ModuleType('mod')
|
2009-03-02 10:38:26 -04:00
|
|
|
if hasattr(module, '__package__'):
|
|
|
|
delattr(module, '__package__')
|
|
|
|
self.verify(module, '')
|
|
|
|
|
|
|
|
def test_leaving_alone(self):
|
|
|
|
# If __package__ is set and not None then leave it alone.
|
|
|
|
for value in (True, False):
|
2013-06-15 19:39:21 -03:00
|
|
|
module = types.ModuleType('mod')
|
2009-03-02 10:38:26 -04:00
|
|
|
module.__package__ = value
|
|
|
|
self.verify(module, value)
|
|
|
|
|
2011-12-15 00:53:13 -04:00
|
|
|
def test_decorator_attrs(self):
|
|
|
|
def fxn(module): pass
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
wrapped = self.util.set_package(fxn)
|
2011-12-15 00:53:13 -04:00
|
|
|
self.assertEqual(wrapped.__name__, fxn.__name__)
|
|
|
|
self.assertEqual(wrapped.__qualname__, fxn.__qualname__)
|
2009-03-02 10:38:26 -04:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
Frozen_SetPackageTests, Source_SetPackageTests = test_util.test_both(
|
|
|
|
SetPackageTests, util=[frozen_util, source_util])
|
|
|
|
|
2012-05-13 14:45:09 -03:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
class SetLoaderTests:
|
2013-03-13 14:41:36 -03:00
|
|
|
|
|
|
|
"""Tests importlib.util.set_loader()."""
|
|
|
|
|
|
|
|
class DummyLoader:
|
|
|
|
@util.set_loader
|
|
|
|
def load_module(self, module):
|
|
|
|
return self.module
|
|
|
|
|
|
|
|
def test_no_attribute(self):
|
|
|
|
loader = self.DummyLoader()
|
2013-06-15 19:39:21 -03:00
|
|
|
loader.module = types.ModuleType('blah')
|
2013-03-13 14:41:36 -03:00
|
|
|
try:
|
|
|
|
del loader.module.__loader__
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
self.assertEqual(loader, loader.load_module('blah').__loader__)
|
2013-03-13 14:41:36 -03:00
|
|
|
|
|
|
|
def test_attribute_is_None(self):
|
|
|
|
loader = self.DummyLoader()
|
2013-06-15 19:39:21 -03:00
|
|
|
loader.module = types.ModuleType('blah')
|
2013-03-13 14:41:36 -03:00
|
|
|
loader.module.__loader__ = None
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
self.assertEqual(loader, loader.load_module('blah').__loader__)
|
2013-03-13 14:41:36 -03:00
|
|
|
|
|
|
|
def test_not_reset(self):
|
|
|
|
loader = self.DummyLoader()
|
2013-06-15 19:39:21 -03:00
|
|
|
loader.module = types.ModuleType('blah')
|
2013-03-13 14:41:36 -03:00
|
|
|
loader.module.__loader__ = 42
|
2014-01-06 23:49:04 -04:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter('ignore', DeprecationWarning)
|
|
|
|
self.assertEqual(42, loader.load_module('blah').__loader__)
|
2013-03-13 14:41:36 -03:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
class Frozen_SetLoaderTests(SetLoaderTests, unittest.TestCase):
|
|
|
|
class DummyLoader:
|
|
|
|
@frozen_util.set_loader
|
|
|
|
def load_module(self, module):
|
|
|
|
return self.module
|
2013-03-13 14:41:36 -03:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
class Source_SetLoaderTests(SetLoaderTests, unittest.TestCase):
|
|
|
|
class DummyLoader:
|
|
|
|
@source_util.set_loader
|
|
|
|
def load_module(self, module):
|
|
|
|
return self.module
|
|
|
|
|
|
|
|
|
|
|
|
class ResolveNameTests:
|
2012-05-13 14:45:09 -03:00
|
|
|
|
|
|
|
"""Tests importlib.util.resolve_name()."""
|
|
|
|
|
|
|
|
def test_absolute(self):
|
|
|
|
# bacon
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual('bacon', self.util.resolve_name('bacon', None))
|
2012-05-13 14:45:09 -03:00
|
|
|
|
|
|
|
def test_aboslute_within_package(self):
|
|
|
|
# bacon in spam
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual('bacon', self.util.resolve_name('bacon', 'spam'))
|
2012-05-13 14:45:09 -03:00
|
|
|
|
|
|
|
def test_no_package(self):
|
|
|
|
# .bacon in ''
|
|
|
|
with self.assertRaises(ValueError):
|
2013-10-18 16:40:11 -03:00
|
|
|
self.util.resolve_name('.bacon', '')
|
2012-05-13 14:45:09 -03:00
|
|
|
|
|
|
|
def test_in_package(self):
|
|
|
|
# .bacon in spam
|
|
|
|
self.assertEqual('spam.eggs.bacon',
|
2013-10-18 16:40:11 -03:00
|
|
|
self.util.resolve_name('.bacon', 'spam.eggs'))
|
2012-05-13 14:45:09 -03:00
|
|
|
|
|
|
|
def test_other_package(self):
|
|
|
|
# ..bacon in spam.bacon
|
|
|
|
self.assertEqual('spam.bacon',
|
2013-10-18 16:40:11 -03:00
|
|
|
self.util.resolve_name('..bacon', 'spam.eggs'))
|
2012-05-13 14:45:09 -03:00
|
|
|
|
|
|
|
def test_escape(self):
|
|
|
|
# ..bacon in spam
|
|
|
|
with self.assertRaises(ValueError):
|
2013-10-18 16:40:11 -03:00
|
|
|
self.util.resolve_name('..bacon', 'spam')
|
2012-05-13 14:45:09 -03:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
Frozen_ResolveNameTests, Source_ResolveNameTests = test_util.test_both(
|
|
|
|
ResolveNameTests,
|
|
|
|
util=[frozen_util, source_util])
|
2012-05-13 14:45:09 -03:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
|
|
|
|
class MagicNumberTests:
|
2013-06-14 20:02:34 -03:00
|
|
|
|
|
|
|
def test_length(self):
|
|
|
|
# Should be 4 bytes.
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(len(self.util.MAGIC_NUMBER), 4)
|
2013-06-14 20:02:34 -03:00
|
|
|
|
|
|
|
def test_incorporates_rn(self):
|
|
|
|
# The magic number uses \r\n to come out wrong when splitting on lines.
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertTrue(self.util.MAGIC_NUMBER.endswith(b'\r\n'))
|
|
|
|
|
|
|
|
Frozen_MagicNumberTests, Source_MagicNumberTests = test_util.test_both(
|
|
|
|
MagicNumberTests, util=[frozen_util, source_util])
|
2013-06-14 20:02:34 -03:00
|
|
|
|
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
class PEP3147Tests:
|
2013-06-14 23:26:30 -03:00
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
"""Tests of PEP 3147-related functions: cache_from_source and source_from_cache."""
|
2013-06-14 23:26:30 -03:00
|
|
|
|
2013-06-15 19:39:21 -03:00
|
|
|
tag = sys.implementation.cache_tag
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
@unittest.skipUnless(sys.implementation.cache_tag is not None,
|
|
|
|
'requires sys.implementation.cache_tag not be None')
|
|
|
|
def test_cache_from_source(self):
|
|
|
|
# Given the path to a .py file, return the path to its PEP 3147
|
|
|
|
# defined .pyc file (i.e. under __pycache__).
|
|
|
|
path = os.path.join('foo', 'bar', 'baz', 'qux.py')
|
|
|
|
expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
|
|
|
|
'qux.{}.pyc'.format(self.tag))
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.cache_from_source(path, True), expect)
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_cache_from_source_no_cache_tag(self):
|
|
|
|
# No cache tag means NotImplementedError.
|
|
|
|
with support.swap_attr(sys.implementation, 'cache_tag', None):
|
|
|
|
with self.assertRaises(NotImplementedError):
|
2013-10-18 16:40:11 -03:00
|
|
|
self.util.cache_from_source('whatever.py')
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_cache_from_source_no_dot(self):
|
|
|
|
# Directory with a dot, filename without dot.
|
|
|
|
path = os.path.join('foo.bar', 'file')
|
|
|
|
expect = os.path.join('foo.bar', '__pycache__',
|
|
|
|
'file{}.pyc'.format(self.tag))
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.cache_from_source(path, True), expect)
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_cache_from_source_optimized(self):
|
|
|
|
# Given the path to a .py file, return the path to its PEP 3147
|
|
|
|
# defined .pyo file (i.e. under __pycache__).
|
|
|
|
path = os.path.join('foo', 'bar', 'baz', 'qux.py')
|
|
|
|
expect = os.path.join('foo', 'bar', 'baz', '__pycache__',
|
|
|
|
'qux.{}.pyo'.format(self.tag))
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.cache_from_source(path, False), expect)
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_cache_from_source_cwd(self):
|
|
|
|
path = 'foo.py'
|
|
|
|
expect = os.path.join('__pycache__', 'foo.{}.pyc'.format(self.tag))
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.cache_from_source(path, True), expect)
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_cache_from_source_override(self):
|
|
|
|
# When debug_override is not None, it can be any true-ish or false-ish
|
|
|
|
# value.
|
|
|
|
path = os.path.join('foo', 'bar', 'baz.py')
|
|
|
|
partial_expect = os.path.join('foo', 'bar', '__pycache__',
|
|
|
|
'baz.{}.py'.format(self.tag))
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.cache_from_source(path, []), partial_expect + 'o')
|
|
|
|
self.assertEqual(self.util.cache_from_source(path, [17]),
|
2013-06-14 23:26:30 -03:00
|
|
|
partial_expect + 'c')
|
|
|
|
# However if the bool-ishness can't be determined, the exception
|
|
|
|
# propagates.
|
|
|
|
class Bearish:
|
|
|
|
def __bool__(self): raise RuntimeError
|
|
|
|
with self.assertRaises(RuntimeError):
|
2013-10-18 16:40:11 -03:00
|
|
|
self.util.cache_from_source('/foo/bar/baz.py', Bearish())
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
@unittest.skipUnless(os.sep == '\\' and os.altsep == '/',
|
|
|
|
'test meaningful only where os.altsep is defined')
|
|
|
|
def test_sep_altsep_and_sep_cache_from_source(self):
|
|
|
|
# Windows path and PEP 3147 where sep is right of altsep.
|
|
|
|
self.assertEqual(
|
2013-10-18 16:40:11 -03:00
|
|
|
self.util.cache_from_source('\\foo\\bar\\baz/qux.py', True),
|
2013-06-14 23:26:30 -03:00
|
|
|
'\\foo\\bar\\baz\\__pycache__\\qux.{}.pyc'.format(self.tag))
|
|
|
|
|
|
|
|
@unittest.skipUnless(sys.implementation.cache_tag is not None,
|
|
|
|
'requires sys.implementation.cache_tag to not be '
|
|
|
|
'None')
|
|
|
|
def test_source_from_cache(self):
|
|
|
|
# Given the path to a PEP 3147 defined .pyc file, return the path to
|
|
|
|
# its source. This tests the good path.
|
|
|
|
path = os.path.join('foo', 'bar', 'baz', '__pycache__',
|
|
|
|
'qux.{}.pyc'.format(self.tag))
|
|
|
|
expect = os.path.join('foo', 'bar', 'baz', 'qux.py')
|
2013-10-18 16:40:11 -03:00
|
|
|
self.assertEqual(self.util.source_from_cache(path), expect)
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_source_from_cache_no_cache_tag(self):
|
|
|
|
# If sys.implementation.cache_tag is None, raise NotImplementedError.
|
|
|
|
path = os.path.join('blah', '__pycache__', 'whatever.pyc')
|
|
|
|
with support.swap_attr(sys.implementation, 'cache_tag', None):
|
|
|
|
with self.assertRaises(NotImplementedError):
|
2013-10-18 16:40:11 -03:00
|
|
|
self.util.source_from_cache(path)
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_source_from_cache_bad_path(self):
|
|
|
|
# When the path to a pyc file is not in PEP 3147 format, a ValueError
|
|
|
|
# is raised.
|
|
|
|
self.assertRaises(
|
2013-10-18 16:40:11 -03:00
|
|
|
ValueError, self.util.source_from_cache, '/foo/bar/bazqux.pyc')
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_source_from_cache_no_slash(self):
|
|
|
|
# No slashes at all in path -> ValueError
|
|
|
|
self.assertRaises(
|
2013-10-18 16:40:11 -03:00
|
|
|
ValueError, self.util.source_from_cache, 'foo.cpython-32.pyc')
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_source_from_cache_too_few_dots(self):
|
|
|
|
# Too few dots in final path component -> ValueError
|
|
|
|
self.assertRaises(
|
2013-10-18 16:40:11 -03:00
|
|
|
ValueError, self.util.source_from_cache, '__pycache__/foo.pyc')
|
2013-06-14 23:26:30 -03:00
|
|
|
|
|
|
|
def test_source_from_cache_too_many_dots(self):
|
|
|
|
# Too many dots in final path component -> ValueError
|
|
|
|
self.assertRaises(
|
2013-10-18 16:40:11 -03:00
|
|
|
ValueError, self.util.source_from_cache,
|
2013-06-14 23:26:30 -03:00
|
|
|
'__pycache__/foo.cpython-32.foo.pyc')
|
|
|
|
|
|
|
|
def test_source_from_cache_no__pycache__(self):
|
|
|
|
# Another problem with the path -> ValueError
|
|
|
|
self.assertRaises(
|
2013-10-18 16:40:11 -03:00
|
|
|
ValueError, self.util.source_from_cache,
|
2013-06-14 23:26:30 -03:00
|
|
|
'/foo/bar/foo.cpython-32.foo.pyc')
|
|
|
|
|
2013-10-18 16:40:11 -03:00
|
|
|
Frozen_PEP3147Tests, Source_PEP3147Tests = test_util.test_both(
|
|
|
|
PEP3147Tests,
|
|
|
|
util=[frozen_util, source_util])
|
|
|
|
|
2013-06-14 23:26:30 -03:00
|
|
|
|
2009-02-16 22:45:03 -04:00
|
|
|
if __name__ == '__main__':
|
2013-03-13 14:41:36 -03:00
|
|
|
unittest.main()
|