From 38227e233b5378338a489aa23fb9920fb0b5fc2b Mon Sep 17 00:00:00 2001 From: "Alexie (Boyong) Madolid" Date: Wed, 28 Aug 2024 17:00:11 +0800 Subject: [PATCH 1/3] [REFACTOR]: Execution Context --- jaclang/cli/cli.py | 92 +++++++----- jaclang/compiler/tests/test_importer.py | 15 +- jaclang/plugin/default.py | 45 +++--- jaclang/plugin/feature.py | 24 ++-- jaclang/plugin/spec.py | 21 ++- jaclang/runtimelib/architype.py | 30 ++-- jaclang/runtimelib/constructs.py | 3 +- jaclang/runtimelib/context.py | 131 +++++++++++------- jaclang/tests/test_language.py | 43 ++---- jaclang/tests/test_reference.py | 8 -- .../streamlit/jac_streamlit/__init__.py | 2 - 11 files changed, 204 insertions(+), 210 deletions(-) diff --git a/jaclang/cli/cli.py b/jaclang/cli/cli.py index 73c00ad14..f2e63ccfe 100644 --- a/jaclang/cli/cli.py +++ b/jaclang/cli/cli.py @@ -22,8 +22,9 @@ from jaclang.plugin.builtin import dotgen from jaclang.plugin.feature import JacCmd as Cmd from jaclang.plugin.feature import JacFeature as Jac -from jaclang.runtimelib.constructs import Anchor, Architype -from jaclang.runtimelib.machine import JacProgram +from jaclang.runtimelib.constructs import Architype, WalkerArchitype +from jaclang.runtimelib.context import ExecutionContext +from jaclang.runtimelib.machine import JacMachine, JacProgram from jaclang.utils.helpers import debugger as db from jaclang.utils.lang_tools import AstTool @@ -90,7 +91,9 @@ def run( base, mod = os.path.split(filename) base = base if base else "./" mod = mod[:-4] - Jac.context().init_memory(base_path=base, session=session) + + jctx = ExecutionContext.create(session=session) + if filename.endswith(".jac"): ret_module = jac_import( target=mod, @@ -104,14 +107,12 @@ def run( (loaded_mod,) = ret_module elif filename.endswith(".jir"): with open(filename, "rb") as f: - ir = pickle.load(f) - jac_program = JacProgram(mod_bundle=ir, bytecode=None) - Jac.context().jac_machine.attach_program(jac_program) ret_module = jac_import( target=mod, base_path=base, cachable=cache, override_name="__main__" if main else None, + jac_program=JacProgram(mod_bundle=pickle.load(f), bytecode=None), ) if ret_module is None: loaded_mod = None @@ -122,13 +123,13 @@ def run( return if not node or node == "root": - entrypoint: Architype = Jac.get_root() - else: - obj = Jac.context().mem.find_by_id(UUID(node)) - if not isinstance(obj, Anchor) or obj.architype is None: - print(f"Entrypoint {node} not found.") - return + entrypoint: Architype = jctx.root.architype + elif obj := jctx.datasource.find_by_id(UUID(node)): entrypoint = obj.architype + else: + print(f"Entrypoint {node} not found.") + jctx.close() + return # TODO: handle no override name if walker: @@ -138,7 +139,7 @@ def run( else: print(f"Walker {walker} not found.") - Jac.reset_context() + jctx.close() @cmd_registry.register @@ -147,21 +148,18 @@ def get_object(id: str, session: str = "") -> dict: if session == "": session = cmd_registry.args.session if "session" in cmd_registry.args else "" - Jac.context().init_memory(session=session) + jctx = ExecutionContext.create(session=session) + data = {} if id == "root": - id_uuid = UUID(int=0) + data = jctx.root.__getstate__() + elif obj := jctx.datasource.find_by_id(UUID(id)): + data = obj.__getstate__() else: - id_uuid = UUID(id) - - obj = Jac.context().mem.find_by_id(id_uuid) - if obj is None: print(f"Object with id {id} not found.") - Jac.reset_context() - return {} - else: - Jac.reset_context() - return obj.__getstate__() + + jctx.close() + return data @cmd_registry.register @@ -211,13 +209,26 @@ def lsp() -> None: @cmd_registry.register -def enter(filename: str, entrypoint: str, args: list) -> None: - """Run the specified entrypoint function in the given .jac file. +def enter( + filename: str, + entrypoint: str, + args: list, + session: str = "", + root: str = "", + node: str = "", +) -> None: + """ + Run the specified entrypoint function in the given .jac file. :param filename: The path to the .jac file. :param entrypoint: The name of the entrypoint function. :param args: Arguments to pass to the entrypoint function. + :param session: shelve.Shelf file path. + :param root: root executor. + :param node: starting node. """ + jctx = ExecutionContext.create(session=session, root=root, entry=node) + if filename.endswith(".jac"): base, mod_name = os.path.split(filename) base = base if base else "./" @@ -225,12 +236,16 @@ def enter(filename: str, entrypoint: str, args: list) -> None: (mod,) = jac_import(target=mod_name, base_path=base) if not mod: print("Errors occurred while importing the module.") - return else: - getattr(mod, entrypoint)(*args) + architype = getattr(mod, entrypoint)(*args) + if isinstance(architype, WalkerArchitype): + Jac.spawn_call(jctx.entry.architype, architype) + else: print("Not a .jac file.") + jctx.close() + @cmd_registry.register def test( @@ -252,6 +267,8 @@ def test( jac test => jac test -d . """ + jctx = ExecutionContext.create() + failcount = Jac.run_test( filepath=filepath, filter=filter, @@ -260,6 +277,9 @@ def test( directory=directory, verbose=verbose, ) + + jctx.close() + if failcount: raise SystemExit(f"Tests failed: {failcount}") @@ -361,13 +381,13 @@ def dot( base, mod = os.path.split(filename) base = base if base else "./" mod = mod[:-4] - Jac.context().init_memory(base_path=base, session=session) + + jctx = ExecutionContext.create(session=session) + if filename.endswith(".jac"): - jac_import( - target=mod, - base_path=base, - ) - module = Jac.context().jac_machine.loaded_modules.get(mod) + jac_machine = JacMachine(base) + jac_import(target=mod, base_path=base, jac_machine=jac_machine) + module = jac_machine.loaded_modules.get(mod) globals().update(vars(module)) try: node = globals().get(initial, eval(initial)) if initial else None @@ -385,7 +405,7 @@ def dot( import traceback traceback.print_exc() - Jac.reset_context() + jctx.close() return file_name = saveto if saveto else f"{mod}.dot" with open(file_name, "w") as file: @@ -394,7 +414,7 @@ def dot( else: print("Not a .jac file.") - Jac.reset_context() + jctx.close() @cmd_registry.register diff --git a/jaclang/compiler/tests/test_importer.py b/jaclang/compiler/tests/test_importer.py index 69ee35d07..410af5f15 100644 --- a/jaclang/compiler/tests/test_importer.py +++ b/jaclang/compiler/tests/test_importer.py @@ -5,34 +5,29 @@ from jaclang import jac_import from jaclang.cli import cli -from jaclang.plugin.feature import JacFeature as Jac +from jaclang.runtimelib.machine import JacMachine from jaclang.utils.test import TestCase class TestLoader(TestCase): """Test Jac self.prse.""" - def setUp(self) -> None: - """Set up test.""" - return super().setUp() - def test_import_basic_python(self) -> None: """Test basic self loading.""" - Jac.context().init_memory(base_path=self.fixture_abs_path(__file__)) (h,) = jac_import("fixtures.hello_world", base_path=__file__) self.assertEqual(h.hello(), "Hello World!") # type: ignore def test_modules_correct(self) -> None: """Test basic self loading.""" - Jac.context().init_memory(base_path=self.fixture_abs_path(__file__)) - jac_import("fixtures.hello_world", base_path=__file__) + jac_machine = JacMachine(__file__) + jac_import("fixtures.hello_world", base_path=__file__, jac_machine=jac_machine) self.assertIn( "module 'fixtures.hello_world'", - str(Jac.context().jac_machine.loaded_modules), + str(jac_machine.loaded_modules), ) self.assertIn( "/tests/fixtures/hello_world.jac", - str(Jac.context().jac_machine.loaded_modules), + str(jac_machine.loaded_modules), ) def test_jac_py_import(self) -> None: diff --git a/jaclang/plugin/default.py b/jaclang/plugin/default.py index 649d23a09..b4bc98c1c 100644 --- a/jaclang/plugin/default.py +++ b/jaclang/plugin/default.py @@ -32,9 +32,9 @@ Root, WalkerAnchor, WalkerArchitype, - exec_context, ) from jaclang.runtimelib.importer import ImportPathSpec, JacImporter, PythonImporter +from jaclang.runtimelib.machine import JacMachine, JacProgram from jaclang.runtimelib.utils import traverse_graph from jaclang.plugin.feature import JacFeature as Jac # noqa: I100 from jaclang.plugin.spec import P, T @@ -69,28 +69,15 @@ class JacFeatureDefaults: @staticmethod @hookimpl - def context(session: str = "") -> ExecutionContext: - """Get the execution context.""" - ctx = exec_context.get() - if ctx is None: - ctx = ExecutionContext() - exec_context.set(ctx) - return ctx + def get_context() -> ExecutionContext: + """Get current execution context.""" + return ExecutionContext.get() @staticmethod @hookimpl - def reset_context() -> None: - """Reset the execution context.""" - ctx = exec_context.get() - if ctx: - ctx.reset() - exec_context.set(None) - - @staticmethod - @hookimpl - def memory_hook() -> Memory | None: - """Return the memory hook.""" - return Jac.context().mem + def get_datasource() -> Memory: + """Get current execution context.""" + return ExecutionContext.get().datasource @staticmethod @hookimpl @@ -251,6 +238,8 @@ def jac_import( lng: Optional[str], items: Optional[dict[str, Union[str, Optional[str]]]], reload_module: Optional[bool], + jac_machine: Optional[JacMachine], + jac_program: Optional[JacProgram], ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" spec = ImportPathSpec( @@ -263,12 +252,14 @@ def jac_import( lng, items, ) + jac_machine = jac_machine or JacMachine(base_path) + jac_machine.attach_program( + jac_program or JacProgram(mod_bundle=None, bytecode=None) + ) if lng == "py": - import_result = PythonImporter(Jac.context().jac_machine).run_import(spec) + import_result = PythonImporter(jac_machine).run_import(spec) else: - import_result = JacImporter(Jac.context().jac_machine).run_import( - spec, reload_module - ) + import_result = JacImporter(jac_machine).run_import(spec, reload_module) return ( (import_result.ret_mod,) if absorb or not items @@ -503,14 +494,14 @@ def disconnect( and node == source and target.architype in right ): - anchor.destroy() + anchor.destroy() if anchor.persistent else anchor.detach() disconnect_occurred = True if ( dir in [EdgeDir.IN, EdgeDir.ANY] and node == target and source.architype in right ): - anchor.destroy() + anchor.destroy() if anchor.persistent else anchor.detach() disconnect_occurred = True return disconnect_occurred @@ -531,7 +522,7 @@ def assign_compr( @hookimpl def get_root() -> Root: """Jac's assign comprehension feature.""" - return Jac.context().get_root() + return ExecutionContext.get_root() @staticmethod @hookimpl diff --git a/jaclang/plugin/feature.py b/jaclang/plugin/feature.py index 9105a5aa4..c0a4b858a 100644 --- a/jaclang/plugin/feature.py +++ b/jaclang/plugin/feature.py @@ -8,7 +8,6 @@ import jaclang.compiler.absyntree as ast from jaclang.compiler.passes.main.pyast_gen_pass import PyastGenPass -from jaclang.plugin.default import ExecutionContext from jaclang.plugin.spec import JacBuiltin, JacCmdSpec, JacFeatureSpec, P, T from jaclang.runtimelib.constructs import ( Architype, @@ -19,6 +18,8 @@ Root, WalkerArchitype, ) +from jaclang.runtimelib.context import ExecutionContext +from jaclang.runtimelib.machine import JacMachine, JacProgram import pluggy @@ -43,19 +44,14 @@ class JacFeature: Walker: TypeAlias = WalkerArchitype @staticmethod - def context(session: str = "") -> ExecutionContext: - """Create execution context.""" - return pm.hook.context(session=session) + def get_context() -> ExecutionContext: + """Get current execution context.""" + return pm.hook.get_context() @staticmethod - def reset_context() -> None: - """Reset execution context.""" - return pm.hook.reset_context() - - @staticmethod - def memory_hook() -> Memory | None: - """Create memory abstraction.""" - return pm.hook.memory_hook() + def get_datasource() -> Memory: + """Get current execution context.""" + return pm.hook.get_datasource() @staticmethod def make_architype( @@ -115,6 +111,8 @@ def jac_import( lng: Optional[str] = "jac", items: Optional[dict[str, Union[str, Optional[str]]]] = None, reload_module: Optional[bool] = False, + jac_machine: Optional[JacMachine] = None, + jac_program: Optional[JacProgram] = None, ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" return pm.hook.jac_import( @@ -127,6 +125,8 @@ def jac_import( lng=lng, items=items, reload_module=reload_module, + jac_machine=jac_machine, + jac_program=jac_program, ) @staticmethod diff --git a/jaclang/plugin/spec.py b/jaclang/plugin/spec.py index 0d68f30de..0edb2d710 100644 --- a/jaclang/plugin/spec.py +++ b/jaclang/plugin/spec.py @@ -21,15 +21,16 @@ from jaclang.compiler.passes.main.pyast_gen_pass import PyastGenPass if TYPE_CHECKING: - from jaclang.runtimelib.constructs import EdgeArchitype, NodeAnchor, NodeArchitype from jaclang.plugin.default import ( Architype, EdgeDir, - ExecutionContext, WalkerArchitype, Root, DSFunc, ) + from jaclang.runtimelib.constructs import EdgeArchitype, NodeAnchor, NodeArchitype + from jaclang.runtimelib.context import ExecutionContext + from jaclang.runtimelib.machine import JacMachine, JacProgram from jaclang.runtimelib.memory import Memory import pluggy @@ -45,20 +46,14 @@ class JacFeatureSpec: @staticmethod @hookspec(firstresult=True) - def context(session: str = "") -> ExecutionContext: - """Get the execution context.""" - raise NotImplementedError - - @staticmethod - @hookspec(firstresult=True) - def reset_context() -> None: - """Reset the execution context.""" + def get_context() -> ExecutionContext: + """Get current execution context.""" raise NotImplementedError @staticmethod @hookspec(firstresult=True) - def memory_hook() -> Memory | None: - """Create memory abstraction.""" + def get_datasource() -> Memory: + """Get current execution context datasource.""" raise NotImplementedError @staticmethod @@ -124,6 +119,8 @@ def jac_import( lng: Optional[str], items: Optional[dict[str, Union[str, Optional[str]]]], reload_module: Optional[bool], + jac_machine: Optional[JacMachine], + jac_program: Optional[JacProgram], ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" raise NotImplementedError diff --git a/jaclang/runtimelib/architype.py b/jaclang/runtimelib/architype.py index f4c43c7ff..c6cd560e5 100644 --- a/jaclang/runtimelib/architype.py +++ b/jaclang/runtimelib/architype.py @@ -16,7 +16,7 @@ @dataclass -class Report: +class AnchorReport: """Report Handler.""" id: str @@ -37,13 +37,13 @@ def save(self) -> None: from jaclang.plugin.feature import JacFeature as Jac self.persistent = True - Jac.context().mem.set(self.id, self) + Jac.get_datasource().set(self.id, self) def destroy(self) -> None: """Destroy Anchor.""" from jaclang.plugin.feature import JacFeature as Jac - Jac.context().mem.remove(self.id) + Jac.get_datasource().remove(self.id) def is_populated(self) -> bool: """Check if state.""" @@ -61,9 +61,9 @@ def populate(self) -> None: """Retrieve the Architype from db and return.""" from jaclang.plugin.feature import JacFeature as Jac - ctx_mem = Jac.context().mem + jsrc = Jac.get_datasource() - if anchor := ctx_mem.find_by_id(self.id): + if anchor := jsrc.find_by_id(self.id): self.__dict__.update(anchor.__dict__) def __getattr__(self, name: str) -> object: @@ -118,9 +118,9 @@ def __repr__(self) -> str: return f"{self.__class__.__name__}({attrs})" - def report(self) -> Report: + def report(self) -> AnchorReport: """Report Anchor.""" - return Report( + return AnchorReport( id=self.id.hex, context=( asdict(self.architype) @@ -245,7 +245,7 @@ def destroy(self) -> None: for edge in self.edges: edge.destroy() - Jac.context().mem.remove(self.id) + Jac.get_datasource().remove(self.id) def __getstate__(self) -> dict[str, object]: """Serialize Node Anchor.""" @@ -285,7 +285,7 @@ def destroy(self) -> None: from jaclang.plugin.feature import JacFeature as Jac self.detach() - Jac.context().mem.remove(self.id) + Jac.get_datasource().remove(self.id) def __getstate__(self) -> dict[str, object]: """Serialize Node Anchor.""" @@ -453,20 +453,10 @@ class Root(NodeArchitype): _jac_entry_funcs_ = [] _jac_exit_funcs_ = [] - reachable_nodes: list[NodeArchitype] = [] - connections: set[tuple[NodeArchitype, NodeArchitype, EdgeArchitype]] = set() def __init__(self) -> None: """Create root node.""" - self.__jac__ = NodeAnchor( - architype=self, id=UUID(int=0), persistent=True, edges=[] - ) - - def reset(self) -> None: - """Reset the root.""" - self.reachable_nodes = [] - self.connections = set() - self.__jac__.edges = [] + self.__jac__ = NodeAnchor(architype=self, persistent=True, edges=[]) @dataclass(eq=False) diff --git a/jaclang/runtimelib/constructs.py b/jaclang/runtimelib/constructs.py index 83403584b..655bd4bc0 100644 --- a/jaclang/runtimelib/constructs.py +++ b/jaclang/runtimelib/constructs.py @@ -16,7 +16,7 @@ WalkerAnchor, WalkerArchitype, ) -from .context import ExecutionContext, exec_context +from .context import ExecutionContext from .memory import Memory, ShelfStorage from .test import JacTestCheck, JacTestResult, JacTextTestRunner @@ -35,7 +35,6 @@ "Memory", "ShelfStorage", "ExecutionContext", - "exec_context", "JacTestResult", "JacTextTestRunner", "JacTestCheck", diff --git a/jaclang/runtimelib/context.py b/jaclang/runtimelib/context.py index 9c0efca46..e4c308c3e 100644 --- a/jaclang/runtimelib/context.py +++ b/jaclang/runtimelib/context.py @@ -4,60 +4,95 @@ import unittest from contextvars import ContextVar -from typing import Callable, Optional +from typing import Any, Callable, Optional, cast from uuid import UUID -from .architype import Root -from .machine import JacMachine, JacProgram +from .architype import NodeAnchor, Root from .memory import Memory, ShelfStorage -class ExecutionContext: - """Default Execution Context implementation.""" - - mem: Memory - root: Optional[Root] - - def __init__(self) -> None: - """Create execution context.""" - super().__init__() - self.mem = ShelfStorage() - self.root = None - self.jac_machine = JacMachine() - jac_program = JacProgram(mod_bundle=None, bytecode=None) - self.jac_machine.attach_program(jac_program) - - def init_memory(self, base_path: str = "", session: str = "") -> None: - """Initialize memory.""" - self.mem = ShelfStorage(session) - self.jac_machine = JacMachine(base_path) - jac_program = JacProgram(mod_bundle=None, bytecode=None) - self.jac_machine.attach_program(jac_program) - - def get_root(self) -> Root: - """Get the root object.""" - if not self.root: - root = self.mem.find_by_id(UUID(int=0)) - if root is None: - root = Root().__jac__ - self.mem.set(root.id, root) - - if not isinstance(root.architype, Root): - raise ValueError(f"Invalid root object: {root}") - else: - self.root = root.architype - return self.root - - def reset(self) -> None: - """Reset the execution context.""" - if self.mem: - self.mem.close() - self.root = None - - -exec_context: ContextVar[ExecutionContext | None] = ContextVar( - "ExecutionContext", default=None +EXECUTION_CONTEXT = ContextVar[Optional["ExecutionContext"]]("ExecutionContext") + +SUPER_ROOT_UUID = "00000000-0000-0000-0000-000000000000" +SUPER_ROOT_ARCHITYPE = object.__new__(Root) +SUPER_ROOT_ANCHOR = NodeAnchor( + id=UUID(SUPER_ROOT_UUID), architype=SUPER_ROOT_ARCHITYPE, persistent=False, edges=[] ) +SUPER_ROOT_ARCHITYPE.__jac__ = SUPER_ROOT_ANCHOR + + +class ExecutionContext: + """Execution Context.""" + + datasource: Memory + reports: list[Any] + system_root: NodeAnchor + root: NodeAnchor + entry: NodeAnchor + + def generate_system_root(self) -> NodeAnchor: + """Generate default system root.""" + architype = object.__new__(Root) + anchor = NodeAnchor( + id=UUID(SUPER_ROOT_UUID), architype=architype, persistent=True, edges=[] + ) + architype.__jac__ = anchor + self.datasource.set(anchor.id, anchor) + return anchor + + def init_anchor( + self, + anchor_id: str | None, + default: NodeAnchor | Callable[[], NodeAnchor], + ) -> NodeAnchor: + """Load initial anchors.""" + if anchor_id and isinstance( + anchor := self.datasource.find_by_id(UUID(anchor_id)), NodeAnchor + ): + return anchor + return default() if callable(default) else default + + def close(self) -> None: + """Close current ExecutionContext.""" + self.datasource.close() + EXECUTION_CONTEXT.set(None) + + @staticmethod + def create( + session: Optional[str] = None, + root: Optional[str] = None, + entry: Optional[str] = None, + auto_close: bool = True, + ) -> ExecutionContext: + """Create ExecutionContext.""" + ctx = ExecutionContext() + ctx.datasource = ShelfStorage(session) + ctx.reports = [] + ctx.system_root = ctx.init_anchor(SUPER_ROOT_UUID, ctx.generate_system_root) + ctx.root = ctx.init_anchor(root, ctx.system_root) + ctx.entry = ctx.init_anchor(entry, ctx.root) + + if auto_close and (old_ctx := EXECUTION_CONTEXT.get(None)): + old_ctx.close() + + EXECUTION_CONTEXT.set(ctx) + + return ctx + + @staticmethod + def get() -> ExecutionContext: + """Get current ExecutionContext.""" + if ctx := EXECUTION_CONTEXT.get(None): + return ctx + raise Exception("ExecutionContext is not yet available!") + + @staticmethod + def get_root() -> Root: + """Get current root.""" + if ctx := EXECUTION_CONTEXT.get(None): + return cast(Root, ctx.root.architype) + + return SUPER_ROOT_ARCHITYPE class JacTestResult(unittest.TextTestResult): diff --git a/jaclang/tests/test_language.py b/jaclang/tests/test_language.py index ecc69196c..33eee04d9 100644 --- a/jaclang/tests/test_language.py +++ b/jaclang/tests/test_language.py @@ -12,7 +12,8 @@ from jaclang.cli import cli from jaclang.compiler.compile import jac_file_to_pass, jac_pass_to_pass, jac_str_to_pass from jaclang.compiler.passes.main.schedules import py_code_gen_typed -from jaclang.plugin.feature import JacFeature as Jac +from jaclang.runtimelib.context import SUPER_ROOT_ANCHOR +from jaclang.runtimelib.machine import JacMachine from jaclang.utils.test import TestCase @@ -21,6 +22,7 @@ class JacLanguageTests(TestCase): def setUp(self) -> None: """Set up test.""" + SUPER_ROOT_ANCHOR.edges.clear() return super().setUp() def test_sub_abilities(self) -> None: @@ -59,7 +61,6 @@ def test_sub_abilities_multi(self) -> None: def test_simple_jac_red(self) -> None: """Parse micro jac file.""" - Jac.context().init_memory(base_path=self.examples_abs_path("")) captured_output = io.StringIO() sys.stdout = captured_output jac_import("micro.simple_walk", base_path=self.examples_abs_path("")) @@ -112,7 +113,6 @@ def test_chandra_bugs2(self) -> None: def test_ignore(self) -> None: """Parse micro jac file.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("ignore_dup", base_path=self.fixture_abs_path("./")) @@ -151,7 +151,6 @@ def test_need_import(self) -> None: def test_filter_compr(self) -> None: """Testing filter comprehension.""" - Jac.context().init_memory(base_path=self.examples_abs_path("./")) captured_output = io.StringIO() sys.stdout = captured_output jac_import( @@ -207,8 +206,6 @@ def test_raw_bytestr(self) -> None: def test_deep_imports(self) -> None: """Parse micro jac file.""" - Jac.get_root().__jac__.edges.clear() - Jac.context().init_memory(base_path=self.fixture_abs_path("./")) captured_output = io.StringIO() sys.stdout = captured_output @@ -229,18 +226,19 @@ def test_deep_imports_mods(self) -> None: for i in targets: if i in sys.modules: del sys.modules[i] - Jac.get_root().__jac__.edges.clear() - Jac.context().init_memory(base_path=self.fixture_abs_path("./")) - jac_import("deep_import_mods", base_path=self.fixture_abs_path("./")) - mods = Jac.context().jac_machine.loaded_modules.keys() + jac_machine = JacMachine(self.fixture_abs_path("./")) + jac_import( + "deep_import_mods", + base_path=self.fixture_abs_path("./"), + jac_machine=jac_machine, + ) + mods = jac_machine.loaded_modules.keys() for i in targets: self.assertIn(i, mods) self.assertEqual(len([i for i in mods if i.startswith("deep")]), 6) def test_deep_outer_imports_one(self) -> None: """Parse micro jac file.""" - Jac.get_root().__jac__.edges.clear() - Jac.context().init_memory(base_path=self.fixture_abs_path("./")) captured_output = io.StringIO() sys.stdout = captured_output jac_import( @@ -253,8 +251,6 @@ def test_deep_outer_imports_one(self) -> None: def test_deep_outer_imports_from_loc(self) -> None: """Parse micro jac file.""" - Jac.get_root().__jac__.edges.clear() - Jac.context().init_memory(base_path=self.fixture_abs_path("./deep/deeper/")) captured_output = io.StringIO() sys.stdout = captured_output os.chdir(self.fixture_abs_path("./deep/deeper/")) @@ -266,7 +262,6 @@ def test_deep_outer_imports_from_loc(self) -> None: # def test_second_deep_outer_imports(self) -> None: # """Parse micro jac file.""" - # Jac.get_root().__jac__.edges.clear() # captured_output = io.StringIO() # sys.stdout = captured_output # jac_import( @@ -279,7 +274,6 @@ def test_deep_outer_imports_from_loc(self) -> None: def test_has_lambda_goodness(self) -> None: """Test has lambda_goodness.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("has_goodness", base_path=self.fixture_abs_path("./")) @@ -290,7 +284,6 @@ def test_has_lambda_goodness(self) -> None: def test_conn_assign_on_edges(self) -> None: """Test conn assign on edges.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("edge_ops", base_path=self.fixture_abs_path("./")) @@ -302,7 +295,6 @@ def test_conn_assign_on_edges(self) -> None: def test_disconnect(self) -> None: """Test conn assign on edges.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("disconn", base_path=self.fixture_abs_path("./")) @@ -320,7 +312,6 @@ def test_disconnect(self) -> None: def test_simple_archs(self) -> None: """Test conn assign on edges.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("simple_archs", base_path=self.fixture_abs_path("./")) @@ -331,7 +322,6 @@ def test_simple_archs(self) -> None: def test_edge_walk(self) -> None: """Test walking through edges.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("edges_walk", base_path=self.fixture_abs_path("./")) @@ -345,7 +335,6 @@ def test_edge_walk(self) -> None: def test_impl_grab(self) -> None: """Test walking through edges.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("impl_grab", base_path=self.fixture_abs_path("./")) @@ -355,7 +344,6 @@ def test_impl_grab(self) -> None: def test_tuple_of_tuple_assign(self) -> None: """Test walking through edges.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("tuplytuples", base_path=self.fixture_abs_path("./")) @@ -368,7 +356,6 @@ def test_tuple_of_tuple_assign(self) -> None: def test_deferred_field(self) -> None: """Test walking through edges.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("deferred_field", base_path=self.fixture_abs_path("./")) @@ -381,7 +368,6 @@ def test_deferred_field(self) -> None: def test_gen_dot_builtin(self) -> None: """Test the dot gen of nodes and edges as a builtin.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("builtin_dotgen", base_path=self.fixture_abs_path("./")) @@ -391,7 +377,6 @@ def test_gen_dot_builtin(self) -> None: def test_with_contexts(self) -> None: """Test walking through edges.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("with_context", base_path=self.fixture_abs_path("./")) @@ -407,7 +392,6 @@ def test_with_contexts(self) -> None: def test_typed_filter_compr(self) -> None: """Parse micro jac file.""" - Jac.context().init_memory(base_path=self.examples_abs_path("")) captured_output = io.StringIO() sys.stdout = captured_output jac_import( @@ -425,7 +409,6 @@ def test_typed_filter_compr(self) -> None: def test_edge_node_walk(self) -> None: """Test walking through edges and nodes.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("edge_node_walk", base_path=self.fixture_abs_path("./")) @@ -439,14 +422,12 @@ def test_edge_node_walk(self) -> None: def test_annotation_tuple_issue(self) -> None: """Test conn assign on edges.""" - Jac.get_root().__jac__.edges.clear() mypass = jac_file_to_pass(self.fixture_abs_path("./slice_vals.jac")) self.assertIn("Annotated[Str, INT, BLAH]", mypass.ir.gen.py) self.assertIn("tuple[int, Optional[type], Optional[tuple]]", mypass.ir.gen.py) def test_impl_decl_resolution_fix(self) -> None: """Test walking through edges and nodes.""" - Jac.get_root().__jac__.edges.clear() captured_output = io.StringIO() sys.stdout = captured_output jac_import("mtest", base_path=self.fixture_abs_path("./")) @@ -821,7 +802,6 @@ def test_override_walker_inherit(self) -> None: def test_ds_type_check_pass(self) -> None: """Test conn assign on edges.""" - Jac.get_root().__jac__.edges.clear() mypass = jac_file_to_pass( self.examples_abs_path("micro/simple_walk.jac"), schedule=py_code_gen_typed, @@ -831,7 +811,6 @@ def test_ds_type_check_pass(self) -> None: def test_ds_type_check_pass2(self) -> None: """Test conn assign on edges.""" - Jac.get_root().__jac__.edges.clear() mypass = jac_file_to_pass( self.examples_abs_path("guess_game/guess_game5.jac"), schedule=py_code_gen_typed, @@ -841,7 +820,6 @@ def test_ds_type_check_pass2(self) -> None: def test_circle_override1_type_check_pass(self) -> None: """Test conn assign on edges.""" - Jac.get_root().__jac__.edges.clear() mypass = jac_file_to_pass( self.examples_abs_path("manual_code/circle.jac"), schedule=py_code_gen_typed, @@ -869,7 +847,6 @@ def test_hash_init_check(self) -> None: # we can get rid of this, isn't? def test_multiline_single_tok(self) -> None: """Test conn assign on edges.""" - Jac.get_root().__jac__.edges.clear() mypass = jac_file_to_pass(self.fixture_abs_path("byllmissue.jac")) self.assertIn("2:5 - 4:8", mypass.ir.pp()) diff --git a/jaclang/tests/test_reference.py b/jaclang/tests/test_reference.py index ce2f8cf5f..f8f117bee 100644 --- a/jaclang/tests/test_reference.py +++ b/jaclang/tests/test_reference.py @@ -7,7 +7,6 @@ import jaclang from jaclang.compiler.compile import jac_file_to_pass -from jaclang.plugin.feature import JacFeature as Jac from jaclang.utils.test import TestCase @@ -52,13 +51,6 @@ def micro_suite_test(self, filename: str) -> None: """Test file.""" def execute_and_capture_output(code: str | bytes, filename: str = "") -> str: - Jac.get_root().reset() - Jac.context().init_memory( - base_path=os.path.join( - os.path.dirname(os.path.dirname(jaclang.__file__)), - "examples/reference", - ) - ) f = io.StringIO() with redirect_stdout(f): exec( diff --git a/support/plugins/streamlit/jac_streamlit/__init__.py b/support/plugins/streamlit/jac_streamlit/__init__.py index 343c262a0..33b44bbca 100644 --- a/support/plugins/streamlit/jac_streamlit/__init__.py +++ b/support/plugins/streamlit/jac_streamlit/__init__.py @@ -6,9 +6,7 @@ def run_streamlit(basename: str, dirname: str) -> None: """Run the Streamlit application.""" from jaclang import jac_import - from jaclang.plugin.feature import JacFeature as Jac - Jac.context().init_memory(base_path=dirname) jac_import( basename, base_path=dirname, reload_module=True ) # TODO: need flag to force reload here From 44b823ff88449ec8136c22235d7d59ec3a1ee396 Mon Sep 17 00:00:00 2001 From: "Alexie (Boyong) Madolid" Date: Thu, 29 Aug 2024 00:22:55 +0800 Subject: [PATCH 2/3] [JAC-MACHINE]: Isolate to other context vars --- jaclang/cli/cli.py | 11 +- jaclang/compiler/tests/test_importer.py | 4 +- jaclang/plugin/default.py | 28 ++++- jaclang/plugin/feature.py | 18 ++- jaclang/plugin/spec.py | 17 ++- jaclang/runtimelib/machine.py | 18 +++ jaclang/tests/fixtures/foo.jac | 8 +- jaclang/tests/test_language.py | 161 +++++++++++++++--------- jaclang/tests/test_reference.py | 2 + 9 files changed, 188 insertions(+), 79 deletions(-) diff --git a/jaclang/cli/cli.py b/jaclang/cli/cli.py index f2e63ccfe..a932be1af 100644 --- a/jaclang/cli/cli.py +++ b/jaclang/cli/cli.py @@ -100,19 +100,24 @@ def run( base_path=base, cachable=cache, override_name="__main__" if main else None, + is_origin=True, ) + if ret_module is None: loaded_mod = None else: (loaded_mod,) = ret_module elif filename.endswith(".jir"): with open(filename, "rb") as f: + JacMachine(base).attach_program( + JacProgram(mod_bundle=pickle.load(f), bytecode=None) + ) ret_module = jac_import( target=mod, base_path=base, cachable=cache, override_name="__main__" if main else None, - jac_program=JacProgram(mod_bundle=pickle.load(f), bytecode=None), + is_origin=True, ) if ret_module is None: loaded_mod = None @@ -233,7 +238,7 @@ def enter( base, mod_name = os.path.split(filename) base = base if base else "./" mod_name = mod_name[:-4] - (mod,) = jac_import(target=mod_name, base_path=base) + (mod,) = jac_import(target=mod_name, base_path=base, is_origin=True) if not mod: print("Errors occurred while importing the module.") else: @@ -386,7 +391,7 @@ def dot( if filename.endswith(".jac"): jac_machine = JacMachine(base) - jac_import(target=mod, base_path=base, jac_machine=jac_machine) + jac_import(target=mod, base_path=base, is_origin=True) module = jac_machine.loaded_modules.get(mod) globals().update(vars(module)) try: diff --git a/jaclang/compiler/tests/test_importer.py b/jaclang/compiler/tests/test_importer.py index 410af5f15..c7b630647 100644 --- a/jaclang/compiler/tests/test_importer.py +++ b/jaclang/compiler/tests/test_importer.py @@ -14,13 +14,13 @@ class TestLoader(TestCase): def test_import_basic_python(self) -> None: """Test basic self loading.""" - (h,) = jac_import("fixtures.hello_world", base_path=__file__) + (h,) = jac_import("fixtures.hello_world", base_path=__file__, is_origin=True) self.assertEqual(h.hello(), "Hello World!") # type: ignore def test_modules_correct(self) -> None: """Test basic self loading.""" jac_machine = JacMachine(__file__) - jac_import("fixtures.hello_world", base_path=__file__, jac_machine=jac_machine) + jac_import("fixtures.hello_world", base_path=__file__, is_origin=True) self.assertIn( "module 'fixtures.hello_world'", str(jac_machine.loaded_modules), diff --git a/jaclang/plugin/default.py b/jaclang/plugin/default.py index b4bc98c1c..5b8fd7127 100644 --- a/jaclang/plugin/default.py +++ b/jaclang/plugin/default.py @@ -79,6 +79,18 @@ def get_datasource() -> Memory: """Get current execution context.""" return ExecutionContext.get().datasource + @staticmethod + @hookimpl + def get_machine(base_path: str) -> JacMachine: + """Get current execution context.""" + return JacMachine.get(base_path) + + @staticmethod + @hookimpl + def detach_machine() -> None: + """Detach current jac machine.""" + JacMachine.detach() + @staticmethod @hookimpl def make_architype( @@ -238,8 +250,7 @@ def jac_import( lng: Optional[str], items: Optional[dict[str, Union[str, Optional[str]]]], reload_module: Optional[bool], - jac_machine: Optional[JacMachine], - jac_program: Optional[JacProgram], + is_origin: bool, ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" spec = ImportPathSpec( @@ -252,14 +263,19 @@ def jac_import( lng, items, ) - jac_machine = jac_machine or JacMachine(base_path) - jac_machine.attach_program( - jac_program or JacProgram(mod_bundle=None, bytecode=None) - ) + + jac_machine = Jac.get_machine(base_path) + if not jac_machine.jac_program: + jac_machine.attach_program(JacProgram(mod_bundle=None, bytecode=None)) + if lng == "py": import_result = PythonImporter(jac_machine).run_import(spec) else: import_result = JacImporter(jac_machine).run_import(spec, reload_module) + + if is_origin: + Jac.detach_machine() + return ( (import_result.ret_mod,) if absorb or not items diff --git a/jaclang/plugin/feature.py b/jaclang/plugin/feature.py index c0a4b858a..24bbef494 100644 --- a/jaclang/plugin/feature.py +++ b/jaclang/plugin/feature.py @@ -19,7 +19,7 @@ WalkerArchitype, ) from jaclang.runtimelib.context import ExecutionContext -from jaclang.runtimelib.machine import JacMachine, JacProgram +from jaclang.runtimelib.machine import JacMachine import pluggy @@ -53,6 +53,16 @@ def get_datasource() -> Memory: """Get current execution context.""" return pm.hook.get_datasource() + @staticmethod + def get_machine(base_path: str = "") -> JacMachine: + """Get current jac machine.""" + return pm.hook.get_machine(base_path=base_path) + + @staticmethod + def detach_machine() -> None: + """Detach current jac machine.""" + return pm.hook.detach_machine() + @staticmethod def make_architype( cls: type, @@ -111,8 +121,7 @@ def jac_import( lng: Optional[str] = "jac", items: Optional[dict[str, Union[str, Optional[str]]]] = None, reload_module: Optional[bool] = False, - jac_machine: Optional[JacMachine] = None, - jac_program: Optional[JacProgram] = None, + is_origin: bool = False, ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" return pm.hook.jac_import( @@ -125,8 +134,7 @@ def jac_import( lng=lng, items=items, reload_module=reload_module, - jac_machine=jac_machine, - jac_program=jac_program, + is_origin=is_origin, ) @staticmethod diff --git a/jaclang/plugin/spec.py b/jaclang/plugin/spec.py index 0edb2d710..a0031041d 100644 --- a/jaclang/plugin/spec.py +++ b/jaclang/plugin/spec.py @@ -30,7 +30,7 @@ ) from jaclang.runtimelib.constructs import EdgeArchitype, NodeAnchor, NodeArchitype from jaclang.runtimelib.context import ExecutionContext - from jaclang.runtimelib.machine import JacMachine, JacProgram + from jaclang.runtimelib.machine import JacMachine from jaclang.runtimelib.memory import Memory import pluggy @@ -56,6 +56,18 @@ def get_datasource() -> Memory: """Get current execution context datasource.""" raise NotImplementedError + @staticmethod + @hookspec(firstresult=True) + def get_machine(base_path: str) -> JacMachine: + """Get current jac machine.""" + raise NotImplementedError + + @staticmethod + @hookspec(firstresult=True) + def detach_machine() -> None: + """Detach current jac machine.""" + raise NotImplementedError + @staticmethod @hookspec(firstresult=True) def make_architype( @@ -119,8 +131,7 @@ def jac_import( lng: Optional[str], items: Optional[dict[str, Union[str, Optional[str]]]], reload_module: Optional[bool], - jac_machine: Optional[JacMachine], - jac_program: Optional[JacProgram], + is_origin: bool, ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" raise NotImplementedError diff --git a/jaclang/runtimelib/machine.py b/jaclang/runtimelib/machine.py index 9f6070654..e1d90873d 100644 --- a/jaclang/runtimelib/machine.py +++ b/jaclang/runtimelib/machine.py @@ -5,6 +5,7 @@ import os import sys import types +from contextvars import ContextVar from typing import Optional from jaclang.compiler.absyntree import Module @@ -17,6 +18,9 @@ logger = logging.getLogger(__name__) +JACMACHINE_CONTEXT = ContextVar["JacMachine | None"]("JacMachine") + + class JacMachine: """JacMachine to handle the VM-related functionalities and loaded programs.""" @@ -33,6 +37,8 @@ def __init__(self, base_path: str = "") -> None: ) self.jac_program: Optional[JacProgram] = None + JACMACHINE_CONTEXT.set(self) + def attach_program(self, jac_program: "JacProgram") -> None: """Attach a JacProgram to the machine.""" self.jac_program = jac_program @@ -99,6 +105,18 @@ def list_edges(self, module_name: str) -> list[str]: return nodes return [] + @staticmethod + def get(base_path: str = "") -> "JacMachine": + """Get current jac machine.""" + if (jac_machine := JACMACHINE_CONTEXT.get(None)) is None: + jac_machine = JacMachine(base_path) + return jac_machine + + @staticmethod + def detach() -> None: + """Detach current jac machine.""" + JACMACHINE_CONTEXT.set(None) + class JacProgram: """Class to hold the mod_bundle and bytecode for Jac modules.""" diff --git a/jaclang/tests/fixtures/foo.jac b/jaclang/tests/fixtures/foo.jac index c98576529..4f288f844 100644 --- a/jaclang/tests/fixtures/foo.jac +++ b/jaclang/tests/fixtures/foo.jac @@ -5,13 +5,13 @@ import:jac from bar, bar_walk; # Test runner to initialize the walker can test_run { # Print the loaded modules - modules = Jac.context().jac_machine.list_modules(); + modules = Jac.get_machine().list_modules(); "Loaded Modules:" |> print; for mod_name in modules { f"Module: {mod_name}" |> print; } # Print walkers - walkers = Jac.context().jac_machine.list_walkers(mod_name); + walkers = Jac.get_machine().list_walkers(mod_name); if walkers{ f"Walkers in {mod_name}:" |> print; for walker in walkers{ @@ -20,7 +20,7 @@ can test_run { } # Print nodes - nodes = Jac.context().jac_machine.list_nodes(mod_name); + nodes = Jac.get_machine().list_nodes(mod_name); if nodes{ f"Nodes in {mod_name}:" |> print; for node in nodes{ @@ -28,7 +28,7 @@ can test_run { } } # Print edges - edges = Jac.context().jac_machine.list_edges(mod_name); + edges = Jac.get_machine().list_edges(mod_name); if edges{ f"Edges in {mod_name}:" |> print; for edge in edges{ diff --git a/jaclang/tests/test_language.py b/jaclang/tests/test_language.py index 33eee04d9..39a702326 100644 --- a/jaclang/tests/test_language.py +++ b/jaclang/tests/test_language.py @@ -63,7 +63,9 @@ def test_simple_jac_red(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("micro.simple_walk", base_path=self.examples_abs_path("")) + jac_import( + "micro.simple_walk", base_path=self.examples_abs_path(""), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -76,7 +78,7 @@ def test_guess_game(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("guess_game", base_path=self.fixture_abs_path("./")) + jac_import("guess_game", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -88,7 +90,9 @@ def test_chandra_bugs(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("chandra_bugs", base_path=self.fixture_abs_path("./")) + jac_import( + "chandra_bugs", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -100,7 +104,9 @@ def test_chandra_bugs2(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("chandra_bugs2", base_path=self.fixture_abs_path("./")) + jac_import( + "chandra_bugs2", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -115,7 +121,7 @@ def test_ignore(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("ignore_dup", base_path=self.fixture_abs_path("./")) + jac_import("ignore_dup", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.split("\n")[0].count("here"), 10) @@ -125,7 +131,9 @@ def test_dataclass_hasability(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("hashcheck_dup", base_path=self.fixture_abs_path("./")) + jac_import( + "hashcheck_dup", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count("check"), 2) @@ -144,7 +152,9 @@ def test_need_import(self) -> None: """Test importing python.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("needs_import", base_path=self.fixture_abs_path("./")) + jac_import( + "needs_import", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn(" None: """Test the dot gen of nodes and edges of bubblesort.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("gendot_bubble_sort", base_path=self.fixture_abs_path("./")) + jac_import( + "gendot_bubble_sort", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn( @@ -177,7 +189,9 @@ def test_assign_compr(self) -> None: """Test assign_compr.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("assign_compr_dup", base_path=self.fixture_abs_path("./")) + jac_import( + "assign_compr_dup", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -189,7 +203,7 @@ def test_semstr(self) -> None: """Test semstring.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("semstr", base_path=self.fixture_abs_path("./")) + jac_import("semstr", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertNotIn("Error", stdout_value) @@ -198,7 +212,9 @@ def test_raw_bytestr(self) -> None: """Test raw string and byte string.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("raw_byte_string", base_path=self.fixture_abs_path("./")) + jac_import( + "raw_byte_string", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count(r"\\\\"), 2) @@ -209,7 +225,7 @@ def test_deep_imports(self) -> None: captured_output = io.StringIO() sys.stdout = captured_output - jac_import("deep_import", base_path=self.fixture_abs_path("./")) + jac_import("deep_import", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.split("\n")[0], "one level deeperslHello World!") @@ -228,9 +244,7 @@ def test_deep_imports_mods(self) -> None: del sys.modules[i] jac_machine = JacMachine(self.fixture_abs_path("./")) jac_import( - "deep_import_mods", - base_path=self.fixture_abs_path("./"), - jac_machine=jac_machine, + "deep_import_mods", base_path=self.fixture_abs_path("./"), is_origin=True ) mods = jac_machine.loaded_modules.keys() for i in targets: @@ -242,7 +256,9 @@ def test_deep_outer_imports_one(self) -> None: captured_output = io.StringIO() sys.stdout = captured_output jac_import( - "deep.deeper.deep_outer_import", base_path=self.fixture_abs_path("./") + "deep.deeper.deep_outer_import", + base_path=self.fixture_abs_path("./"), + is_origin=True, ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() @@ -276,7 +292,9 @@ def test_has_lambda_goodness(self) -> None: """Test has lambda_goodness.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("has_goodness", base_path=self.fixture_abs_path("./")) + jac_import( + "has_goodness", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.split("\n")[0], "mylist: [1, 2, 3]") @@ -286,7 +304,7 @@ def test_conn_assign_on_edges(self) -> None: """Test conn assign on edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("edge_ops", base_path=self.fixture_abs_path("./")) + jac_import("edge_ops", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("[(3, 5), (14, 1), (5, 1)]", stdout_value) @@ -297,7 +315,7 @@ def test_disconnect(self) -> None: """Test conn assign on edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("disconn", base_path=self.fixture_abs_path("./")) + jac_import("disconn", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertIn("c(cc=0)", stdout_value[0]) @@ -314,7 +332,9 @@ def test_simple_archs(self) -> None: """Test conn assign on edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("simple_archs", base_path=self.fixture_abs_path("./")) + jac_import( + "simple_archs", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.split("\n")[0], "1 2 0") @@ -324,7 +344,7 @@ def test_edge_walk(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("edges_walk", base_path=self.fixture_abs_path("./")) + jac_import("edges_walk", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("creator()\n", stdout_value) @@ -337,7 +357,7 @@ def test_impl_grab(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("impl_grab", base_path=self.fixture_abs_path("./")) + jac_import("impl_grab", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("1.414", stdout_value) @@ -346,7 +366,7 @@ def test_tuple_of_tuple_assign(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("tuplytuples", base_path=self.fixture_abs_path("./")) + jac_import("tuplytuples", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn( @@ -358,7 +378,9 @@ def test_deferred_field(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("deferred_field", base_path=self.fixture_abs_path("./")) + jac_import( + "deferred_field", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn( @@ -370,7 +392,9 @@ def test_gen_dot_builtin(self) -> None: """Test the dot gen of nodes and edges as a builtin.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("builtin_dotgen", base_path=self.fixture_abs_path("./")) + jac_import( + "builtin_dotgen", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count("True"), 14) @@ -379,7 +403,9 @@ def test_with_contexts(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("with_context", base_path=self.fixture_abs_path("./")) + jac_import( + "with_context", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("im in", stdout_value) @@ -397,6 +423,7 @@ def test_typed_filter_compr(self) -> None: jac_import( "micro.typed_filter_compr", base_path=self.examples_abs_path(""), + is_origin=True, ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() @@ -411,7 +438,9 @@ def test_edge_node_walk(self) -> None: """Test walking through edges and nodes.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("edge_node_walk", base_path=self.fixture_abs_path("./")) + jac_import( + "edge_node_walk", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("creator()\n", stdout_value) @@ -430,7 +459,7 @@ def test_impl_decl_resolution_fix(self) -> None: """Test walking through edges and nodes.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("mtest", base_path=self.fixture_abs_path("./")) + jac_import("mtest", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("2.0\n", stdout_value) @@ -439,7 +468,7 @@ def test_registry(self) -> None: """Test Jac registry feature.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("registry", base_path=self.fixture_abs_path("./")) + jac_import("registry", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertNotIn("Error", stdout_value) @@ -462,7 +491,11 @@ def test_enum_inside_arch(self) -> None: """Test Enum as member stmt.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("enum_inside_archtype", base_path=self.fixture_abs_path("./")) + jac_import( + "enum_inside_archtype", + base_path=self.fixture_abs_path("./"), + is_origin=True, + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual("2\n", stdout_value) @@ -489,7 +522,9 @@ def test_needs_import_1(self) -> None: self.assertEqual(len(ir.get_all_sub_nodes(ast.Architype)), 7) captured_output = io.StringIO() sys.stdout = captured_output - jac_import("needs_import_1", base_path=self.fixture_abs_path("./")) + jac_import( + "needs_import_1", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("pyfunc_1 imported", stdout_value) @@ -549,7 +584,9 @@ def test_needs_import_2(self) -> None: ) # Because of the Architype from math captured_output = io.StringIO() sys.stdout = captured_output - jac_import("needs_import_2", base_path=self.fixture_abs_path("./")) + jac_import( + "needs_import_2", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("pyfunc_2 imported", stdout_value) @@ -597,7 +634,9 @@ def test_needs_import_3(self) -> None: ) # Because of the Architype from other imports captured_output = io.StringIO() sys.stdout = captured_output - jac_import("needs_import_3", base_path=self.fixture_abs_path("./")) + jac_import( + "needs_import_3", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("pyfunc_3 imported", stdout_value) @@ -662,7 +701,9 @@ def test_inherit_check(self) -> None: """Test py ast to Jac ast conversion output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("inherit_check", base_path=self.fixture_abs_path("./")) + jac_import( + "inherit_check", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual("I am in b\nI am in b\nwww is also in b\n", stdout_value) @@ -671,7 +712,7 @@ def test_tuple_unpack(self) -> None: """Test tuple unpack.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("tupleunpack", base_path=self.fixture_abs_path("./")) + jac_import("tupleunpack", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertIn("1", stdout_value[0]) @@ -681,7 +722,7 @@ def test_try_finally(self) -> None: """Test try finally.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("try_finally", base_path=self.fixture_abs_path("./")) + jac_import("try_finally", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertIn("try block", stdout_value[0]) @@ -694,7 +735,9 @@ def test_arithmetic_bug(self) -> None: """Test arithmetic bug.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("arithmetic_bug", base_path=self.fixture_abs_path("./")) + jac_import( + "arithmetic_bug", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertEqual("0.0625", stdout_value[0]) @@ -707,7 +750,7 @@ def test_lambda_expr(self) -> None: """Test lambda expr.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("lambda", base_path=self.fixture_abs_path("./")) + jac_import("lambda", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertEqual("9", stdout_value[0]) @@ -786,7 +829,9 @@ def test_deep_convert(self) -> None: self.assertEqual(len(ir.get_all_sub_nodes(ast.SubNodeList)), 269) captured_output = io.StringIO() sys.stdout = captured_output - jac_import("deep_convert", base_path=self.fixture_abs_path("./")) + jac_import( + "deep_convert", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("Deep convo is imported", stdout_value) @@ -795,7 +840,9 @@ def test_override_walker_inherit(self) -> None: """Test py ast to Jac ast conversion output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("walker_override", base_path=self.fixture_abs_path("./")) + jac_import( + "walker_override", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual("baz\nbar\n", stdout_value) @@ -831,7 +878,7 @@ def test_self_with_no_sig(self) -> None: # we can get rid of this, isn't? """Test py ast to Jac ast conversion output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("nosigself", base_path=self.fixture_abs_path("./")) + jac_import("nosigself", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count("5"), 2) @@ -840,7 +887,9 @@ def test_hash_init_check(self) -> None: # we can get rid of this, isn't? """Test py ast to Jac ast conversion output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("hash_init_check", base_path=self.fixture_abs_path("./")) + jac_import( + "hash_init_check", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("Test Passed", stdout_value) @@ -889,7 +938,9 @@ def test_edge_expr_not_type(self) -> None: """Test importing python.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("edgetypeissue", base_path=self.fixture_abs_path("./")) + jac_import( + "edgetypeissue", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("[x()]", stdout_value) @@ -898,7 +949,9 @@ def test_blank_with_entry(self) -> None: """Test importing python.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("blankwithentry", base_path=self.fixture_abs_path("./")) + jac_import( + "blankwithentry", base_path=self.fixture_abs_path("./"), is_origin=True + ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("i work", stdout_value) @@ -907,7 +960,7 @@ def test_double_import_exec(self) -> None: """Test importing python.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("dblhello", base_path=self.fixture_abs_path("./")) + jac_import("dblhello", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count("Hello World!"), 1) @@ -917,7 +970,7 @@ def test_cls_method(self) -> None: """Test class method output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("cls_method", base_path=self.fixture_abs_path("./")) + jac_import("cls_method", base_path=self.fixture_abs_path("./"), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertEqual("MyClass", stdout_value[0]) @@ -929,28 +982,24 @@ def test_list_methods(self) -> None: captured_output = io.StringIO() sys.stdout = captured_output - jac_import("foo", base_path=self.fixture_abs_path(".")) + jac_import("foo", base_path=self.fixture_abs_path("."), is_origin=True) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn( - "Module: jaclang.tests.fixtures.foo", + "Module: foo", stdout_value, ) self.assertIn( - "Module: jaclang.tests.fixtures.bar", + "Module: bar", stdout_value, ) self.assertIn( - "Walkers in jaclang.tests.fixtures.bar:\n - Walker: bar_walk", + "Walkers in bar:\n - Walker: bar_walk", stdout_value, ) - self.assertIn( - "Nodes in jaclang.tests.fixtures.bar:\n - Node: Item", stdout_value - ) - self.assertIn( - "Edges in jaclang.tests.fixtures.bar:\n - Edge: Link", stdout_value - ) + self.assertIn("Nodes in bar:\n - Node: Item", stdout_value) + self.assertIn("Edges in bar:\n - Edge: Link", stdout_value) self.assertIn("Item value: 0", stdout_value) self.assertIn("Created 5 items.", stdout_value) diff --git a/jaclang/tests/test_reference.py b/jaclang/tests/test_reference.py index f8f117bee..b635d96a2 100644 --- a/jaclang/tests/test_reference.py +++ b/jaclang/tests/test_reference.py @@ -7,6 +7,7 @@ import jaclang from jaclang.compiler.compile import jac_file_to_pass +from jaclang.runtimelib.context import SUPER_ROOT_ANCHOR from jaclang.utils.test import TestCase @@ -51,6 +52,7 @@ def micro_suite_test(self, filename: str) -> None: """Test file.""" def execute_and_capture_output(code: str | bytes, filename: str = "") -> str: + SUPER_ROOT_ANCHOR.edges.clear() f = io.StringIO() with redirect_stdout(f): exec( From d8d20cb7261c56137d024ced40fd6c3e324844af Mon Sep 17 00:00:00 2001 From: Yiping Kang Date: Thu, 29 Aug 2024 12:49:45 -0400 Subject: [PATCH 3/3] Remove is_origin, rename datasource to mem. prune plugin methods --- jaclang/cli/cli.py | 16 ++- jaclang/compiler/tests/test_importer.py | 19 ++- jaclang/plugin/default.py | 31 +---- jaclang/plugin/feature.py | 19 --- jaclang/plugin/spec.py | 21 --- jaclang/runtimelib/architype.py | 10 +- jaclang/runtimelib/context.py | 10 +- jaclang/tests/fixtures/foo.jac | 33 +++-- jaclang/tests/test_language.py | 165 ++++++++---------------- 9 files changed, 109 insertions(+), 215 deletions(-) diff --git a/jaclang/cli/cli.py b/jaclang/cli/cli.py index a932be1af..5feaf6edb 100644 --- a/jaclang/cli/cli.py +++ b/jaclang/cli/cli.py @@ -93,14 +93,14 @@ def run( mod = mod[:-4] jctx = ExecutionContext.create(session=session) - if filename.endswith(".jac"): + JacMachine(base).attach_program(JacProgram(mod_bundle=None, bytecode=None)) + ret_module = jac_import( target=mod, base_path=base, cachable=cache, override_name="__main__" if main else None, - is_origin=True, ) if ret_module is None: @@ -117,7 +117,6 @@ def run( base_path=base, cachable=cache, override_name="__main__" if main else None, - is_origin=True, ) if ret_module is None: loaded_mod = None @@ -129,7 +128,7 @@ def run( if not node or node == "root": entrypoint: Architype = jctx.root.architype - elif obj := jctx.datasource.find_by_id(UUID(node)): + elif obj := jctx.mem.find_by_id(UUID(node)): entrypoint = obj.architype else: print(f"Entrypoint {node} not found.") @@ -145,6 +144,7 @@ def run( print(f"Walker {walker} not found.") jctx.close() + JacMachine.detach() @cmd_registry.register @@ -158,7 +158,7 @@ def get_object(id: str, session: str = "") -> dict: data = {} if id == "root": data = jctx.root.__getstate__() - elif obj := jctx.datasource.find_by_id(UUID(id)): + elif obj := jctx.mem.find_by_id(UUID(id)): data = obj.__getstate__() else: print(f"Object with id {id} not found.") @@ -238,13 +238,15 @@ def enter( base, mod_name = os.path.split(filename) base = base if base else "./" mod_name = mod_name[:-4] - (mod,) = jac_import(target=mod_name, base_path=base, is_origin=True) + (mod,) = jac_import(target=mod_name, base_path=base) + JacMachine(base).attach_program(JacProgram(mod_bundle=None, bytecode=None)) if not mod: print("Errors occurred while importing the module.") else: architype = getattr(mod, entrypoint)(*args) if isinstance(architype, WalkerArchitype): Jac.spawn_call(jctx.entry.architype, architype) + JacMachine.detach() else: print("Not a .jac file.") @@ -391,7 +393,7 @@ def dot( if filename.endswith(".jac"): jac_machine = JacMachine(base) - jac_import(target=mod, base_path=base, is_origin=True) + jac_import(target=mod, base_path=base) module = jac_machine.loaded_modules.get(mod) globals().update(vars(module)) try: diff --git a/jaclang/compiler/tests/test_importer.py b/jaclang/compiler/tests/test_importer.py index c7b630647..135481918 100644 --- a/jaclang/compiler/tests/test_importer.py +++ b/jaclang/compiler/tests/test_importer.py @@ -5,7 +5,7 @@ from jaclang import jac_import from jaclang.cli import cli -from jaclang.runtimelib.machine import JacMachine +from jaclang.runtimelib.machine import JacMachine, JacProgram from jaclang.utils.test import TestCase @@ -14,21 +14,28 @@ class TestLoader(TestCase): def test_import_basic_python(self) -> None: """Test basic self loading.""" - (h,) = jac_import("fixtures.hello_world", base_path=__file__, is_origin=True) + JacMachine(self.fixture_abs_path(__file__)).attach_program( + JacProgram(mod_bundle=None, bytecode=None) + ) + (h,) = jac_import("fixtures.hello_world", base_path=__file__) self.assertEqual(h.hello(), "Hello World!") # type: ignore + JacMachine.detach() def test_modules_correct(self) -> None: """Test basic self loading.""" - jac_machine = JacMachine(__file__) - jac_import("fixtures.hello_world", base_path=__file__, is_origin=True) + JacMachine(self.fixture_abs_path(__file__)).attach_program( + JacProgram(mod_bundle=None, bytecode=None) + ) + jac_import("fixtures.hello_world", base_path=__file__) self.assertIn( "module 'fixtures.hello_world'", - str(jac_machine.loaded_modules), + str(JacMachine.get().loaded_modules), ) self.assertIn( "/tests/fixtures/hello_world.jac", - str(jac_machine.loaded_modules), + str(JacMachine.get().loaded_modules), ) + JacMachine.detach() def test_jac_py_import(self) -> None: """Basic test for pass.""" diff --git a/jaclang/plugin/default.py b/jaclang/plugin/default.py index 5b8fd7127..37cb3190e 100644 --- a/jaclang/plugin/default.py +++ b/jaclang/plugin/default.py @@ -26,7 +26,6 @@ ExecutionContext, GenericEdge, JacTestCheck, - Memory, NodeAnchor, NodeArchitype, Root, @@ -73,24 +72,6 @@ def get_context() -> ExecutionContext: """Get current execution context.""" return ExecutionContext.get() - @staticmethod - @hookimpl - def get_datasource() -> Memory: - """Get current execution context.""" - return ExecutionContext.get().datasource - - @staticmethod - @hookimpl - def get_machine(base_path: str) -> JacMachine: - """Get current execution context.""" - return JacMachine.get(base_path) - - @staticmethod - @hookimpl - def detach_machine() -> None: - """Detach current jac machine.""" - JacMachine.detach() - @staticmethod @hookimpl def make_architype( @@ -250,7 +231,6 @@ def jac_import( lng: Optional[str], items: Optional[dict[str, Union[str, Optional[str]]]], reload_module: Optional[bool], - is_origin: bool, ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" spec = ImportPathSpec( @@ -264,17 +244,16 @@ def jac_import( items, ) - jac_machine = Jac.get_machine(base_path) + jac_machine = JacMachine.get(base_path) if not jac_machine.jac_program: jac_machine.attach_program(JacProgram(mod_bundle=None, bytecode=None)) if lng == "py": - import_result = PythonImporter(jac_machine).run_import(spec) + import_result = PythonImporter(JacMachine.get()).run_import(spec) else: - import_result = JacImporter(jac_machine).run_import(spec, reload_module) - - if is_origin: - Jac.detach_machine() + import_result = JacImporter(JacMachine.get()).run_import( + spec, reload_module + ) return ( (import_result.ret_mod,) diff --git a/jaclang/plugin/feature.py b/jaclang/plugin/feature.py index 24bbef494..a862be7e9 100644 --- a/jaclang/plugin/feature.py +++ b/jaclang/plugin/feature.py @@ -12,14 +12,12 @@ from jaclang.runtimelib.constructs import ( Architype, EdgeArchitype, - Memory, NodeAnchor, NodeArchitype, Root, WalkerArchitype, ) from jaclang.runtimelib.context import ExecutionContext -from jaclang.runtimelib.machine import JacMachine import pluggy @@ -48,21 +46,6 @@ def get_context() -> ExecutionContext: """Get current execution context.""" return pm.hook.get_context() - @staticmethod - def get_datasource() -> Memory: - """Get current execution context.""" - return pm.hook.get_datasource() - - @staticmethod - def get_machine(base_path: str = "") -> JacMachine: - """Get current jac machine.""" - return pm.hook.get_machine(base_path=base_path) - - @staticmethod - def detach_machine() -> None: - """Detach current jac machine.""" - return pm.hook.detach_machine() - @staticmethod def make_architype( cls: type, @@ -121,7 +104,6 @@ def jac_import( lng: Optional[str] = "jac", items: Optional[dict[str, Union[str, Optional[str]]]] = None, reload_module: Optional[bool] = False, - is_origin: bool = False, ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" return pm.hook.jac_import( @@ -134,7 +116,6 @@ def jac_import( lng=lng, items=items, reload_module=reload_module, - is_origin=is_origin, ) @staticmethod diff --git a/jaclang/plugin/spec.py b/jaclang/plugin/spec.py index a0031041d..e230023ba 100644 --- a/jaclang/plugin/spec.py +++ b/jaclang/plugin/spec.py @@ -30,8 +30,6 @@ ) from jaclang.runtimelib.constructs import EdgeArchitype, NodeAnchor, NodeArchitype from jaclang.runtimelib.context import ExecutionContext - from jaclang.runtimelib.machine import JacMachine - from jaclang.runtimelib.memory import Memory import pluggy @@ -50,24 +48,6 @@ def get_context() -> ExecutionContext: """Get current execution context.""" raise NotImplementedError - @staticmethod - @hookspec(firstresult=True) - def get_datasource() -> Memory: - """Get current execution context datasource.""" - raise NotImplementedError - - @staticmethod - @hookspec(firstresult=True) - def get_machine(base_path: str) -> JacMachine: - """Get current jac machine.""" - raise NotImplementedError - - @staticmethod - @hookspec(firstresult=True) - def detach_machine() -> None: - """Detach current jac machine.""" - raise NotImplementedError - @staticmethod @hookspec(firstresult=True) def make_architype( @@ -131,7 +111,6 @@ def jac_import( lng: Optional[str], items: Optional[dict[str, Union[str, Optional[str]]]], reload_module: Optional[bool], - is_origin: bool, ) -> tuple[types.ModuleType, ...]: """Core Import Process.""" raise NotImplementedError diff --git a/jaclang/runtimelib/architype.py b/jaclang/runtimelib/architype.py index c6cd560e5..e6a1111dc 100644 --- a/jaclang/runtimelib/architype.py +++ b/jaclang/runtimelib/architype.py @@ -37,13 +37,13 @@ def save(self) -> None: from jaclang.plugin.feature import JacFeature as Jac self.persistent = True - Jac.get_datasource().set(self.id, self) + Jac.get_context().mem.set(self.id, self) def destroy(self) -> None: """Destroy Anchor.""" from jaclang.plugin.feature import JacFeature as Jac - Jac.get_datasource().remove(self.id) + Jac.get_context().mem.remove(self.id) def is_populated(self) -> bool: """Check if state.""" @@ -61,7 +61,7 @@ def populate(self) -> None: """Retrieve the Architype from db and return.""" from jaclang.plugin.feature import JacFeature as Jac - jsrc = Jac.get_datasource() + jsrc = Jac.get_context().mem if anchor := jsrc.find_by_id(self.id): self.__dict__.update(anchor.__dict__) @@ -245,7 +245,7 @@ def destroy(self) -> None: for edge in self.edges: edge.destroy() - Jac.get_datasource().remove(self.id) + Jac.get_context().mem.remove(self.id) def __getstate__(self) -> dict[str, object]: """Serialize Node Anchor.""" @@ -285,7 +285,7 @@ def destroy(self) -> None: from jaclang.plugin.feature import JacFeature as Jac self.detach() - Jac.get_datasource().remove(self.id) + Jac.get_context().mem.remove(self.id) def __getstate__(self) -> dict[str, object]: """Serialize Node Anchor.""" diff --git a/jaclang/runtimelib/context.py b/jaclang/runtimelib/context.py index e4c308c3e..e24d58c1c 100644 --- a/jaclang/runtimelib/context.py +++ b/jaclang/runtimelib/context.py @@ -24,7 +24,7 @@ class ExecutionContext: """Execution Context.""" - datasource: Memory + mem: Memory reports: list[Any] system_root: NodeAnchor root: NodeAnchor @@ -37,7 +37,7 @@ def generate_system_root(self) -> NodeAnchor: id=UUID(SUPER_ROOT_UUID), architype=architype, persistent=True, edges=[] ) architype.__jac__ = anchor - self.datasource.set(anchor.id, anchor) + self.mem.set(anchor.id, anchor) return anchor def init_anchor( @@ -47,14 +47,14 @@ def init_anchor( ) -> NodeAnchor: """Load initial anchors.""" if anchor_id and isinstance( - anchor := self.datasource.find_by_id(UUID(anchor_id)), NodeAnchor + anchor := self.mem.find_by_id(UUID(anchor_id)), NodeAnchor ): return anchor return default() if callable(default) else default def close(self) -> None: """Close current ExecutionContext.""" - self.datasource.close() + self.mem.close() EXECUTION_CONTEXT.set(None) @staticmethod @@ -66,7 +66,7 @@ def create( ) -> ExecutionContext: """Create ExecutionContext.""" ctx = ExecutionContext() - ctx.datasource = ShelfStorage(session) + ctx.mem = ShelfStorage(session) ctx.reports = [] ctx.system_root = ctx.init_anchor(SUPER_ROOT_UUID, ctx.generate_system_root) ctx.root = ctx.init_anchor(root, ctx.system_root) diff --git a/jaclang/tests/fixtures/foo.jac b/jaclang/tests/fixtures/foo.jac index 4f288f844..4b3d756cf 100644 --- a/jaclang/tests/fixtures/foo.jac +++ b/jaclang/tests/fixtures/foo.jac @@ -1,44 +1,43 @@ import:py from jaclang.plugin.feature, JacFeature as Jac; +import:py from jaclang.runtimelib.machine, JacMachine; import:jac from bar, bar_walk; - - # Test runner to initialize the walker + can test_run { # Print the loaded modules - modules = Jac.get_machine().list_modules(); + modules = JacMachine.get().list_modules(); "Loaded Modules:" |> print; for mod_name in modules { f"Module: {mod_name}" |> print; } # Print walkers - walkers = Jac.get_machine().list_walkers(mod_name); - if walkers{ - f"Walkers in {mod_name}:" |> print; - for walker in walkers{ - f" - Walker: {walker}" |> print; - } + walkers = JacMachine.get().list_walkers(mod_name); + if walkers { + f"Walkers in {mod_name}:" |> print; + for walker in walkers { + f" - Walker: {walker}" |> print; } - + } # Print nodes - nodes = Jac.get_machine().list_nodes(mod_name); - if nodes{ + nodes = JacMachine.get().list_nodes(mod_name); + if nodes { f"Nodes in {mod_name}:" |> print; - for node in nodes{ + for node in nodes { f" - Node: {node}" |> print; } } # Print edges - edges = Jac.get_machine().list_edges(mod_name); - if edges{ + edges = JacMachine.get().list_edges(mod_name); + if edges { f"Edges in {mod_name}:" |> print; - for edge in edges{ + for edge in edges { f" - Edge: {edge}" |> print; } } root spawn bar_walk(); } - # Define the entry point to run the test + with entry { test_run(); } diff --git a/jaclang/tests/test_language.py b/jaclang/tests/test_language.py index 39a702326..99d77400f 100644 --- a/jaclang/tests/test_language.py +++ b/jaclang/tests/test_language.py @@ -13,7 +13,7 @@ from jaclang.compiler.compile import jac_file_to_pass, jac_pass_to_pass, jac_str_to_pass from jaclang.compiler.passes.main.schedules import py_code_gen_typed from jaclang.runtimelib.context import SUPER_ROOT_ANCHOR -from jaclang.runtimelib.machine import JacMachine +from jaclang.runtimelib.machine import JacMachine, JacProgram from jaclang.utils.test import TestCase @@ -23,8 +23,16 @@ class JacLanguageTests(TestCase): def setUp(self) -> None: """Set up test.""" SUPER_ROOT_ANCHOR.edges.clear() + JacMachine(self.fixture_abs_path("./")).attach_program( + JacProgram(mod_bundle=None, bytecode=None) + ) return super().setUp() + def tearDown(self) -> None: + """Tear down test.""" + JacMachine.detach() + return super().tearDown() + def test_sub_abilities(self) -> None: """Basic test for pass.""" captured_output = io.StringIO() @@ -63,9 +71,7 @@ def test_simple_jac_red(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "micro.simple_walk", base_path=self.examples_abs_path(""), is_origin=True - ) + jac_import("micro.simple_walk", base_path=self.examples_abs_path("")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -78,7 +84,7 @@ def test_guess_game(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("guess_game", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("guess_game", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -90,9 +96,7 @@ def test_chandra_bugs(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "chandra_bugs", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("chandra_bugs", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -104,9 +108,7 @@ def test_chandra_bugs2(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "chandra_bugs2", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("chandra_bugs2", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -121,7 +123,7 @@ def test_ignore(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("ignore_dup", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("ignore_dup", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.split("\n")[0].count("here"), 10) @@ -131,9 +133,7 @@ def test_dataclass_hasability(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "hashcheck_dup", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("hashcheck_dup", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count("check"), 2) @@ -152,9 +152,7 @@ def test_need_import(self) -> None: """Test importing python.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "needs_import", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("needs_import", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn(" None: """Test the dot gen of nodes and edges of bubblesort.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "gendot_bubble_sort", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("gendot_bubble_sort", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn( @@ -189,9 +185,7 @@ def test_assign_compr(self) -> None: """Test assign_compr.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "assign_compr_dup", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("assign_compr_dup", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual( @@ -203,7 +197,7 @@ def test_semstr(self) -> None: """Test semstring.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("semstr", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("semstr", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertNotIn("Error", stdout_value) @@ -212,9 +206,7 @@ def test_raw_bytestr(self) -> None: """Test raw string and byte string.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "raw_byte_string", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("raw_byte_string", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count(r"\\\\"), 2) @@ -225,7 +217,7 @@ def test_deep_imports(self) -> None: captured_output = io.StringIO() sys.stdout = captured_output - jac_import("deep_import", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("deep_import", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.split("\n")[0], "one level deeperslHello World!") @@ -242,11 +234,8 @@ def test_deep_imports_mods(self) -> None: for i in targets: if i in sys.modules: del sys.modules[i] - jac_machine = JacMachine(self.fixture_abs_path("./")) - jac_import( - "deep_import_mods", base_path=self.fixture_abs_path("./"), is_origin=True - ) - mods = jac_machine.loaded_modules.keys() + jac_import("deep_import_mods", base_path=self.fixture_abs_path("./")) + mods = JacMachine.get().loaded_modules.keys() for i in targets: self.assertIn(i, mods) self.assertEqual(len([i for i in mods if i.startswith("deep")]), 6) @@ -256,9 +245,7 @@ def test_deep_outer_imports_one(self) -> None: captured_output = io.StringIO() sys.stdout = captured_output jac_import( - "deep.deeper.deep_outer_import", - base_path=self.fixture_abs_path("./"), - is_origin=True, + "deep.deeper.deep_outer_import", base_path=self.fixture_abs_path("./") ) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() @@ -292,9 +279,7 @@ def test_has_lambda_goodness(self) -> None: """Test has lambda_goodness.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "has_goodness", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("has_goodness", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.split("\n")[0], "mylist: [1, 2, 3]") @@ -304,7 +289,7 @@ def test_conn_assign_on_edges(self) -> None: """Test conn assign on edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("edge_ops", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("edge_ops", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("[(3, 5), (14, 1), (5, 1)]", stdout_value) @@ -315,7 +300,7 @@ def test_disconnect(self) -> None: """Test conn assign on edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("disconn", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("disconn", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertIn("c(cc=0)", stdout_value[0]) @@ -332,9 +317,7 @@ def test_simple_archs(self) -> None: """Test conn assign on edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "simple_archs", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("simple_archs", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.split("\n")[0], "1 2 0") @@ -344,7 +327,7 @@ def test_edge_walk(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("edges_walk", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("edges_walk", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("creator()\n", stdout_value) @@ -357,7 +340,7 @@ def test_impl_grab(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("impl_grab", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("impl_grab", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("1.414", stdout_value) @@ -366,7 +349,7 @@ def test_tuple_of_tuple_assign(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("tuplytuples", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("tuplytuples", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn( @@ -378,9 +361,7 @@ def test_deferred_field(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "deferred_field", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("deferred_field", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn( @@ -392,9 +373,7 @@ def test_gen_dot_builtin(self) -> None: """Test the dot gen of nodes and edges as a builtin.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "builtin_dotgen", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("builtin_dotgen", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count("True"), 14) @@ -403,9 +382,7 @@ def test_with_contexts(self) -> None: """Test walking through edges.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "with_context", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("with_context", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("im in", stdout_value) @@ -420,11 +397,7 @@ def test_typed_filter_compr(self) -> None: """Parse micro jac file.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "micro.typed_filter_compr", - base_path=self.examples_abs_path(""), - is_origin=True, - ) + jac_import("micro.typed_filter_compr", base_path=self.examples_abs_path("")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn( @@ -438,9 +411,7 @@ def test_edge_node_walk(self) -> None: """Test walking through edges and nodes.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "edge_node_walk", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("edge_node_walk", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("creator()\n", stdout_value) @@ -459,7 +430,7 @@ def test_impl_decl_resolution_fix(self) -> None: """Test walking through edges and nodes.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("mtest", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("mtest", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("2.0\n", stdout_value) @@ -468,7 +439,7 @@ def test_registry(self) -> None: """Test Jac registry feature.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("registry", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("registry", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertNotIn("Error", stdout_value) @@ -491,11 +462,7 @@ def test_enum_inside_arch(self) -> None: """Test Enum as member stmt.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "enum_inside_archtype", - base_path=self.fixture_abs_path("./"), - is_origin=True, - ) + jac_import("enum_inside_archtype", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual("2\n", stdout_value) @@ -522,9 +489,7 @@ def test_needs_import_1(self) -> None: self.assertEqual(len(ir.get_all_sub_nodes(ast.Architype)), 7) captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "needs_import_1", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("needs_import_1", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("pyfunc_1 imported", stdout_value) @@ -584,9 +549,7 @@ def test_needs_import_2(self) -> None: ) # Because of the Architype from math captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "needs_import_2", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("needs_import_2", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("pyfunc_2 imported", stdout_value) @@ -634,9 +597,7 @@ def test_needs_import_3(self) -> None: ) # Because of the Architype from other imports captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "needs_import_3", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("needs_import_3", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("pyfunc_3 imported", stdout_value) @@ -701,9 +662,7 @@ def test_inherit_check(self) -> None: """Test py ast to Jac ast conversion output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "inherit_check", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("inherit_check", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual("I am in b\nI am in b\nwww is also in b\n", stdout_value) @@ -712,7 +671,7 @@ def test_tuple_unpack(self) -> None: """Test tuple unpack.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("tupleunpack", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("tupleunpack", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertIn("1", stdout_value[0]) @@ -722,7 +681,7 @@ def test_try_finally(self) -> None: """Test try finally.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("try_finally", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("try_finally", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertIn("try block", stdout_value[0]) @@ -735,9 +694,7 @@ def test_arithmetic_bug(self) -> None: """Test arithmetic bug.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "arithmetic_bug", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("arithmetic_bug", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertEqual("0.0625", stdout_value[0]) @@ -750,7 +707,7 @@ def test_lambda_expr(self) -> None: """Test lambda expr.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("lambda", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("lambda", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertEqual("9", stdout_value[0]) @@ -829,9 +786,7 @@ def test_deep_convert(self) -> None: self.assertEqual(len(ir.get_all_sub_nodes(ast.SubNodeList)), 269) captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "deep_convert", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("deep_convert", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("Deep convo is imported", stdout_value) @@ -840,9 +795,7 @@ def test_override_walker_inherit(self) -> None: """Test py ast to Jac ast conversion output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "walker_override", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("walker_override", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual("baz\nbar\n", stdout_value) @@ -878,7 +831,7 @@ def test_self_with_no_sig(self) -> None: # we can get rid of this, isn't? """Test py ast to Jac ast conversion output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("nosigself", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("nosigself", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count("5"), 2) @@ -887,9 +840,7 @@ def test_hash_init_check(self) -> None: # we can get rid of this, isn't? """Test py ast to Jac ast conversion output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "hash_init_check", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("hash_init_check", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("Test Passed", stdout_value) @@ -938,9 +889,7 @@ def test_edge_expr_not_type(self) -> None: """Test importing python.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "edgetypeissue", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("edgetypeissue", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("[x()]", stdout_value) @@ -949,9 +898,7 @@ def test_blank_with_entry(self) -> None: """Test importing python.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import( - "blankwithentry", base_path=self.fixture_abs_path("./"), is_origin=True - ) + jac_import("blankwithentry", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertIn("i work", stdout_value) @@ -960,7 +907,7 @@ def test_double_import_exec(self) -> None: """Test importing python.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("dblhello", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("dblhello", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue() self.assertEqual(stdout_value.count("Hello World!"), 1) @@ -970,7 +917,7 @@ def test_cls_method(self) -> None: """Test class method output.""" captured_output = io.StringIO() sys.stdout = captured_output - jac_import("cls_method", base_path=self.fixture_abs_path("./"), is_origin=True) + jac_import("cls_method", base_path=self.fixture_abs_path("./")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue().split("\n") self.assertEqual("MyClass", stdout_value[0]) @@ -982,7 +929,7 @@ def test_list_methods(self) -> None: captured_output = io.StringIO() sys.stdout = captured_output - jac_import("foo", base_path=self.fixture_abs_path("."), is_origin=True) + jac_import("foo", base_path=self.fixture_abs_path(".")) sys.stdout = sys.__stdout__ stdout_value = captured_output.getvalue()