diff options
| author | Brett Cannon <brett@python.org> | 2020-11-07 02:45:56 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-07 02:45:56 (GMT) |
| commit | 825ac383327255d38b69a753e5e41710bb3ed010 (patch) | |
| tree | 86543aba40795918c174dbb899528e339c619208 /Lib/doctest.py | |
| parent | 7c01f1540f958d4f52188b28afca721a9a6925c3 (diff) | |
| download | cpython-825ac383327255d38b69a753e5e41710bb3ed010.zip cpython-825ac383327255d38b69a753e5e41710bb3ed010.tar.gz cpython-825ac383327255d38b69a753e5e41710bb3ed010.tar.bz2 | |
bpo-42133: update parts of the stdlib to fall back to `__spec__.loader` when `__loader__` is missing (#22929)
Diffstat (limited to 'Lib/doctest.py')
| -rw-r--r-- | Lib/doctest.py | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index baa503c..5bb35c9 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -222,13 +222,17 @@ def _load_testfile(filename, package, module_relative, encoding): if module_relative: package = _normalize_module(package, 3) filename = _module_relative_path(package, filename) - if getattr(package, '__loader__', None) is not None: - if hasattr(package.__loader__, 'get_data'): - file_contents = package.__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 + 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 |
