2016-06-05 00:54:28 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
2016-06-05 16:15:12 +00:00
|
|
|
test_polyloader
|
2016-06-05 00:54:28 +00:00
|
|
|
----------------------------------
|
|
|
|
|
2016-06-05 16:15:12 +00:00
|
|
|
Tests for `polyloader` module.
|
2016-06-05 00:54:28 +00:00
|
|
|
"""
|
|
|
|
|
2016-07-01 05:11:10 +00:00
|
|
|
import polyloader
|
|
|
|
import copy
|
|
|
|
import sys
|
2016-06-05 00:54:28 +00:00
|
|
|
|
2016-06-05 16:15:12 +00:00
|
|
|
# Note that these compilers don't actually load much out of the
|
2016-06-05 00:54:28 +00:00
|
|
|
# source files. That's not the point. The point is to show that the
|
|
|
|
# correct compiler has been found for a given extension.
|
|
|
|
|
2016-07-01 05:11:10 +00:00
|
|
|
|
|
|
|
class ImportEnvironment(object):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
polyloader.reset()
|
|
|
|
self.path = copy.copy(sys.path)
|
|
|
|
self.path_hooks = copy.copy(sys.path_hooks)
|
|
|
|
self.meta_path = copy.copy(sys.meta_path)
|
|
|
|
self.modules = copy.copy(sys.modules)
|
|
|
|
self.path_importer_cache = copy.copy(sys.path_importer_cache)
|
|
|
|
return sys
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
sys.path = self.path
|
|
|
|
sys.path_hooks = self.path_hooks
|
|
|
|
sys.meta_path = self.meta_path
|
|
|
|
sys.modules = self.modules
|
|
|
|
sys.path_importer_cache = self.path_importer_cache
|
|
|
|
|
|
|
|
|
|
|
|
class Compiler:
|
|
|
|
def __init__(self, pt):
|
|
|
|
self.pt = pt
|
|
|
|
|
|
|
|
def __call__(self, source_text, filename, *extra):
|
|
|
|
return compile("result='Success for %s: %s'" %
|
2016-07-09 21:53:45 +00:00
|
|
|
(self.pt, source_text.decode('utf-8').rstrip()), filename, "exec")
|
2016-07-01 05:11:10 +00:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "Compiler %s" % (self.pt)
|
|
|
|
|
2016-06-05 16:15:12 +00:00
|
|
|
def compiler(pt):
|
2016-07-01 05:11:10 +00:00
|
|
|
return Compiler(pt)
|
|
|
|
|
|
|
|
# Functionally, the "From" test and the "Direct" test should be
|
|
|
|
# indistinguishable. What's interesting about them, though, is that
|
|
|
|
# in the context of the caller, the "Direct" test now has test and
|
|
|
|
# test.polytestmix as objects in the calling context. They're fairly
|
|
|
|
# lightweight, but they do exist, and they do honor the __all__ and
|
|
|
|
# __path__ cases.
|
|
|
|
#
|
|
|
|
# Also:
|
|
|
|
# See http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
|
|
|
|
# for some 'gotchas' to look forward to. Whee.
|
2016-06-05 00:54:28 +00:00
|
|
|
|
2016-07-01 05:11:10 +00:00
|
|
|
class Test_Polymorph_From(object):
|
2016-06-05 00:54:28 +00:00
|
|
|
def test_import1(self):
|
2016-07-01 05:11:10 +00:00
|
|
|
with ImportEnvironment() as sys:
|
|
|
|
polyloader.install(compiler("2"), ['2'])
|
|
|
|
polyloader.install(compiler("3"), ['3'])
|
|
|
|
from .polytestmix import test2
|
|
|
|
from .polytestmix import test3
|
|
|
|
from .polytestmix import test1
|
|
|
|
assert(test1.result == "Success for 1: Test One")
|
|
|
|
assert(test2.result == "Success for 2: Test Two")
|
|
|
|
assert(test3.result == "Success for 3: Test Three")
|
|
|
|
|
|
|
|
class Test_Polymorph_Direct(object):
|
|
|
|
def test_import2(self):
|
|
|
|
with ImportEnvironment() as sys:
|
|
|
|
polyloader.install(compiler("2"), ['2'])
|
|
|
|
polyloader.install(compiler("3"), ['3'])
|
2016-07-09 21:53:45 +00:00
|
|
|
import tests_py3.polytestmix.test2
|
|
|
|
import tests_py3.polytestmix.test3
|
|
|
|
import tests_py3.polytestmix.test1
|
|
|
|
assert(tests_py3.polytestmix.test1.result == "Success for 1: Test One")
|
|
|
|
assert(tests_py3.polytestmix.test2.result == "Success for 2: Test Two")
|
|
|
|
assert(tests_py3.polytestmix.test3.result == "Success for 3: Test Three")
|
2016-07-01 05:11:10 +00:00
|
|
|
|
|
|
|
class Test_Polymorph_Module(object):
|
|
|
|
def test_import3(self):
|
|
|
|
with ImportEnvironment() as sys:
|
|
|
|
polyloader.install(compiler("3"), ['3'])
|
|
|
|
polyloader.install(compiler("2"), ['2'])
|
2016-07-09 21:53:45 +00:00
|
|
|
from tests_py3.polytestmix.test3 import result as result3
|
|
|
|
from tests_py3.polytestmix.test2 import result as result2
|
|
|
|
from tests_py3.polytestmix.test1 import result as result1
|
2016-07-01 05:11:10 +00:00
|
|
|
assert(result1 == "Success for 1: Test One")
|
|
|
|
assert(result2 == "Success for 2: Test Two")
|
|
|
|
assert(result3 == "Success for 3: Test Three")
|
2016-06-05 00:54:28 +00:00
|
|
|
|
|
|
|
class Test_Polymorph_Iterator(object):
|
2016-06-05 16:15:12 +00:00
|
|
|
''' The Django Compatibility test: Can we load arbitrary modules from a package? '''
|
2016-06-05 00:54:28 +00:00
|
|
|
def test_iterator(self):
|
2016-07-01 05:11:10 +00:00
|
|
|
with ImportEnvironment() as sys:
|
|
|
|
import os
|
|
|
|
import inspect
|
2016-07-09 21:53:45 +00:00
|
|
|
polyloader.install(compiler("2"), ['2'])
|
|
|
|
polyloader.install(compiler("3"), ['3'])
|
2016-07-01 05:11:10 +00:00
|
|
|
import pkgutil
|
|
|
|
target_dir = os.path.join(
|
|
|
|
os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))),
|
|
|
|
'polytestmix')
|
|
|
|
modules = set([name for (_, name, is_pkg) in pkgutil.iter_modules([target_dir])
|
|
|
|
if not is_pkg and not name.startswith('_')])
|
|
|
|
assert(modules == set(['test1', 'test2', 'test3']))
|
2016-06-05 16:15:12 +00:00
|
|
|
|