Skip to content

Commit 576d652

Browse files
committed
implement loading java packages without java. prefix
1 parent ed39066 commit 576d652

File tree

3 files changed

+56
-48
lines changed

3 files changed

+56
-48
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_interop.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,12 @@ def test_java_imports():
340340

341341
assert java.util.ArrayList == ArrayList
342342

343+
import sun.misc.Signal
344+
assert sun.misc.Signal is not None
345+
346+
import org
347+
assert type(org.antlr) is type(java)
348+
343349
def test_foreign_object_does_not_leak_Javas_toString():
344350
try:
345351
from java.util import ArrayList

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SysModuleBuiltins.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public void postInitialize(PythonCore core) {
176176
}
177177

178178
sys.setAttribute("executable", PythonOptions.getOption(context, PythonOptions.Executable));
179+
sys.setAttribute("graal_python_host_import_enabled", context.getEnv().isHostLookupAllowed());
179180
sys.setAttribute("graal_python_home", context.getLanguage().getHome());
180181
sys.setAttribute("graal_python_core_home", context.getCoreHome());
181182
sys.setAttribute("graal_python_stdlib_home", context.getStdlibHome());

graalpython/lib-graalpython/java.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -40,63 +40,64 @@
4040
import _frozen_importlib
4141

4242

43-
class JavaPackageLoader:
44-
@staticmethod
45-
def _make_getattr(modname):
46-
if modname.startswith("java."):
47-
modname_wo = modname[len("java."):] + "."
48-
else:
49-
modname_wo = None
50-
modname = modname + "."
51-
def __getattr__(key, default=None):
52-
try:
53-
return type(modname + key)
54-
except KeyError:
55-
pass
56-
if modname_wo:
57-
try:
58-
return type(modname_wo + key)
59-
except KeyError:
60-
pass
61-
raise AttributeError(key)
62-
return __getattr__
43+
if sys.graal_python_host_import_enabled:
44+
class JavaPackageLoader:
45+
@staticmethod
46+
def is_java_package(name):
47+
return any(p.getName().startswith(name) for p in type("java.lang.Package").getPackages())
48+
49+
@staticmethod
50+
def _make_getattr(modname):
51+
modname = modname + "."
52+
def __getattr__(key, default=None):
53+
loadname = modname + key
54+
if JavaPackageLoader.is_java_package(loadname):
55+
return JavaPackageLoader._create_module(loadname)
56+
else:
57+
try:
58+
return type(modname + key)
59+
except KeyError:
60+
raise AttributeError(key)
61+
return __getattr__
62+
63+
@staticmethod
64+
def create_module(spec):
65+
return JavaPackageLoader._create_module(spec.name)
6366

64-
@staticmethod
65-
def create_module(spec):
66-
newmod = _frozen_importlib._new_module(spec.name)
67-
newmod.__getattr__ = JavaPackageLoader._make_getattr(spec.name)
68-
newmod.__path__ = __path__
69-
return newmod
67+
@staticmethod
68+
def _create_module(name):
69+
newmod = _frozen_importlib._new_module(name)
70+
newmod.__getattr__ = JavaPackageLoader._make_getattr(name)
71+
newmod.__path__ = __path__
72+
return newmod
7073

71-
@staticmethod
72-
def exec_module(module):
73-
pass
74+
@staticmethod
75+
def exec_module(module):
76+
pass
7477

7578

76-
class JavaTypeLoader:
77-
@staticmethod
78-
def create_module(spec):
79-
pass
79+
class JavaTypeLoader:
80+
@staticmethod
81+
def create_module(spec):
82+
pass
8083

81-
@staticmethod
82-
def exec_module(module):
83-
try:
84+
@staticmethod
85+
def exec_module(module):
8486
sys.modules[module.__name__] = type(module.__name__)
85-
except KeyError:
86-
if module.__name__.startswith("java."):
87-
sys.modules[module.__name__] = type(module.__name__[len("java."):])
88-
else:
89-
raise
9087

9188

92-
class JavaImportFinder:
93-
@staticmethod
94-
def find_spec(fullname, path, target=None):
95-
if path and path == __path__:
96-
if fullname.rpartition('.')[2].islower():
89+
class JavaImportFinder:
90+
@staticmethod
91+
def find_spec(fullname, path, target=None):
92+
if JavaPackageLoader.is_java_package(fullname):
9793
return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True)
9894
else:
99-
return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False)
95+
try:
96+
type(fullname)
97+
return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False)
98+
except KeyError:
99+
pass
100100

101101

102-
sys.meta_path.append(JavaImportFinder)
102+
sys.meta_path.append(JavaImportFinder)
103+
__getattr__ = JavaPackageLoader._make_getattr("java")

0 commit comments

Comments
 (0)