Skip to content

gh-128862: use importlib.resources to acquire doctest resources #128865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 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 All @@ -102,8 +103,8 @@ def _test():
import sys
import traceback
import unittest
from io import StringIO, IncrementalNewlineDecoder
from collections import namedtuple
from io import StringIO
import _colorize # Used in doctests
from _colorize import ANSIColors, can_colorize

Expand Down Expand Up @@ -235,27 +236,21 @@ def _normalize_module(module, depth=2):
else:
raise TypeError("Expected a module, string, or None")

def _newline_convert(data):
# The IO module provides a handy decoder for universal newline conversion
return IncrementalNewlineDecoder(None, True).decode(data, True)

def _load_testfile(filename, package, module_relative, encoding):
text = None
if module_relative:
package = _normalize_module(package, 3)
filename = _module_relative_path(package, filename)
if (loader := getattr(package, '__loader__', None)) is None:
try:
loader = package.__spec__.loader
except AttributeError:
pass
if hasattr(loader, 'get_data'):
file_contents = loader.get_data(filename)
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), filename
with open(filename, encoding=encoding) as f:
return f.read(), filename
package = _normalize_module(package, depth=3)
try:
file = importlib.resources.files(package) / filename
text = file.read_text(encoding=encoding)
except AttributeError:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it raised by importlib.resources.files() or by file.read_text()? Can text be None?

filename = _module_relative_path(package, filename)

if text is None:
with open(filename, encoding=encoding) as f:
text = f.read()

return text, filename

def _indent(s, indent=4):
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use ``importlib.resources`` to acquire test files in ``doctest``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Use ``importlib.resources`` to acquire test files in ``doctest``
Use :mod:`importlib.resources` to acquire test files in :mod:`doctest`.

Loading