Skip to content

Commit eb74504

Browse files
committed
[GR-18308] Fix up another case how Jython imports may be used
PullRequest: graalpython/669
2 parents 69b2dad + 7c051f7 commit eb74504

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ language runtime. The main focus is on user-observable behavior of the engine.
2121
* Improve performance of our parser
2222
* Improve performance of catching exceptions when the exception does not leave the handler block and the traceback is not accessed
2323
* Improve performance of Java interop when Python objects are accessed from Java
24-
* Add a new `--python.EmulateJython` flag to support importing Java classes using normal Python import syntax when the package is known and to catch Java exceptions from Python code
24+
* Add a new `--python.EmulateJython` flag to support importing Java classes using normal Python import syntax and to catch Java exceptions from Python code
2525
* Update standard library to Python 3.7.4
2626
* Initial implementatin of PEP 498 -- Literal String Interpolation
2727

graalpython/lib-graalpython/java.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -127,38 +127,43 @@ def exec_module(module):
127127

128128

129129
class JavaImportFinder:
130+
def is_type(self, name):
131+
try:
132+
type(name)
133+
return True
134+
except KeyError:
135+
return False
136+
130137
if sys.graal_python_jython_emulation_enabled:
131-
@staticmethod
132-
def find_spec(fullname, path, target=None):
138+
def find_spec(self, fullname, path, target=None):
133139
# Because of how Jython allows you to import classes that have not
134-
# been loaded, yet, we need attempt to load types with the full
135-
# string. This will ensure that if there is such a Java class, its
136-
# package will have been initialized and thus the is_java_package
137-
# check below will work
138-
try:
139-
type(__jython_current_import__())
140-
except KeyError:
141-
pass
142-
# continue normally with import
143-
if JavaPackageLoader.is_java_package(fullname):
144-
return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True)
140+
# been loaded, yet, we need attempt to load types very eagerly.
141+
if self.is_type(fullname):
142+
# the fullname is already a type, let's load it
143+
return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False)
145144
else:
146-
try:
147-
type(fullname)
148-
return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False)
149-
except KeyError:
150-
pass
145+
current_import_name = __jython_current_import__()
146+
if current_import_name and self.is_type(current_import_name):
147+
# We are currently handling an import that will lead to a
148+
# Java type. The fullname is not a type itself, so it must
149+
# be a package, not an enclosing class.
150+
return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True)
151+
else:
152+
# We are not currently handling an import statement, and the
153+
# fullname is not a type. Thus we can only check if it is a
154+
# known package.
155+
if JavaPackageLoader.is_java_package(fullname):
156+
return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True)
151157
else:
152-
@staticmethod
153-
def find_spec(fullname, path, target=None):
158+
def find_spec(self, fullname, path, target=None):
154159
if path and path == __path__:
155160
if fullname.rpartition('.')[2].islower():
156161
return _frozen_importlib.ModuleSpec(fullname, JavaPackageLoader, is_package=True)
157162
else:
158163
return _frozen_importlib.ModuleSpec(fullname, JavaTypeLoader, is_package=False)
159164

160165

161-
sys.meta_path.append(JavaImportFinder)
166+
sys.meta_path.append(JavaImportFinder())
162167
if sys.graal_python_jython_emulation_enabled:
163168
__getattr__ = JavaPackageLoader._make_getattr("java")
164169
else:

0 commit comments

Comments
 (0)