Thom1729 / YAML-Macros

A macro system for YAML files powered by Python. Intended for Sublime Text development.
MIT License
21 stars 3 forks source link

TypeError: exceptions must derive from BaseException #35

Closed ratijas closed 4 years ago

ratijas commented 4 years ago

Summary

I've just started hacking on my first syntax definition, and I got this in console while "building" my *.sublime-syntax.yaml-macros definitions with YAMLMacros build system:

Traceback (most recent call last):
  File "/opt/sublime_text/sublime_plugin.py", line 1050, in run_
    return self.run(**args)
  File "$HOME/.config/sublime-text-3/Installed Packages/YAMLMacros.sublime-package/build_yaml_macros.py", line 24, in run
TypeError: exceptions must derive from BaseException

My syntax definitions starts like this:

%YAML 1.2
%TAG ! tag:yaml-macros:amazing_macros:
---
name: The Amazing Language
file_extensions: [amazing]
scope: source.amazing
first_line_match: \.\\\"
contexts:
  markups:
    - match: !wrap "**"
      scope: markup.bold.amazing

And the amazing_macros.py is almost empty:

def wrap(string):
    return string

Version

ratijas commented 4 years ago

Other times, it gives me error like this:

Building Amazing.sublime-syntax.yaml-macros... ($HOME/.config/sublime-text-3/Packages/The Amazing Language/Amazing.sublime-syntax.yaml-macros)

Error in macro execution.
  in "<file>", line 37, column 14

Traceback (most recent call last):
  File "$HOME/.config/sublime-text-3/Installed Packages/YAMLMacros.sublime-package/src/engine.py", line 69, in multi_constructor
    return apply_transformation(loader, node, macros[suffix])
  File "$HOME/.config/sublime-text-3/Installed Packages/YAMLMacros.sublime-package/src/engine.py", line 59, in apply_transformation
    return apply(transform, args)
  File "$HOME/.config/sublime-text-3/Installed Packages/YAMLMacros.sublime-package/src/util.py", line 18, in apply
    return fn(args)
TypeError: wrap() takes 0 positional arguments but 1 was given

[Failed in 0.05 seconds.]

~Looks like script is cached, and won't reload. Because I sure added a positional parameter there.~

UPD: There is a section on Automatic Plugin Reload at Sublime Text Community Documentation:

Sublime Text will reload topmost Python modules as they change (perhaps because you are editing a .py file within Packages). By contrast, Python subpackages won't be reloaded automatically, and this can lead to confusion while you're developing plugins. Generally speaking, it's best to restart Sublime Text after you've made changes to plugin files, so all changes can take effect.

ratijas commented 4 years ago

The line in question is clearly faulty. One must not raise a string in Python 3:

            if extension != '.yaml-macros': raise "Not a .yaml-macros file!"

https://github.com/Thom1729/YAML-Macros/blob/aa1956478799228a995f364f73dc526f7be92f41/build_yaml_macros.py#L24

I searched across all repository, and this appears to be the only misuse of the raise statement:

Searching 40 files for "raise"

/YAML-Macros/build_yaml_macros.py:
   22          if not destination_path:
   23              destination_path, extension = path.splitext(source_path)
   24:             if extension != '.yaml-macros': raise "Not a .yaml-macros file!"
   25  
   26          arguments['file_path'] = source_path

/YAML-Macros/lib/arguments.py:
   35          items = enumerate(collection)
   36      else:
   37:         raise TypeError('Invalid collection.')
   38  
   39      if as_:

/YAML-Macros/src/engine.py:
   64              macro = macros[suffix]
   65          except KeyError as e:
   66:             raise MacroError('Unknown macro "%s".' % suffix, node) from e
   67  
   68          try:
   69              return apply_transformation(loader, node, macros[suffix])
   70          except Exception as e:
   71:             raise MacroError('Error in macro execution.', node) from e
   72  
   73      return multi_constructor

/YAML-Macros/src/sublime_resources.py:
  113              return get_file_from_zip(full_path, sub_path)
  114  
  115:     raise IOError('resource not found')
  116  
  117  def get_files_in_zip(zipfile_path):

/YAML-Macros/src/util.py:
   63              collisions = (set(kwargs) & set(extras))
   64              if collisions:
   65:                 raise TypeError('Keyword parameters %s would be shadowed by raw macro parameters.' % str(collisions))
   66  
   67              return fn(**merge(kwargs, extras))
   ..
   71          for name, param in signature(fn).parameters.items()
   72      ):
   73:         raise TypeError('Raw macros using this decorator may not use **kwargs.')
   74  
   75      arg_names = { name for name, param in signature(fn).parameters.items() }

7 matches across 5 files
ratijas commented 4 years ago

Ideally, it should make use of an output panel, as presented within an Advanced Example at Sublime Text documentation. But unfortunately OutputPanel is not created until later in this method, at line 32:

https://github.com/Thom1729/YAML-Macros/blob/aa1956478799228a995f364f73dc526f7be92f41/build_yaml_macros.py#L28-L34

ratijas commented 4 years ago

@Thom1729 Are Pull Requests still welcome here? I might fix this, but I wanna be sure my PR won't stall forever.

Thom1729 commented 4 years ago

Absolutely, and thanks. I haven't done much work on this recently, and in the medium term I'm considering a complete rewrite when next-generation Package Control dependencies are sorted out, but for now, I'd appreciate the fix.

ratijas commented 4 years ago

Nice. The next step, I suppose, would be to wrap everything in try-catch and print exceptions to the output view.

It's kinda like FFI. At C/C++ boundaries, exceptions are typically "Not FFI-safe" — they cannot freely travel across the worlds they were not designed for. While Sublime Text plugins have little to do with C++ and FFI, we may still think of Python exceptions as "Not User-safe": you can't just let exception blow up your plugin's internals — it would not be user friendly.

I'll finish it tomorrow, I hope.

And hey!.. While we are at it, would you mind, please, tagging this repo with hacktoberfest topic or labeling #36 with hacktoberfest-accepted label? I mean, I'm hanging out at GitHub a lot anyway, so why not. Coincidentally, I have some improvements suggestions for the readme, but I promise they aren't bullsh|t. 🙃

ratijas commented 4 years ago

Hi Thomas @Thom1729,

I'm sorry to ping you like that, but I haven't heard from you neither 'yes' or 'no' about my previous comment. I assume, you might've simply missed it, since it was posted after this issue got closed by successful merge.

And hey!.. While we are at it, would you mind, please, tagging this repo with hacktoberfest topic or labeling #36 with hacktoberfest-accepted label? I mean, I'm hanging out at GitHub a lot anyway, so why not. Coincidentally, I have some improvements suggestions for the readme, but I promise they aren't bullsh|t. upside_down_face

In the meantime, I'm finally got myself to hack around the output buffer panel...

Thom1729 commented 4 years ago

@ratijas I've tagged #36. Please let me know if I need to do anything different with it.