Skip to content

Commit

Permalink
Fix importer bug and error reporting problem
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JoshDreamland committed Dec 7, 2024
1 parent 2042138 commit 529704e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions yamlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 529704e

Please sign in to comment.