13
13
14
14
global_dict_to_instance = dict ()
15
15
global_dict_to_key_set = dict ()
16
+ java_globals_to_python_globals = dict ()
17
+
16
18
type_to_compiled_java_class = dict ()
17
19
type_to_annotations = dict ()
18
20
type_to_java_interfaces = dict ()
@@ -136,26 +138,33 @@ def copy_closure(closure):
136
138
return out
137
139
138
140
139
- def copy_globals (globals_dict , co_names ):
141
+ def copy_globals (globals_dict , co_names , python_class ):
140
142
global global_dict_to_instance
141
143
global global_dict_to_key_set
142
144
from .conversions import convert_to_java_python_like_object
143
- from java . util import HashMap
145
+ from ai . timefold . jpyinterpreter . util import PythonGlobalsBackedMap
144
146
from ai .timefold .jpyinterpreter import CPythonBackedPythonInterpreter
145
147
146
148
globals_dict_key = id (globals_dict )
147
149
if globals_dict_key in global_dict_to_instance :
148
150
out = global_dict_to_instance [globals_dict_key ]
149
151
key_set = global_dict_to_key_set [globals_dict_key ]
150
152
else :
151
- out = HashMap ( )
153
+ out = PythonGlobalsBackedMap ( globals_dict_key )
152
154
key_set = set ()
153
155
global_dict_to_instance [globals_dict_key ] = out
154
156
global_dict_to_key_set [globals_dict_key ] = key_set
157
+ java_globals_to_python_globals [globals_dict_key ] = globals_dict
155
158
156
159
instance_map = CPythonBackedPythonInterpreter .pythonObjectIdToConvertedObjectMap
157
160
for key , value in globals_dict .items ():
158
161
if key not in key_set and key in co_names :
162
+ if python_class is not None :
163
+ if isinstance (value , type ):
164
+ if issubclass (value , python_class ):
165
+ continue
166
+ elif isinstance (value , python_class ):
167
+ continue
159
168
key_set .add (key )
160
169
out .put (key , convert_to_java_python_like_object (value , instance_map ))
161
170
return out
@@ -216,7 +225,7 @@ def get_python_exception_table(python_code):
216
225
return out
217
226
218
227
219
- def get_function_bytecode_object (python_function ):
228
+ def get_function_bytecode_object (python_function , python_class : type = None ):
220
229
from .annotations import copy_type_annotations
221
230
from .conversions import copy_iterable , init_type_to_compiled_java_class , convert_to_java_python_like_object
222
231
from java .util import ArrayList
@@ -251,7 +260,8 @@ def get_function_bytecode_object(python_function):
251
260
python_compiled_function .co_argcount = python_function .__code__ .co_argcount
252
261
python_compiled_function .co_kwonlyargcount = python_function .__code__ .co_kwonlyargcount
253
262
python_compiled_function .closure = copy_closure (python_function .__closure__ )
254
- python_compiled_function .globalsMap = copy_globals (python_function .__globals__ , python_function .__code__ .co_names )
263
+ python_compiled_function .globalsMap = copy_globals (python_function .__globals__ , python_function .__code__ .co_names ,
264
+ python_class )
255
265
python_compiled_function .typeAnnotations = copy_type_annotations (python_function ,
256
266
get_default_args (python_function ),
257
267
inspect .getfullargspec (python_function ).varargs ,
@@ -267,7 +277,7 @@ def get_function_bytecode_object(python_function):
267
277
268
278
269
279
def get_static_function_bytecode_object (the_class , python_function ):
270
- return get_function_bytecode_object (python_function .__get__ (the_class ))
280
+ return get_function_bytecode_object (python_function .__get__ (the_class ), python_class = the_class )
271
281
272
282
273
283
def copy_variable_names (iterable ):
@@ -673,7 +683,7 @@ def translate_python_class_to_java_class(python_class):
673
683
674
684
instance_method_map = HashMap ()
675
685
for method in instance_methods :
676
- instance_method_map .put (method [0 ], get_function_bytecode_object (method [1 ]))
686
+ instance_method_map .put (method [0 ], get_function_bytecode_object (method [1 ], python_class = python_class ))
677
687
678
688
static_attributes_map = HashMap ()
679
689
static_attributes_to_class_instance_map = HashMap ()
@@ -683,7 +693,7 @@ def translate_python_class_to_java_class(python_class):
683
693
684
694
for attribute in static_attributes :
685
695
attribute_type = type (attribute [1 ])
686
- if attribute_type == python_class :
696
+ if issubclass ( attribute_type , python_class ) :
687
697
static_attributes_to_class_instance_map .put (attribute [0 ],
688
698
JProxy (OpaquePythonReference ,
689
699
inst = attribute [1 ], convert = True ))
0 commit comments