Commit 1e3a68d3 authored by Éric Araujo's avatar Éric Araujo

Modernize modulefinder module and tests a bit.

The tests don’t use an internal distutils function anymore, and use
regular assertEqual with sorted lists instead of a convoluted manual
diff.
parent cdb31093
"""Find modules used by a script, using introspection.""" """Find modules used by a script, using introspection."""
from __future__ import generators
import dis import dis
import imp import imp
import marshal import marshal
...@@ -9,8 +8,6 @@ import sys ...@@ -9,8 +8,6 @@ import sys
import types import types
import struct import struct
READ_MODE = "rU"
# XXX Clean up once str8's cstor matches bytes. # XXX Clean up once str8's cstor matches bytes.
LOAD_CONST = bytes([dis.opname.index('LOAD_CONST')]) LOAD_CONST = bytes([dis.opname.index('LOAD_CONST')])
IMPORT_NAME = bytes([dis.opname.index('IMPORT_NAME')]) IMPORT_NAME = bytes([dis.opname.index('IMPORT_NAME')])
...@@ -29,8 +26,7 @@ packagePathMap = {} ...@@ -29,8 +26,7 @@ packagePathMap = {}
# A Public interface # A Public interface
def AddPackagePath(packagename, path): def AddPackagePath(packagename, path):
paths = packagePathMap.get(packagename, []) paths = packagePathMap.setdefault(packagename, []).append(path)
paths.append(path)
packagePathMap[packagename] = paths packagePathMap[packagename] = paths
replacePackageMap = {} replacePackageMap = {}
...@@ -106,14 +102,14 @@ class ModuleFinder: ...@@ -106,14 +102,14 @@ class ModuleFinder:
def run_script(self, pathname): def run_script(self, pathname):
self.msg(2, "run_script", pathname) self.msg(2, "run_script", pathname)
with open(pathname, READ_MODE) as fp: with open(pathname) as fp:
stuff = ("", "r", imp.PY_SOURCE) stuff = ("", "r", imp.PY_SOURCE)
self.load_module('__main__', fp, pathname, stuff) self.load_module('__main__', fp, pathname, stuff)
def load_file(self, pathname): def load_file(self, pathname):
dir, name = os.path.split(pathname) dir, name = os.path.split(pathname)
name, ext = os.path.splitext(name) name, ext = os.path.splitext(name)
with open(pathname, READ_MODE) as fp: with open(pathname) as fp:
stuff = (ext, "r", imp.PY_SOURCE) stuff = (ext, "r", imp.PY_SOURCE)
self.load_module(name, fp, pathname, stuff) self.load_module(name, fp, pathname, stuff)
...@@ -270,7 +266,8 @@ class ModuleFinder: ...@@ -270,7 +266,8 @@ class ModuleFinder:
try: try:
m = self.load_module(fqname, fp, pathname, stuff) m = self.load_module(fqname, fp, pathname, stuff)
finally: finally:
if fp: fp.close() if fp:
fp.close()
if parent: if parent:
setattr(parent, partname, m) setattr(parent, partname, m)
self.msgout(3, "import_module ->", m) self.msgout(3, "import_module ->", m)
...@@ -662,4 +659,4 @@ if __name__ == '__main__': ...@@ -662,4 +659,4 @@ if __name__ == '__main__':
try: try:
mf = test() mf = test()
except KeyboardInterrupt: except KeyboardInterrupt:
print("\n[interrupt]") print("\n[interrupted]")
import __future__
import os import os
import errno
import shutil
import unittest import unittest
import distutils.dir_util
import tempfile import tempfile
from test import support from test import support
...@@ -9,7 +9,7 @@ from test import support ...@@ -9,7 +9,7 @@ from test import support
import modulefinder import modulefinder
TEST_DIR = tempfile.mkdtemp() TEST_DIR = tempfile.mkdtemp()
TEST_PATH = [TEST_DIR, os.path.dirname(__future__.__file__)] TEST_PATH = [TEST_DIR, os.path.dirname(tempfile.__file__)]
# Each test description is a list of 5 items: # Each test description is a list of 5 items:
# #
...@@ -196,12 +196,17 @@ a/module.py ...@@ -196,12 +196,17 @@ a/module.py
from . import bar from . import bar
"""] """]
def open_file(path): def open_file(path):
##print "#", os.path.abspath(path)
dirname = os.path.dirname(path) dirname = os.path.dirname(path)
distutils.dir_util.mkpath(dirname) try:
os.makedirs(dirname)
except OSError as e:
if e.errno != errno.EEXIST:
raise
return open(path, "w") return open(path, "w")
def create_package(source): def create_package(source):
ofi = None ofi = None
try: try:
...@@ -216,6 +221,7 @@ def create_package(source): ...@@ -216,6 +221,7 @@ def create_package(source):
if ofi: if ofi:
ofi.close() ofi.close()
class ModuleFinderTest(unittest.TestCase): class ModuleFinderTest(unittest.TestCase):
def _do_test(self, info, report=False): def _do_test(self, info, report=False):
import_this, modules, missing, maybe_missing, source = info import_this, modules, missing, maybe_missing, source = info
...@@ -234,19 +240,17 @@ class ModuleFinderTest(unittest.TestCase): ...@@ -234,19 +240,17 @@ class ModuleFinderTest(unittest.TestCase):
## import traceback; traceback.print_exc() ## import traceback; traceback.print_exc()
## sys.path = opath ## sys.path = opath
## return ## return
modules = set(modules) modules = sorted(set(modules))
found = set(mf.modules.keys()) found = sorted(mf.modules)
more = list(found - modules)
less = list(modules - found)
# check if we found what we expected, not more, not less # check if we found what we expected, not more, not less
self.assertEqual((more, less), ([], [])) self.assertEqual(found, modules)
# check for missing and maybe missing modules # check for missing and maybe missing modules
bad, maybe = mf.any_missing_maybe() bad, maybe = mf.any_missing_maybe()
self.assertEqual(bad, missing) self.assertEqual(bad, missing)
self.assertEqual(maybe, maybe_missing) self.assertEqual(maybe, maybe_missing)
finally: finally:
distutils.dir_util.remove_tree(TEST_DIR) shutil.rmtree(TEST_DIR)
def test_package(self): def test_package(self):
self._do_test(package_test) self._do_test(package_test)
...@@ -254,25 +258,23 @@ class ModuleFinderTest(unittest.TestCase): ...@@ -254,25 +258,23 @@ class ModuleFinderTest(unittest.TestCase):
def test_maybe(self): def test_maybe(self):
self._do_test(maybe_test) self._do_test(maybe_test)
if getattr(__future__, "absolute_import", None): def test_maybe_new(self):
self._do_test(maybe_test_new)
def test_maybe_new(self): def test_absolute_imports(self):
self._do_test(maybe_test_new) self._do_test(absolute_import_test)
def test_absolute_imports(self): def test_relative_imports(self):
self._do_test(absolute_import_test) self._do_test(relative_import_test)
def test_relative_imports(self): def test_relative_imports_2(self):
self._do_test(relative_import_test) self._do_test(relative_import_test_2)
def test_relative_imports_2(self): def test_relative_imports_3(self):
self._do_test(relative_import_test_2) self._do_test(relative_import_test_3)
def test_relative_imports_3(self):
self._do_test(relative_import_test_3)
def test_main(): def test_main():
distutils.log.set_threshold(distutils.log.WARN)
support.run_unittest(ModuleFinderTest) support.run_unittest(ModuleFinderTest)
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment