summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉric Araujo <merwok@netwok.org>2011-07-28 23:35:29 +0200
committerÉric Araujo <merwok@netwok.org>2011-07-28 23:35:29 +0200
commit1e3a68d36b08cd9d59084a37c8cb6c2d911868ce (patch)
tree019958677ee53cc3474f15a79942b0e708ce5996 /Lib/modulefinder.py
parentFix reST role and missing word (diff)
downloadcpython-1e3a68d36b08cd9d59084a37c8cb6c2d911868ce.tar.gz
cpython-1e3a68d36b08cd9d59084a37c8cb6c2d911868ce.tar.bz2
cpython-1e3a68d36b08cd9d59084a37c8cb6c2d911868ce.zip
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.
Diffstat (limited to 'Lib/modulefinder.py')
-rw-r--r--Lib/modulefinder.py15
1 files changed, 6 insertions, 9 deletions
diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py
index f033ba98efb..da5b56d84c7 100644
--- a/Lib/modulefinder.py
+++ b/Lib/modulefinder.py
@@ -1,6 +1,5 @@
"""Find modules used by a script, using introspection."""
-from __future__ import generators
import dis
import imp
import marshal
@@ -9,8 +8,6 @@ import sys
import types
import struct
-READ_MODE = "rU"
-
# XXX Clean up once str8's cstor matches bytes.
LOAD_CONST = bytes([dis.opname.index('LOAD_CONST')])
IMPORT_NAME = bytes([dis.opname.index('IMPORT_NAME')])
@@ -29,8 +26,7 @@ packagePathMap = {}
# A Public interface
def AddPackagePath(packagename, path):
- paths = packagePathMap.get(packagename, [])
- paths.append(path)
+ paths = packagePathMap.setdefault(packagename, []).append(path)
packagePathMap[packagename] = paths
replacePackageMap = {}
@@ -106,14 +102,14 @@ class ModuleFinder:
def run_script(self, pathname):
self.msg(2, "run_script", pathname)
- with open(pathname, READ_MODE) as fp:
+ with open(pathname) as fp:
stuff = ("", "r", imp.PY_SOURCE)
self.load_module('__main__', fp, pathname, stuff)
def load_file(self, pathname):
dir, name = os.path.split(pathname)
name, ext = os.path.splitext(name)
- with open(pathname, READ_MODE) as fp:
+ with open(pathname) as fp:
stuff = (ext, "r", imp.PY_SOURCE)
self.load_module(name, fp, pathname, stuff)
@@ -270,7 +266,8 @@ class ModuleFinder:
try:
m = self.load_module(fqname, fp, pathname, stuff)
finally:
- if fp: fp.close()
+ if fp:
+ fp.close()
if parent:
setattr(parent, partname, m)
self.msgout(3, "import_module ->", m)
@@ -662,4 +659,4 @@ if __name__ == '__main__':
try:
mf = test()
except KeyboardInterrupt:
- print("\n[interrupt]")
+ print("\n[interrupted]")