Skip to content

Commit

Permalink
pythongh-121735: Allow to list package resources inside zip by module…
Browse files Browse the repository at this point in the history
… name

which is inside that package
  • Loading branch information
Gatsik committed Aug 16, 2024
1 parent 50c1596 commit e5a32a7
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Lib/importlib/resources/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def files(self):
class ZipReader(abc.TraversableResources):
def __init__(self, loader, module):
_, _, name = module.rpartition('.')
self.prefix = loader.prefix.replace('\\', '/') + name + '/'
self.prefix = loader.prefix.replace('\\', '/') + name
if not self.prefix.endswith('/'):
self.prefix += '/'
self.archive = loader.archive

def open_resource(self, resource):
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data
21 changes: 21 additions & 0 deletions Lib/test/test_importlib/resources/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ def test_unrelated_contents(self):
)


class ResourceFromZipsTest04(util.ZipSetupBase, unittest.TestCase):
ZIP_MODULE = 'data04'

def test_package_resources(self):
self.assertEqual(
names(resources.files('data04.package')),
{'__init__.py', 'module.py', 'resource.txt'},
)

def test_resources_of_module_inside_package(self):
"""
A module inside a package can have resources found adjacent to the module.
"""
package_files = resources.files('data04.package.module')
self.assertEqual(
names(package_files),
{'__init__.py', 'module.py', 'resource.txt'},
)
self.assertEqual(package_files.joinpath('resource.txt').read_text(), 'data\n')


class DeletingZipsTest(util.ZipSetupBase, unittest.TestCase):
"""Having accessed resources in a zip file should not keep an open
reference to the zip.
Expand Down
14 changes: 9 additions & 5 deletions Lib/zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,22 @@ def load_module(self, fullname):


def get_resource_reader(self, fullname):
"""Return the ResourceReader for a package in a zip file.
"""Return the ResourceReader for a package/module in a zip file.
If 'fullname' is a package within the zip file, return the
If 'fullname' is a package or a module within a package within the zip file, return the
'ResourceReader' object for the package. Otherwise return None.
"""
try:
if not self.is_package(fullname):
if self.is_package(fullname):
package = fullname
elif self.is_package("."):
package = "."
else:
return None
except ZipImportError:
return None
from importlib.readers import ZipReader
return ZipReader(self, fullname)
return ZipReader(self, package)


def _get_files(self):
Expand Down Expand Up @@ -307,7 +311,7 @@ def __repr__(self):
# Given a module name, return the potential file path in the
# archive (without extension).
def _get_module_path(self, fullname):
return self.prefix + fullname.rpartition('.')[2]
return (self.prefix + fullname.rpartition('.')[2]).rstrip(path_sep)

# Does this path represent a directory?
def _is_dir(self, path):
Expand Down

0 comments on commit e5a32a7

Please sign in to comment.