From 529704e542a90a6088e2d1d1c65a38601529e69a Mon Sep 17 00:00:00 2001 From: Josh Ventura Date: Fri, 6 Dec 2024 21:58:01 -0500 Subject: [PATCH] Fix importer bug and error reporting problem Two issues: 1. File imports were not using the stream wrapper, so imported files needed spaces between `!else` and the following `:`. 2. An exception during the actual Ruamel parsing of a file import would end up resulting in a later exception insinuating that a circular import had occurred. A circular import should never actually occur... as far as I know it can't. Something would need to force the system to evaluate parts of the Yamlet logic at load time, which is expressly not how Yamlet works. --- yamlet.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/yamlet.py b/yamlet.py index b2273b5..e3897e1 100644 --- a/yamlet.py +++ b/yamlet.py @@ -170,7 +170,11 @@ def LoadCachedFile(self, fn): 'are deferred until name lookup.') return res self.loaded_modules[fn] = None - with open(fn) as file: res = self._ProcessYamlGcl(file) + with open(fn) as file: + try: res = self._ProcessYamlGcl(file) + except Exception as e: + self.loaded_modules.pop(fn) + raise self.loaded_modules[fn] = res return res @@ -208,7 +212,7 @@ def _ConstructElse(loader, node): f'Yamlet `!else` got unexpected node type: {repr(node)}') def _ProcessYamlGcl(self, ygcl): - tup = super().load(ygcl) + tup = super().load(_WrapStream(ygcl)) ectx = None while isinstance(tup, DeferredValue): if not ectx: @@ -219,7 +223,7 @@ def _ProcessYamlGcl(self, ygcl): def load_file(self, filename): with open(filename) as fn: return self.load(fn) - def load(self, yaml_gcl): return self._ProcessYamlGcl(_WrapStream(yaml_gcl)) + def load(self, yaml_gcl): return self._ProcessYamlGcl(yaml_gcl) class _DebugOpts: @@ -652,9 +656,9 @@ def _gcl_evaluate_(self, value, ectx): if not fn.exists(): if value == fn: ectx.Raise(FileNotFoundError, - f'Could not import YamlBcl file: {value}') + f'Could not import Yamlet file: {value}') ectx.Raise(FileNotFoundError, - f'Could not import YamlBcl file: `{fn}`\n' + f'Could not import Yamlet file: `{fn}`\n' f'As evaluated from this expression: `{value}`.\n') loaded = self._gcl_loader_(fn) return loaded