Use fsdecode for template substitution

The idea in this PR is to converge on Python's `fsdecode` and `fsencode`
for argument manipulation. This seems to match up with the Python
standard library's assumptions: namely, on Windows, we use `fsdecode` to
get back to Unicode strings:
54bbb5e336/Lib/subprocess.py (L561)

So let's start by dropping this utility and going straight for
`fsdecode` here to match.
This commit is contained in:
Adrian Sampson 2022-10-01 16:37:47 -07:00
parent c8ab0cfa6b
commit 82d41446a2
No known key found for this signature in database
GPG key ID: BDB93AB409CC8705
3 changed files with 6 additions and 23 deletions

View file

@ -1092,21 +1092,3 @@ def lazy_property(func):
return value
return wrapper
def decode_commandline_path(path):
"""Prepare a path for substitution into commandline template.
On Python 3, we need to construct the subprocess commands to invoke as a
Unicode string. On Unix, this is a little unfortunate---the OS is
expecting bytes---so we use surrogate escaping and decode with the
argument encoding, which is the same encoding that will then be
*reversed* to recover the same bytes before invoking the OS. On
Windows, we want to preserve the Unicode filename "as is."
"""
# On Python 3, the template is a Unicode string, which only supports
# substitution of Unicode variables.
if platform.system() == 'Windows':
return path.decode(_fsencoding())
else:
return path.decode(arg_encoding(), 'surrogateescape')

View file

@ -14,7 +14,7 @@
"""Converts tracks or albums to external directory
"""
from beets.util import par_map, decode_commandline_path, arg_encoding
from beets.util import par_map, arg_encoding
import os
import threading
@ -214,8 +214,8 @@ class ConvertPlugin(BeetsPlugin):
self._log.info('Encoding {0}', util.displayable_path(source))
command = command.decode(arg_encoding(), 'surrogateescape')
source = decode_commandline_path(source)
dest = decode_commandline_path(dest)
source = os.fsdecode(source)
dest = os.fsdecode(dest)
# Substitute $source and $dest in the argument list.
args = shlex.split(command)

View file

@ -16,11 +16,12 @@
"""
import shlex
import os
from beets.plugins import BeetsPlugin
from beets.ui import decargs, print_, Subcommand, UserError
from beets.util import command_output, displayable_path, subprocess, \
bytestring_path, MoveOperation, decode_commandline_path
bytestring_path, MoveOperation
from beets.library import Item, Album
@ -200,7 +201,7 @@ class DuplicatesPlugin(BeetsPlugin):
output as flexattr on a key that is the name of the program, and
return the key, checksum tuple.
"""
args = [p.format(file=decode_commandline_path(item.path))
args = [p.format(file=os.fsdecode(item.path))
for p in shlex.split(prog)]
key = args[0]
checksum = getattr(item, key, False)