Skip to content

Commit

Permalink
Rely on importlib.resources for loading resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Feb 2, 2025
1 parent 8d87464 commit 16f7b32
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _test():

import __future__
import difflib
import importlib.resources
import inspect
import linecache
import os
Expand Down Expand Up @@ -251,18 +252,12 @@ def _package_load(filename, package, encoding):
"""
module = _normalize_module(package, 4)
path = _module_relative_path(module, filename)
if (loader := getattr(module, '__loader__', None)) is None:
try:
loader = module.__spec__.loader
except AttributeError:
pass
if hasattr(loader, 'get_data'):
file_contents = loader.get_data(path)
file_contents = file_contents.decode(encoding)
# get_data() opens files as 'rb', so one must do the equivalent
# conversion as universal newlines would do.
return _newline_convert(file_contents), path
return _fs_load(path, package=None, encoding=encoding)
try:
return importlib.resources.read_text(module, path, encoding=encoding), path
except AttributeError:
# importlib.resources raises AttributeError when module.__spec__ is None
# fallback to file system loader
return _fs_load(path, package=None, encoding=encoding)


def _fs_load(filename, package, encoding):
Expand Down

0 comments on commit 16f7b32

Please sign in to comment.