diff --git a/beetsplug/inline.py b/beetsplug/inline.py index 48e45e5b6..ee5b38e08 100644 --- a/beetsplug/inline.py +++ b/beetsplug/inline.py @@ -18,9 +18,19 @@ import logging import traceback from beets.plugins import BeetsPlugin +from beets import ui log = logging.getLogger('beets') +class InlineError(Exception): + """Raised when a runtime error occurs in an inline expression. + """ + def __init__(self, expr, exc): + super(InlineError, self).__init__( + (u"error in inline path field expression:\n" \ + u"%s\n%s: %s") % (expr, type(exc).__name__, unicode(exc)) + ) + def compile_expr(expr): """Given a Python expression, compile it as a path field function. The returned function takes a single argument, an Item, and returns @@ -35,7 +45,11 @@ def compile_expr(expr): return None def field_func(item): - return eval(code, dict(item.record)) + values = dict(item.record) + try: + return eval(code, values) + except Exception, exc: + raise InlineError(expr, exc) return field_func class InlinePlugin(BeetsPlugin): diff --git a/docs/changelog.rst b/docs/changelog.rst index 37e043b3e..d2829e93e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -15,6 +15,8 @@ Changelog instead of 24. * The autotagger now also tolerates tracks whose track artists tags are set to "Various Artists". +* The ``inline`` plugin now prints a more comprehensible error when exceptions + occur in Python snippets. * Fix a bug in the ``rewrite`` plugin that broke the use of multiple rules for a single field. * Fix a crash with non-ASCII characters in bytestring metadata fields (e.g.,