Fix the edit plugin displaying bogus data or even crashing on re-imports #2659
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There seems to be a bug in the edit plugin when re-importing (
beet import -L
) items that are in the library already:As can be seen in the diff, the plugin assigns temporary
id
s to the items to be edited (presumably for applying the changes to the correct obejct inEditPlugin.apply_data
or because some function deeper in the guts of beets requires a non-None
id). This doesn't really cause any problems if none of the items is in the database:EditPlugin.edit_objects
will create adeepcopy
for any object with a false-yItem._db
and hand that tobeets.ui.show_model_changes
.If however
Item._db
is set,EditPlugin.edit_objects
will provideNone
as the old object tobeets.ui.show_model_changes
which will in turn query the database with the (invalid temporary!)id
of the new object to obtain the previous object and show the diff. Now, since theid
is invalid, two things can happen:id
actually exists: No crash will happen (and the edit finishes cleanly), but the old state for the diff will be a totally unrelatedItem
.None
, and the following traceback happens:I'd appreciate if someone could check the correctness of that analysis. I assumed negative temporary
id
s to be safe (see the code) since these items are never comitted to the database (which would create a newid
anyway). I'm not an exactly expert on the database code, though. Is that a reasonable assumption?(The actual context how I found this bug is somewhat different: I'm writing a plugin, that puts new
Item
s in the database in order to attach a few flexattrs. It then runs aTerminalImportSession
to fill in the remaining metadata. Using the eDit option in that situation triggers the same bug.)