Skip to content

Commit

Permalink
Merge pull request #924 from dbnicholson/import-subdir
Browse files Browse the repository at this point in the history
Fix bare imports in AssetFinder subdirectories
  • Loading branch information
mhsmith committed Aug 19, 2023
2 parents c31018d + 8228fbf commit bac5548
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions product/runtime/src/main/python/java/android/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ def __init__(self, context, build_json, path):
self.extract_root = parent.extract_root
self.prefix = relpath(path, self.extract_root)
self.zip_files = parent.zip_files
self.extract_packages = parent.extract_packages

def __repr__(self):
return f"{type(self).__name__}({self.path!r})"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,19 @@ def check_top_level(real_name, load_name, id):
# important enough to investigate just now.
self.assertFalse(hasattr(imp_rename_2, "mod_3"))

# Ensure that a package can be imported by a bare name when it's in an AssetFinder subdirectory.
# This is typically done when vendoring packages and dynamically adjusting sys.path. See #820.
def test_path_subdir(self):
sys.modules.pop("tests", None)
murmur_path = asset_path(REQS_COMMON_ZIP, "murmurhash")
tests_path = join(murmur_path, "tests/__init__.py")
sys.path.insert(0, murmur_path)
try:
import tests
finally:
sys.path.remove(murmur_path)
self.assertEqual(tests_path, tests.__file__)

# Make sure the standard library importer implements the new loader API
# (https://stackoverflow.com/questions/63574951).
def test_zipimport(self):
Expand Down

0 comments on commit bac5548

Please sign in to comment.