Skip to content

Commit 1e3a68d

Browse files
committed
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.
1 parent cdb3109 commit 1e3a68d

2 files changed

Lines changed: 31 additions & 32 deletions

File tree

Lib/modulefinder.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Find modules used by a script, using introspection."""
22

3-
from __future__ import generators
43
import dis
54
import imp
65
import marshal
@@ -9,8 +8,6 @@
98
import types
109
import struct
1110

12-
READ_MODE = "rU"
13-
1411
# XXX Clean up once str8's cstor matches bytes.
1512
LOAD_CONST = bytes([dis.opname.index('LOAD_CONST')])
1613
IMPORT_NAME = bytes([dis.opname.index('IMPORT_NAME')])
@@ -29,8 +26,7 @@
2926

3027
# A Public interface
3128
def AddPackagePath(packagename, path):
32-
paths = packagePathMap.get(packagename, [])
33-
paths.append(path)
29+
paths = packagePathMap.setdefault(packagename, []).append(path)
3430
packagePathMap[packagename] = paths
3531

3632
replacePackageMap = {}
@@ -106,14 +102,14 @@ def msgout(self, *args):
106102

107103
def run_script(self, pathname):
108104
self.msg(2, "run_script", pathname)
109-
with open(pathname, READ_MODE) as fp:
105+
with open(pathname) as fp:
110106
stuff = ("", "r", imp.PY_SOURCE)
111107
self.load_module('__main__', fp, pathname, stuff)
112108

113109
def load_file(self, pathname):
114110
dir, name = os.path.split(pathname)
115111
name, ext = os.path.splitext(name)
116-
with open(pathname, READ_MODE) as fp:
112+
with open(pathname) as fp:
117113
stuff = (ext, "r", imp.PY_SOURCE)
118114
self.load_module(name, fp, pathname, stuff)
119115

@@ -270,7 +266,8 @@ def import_module(self, partname, fqname, parent):
270266
try:
271267
m = self.load_module(fqname, fp, pathname, stuff)
272268
finally:
273-
if fp: fp.close()
269+
if fp:
270+
fp.close()
274271
if parent:
275272
setattr(parent, partname, m)
276273
self.msgout(3, "import_module ->", m)
@@ -662,4 +659,4 @@ def test():
662659
try:
663660
mf = test()
664661
except KeyboardInterrupt:
665-
print("\n[interrupt]")
662+
print("\n[interrupted]")

Lib/test/test_modulefinder.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import __future__
21
import os
2+
import errno
3+
import shutil
34
import unittest
4-
import distutils.dir_util
55
import tempfile
66

77
from test import support
88

99
import modulefinder
1010

1111
TEST_DIR = tempfile.mkdtemp()
12-
TEST_PATH = [TEST_DIR, os.path.dirname(__future__.__file__)]
12+
TEST_PATH = [TEST_DIR, os.path.dirname(tempfile.__file__)]
1313

1414
# Each test description is a list of 5 items:
1515
#
@@ -196,12 +196,17 @@ def foo(): pass
196196
from . import bar
197197
"""]
198198

199+
199200
def open_file(path):
200-
##print "#", os.path.abspath(path)
201201
dirname = os.path.dirname(path)
202-
distutils.dir_util.mkpath(dirname)
202+
try:
203+
os.makedirs(dirname)
204+
except OSError as e:
205+
if e.errno != errno.EEXIST:
206+
raise
203207
return open(path, "w")
204208

209+
205210
def create_package(source):
206211
ofi = None
207212
try:
@@ -216,6 +221,7 @@ def create_package(source):
216221
if ofi:
217222
ofi.close()
218223

224+
219225
class ModuleFinderTest(unittest.TestCase):
220226
def _do_test(self, info, report=False):
221227
import_this, modules, missing, maybe_missing, source = info
@@ -234,45 +240,41 @@ def _do_test(self, info, report=False):
234240
## import traceback; traceback.print_exc()
235241
## sys.path = opath
236242
## return
237-
modules = set(modules)
238-
found = set(mf.modules.keys())
239-
more = list(found - modules)
240-
less = list(modules - found)
243+
modules = sorted(set(modules))
244+
found = sorted(mf.modules)
241245
# check if we found what we expected, not more, not less
242-
self.assertEqual((more, less), ([], []))
246+
self.assertEqual(found, modules)
243247

244248
# check for missing and maybe missing modules
245249
bad, maybe = mf.any_missing_maybe()
246250
self.assertEqual(bad, missing)
247251
self.assertEqual(maybe, maybe_missing)
248252
finally:
249-
distutils.dir_util.remove_tree(TEST_DIR)
253+
shutil.rmtree(TEST_DIR)
250254

251255
def test_package(self):
252256
self._do_test(package_test)
253257

254258
def test_maybe(self):
255259
self._do_test(maybe_test)
256260

257-
if getattr(__future__, "absolute_import", None):
261+
def test_maybe_new(self):
262+
self._do_test(maybe_test_new)
258263

259-
def test_maybe_new(self):
260-
self._do_test(maybe_test_new)
264+
def test_absolute_imports(self):
265+
self._do_test(absolute_import_test)
261266

262-
def test_absolute_imports(self):
263-
self._do_test(absolute_import_test)
267+
def test_relative_imports(self):
268+
self._do_test(relative_import_test)
264269

265-
def test_relative_imports(self):
266-
self._do_test(relative_import_test)
270+
def test_relative_imports_2(self):
271+
self._do_test(relative_import_test_2)
267272

268-
def test_relative_imports_2(self):
269-
self._do_test(relative_import_test_2)
273+
def test_relative_imports_3(self):
274+
self._do_test(relative_import_test_3)
270275

271-
def test_relative_imports_3(self):
272-
self._do_test(relative_import_test_3)
273276

274277
def test_main():
275-
distutils.log.set_threshold(distutils.log.WARN)
276278
support.run_unittest(ModuleFinderTest)
277279

278280
if __name__ == "__main__":

0 commit comments

Comments
 (0)