|
2 | 2 | import tempfile
|
3 | 3 |
|
4 | 4 | from . import abc as resources_abc
|
5 |
| -from builtins import open as builtins_open |
6 | 5 | from contextlib import contextmanager, suppress
|
7 | 6 | from importlib import import_module
|
8 | 7 | from importlib.abc import ResourceLoader
|
|
15 | 14 | from zipimport import ZipImportError
|
16 | 15 |
|
17 | 16 |
|
| 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 | + |
18 | 30 | Package = Union[str, ModuleType]
|
19 | 31 | Resource = Union[str, os.PathLike]
|
20 | 32 |
|
@@ -82,7 +94,7 @@ def open_binary(package: Package, resource: Resource) -> BinaryIO:
|
82 | 94 | package_path = os.path.dirname(absolute_package_path)
|
83 | 95 | full_path = os.path.join(package_path, resource)
|
84 | 96 | try:
|
85 |
| - return builtins_open(full_path, mode='rb') |
| 97 | + return open(full_path, mode='rb') |
86 | 98 | except OSError:
|
87 | 99 | # Just assume the loader is a resource loader; all the relevant
|
88 | 100 | # importlib.machinery loaders are and an AttributeError for
|
@@ -116,8 +128,7 @@ def open_text(package: Package,
|
116 | 128 | package_path = os.path.dirname(absolute_package_path)
|
117 | 129 | full_path = os.path.join(package_path, resource)
|
118 | 130 | 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) |
121 | 132 | except OSError:
|
122 | 133 | # Just assume the loader is a resource loader; all the relevant
|
123 | 134 | # importlib.machinery loaders are and an AttributeError for
|
@@ -248,10 +259,10 @@ def contents(package: Package) -> Iterable[str]:
|
248 | 259 | return os.listdir(package_directory)
|
249 | 260 |
|
250 | 261 |
|
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 |
255 | 266 | # effort.
|
256 | 267 |
|
257 | 268 | class _ZipImportResourceReader(resources_abc.ResourceReader):
|
@@ -322,6 +333,7 @@ def contents(self):
|
322 | 333 | yield parent_name
|
323 | 334 |
|
324 | 335 |
|
| 336 | +# Called from zipimport.c |
325 | 337 | def _zipimport_get_resource_reader(zipimporter, fullname):
|
326 | 338 | try:
|
327 | 339 | if not zipimporter.is_package(fullname):
|
|
0 commit comments