2009-01-22 18:43:07 -04:00
|
|
|
from importlib import machinery
|
2009-03-14 23:20:16 -03:00
|
|
|
import imp
|
|
|
|
import unittest
|
2009-01-31 21:34:13 -04:00
|
|
|
from .. import abc
|
2009-02-01 00:00:05 -04:00
|
|
|
from .. import util
|
2011-05-16 11:57:18 -03:00
|
|
|
from test.support import captured_stdout
|
2009-01-31 21:34:13 -04:00
|
|
|
|
|
|
|
class LoaderTests(abc.LoaderTests):
|
|
|
|
|
|
|
|
def test_module(self):
|
2011-05-16 11:57:18 -03:00
|
|
|
with util.uncache('__hello__'), captured_stdout() as stdout:
|
2009-01-31 21:34:13 -04:00
|
|
|
module = machinery.FrozenImporter.load_module('__hello__')
|
2012-05-24 21:21:04 -03:00
|
|
|
check = {'__name__': '__hello__',
|
2012-05-24 21:22:10 -03:00
|
|
|
'__package__': '',
|
2012-05-24 21:21:04 -03:00
|
|
|
'__loader__': machinery.FrozenImporter,
|
|
|
|
}
|
2009-01-31 21:34:13 -04:00
|
|
|
for attr, value in check.items():
|
|
|
|
self.assertEqual(getattr(module, attr), value)
|
2011-05-16 11:57:18 -03:00
|
|
|
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
|
2012-05-24 21:21:04 -03:00
|
|
|
self.assertFalse(hasattr(module, '__file__'))
|
2009-01-31 21:34:13 -04:00
|
|
|
|
|
|
|
def test_package(self):
|
2011-05-16 11:57:18 -03:00
|
|
|
with util.uncache('__phello__'), captured_stdout() as stdout:
|
2009-01-31 21:34:13 -04:00
|
|
|
module = machinery.FrozenImporter.load_module('__phello__')
|
2012-05-24 21:22:10 -03:00
|
|
|
check = {'__name__': '__phello__',
|
|
|
|
'__package__': '__phello__',
|
2012-05-24 21:21:04 -03:00
|
|
|
'__path__': ['__phello__'],
|
|
|
|
'__loader__': machinery.FrozenImporter,
|
|
|
|
}
|
2009-01-31 21:34:13 -04:00
|
|
|
for attr, value in check.items():
|
|
|
|
attr_value = getattr(module, attr)
|
|
|
|
self.assertEqual(attr_value, value,
|
|
|
|
"for __phello__.%s, %r != %r" %
|
|
|
|
(attr, attr_value, value))
|
2011-05-16 11:57:18 -03:00
|
|
|
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
|
2012-05-24 21:21:04 -03:00
|
|
|
self.assertFalse(hasattr(module, '__file__'))
|
2009-01-31 21:34:13 -04:00
|
|
|
|
|
|
|
def test_lacking_parent(self):
|
2011-05-16 11:57:18 -03:00
|
|
|
with util.uncache('__phello__', '__phello__.spam'), \
|
|
|
|
captured_stdout() as stdout:
|
2009-01-31 21:34:13 -04:00
|
|
|
module = machinery.FrozenImporter.load_module('__phello__.spam')
|
2012-05-24 21:21:04 -03:00
|
|
|
check = {'__name__': '__phello__.spam',
|
2009-03-10 02:17:37 -03:00
|
|
|
'__package__': '__phello__',
|
2012-05-24 21:21:04 -03:00
|
|
|
'__loader__': machinery.FrozenImporter,
|
|
|
|
}
|
2009-01-31 21:34:13 -04:00
|
|
|
for attr, value in check.items():
|
|
|
|
attr_value = getattr(module, attr)
|
|
|
|
self.assertEqual(attr_value, value,
|
|
|
|
"for __phello__.spam.%s, %r != %r" %
|
|
|
|
(attr, attr_value, value))
|
2011-05-16 11:57:18 -03:00
|
|
|
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
|
2012-05-24 21:21:04 -03:00
|
|
|
self.assertFalse(hasattr(module, '__file__'))
|
2009-01-31 21:34:13 -04:00
|
|
|
|
|
|
|
def test_module_reuse(self):
|
2011-05-16 11:57:18 -03:00
|
|
|
with util.uncache('__hello__'), captured_stdout() as stdout:
|
2009-01-31 21:34:13 -04:00
|
|
|
module1 = machinery.FrozenImporter.load_module('__hello__')
|
|
|
|
module2 = machinery.FrozenImporter.load_module('__hello__')
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertIs(module1, module2)
|
2011-05-16 11:57:18 -03:00
|
|
|
self.assertEqual(stdout.getvalue(),
|
|
|
|
'Hello world!\nHello world!\n')
|
2009-01-31 21:34:13 -04:00
|
|
|
|
2012-05-24 21:21:04 -03:00
|
|
|
def test_module_repr(self):
|
|
|
|
with util.uncache('__hello__'), captured_stdout():
|
|
|
|
module = machinery.FrozenImporter.load_module('__hello__')
|
2012-05-24 21:22:10 -03:00
|
|
|
self.assertEqual(repr(module),
|
2012-05-24 21:21:04 -03:00
|
|
|
"<module '__hello__' (frozen)>")
|
|
|
|
|
2009-01-31 21:34:13 -04:00
|
|
|
def test_state_after_failure(self):
|
|
|
|
# No way to trigger an error in a frozen module.
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_unloadable(self):
|
|
|
|
assert machinery.FrozenImporter.find_module('_not_real') is None
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2009-08-27 20:49:21 -03:00
|
|
|
machinery.FrozenImporter.load_module('_not_real')
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, '_not_real')
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
2009-03-14 23:20:16 -03:00
|
|
|
class InspectLoaderTests(unittest.TestCase):
|
|
|
|
|
|
|
|
"""Tests for the InspectLoader methods for FrozenImporter."""
|
|
|
|
|
|
|
|
def test_get_code(self):
|
|
|
|
# Make sure that the code object is good.
|
|
|
|
name = '__hello__'
|
2011-05-16 11:57:18 -03:00
|
|
|
with captured_stdout() as stdout:
|
|
|
|
code = machinery.FrozenImporter.get_code(name)
|
|
|
|
mod = imp.new_module(name)
|
|
|
|
exec(code, mod.__dict__)
|
|
|
|
self.assertTrue(hasattr(mod, 'initialized'))
|
|
|
|
self.assertEqual(stdout.getvalue(), 'Hello world!\n')
|
2009-03-14 23:20:16 -03:00
|
|
|
|
|
|
|
def test_get_source(self):
|
|
|
|
# Should always return None.
|
|
|
|
result = machinery.FrozenImporter.get_source('__hello__')
|
2012-06-28 07:15:01 -03:00
|
|
|
self.assertIsNone(result)
|
2009-03-14 23:20:16 -03:00
|
|
|
|
|
|
|
def test_is_package(self):
|
|
|
|
# Should be able to tell what is a package.
|
|
|
|
test_for = (('__hello__', False), ('__phello__', True),
|
|
|
|
('__phello__.spam', False))
|
|
|
|
for name, is_package in test_for:
|
|
|
|
result = machinery.FrozenImporter.is_package(name)
|
2012-06-27 16:26:26 -03:00
|
|
|
self.assertEqual(bool(result), is_package)
|
2009-03-14 23:20:16 -03:00
|
|
|
|
|
|
|
def test_failure(self):
|
|
|
|
# Raise ImportError for modules that are not frozen.
|
|
|
|
for meth_name in ('get_code', 'get_source', 'is_package'):
|
|
|
|
method = getattr(machinery.FrozenImporter, meth_name)
|
2012-04-12 22:09:01 -03:00
|
|
|
with self.assertRaises(ImportError) as cm:
|
2009-08-27 20:49:21 -03:00
|
|
|
method('importlib')
|
2012-04-12 22:09:01 -03:00
|
|
|
self.assertEqual(cm.exception.name, 'importlib')
|
2009-03-14 23:20:16 -03:00
|
|
|
|
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
def test_main():
|
|
|
|
from test.support import run_unittest
|
2009-03-14 23:20:16 -03:00
|
|
|
run_unittest(LoaderTests, InspectLoaderTests)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|