Skip to content

Commit 24c06b8

Browse files
committed
Apply suggestion
1 parent 0621551 commit 24c06b8

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

Lib/doctest.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def _test():
9393
]
9494

9595
import __future__
96-
import contextlib
9796
import difflib
97+
import importlib.resources
9898
import inspect
9999
import linecache
100100
import os
@@ -103,9 +103,8 @@ def _test():
103103
import sys
104104
import traceback
105105
import unittest
106-
import importlib.resources
107-
from io import StringIO
108106
from collections import namedtuple
107+
from io import StringIO
109108
import _colorize # Used in doctests
110109
from _colorize import ANSIColors, can_colorize
111110

@@ -238,17 +237,20 @@ def _normalize_module(module, depth=2):
238237
raise TypeError("Expected a module, string, or None")
239238

240239
def _load_testfile(filename, package, module_relative, encoding):
240+
text = None
241241
if module_relative:
242-
package = _normalize_module(package, 3)
243-
with contextlib.suppress(Exception):
244-
text = importlib.resources.read_text(package, filename,
245-
encoding=encoding)
246-
return text, filename
242+
package = _normalize_module(package, depth=3)
243+
try:
244+
file = importlib.resources.files(package) / filename
245+
text = file.read_text(encoding=encoding)
246+
except AttributeError:
247+
filename = _module_relative_path(package, filename)
247248

248-
filename = _module_relative_path(package, filename)
249+
if text is None:
250+
with open(filename, encoding=encoding) as f:
251+
text = f.read()
249252

250-
with open(filename, encoding=encoding) as f:
251-
return f.read(), filename
253+
return text, filename
252254

253255
def _indent(s, indent=4):
254256
"""

0 commit comments

Comments
 (0)