Skip to content

Commit

Permalink
Work around incomplete implementation of importlib.resources.files on…
Browse files Browse the repository at this point in the history
… Python 3.9
  • Loading branch information
mhsmith committed Nov 27, 2023
1 parent 6ce9546 commit 8b07806
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions product/runtime/src/main/python/java/android/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def hook(path):
# get_importer returns.
site.addsitedir(finder.extract_root)

if sys.version_info[:2] == (3, 9):
from importlib import _common
global fallback_resources_original
fallback_resources_original = _common.fallback_resources
_common.fallback_resources = fallback_resources_39

global spec_from_file_location_original
spec_from_file_location_original = util.spec_from_file_location
util.spec_from_file_location = spec_from_file_location_override
Expand All @@ -96,6 +102,14 @@ def hook(path):
machinery.ExtensionFileLoader.create_module = extension_create_module_override


# Python 3.9 only supports importlib.resources.files for the standard importers.
def fallback_resources_39(spec):
if isinstance(spec.loader, AssetLoader):
return spec.loader.get_resource_reader(spec.name).files()
else:
return fallback_resources_original(spec)


def spec_from_file_location_override(name, location=None, *args, loader=None, **kwargs):
if location and not loader:
head, tail = split(location)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from unittest import skipIf
import types
from warnings import catch_warnings, filterwarnings
import zipfile

from java.android import importer

Expand Down Expand Up @@ -953,6 +954,20 @@ def test_resources_new(self):
"// MurmurHash2 was written by Austin Appleby")
self.check_resource_new(REQS_ABI_ZIP, pkg, "mrmr.so", b"\x7fELF")

# Stdlib
pkg_path = resources.files("email")
self.assertIsInstance(pkg_path, zipfile.Path)
self.assertTrue(pkg_path.is_dir())

children = [child.name for child in pkg_path.iterdir()]
self.assertIn("mime", children)
self.assertTrue((pkg_path / "mime").is_dir())

self.assertIn("parser.pyc", children)
path = pkg_path / "parser.pyc"
self.assertFalse(path.is_dir())
self.assertPredicate(path.read_bytes().startswith, MAGIC_NUMBER)

def check_resource_dir(self, path, children):
self.assertTrue(path.exists())
self.assertTrue(path.is_dir())
Expand Down

0 comments on commit 8b07806

Please sign in to comment.