@@ -127,38 +127,43 @@ def exec_module(module):
127
127
128
128
129
129
class JavaImportFinder :
130
+ def is_type (self , name ):
131
+ try :
132
+ type (name )
133
+ return True
134
+ except KeyError :
135
+ return False
136
+
130
137
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 ):
133
139
# 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 )
145
144
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 )
151
157
else :
152
- @staticmethod
153
- def find_spec (fullname , path , target = None ):
158
+ def find_spec (self , fullname , path , target = None ):
154
159
if path and path == __path__ :
155
160
if fullname .rpartition ('.' )[2 ].islower ():
156
161
return _frozen_importlib .ModuleSpec (fullname , JavaPackageLoader , is_package = True )
157
162
else :
158
163
return _frozen_importlib .ModuleSpec (fullname , JavaTypeLoader , is_package = False )
159
164
160
165
161
- sys .meta_path .append (JavaImportFinder )
166
+ sys .meta_path .append (JavaImportFinder () )
162
167
if sys .graal_python_jython_emulation_enabled :
163
168
__getattr__ = JavaPackageLoader ._make_getattr ("java" )
164
169
else :
0 commit comments