2009-02-15 01:48:13 -04:00
|
|
|
from importlib import _bootstrap
|
2009-02-04 22:53:23 -04:00
|
|
|
from importlib import machinery
|
2009-02-01 00:00:05 -04:00
|
|
|
from .. import util
|
2009-02-01 00:28:04 -04:00
|
|
|
from . import util as import_util
|
2009-02-05 20:07:49 -04:00
|
|
|
import imp
|
2009-02-05 18:02:03 -04:00
|
|
|
import os
|
2009-01-17 20:24:28 -04:00
|
|
|
import sys
|
2010-07-03 18:48:25 -03:00
|
|
|
import tempfile
|
2009-02-05 20:07:49 -04:00
|
|
|
from test import support
|
2009-01-17 20:24:28 -04:00
|
|
|
from types import MethodType
|
|
|
|
import unittest
|
2012-04-25 21:54:04 -03:00
|
|
|
import warnings
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
|
2009-02-04 22:53:23 -04:00
|
|
|
class FinderTests(unittest.TestCase):
|
|
|
|
|
2009-02-15 01:48:13 -04:00
|
|
|
"""Tests for PathFinder."""
|
2009-02-04 22:53:23 -04:00
|
|
|
|
|
|
|
def test_failure(self):
|
|
|
|
# Test None returned upon not finding a suitable finder.
|
2009-02-15 01:48:13 -04:00
|
|
|
module = '<test module>'
|
|
|
|
with util.import_state():
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(machinery.PathFinder.find_module(module) is None)
|
2009-02-04 22:53:23 -04:00
|
|
|
|
|
|
|
def test_sys_path(self):
|
|
|
|
# Test that sys.path is used when 'path' is None.
|
|
|
|
# Implicitly tests that sys.path_importer_cache is used.
|
|
|
|
module = '<test module>'
|
|
|
|
path = '<test path>'
|
|
|
|
importer = util.mock_modules(module)
|
|
|
|
with util.import_state(path_importer_cache={path: importer},
|
|
|
|
path=[path]):
|
|
|
|
loader = machinery.PathFinder.find_module(module)
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(loader is importer)
|
2009-02-04 22:53:23 -04:00
|
|
|
|
|
|
|
def test_path(self):
|
|
|
|
# Test that 'path' is used when set.
|
|
|
|
# Implicitly tests that sys.path_importer_cache is used.
|
|
|
|
module = '<test module>'
|
|
|
|
path = '<test path>'
|
|
|
|
importer = util.mock_modules(module)
|
|
|
|
with util.import_state(path_importer_cache={path: importer}):
|
|
|
|
loader = machinery.PathFinder.find_module(module, [path])
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(loader is importer)
|
2009-02-04 22:53:23 -04:00
|
|
|
|
2012-04-17 22:41:35 -03:00
|
|
|
def test_empty_list(self):
|
|
|
|
# An empty list should not count as asking for sys.path.
|
|
|
|
module = 'module'
|
|
|
|
path = '<test path>'
|
|
|
|
importer = util.mock_modules(module)
|
|
|
|
with util.import_state(path_importer_cache={path: importer},
|
|
|
|
path=[path]):
|
|
|
|
self.assertIsNone(machinery.PathFinder.find_module('module', []))
|
|
|
|
|
2009-02-04 22:53:23 -04:00
|
|
|
def test_path_hooks(self):
|
|
|
|
# Test that sys.path_hooks is used.
|
2009-02-05 19:36:02 -04:00
|
|
|
# Test that sys.path_importer_cache is set.
|
|
|
|
module = '<test module>'
|
|
|
|
path = '<test path>'
|
|
|
|
importer = util.mock_modules(module)
|
|
|
|
hook = import_util.mock_path_hook(path, importer=importer)
|
|
|
|
with util.import_state(path_hooks=[hook]):
|
|
|
|
loader = machinery.PathFinder.find_module(module, [path])
|
2009-06-30 20:06:06 -03:00
|
|
|
self.assertTrue(loader is importer)
|
|
|
|
self.assertTrue(path in sys.path_importer_cache)
|
|
|
|
self.assertTrue(sys.path_importer_cache[path] is importer)
|
2009-02-04 22:53:23 -04:00
|
|
|
|
2012-04-25 21:54:04 -03:00
|
|
|
def test_empty_path_hooks(self):
|
2012-04-27 16:30:58 -03:00
|
|
|
# Test that if sys.path_hooks is empty a warning is raised,
|
|
|
|
# sys.path_importer_cache gets None set, and PathFinder returns None.
|
|
|
|
path_entry = 'bogus_path'
|
2012-04-25 21:54:04 -03:00
|
|
|
with util.import_state(path_importer_cache={}, path_hooks=[],
|
2012-04-27 16:30:58 -03:00
|
|
|
path=[path_entry]):
|
2012-04-25 21:54:04 -03:00
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
self.assertIsNone(machinery.PathFinder.find_module('os'))
|
2012-04-27 16:30:58 -03:00
|
|
|
self.assertIsNone(sys.path_importer_cache[path_entry])
|
2012-04-25 21:54:04 -03:00
|
|
|
self.assertEqual(len(w), 1)
|
|
|
|
self.assertTrue(issubclass(w[-1].category, ImportWarning))
|
2009-03-30 16:57:15 -03:00
|
|
|
|
2012-02-08 19:52:56 -04:00
|
|
|
def test_path_importer_cache_empty_string(self):
|
|
|
|
# The empty string should create a finder using the cwd.
|
|
|
|
path = ''
|
|
|
|
module = '<test module>'
|
|
|
|
importer = util.mock_modules(module)
|
2012-02-19 20:48:16 -04:00
|
|
|
hook = import_util.mock_path_hook(os.curdir, importer=importer)
|
2012-02-08 19:52:56 -04:00
|
|
|
with util.import_state(path=[path], path_hooks=[hook]):
|
|
|
|
loader = machinery.PathFinder.find_module(module)
|
|
|
|
self.assertIs(loader, importer)
|
2012-02-19 20:48:16 -04:00
|
|
|
self.assertIn(os.curdir, sys.path_importer_cache)
|
2009-03-30 16:57:15 -03:00
|
|
|
|
2009-02-15 01:48:13 -04:00
|
|
|
|
2009-01-17 20:24:28 -04:00
|
|
|
def test_main():
|
|
|
|
from test.support import run_unittest
|
2012-04-25 21:54:04 -03:00
|
|
|
run_unittest(FinderTests)
|
2009-01-17 20:24:28 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_main()
|