Skip to content

Commit fe3e43b

Browse files
committed
[GR-17359] Implement loading java packages without java.
PullRequest: graalpython/603
2 parents 3efa357 + f6a8042 commit fe3e43b

File tree

5 files changed

+57
-26
lines changed

5 files changed

+57
-26
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
This changelog summarizes major changes between GraalVM versions of the Python
44
language runtime. The main focus is on user-observable behavior of the engine.
55

6+
## Version 19.3.0
7+
8+
* Implement `charmap_build` function
9+
* Implement `hexversion` in sys module
10+
* Implement `_lzma` module
11+
* Fix printing of Pandas data frames
12+
* Fix a bug in `bytes.startswith` for tuple arguments
13+
* Fix destructuring assignments of arbitrary iterators
14+
* Fix `dict.__contains__` for dictionaries with only `str` keys for subclasses of `str`
15+
* Support NumPy 1.16.4 and Pandas 0.25.0
16+
* Support importing Java classes using normal Python import syntax when the package is known
17+
* Improve performance across many Python benchmarks
18+
619
## Version 19.2.0
720

821
* Implement PyStructSequence_* C API functions

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

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

341341
assert java.util.ArrayList == ArrayList
342342

343+
import sun
344+
assert type(sun.misc) is type(java)
345+
346+
import sun.misc.Signal
347+
assert sun.misc.Signal is not None
348+
349+
343350
def test_foreign_object_does_not_leak_Javas_toString():
344351
try:
345352
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: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,41 @@
4141

4242

4343
class JavaPackageLoader:
44+
@staticmethod
45+
def is_java_package(name):
46+
try:
47+
package = type("java.lang.Package")
48+
return any(p.getName().startswith(name) for p in package.getPackages())
49+
except KeyError:
50+
if sys.flags.verbose:
51+
from _warnings import _warn
52+
_warn("Host lookup allowed, but java.lang.Package not available. Importing from Java cannot work.")
53+
return False
54+
4455
@staticmethod
4556
def _make_getattr(modname):
46-
if modname.startswith("java."):
47-
modname_wo = modname[len("java."):] + "."
48-
else:
49-
modname_wo = None
5057
modname = modname + "."
5158
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
59+
if sys.graal_python_host_import_enabled:
60+
loadname = modname + key
61+
if JavaPackageLoader.is_java_package(loadname):
62+
return JavaPackageLoader._create_module(loadname)
63+
else:
64+
try:
65+
return type(modname + key)
66+
except KeyError:
67+
pass
6168
raise AttributeError(key)
6269
return __getattr__
6370

6471
@staticmethod
6572
def create_module(spec):
66-
newmod = _frozen_importlib._new_module(spec.name)
67-
newmod.__getattr__ = JavaPackageLoader._make_getattr(spec.name)
73+
return JavaPackageLoader._create_module(spec.name)
74+
75+
@staticmethod
76+
def _create_module(name):
77+
newmod = _frozen_importlib._new_module(name)
78+
newmod.__getattr__ = JavaPackageLoader._make_getattr(name)
6879
newmod.__path__ = __path__
6980
return newmod
7081

@@ -80,23 +91,22 @@ def create_module(spec):
8091

8192
@staticmethod
8293
def exec_module(module):
83-
try:
84-
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
94+
sys.modules[module.__name__] = type(module.__name__)
9095

9196

9297
class JavaImportFinder:
9398
@staticmethod
9499
def find_spec(fullname, path, target=None):
95-
if path and path == __path__:
96-
if fullname.rpartition('.')[2].islower():
100+
if sys.graal_python_host_import_enabled:
101+
if JavaPackageLoader.is_java_package(fullname):
97102
return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True)
98103
else:
99-
return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False)
104+
try:
105+
type(fullname)
106+
return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False)
107+
except KeyError:
108+
pass
100109

101110

102111
sys.meta_path.append(JavaImportFinder)
112+
__getattr__ = JavaPackageLoader._make_getattr("java")

mx.graalpython/mx_graalpython.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def python_gvm(args=None):
270270

271271
def python_svm(args=None):
272272
"Build and run the native graalpython image"
273-
with set_env(FORCE_BASH_LAUNCHERS="lli,native-image", DISABLE_LIBPOLYGLOT="true", DISABLE_POLYGLOT="true"):
273+
with set_env(FORCE_BASH_LAUNCHERS="lli,native-image,gu,graalvm-native-clang,graalvm-native-clang++", DISABLE_LIBPOLYGLOT="true", DISABLE_POLYGLOT="true"):
274274
return _python_graalvm_launcher(args or [])
275275

276276

0 commit comments

Comments
 (0)