mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-29 03:04:38 +08:00
* Refresh canonical importlib and preserve native import callback Cache the original builtin __import__ callback so CPython 3.15 importlib's hierarchical locking uses the existing native parent-initialization path. Refresh the complete canonical importlib and related test/helper families from CPython d220409a13b77de7a6c30abe16ea90db01528edc through update_lib. Preserve both nondecorator importlib guards in the updater. Add explicit hash-pinned removal of reviewed obsolete canonical files, with full preflight, dry-run, idempotence and refusal tests. Preserve empty fixtures. Assisted-by: OpenAI Codex:model version unavailable * Complete canonical inspect and types closure for importlib metadata Refresh whole canonical inspect/types production and related test families through update_lib after hosted CI exposed stale __cached__ consumers. Add coroutine and async-generator state getters, with closed-state priority for reentrant frame-local finalizers, and expose the existing frame-locals proxy type with its CPython class documentation. Preserve the optional _datetime and unittest import blocks through a narrow migration adapter. Retain canonical assertions and existing guards; record the missing LazyImportType expectation before adding a conditional expected failure, and capability-skip only the two absent-_testcapi CLI fixtures. Assisted-by: OpenAI Codex:model version unavailable * Reject canonical removal junctions and read UTF-8 manifests Reject junctions alongside symlinks in the existing path-component preflight and revalidation. Decode removal manifests explicitly as UTF-8. Add regressions for whole-batch refusal, revalidation, and Unicode dry-run/apply/idempotence. Assisted-by: OpenAI Codex:model version unavailable
85 lines
2.7 KiB
Python
Vendored
85 lines
2.7 KiB
Python
Vendored
"""Simplified function-based API for importlib.resources"""
|
|
|
|
import warnings
|
|
|
|
from ._common import as_file, files
|
|
from .abc import TraversalError
|
|
|
|
_MISSING = object()
|
|
|
|
|
|
def open_binary(anchor, *path_names):
|
|
"""Open for binary reading the *resource* within *package*."""
|
|
return _get_resource(anchor, path_names).open('rb')
|
|
|
|
|
|
def open_text(anchor, *path_names, encoding=_MISSING, errors='strict'):
|
|
"""Open for text reading the *resource* within *package*."""
|
|
encoding = _get_encoding_arg(path_names, encoding)
|
|
resource = _get_resource(anchor, path_names)
|
|
return resource.open('r', encoding=encoding, errors=errors)
|
|
|
|
|
|
def read_binary(anchor, *path_names):
|
|
"""Read and return contents of *resource* within *package* as bytes."""
|
|
return _get_resource(anchor, path_names).read_bytes()
|
|
|
|
|
|
def read_text(anchor, *path_names, encoding=_MISSING, errors='strict'):
|
|
"""Read and return contents of *resource* within *package* as str."""
|
|
encoding = _get_encoding_arg(path_names, encoding)
|
|
resource = _get_resource(anchor, path_names)
|
|
return resource.read_text(encoding=encoding, errors=errors)
|
|
|
|
|
|
def path(anchor, *path_names):
|
|
"""Return the path to the *resource* as an actual file system path."""
|
|
return as_file(_get_resource(anchor, path_names))
|
|
|
|
|
|
def is_resource(anchor, *path_names):
|
|
"""Return ``True`` if there is a resource named *name* in the package,
|
|
|
|
Otherwise returns ``False``.
|
|
"""
|
|
try:
|
|
return _get_resource(anchor, path_names).is_file()
|
|
except TraversalError:
|
|
return False
|
|
|
|
|
|
def contents(anchor, *path_names):
|
|
"""Return an iterable over the named resources within the package.
|
|
|
|
The iterable returns :class:`str` resources (e.g. files).
|
|
The iterable does not recurse into subdirectories.
|
|
"""
|
|
warnings.warn(
|
|
"importlib.resources.contents is deprecated. "
|
|
"Use files(anchor).iterdir() instead.",
|
|
DeprecationWarning,
|
|
stacklevel=1,
|
|
)
|
|
return (resource.name for resource in _get_resource(anchor, path_names).iterdir())
|
|
|
|
|
|
def _get_encoding_arg(path_names, encoding):
|
|
# For compatibility with versions where *encoding* was a positional
|
|
# argument, it needs to be given explicitly when there are multiple
|
|
# *path_names*.
|
|
# This limitation can be removed in Python 3.15.
|
|
if encoding is _MISSING:
|
|
if len(path_names) > 1:
|
|
raise TypeError(
|
|
"'encoding' argument required with multiple path names",
|
|
)
|
|
else:
|
|
return 'utf-8'
|
|
return encoding
|
|
|
|
|
|
def _get_resource(anchor, path_names):
|
|
if anchor is None:
|
|
raise TypeError("anchor must be module or string, got None")
|
|
return files(anchor).joinpath(*path_names)
|