Skip to content

Commit 0ed66df

Browse files
authored
bpo-33537: Add an __all__ to importlib.resources (#6920)
1 parent dff4675 commit 0ed66df

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

Lib/importlib/resources.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import tempfile
33

44
from . import abc as resources_abc
5-
from builtins import open as builtins_open
65
from contextlib import contextmanager, suppress
76
from importlib import import_module
87
from importlib.abc import ResourceLoader
@@ -15,6 +14,19 @@
1514
from zipimport import ZipImportError
1615

1716

17+
__all__ = [
18+
'Package',
19+
'Resource',
20+
'contents',
21+
'is_resource',
22+
'open_binary',
23+
'open_text',
24+
'path',
25+
'read_binary',
26+
'read_text',
27+
]
28+
29+
1830
Package = Union[str, ModuleType]
1931
Resource = Union[str, os.PathLike]
2032

@@ -82,7 +94,7 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO:
8294
package_path = os.path.dirname(absolute_package_path)
8395
full_path = os.path.join(package_path, resource)
8496
try:
85-
return builtins_open(full_path, mode='rb')
97+
return open(full_path, mode='rb')
8698
except OSError:
8799
# Just assume the loader is a resource loader; all the relevant
88100
# importlib.machinery loaders are and an AttributeError for
@@ -116,8 +128,7 @@ def open_text(package: Package,
116128
package_path = os.path.dirname(absolute_package_path)
117129
full_path = os.path.join(package_path, resource)
118130
try:
119-
return builtins_open(
120-
full_path, mode='r', encoding=encoding, errors=errors)
131+
return open(full_path, mode='r', encoding=encoding, errors=errors)
121132
except OSError:
122133
# Just assume the loader is a resource loader; all the relevant
123134
# importlib.machinery loaders are and an AttributeError for
@@ -248,10 +259,10 @@ def contents(package: Package) -> Iterable[str]:
248259
return os.listdir(package_directory)
249260

250261

251-
# Private implementation of ResourceReader and get_resource_reader() for
252-
# zipimport. Don't use these directly! We're implementing these in Python
253-
# because 1) it's easier, 2) zipimport will likely get rewritten in Python
254-
# itself at some point, so doing this all in C would just be a waste of
262+
# Private implementation of ResourceReader and get_resource_reader() called
263+
# from zipimport.c. Don't use these directly! We're implementing these in
264+
# Python because 1) it's easier, 2) zipimport may get rewritten in Python
265+
# itself at some point, so doing this all in C would difficult and a waste of
255266
# effort.
256267

257268
class _ZipImportResourceReader(resources_abc.ResourceReader):
@@ -322,6 +333,7 @@ def contents(self):
322333
yield parent_name
323334

324335

336+
# Called from zipimport.c
325337
def _zipimport_get_resource_reader(zipimporter, fullname):
326338
try:
327339
if not zipimporter.is_package(fullname):

0 commit comments

Comments
 (0)