[Python-checkins] bpo-33537: Add an __all__ to importlib.resources (GH-6920) (#6941)
Barry Warsaw
webhook-mailer at python.org
Thu May 17 12:27:52 EDT 2018
https://github.com/python/cpython/commit/6417d33633a3979d996015e52e4ff6c7a88e93e5
commit: 6417d33633a3979d996015e52e4ff6c7a88e93e5
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Barry Warsaw <barry at python.org>
date: 2018年05月17日T12:27:49-04:00
summary:
bpo-33537: Add an __all__ to importlib.resources (GH-6920) (#6941)
(cherry picked from commit 0ed66df5242138fc599b4735749e55f953d9a1e4)
Co-authored-by: Barry Warsaw <barry at python.org>
files:
M Lib/importlib/resources.py
diff --git a/Lib/importlib/resources.py b/Lib/importlib/resources.py
index 4d70186c1a43..cbefdd540e9e 100644
--- a/Lib/importlib/resources.py
+++ b/Lib/importlib/resources.py
@@ -2,7 +2,6 @@
import tempfile
from . import abc as resources_abc
-from builtins import open as builtins_open
from contextlib import contextmanager, suppress
from importlib import import_module
from importlib.abc import ResourceLoader
@@ -15,6 +14,19 @@
from zipimport import ZipImportError
+__all__ = [
+ 'Package',
+ 'Resource',
+ 'contents',
+ 'is_resource',
+ 'open_binary',
+ 'open_text',
+ 'path',
+ 'read_binary',
+ 'read_text',
+ ]
+
+
Package = Union[str, ModuleType]
Resource = Union[str, os.PathLike]
@@ -82,7 +94,7 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO:
package_path = os.path.dirname(absolute_package_path)
full_path = os.path.join(package_path, resource)
try:
- return builtins_open(full_path, mode='rb')
+ return open(full_path, mode='rb')
except OSError:
# Just assume the loader is a resource loader; all the relevant
# importlib.machinery loaders are and an AttributeError for
@@ -116,8 +128,7 @@ def open_text(package: Package,
package_path = os.path.dirname(absolute_package_path)
full_path = os.path.join(package_path, resource)
try:
- return builtins_open(
- full_path, mode='r', encoding=encoding, errors=errors)
+ return open(full_path, mode='r', encoding=encoding, errors=errors)
except OSError:
# Just assume the loader is a resource loader; all the relevant
# importlib.machinery loaders are and an AttributeError for
@@ -248,10 +259,10 @@ def contents(package: Package) -> Iterable[str]:
return os.listdir(package_directory)
-# Private implementation of ResourceReader and get_resource_reader() for
-# zipimport. Don't use these directly! We're implementing these in Python
-# because 1) it's easier, 2) zipimport will likely get rewritten in Python
-# itself at some point, so doing this all in C would just be a waste of
+# Private implementation of ResourceReader and get_resource_reader() called
+# from zipimport.c. Don't use these directly! We're implementing these in
+# Python because 1) it's easier, 2) zipimport may get rewritten in Python
+# itself at some point, so doing this all in C would difficult and a waste of
# effort.
class _ZipImportResourceReader(resources_abc.ResourceReader):
@@ -322,6 +333,7 @@ def contents(self):
yield parent_name
+# Called from zipimport.c
def _zipimport_get_resource_reader(zipimporter, fullname):
try:
if not zipimporter.is_package(fullname):
More information about the Python-checkins
mailing list