Skip to content

Commit

Permalink
inline: Fix a ridiculously subtle flexattr bug
Browse files Browse the repository at this point in the history
As detailed here:
#2406 (comment)

In a *function-style* definition, we didn't properly *un-define* the
values for a given item after each function invocation. So when a field
wasn't defined, it would get the value for the previously-formatted
object instead. It now properly throws a NameError.
  • Loading branch information
sampsyo committed Jun 1, 2019
1 parent cd66c5d commit 81b1faa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions beetsplug/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ def _expr_func(obj):
# For function bodies, invoke the function with values as global
# variables.
def _func_func(obj):
old_globals = dict(func.__globals__)
func.__globals__.update(_dict_for(obj))
try:
return func()
except Exception as exc:
raise InlineError(python_code, exc)
finally:
func.__globals__.clear()
func.__globals__.update(old_globals)
return _func_func
9 changes: 9 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ New features:
Thanks to :user:`dosoe`.
:bug:`2580` :bug:`3272`

Fixes:

* :doc:`/plugins/inline`: In function-style field definitions that refer to
flexible attributes, values could stick around from one function invocation
to the next. This meant that, when displaying a list of objects, later
objects could seem to reuse values from earlier objects when they were
missing a value for a given field. These values are now properly undefined.
:bug:`2406`

For plugin developers:

* `MediaFile`_ has been split into a standalone project. Where you used to do
Expand Down

1 comment on commit 81b1faa

@ctrueden
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super grateful to see this bug fixed! 🙏 It bit me when running beet move, causing inconsistent destinations depending on the item set.

Please sign in to comment.