core: allow re-creation of Core object, do not use _allow_registration global variable

This commit is contained in:
Sebastien Bourdeauducq 2022-06-06 23:13:46 +08:00
parent b77f6886be
commit ba30705fa5
2 changed files with 3 additions and 5 deletions

View File

@ -56,15 +56,16 @@ class Core:
self.core = self self.core = self
self.comm.core = self self.comm.core = self
self.target = target self.target = target
self.analyzed = False
self.compiler = nac3artiq.NAC3(target) self.compiler = nac3artiq.NAC3(target)
def close(self): def close(self):
self.comm.close() self.comm.close()
def compile(self, method, args, kwargs, embedding_map, file_output=None): def compile(self, method, args, kwargs, embedding_map, file_output=None):
if core_language._allow_registration: if not self.analyzed:
self.compiler.analyze(core_language._registered_functions, core_language._registered_classes) self.compiler.analyze(core_language._registered_functions, core_language._registered_classes)
core_language._allow_registration = False self.analyzed = True
if hasattr(method, "__self__"): if hasattr(method, "__self__"):
obj = method.__self__ obj = method.__self__

View File

@ -46,18 +46,15 @@ def ceil64(x):
return ceil(x) return ceil(x)
_allow_registration = True
# Delay NAC3 analysis until all referenced variables are supposed to exist on the CPython side. # Delay NAC3 analysis until all referenced variables are supposed to exist on the CPython side.
_registered_functions = set() _registered_functions = set()
_registered_classes = set() _registered_classes = set()
def _register_function(fun): def _register_function(fun):
assert _allow_registration
import_cache.add_module_to_cache(getmodule(fun)) import_cache.add_module_to_cache(getmodule(fun))
_registered_functions.add(fun) _registered_functions.add(fun)
def _register_class(cls): def _register_class(cls):
assert _allow_registration
import_cache.add_module_to_cache(getmodule(cls)) import_cache.add_module_to_cache(getmodule(cls))
_registered_classes.add(cls) _registered_classes.add(cls)