[3.13] gh-116608: Apply style and compatibility changes from importlib_resources. (GH-123028) (#123051)

gh-116608: Apply style and compatibility changes from importlib_metadata. (GH-123028)
(cherry picked from commit e913d2c87f)

Co-authored-by: Jason R. Coombs <jaraco@jaraco.com>
This commit is contained in:
Miss Islington (bot) 2024-08-15 21:57:07 +02:00 committed by GitHub
parent 009f9efe65
commit 5ac14eeea6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 36 deletions

View file

@ -57,11 +57,7 @@ def contents(anchor, *path_names):
DeprecationWarning, DeprecationWarning,
stacklevel=1, stacklevel=1,
) )
return ( return (resource.name for resource in _get_resource(anchor, path_names).iterdir())
resource.name
for resource
in _get_resource(anchor, path_names).iterdir()
)
def _get_encoding_arg(path_names, encoding): def _get_encoding_arg(path_names, encoding):

View file

@ -1,9 +1,9 @@
import unittest import unittest
import os import os
from test.support.warnings_helper import ignore_warnings, check_warnings from test.support import warnings_helper
import importlib.resources as resources from importlib import resources
# Since the functional API forwards to Traversable, we only test # Since the functional API forwards to Traversable, we only test
# filesystem resources here -- not zip files, namespace packages etc. # filesystem resources here -- not zip files, namespace packages etc.
@ -22,8 +22,7 @@ class ModuleAnchorMixin:
class FunctionalAPIBase: class FunctionalAPIBase:
def _gen_resourcetxt_path_parts(self): def _gen_resourcetxt_path_parts(self):
"""Yield various names of a text file in anchor02, each in a subTest """Yield various names of a text file in anchor02, each in a subTest"""
"""
for path_parts in ( for path_parts in (
('subdirectory', 'subsubdir', 'resource.txt'), ('subdirectory', 'subsubdir', 'resource.txt'),
('subdirectory/subsubdir/resource.txt',), ('subdirectory/subsubdir/resource.txt',),
@ -36,7 +35,7 @@ class FunctionalAPIBase:
"""Assert that `string` ends with `suffix`. """Assert that `string` ends with `suffix`.
Used to ignore an architecture-specific UTF-16 byte-order mark.""" Used to ignore an architecture-specific UTF-16 byte-order mark."""
self.assertEqual(string[-len(suffix):], suffix) self.assertEqual(string[-len(suffix) :], suffix)
def test_read_text(self): def test_read_text(self):
self.assertEqual( self.assertEqual(
@ -45,7 +44,10 @@ class FunctionalAPIBase:
) )
self.assertEqual( self.assertEqual(
resources.read_text( resources.read_text(
self.anchor02, 'subdirectory', 'subsubdir', 'resource.txt', self.anchor02,
'subdirectory',
'subsubdir',
'resource.txt',
encoding='utf-8', encoding='utf-8',
), ),
'a resource', 'a resource',
@ -53,7 +55,9 @@ class FunctionalAPIBase:
for path_parts in self._gen_resourcetxt_path_parts(): for path_parts in self._gen_resourcetxt_path_parts():
self.assertEqual( self.assertEqual(
resources.read_text( resources.read_text(
self.anchor02, *path_parts, encoding='utf-8', self.anchor02,
*path_parts,
encoding='utf-8',
), ),
'a resource', 'a resource',
) )
@ -67,13 +71,16 @@ class FunctionalAPIBase:
resources.read_text(self.anchor01, 'utf-16.file') resources.read_text(self.anchor01, 'utf-16.file')
self.assertEqual( self.assertEqual(
resources.read_text( resources.read_text(
self.anchor01, 'binary.file', encoding='latin1', self.anchor01,
'binary.file',
encoding='latin1',
), ),
'\x00\x01\x02\x03', '\x00\x01\x02\x03',
) )
self.assertEndsWith( # ignore the BOM self.assertEndsWith( # ignore the BOM
resources.read_text( resources.read_text(
self.anchor01, 'utf-16.file', self.anchor01,
'utf-16.file',
errors='backslashreplace', errors='backslashreplace',
), ),
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode( 'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
@ -97,7 +104,8 @@ class FunctionalAPIBase:
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n') self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
for path_parts in self._gen_resourcetxt_path_parts(): for path_parts in self._gen_resourcetxt_path_parts():
with resources.open_text( with resources.open_text(
self.anchor02, *path_parts, self.anchor02,
*path_parts,
encoding='utf-8', encoding='utf-8',
) as f: ) as f:
self.assertEqual(f.read(), 'a resource') self.assertEqual(f.read(), 'a resource')
@ -111,11 +119,14 @@ class FunctionalAPIBase:
with self.assertRaises(UnicodeDecodeError): with self.assertRaises(UnicodeDecodeError):
f.read() f.read()
with resources.open_text( with resources.open_text(
self.anchor01, 'binary.file', encoding='latin1', self.anchor01,
'binary.file',
encoding='latin1',
) as f: ) as f:
self.assertEqual(f.read(), '\x00\x01\x02\x03') self.assertEqual(f.read(), '\x00\x01\x02\x03')
with resources.open_text( with resources.open_text(
self.anchor01, 'utf-16.file', self.anchor01,
'utf-16.file',
errors='backslashreplace', errors='backslashreplace',
) as f: ) as f:
self.assertEndsWith( # ignore the BOM self.assertEndsWith( # ignore the BOM
@ -130,16 +141,17 @@ class FunctionalAPIBase:
self.assertEqual(f.read(), b'Hello, UTF-8 world!\n') self.assertEqual(f.read(), b'Hello, UTF-8 world!\n')
for path_parts in self._gen_resourcetxt_path_parts(): for path_parts in self._gen_resourcetxt_path_parts():
with resources.open_binary( with resources.open_binary(
self.anchor02, *path_parts, self.anchor02,
*path_parts,
) as f: ) as f:
self.assertEqual(f.read(), b'a resource') self.assertEqual(f.read(), b'a resource')
def test_path(self): def test_path(self):
with resources.path(self.anchor01, 'utf-8.file') as path: with resources.path(self.anchor01, 'utf-8.file') as path:
with open(str(path)) as f: with open(str(path), encoding='utf-8') as f:
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n') self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
with resources.path(self.anchor01) as path: with resources.path(self.anchor01) as path:
with open(os.path.join(path, 'utf-8.file')) as f: with open(os.path.join(path, 'utf-8.file'), encoding='utf-8') as f:
self.assertEqual(f.read(), 'Hello, UTF-8 world!\n') self.assertEqual(f.read(), 'Hello, UTF-8 world!\n')
def test_is_resource(self): def test_is_resource(self):
@ -152,32 +164,32 @@ class FunctionalAPIBase:
self.assertTrue(is_resource(self.anchor02, *path_parts)) self.assertTrue(is_resource(self.anchor02, *path_parts))
def test_contents(self): def test_contents(self):
is_resource = resources.is_resource with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)):
with check_warnings((".*contents.*", DeprecationWarning)):
c = resources.contents(self.anchor01) c = resources.contents(self.anchor01)
self.assertGreaterEqual( self.assertGreaterEqual(
set(c), set(c),
{'utf-8.file', 'utf-16.file', 'binary.file', 'subdirectory'}, {'utf-8.file', 'utf-16.file', 'binary.file', 'subdirectory'},
) )
with ( with self.assertRaises(OSError), warnings_helper.check_warnings((
self.assertRaises(OSError), ".*contents.*",
check_warnings((".*contents.*", DeprecationWarning)), DeprecationWarning,
): )):
list(resources.contents(self.anchor01, 'utf-8.file')) list(resources.contents(self.anchor01, 'utf-8.file'))
for path_parts in self._gen_resourcetxt_path_parts(): for path_parts in self._gen_resourcetxt_path_parts():
with ( with self.assertRaises(OSError), warnings_helper.check_warnings((
self.assertRaises(OSError), ".*contents.*",
check_warnings((".*contents.*", DeprecationWarning)), DeprecationWarning,
): )):
list(resources.contents(self.anchor01, *path_parts)) list(resources.contents(self.anchor01, *path_parts))
with check_warnings((".*contents.*", DeprecationWarning)): with warnings_helper.check_warnings((".*contents.*", DeprecationWarning)):
c = resources.contents(self.anchor01, 'subdirectory') c = resources.contents(self.anchor01, 'subdirectory')
self.assertGreaterEqual( self.assertGreaterEqual(
set(c), set(c),
{'binary.file'}, {'binary.file'},
) )
@ignore_warnings(category=DeprecationWarning) @warnings_helper.ignore_warnings(category=DeprecationWarning)
def test_common_errors(self): def test_common_errors(self):
for func in ( for func in (
resources.read_text, resources.read_text,
@ -208,18 +220,24 @@ class FunctionalAPIBase:
# Multiple path arguments need explicit encoding argument. # Multiple path arguments need explicit encoding argument.
with self.assertRaises(TypeError): with self.assertRaises(TypeError):
func( func(
self.anchor02, 'subdirectory', self.anchor02,
'subsubdir', 'resource.txt', 'subdirectory',
'subsubdir',
'resource.txt',
) )
class FunctionalAPITest_StringAnchor( class FunctionalAPITest_StringAnchor(
unittest.TestCase, FunctionalAPIBase, StringAnchorMixin, unittest.TestCase,
FunctionalAPIBase,
StringAnchorMixin,
): ):
pass pass
class FunctionalAPITest_ModuleAnchor( class FunctionalAPITest_ModuleAnchor(
unittest.TestCase, FunctionalAPIBase, ModuleAnchorMixin, unittest.TestCase,
FunctionalAPIBase,
ModuleAnchorMixin,
): ):
pass pass