diff options
Diffstat (limited to 'Lib/pkgutil.py')
-rw-r--r-- | Lib/pkgutil.py | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index e2d630ab2b..51da0b1bb5 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -191,8 +191,11 @@ class ImpImporter: yielded = {} import inspect - - filenames = os.listdir(self.path) + try: + filenames = os.listdir(self.path) + except OSError: + # ignore unreadable directories like import does + filenames = [] filenames.sort() # handle packages before same-named modules for fn in filenames: @@ -205,7 +208,12 @@ class ImpImporter: if not modname and os.path.isdir(path) and '.' not in fn: modname = fn - for fn in os.listdir(path): + try: + dircontents = os.listdir(path) + except OSError: + # ignore unreadable directories like import does + dircontents = [] + for fn in dircontents: subname = inspect.getmodulename(fn) if subname=='__init__': ispkg = True @@ -241,7 +249,8 @@ class ImpLoader: return mod def get_data(self, pathname): - return open(pathname, "rb").read() + with open(pathname, "rb") as file: + return file.read() def _reopen(self): if self.file and self.file.closed: |