Skip to content

Commit 179ed13

Browse files
committed
Say bye to util._fsencoding
1 parent 9801996 commit 179ed13

File tree

6 files changed

+8
-37
lines changed

6 files changed

+8
-37
lines changed

beets/util/__init__.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -376,21 +376,6 @@ def arg_encoding() -> str:
376376
return sys.getfilesystemencoding()
377377

378378

379-
def _fsencoding() -> str:
380-
"""Get the system's filesystem encoding. On Windows, this is always
381-
UTF-8 (not MBCS).
382-
"""
383-
encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
384-
if encoding == "mbcs":
385-
# On Windows, a broken encoding known to Python as "MBCS" is
386-
# used for the filesystem. However, we only use the Unicode API
387-
# for Windows paths, so the encoding is actually immaterial so
388-
# we can avoid dealing with this nastiness. We arbitrarily
389-
# choose UTF-8.
390-
encoding = "utf-8"
391-
return encoding
392-
393-
394379
def bytestring_path(path: PathLike) -> bytes:
395380
"""Given a path, which is either a bytes or a unicode, returns a str
396381
path (ensuring that we never deal with Unicode pathnames). Path should be
@@ -432,10 +417,7 @@ def displayable_path(
432417
# A non-string object: just get its unicode representation.
433418
return str(path)
434419

435-
try:
436-
return path.decode(_fsencoding(), "ignore")
437-
except (UnicodeError, LookupError):
438-
return path.decode("utf-8", "ignore")
420+
return os.fsdecode(path)
439421

440422

441423
def syspath(path: PathLike, prefix: bool = True) -> str:

beetsplug/ipfs.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from beets import config, library, ui, util
2222
from beets.plugins import BeetsPlugin
23-
from beets.util import syspath
2423

2524

2625
class IPFSPlugin(BeetsPlugin):
@@ -193,7 +192,7 @@ def ipfs_get_from_hash(self, lib, _hash):
193192
# This uses a relative path, hence we cannot use util.syspath(_hash,
194193
# prefix=True). However, that should be fine since the hash will not
195194
# exceed MAX_PATH.
196-
shutil.rmtree(syspath(_hash, prefix=False))
195+
shutil.rmtree(util.syspath(_hash, prefix=False))
197196

198197
def ipfs_publish(self, lib):
199198
with tempfile.NamedTemporaryFile() as tmp:
@@ -299,9 +298,7 @@ def create_new_album(self, album, tmplib):
299298
break
300299
except AttributeError:
301300
pass
302-
item_path = os.path.basename(item.path).decode(
303-
util._fsencoding(), "ignore"
304-
)
301+
item_path = os.fsdecode(os.path.basename(item.path))
305302
# Clear current path from item
306303
item.path = f"/ipfs/{album.ipfs}/{item_path}"
307304

beetsplug/thumbnails.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
from xdg import BaseDirectory
2929

30-
from beets import util
3130
from beets.plugins import BeetsPlugin
3231
from beets.ui import Subcommand, decargs
3332
from beets.util import bytestring_path, displayable_path, syspath
@@ -288,6 +287,6 @@ def uri(self, path):
288287
self.libgio.g_free(uri_ptr)
289288

290289
try:
291-
return uri.decode(util._fsencoding())
290+
return os.fsdecode(uri)
292291
except UnicodeDecodeError:
293292
raise RuntimeError(f"Could not decode filename from GIO: {uri!r}")

beetsplug/web/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,8 @@ def item_file(item_id):
315315
item_path = os.fsdecode(item.path)
316316

317317
base_filename = os.path.basename(item_path)
318-
# FIXME: Arguably, this should just use `displayable_path`: The latter
319-
# tries `_fsencoding()` first, but then falls back to `utf-8`, too.
320318
if isinstance(base_filename, bytes):
321-
try:
322-
unicode_base_filename = base_filename.decode("utf-8")
323-
except UnicodeError:
324-
unicode_base_filename = util.displayable_path(base_filename)
319+
unicode_base_filename = util.displayable_path(base_filename)
325320
else:
326321
unicode_base_filename = base_filename
327322

test/plugins/test_convert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def run_convert_path(self, path, *args):
147147
"""Run the `convert` command on a given path."""
148148
# The path is currently a filesystem bytestring. Convert it to
149149
# an argument bytestring.
150-
path = path.decode(util._fsencoding()).encode(util.arg_encoding())
150+
path = os.fsencode(os.fsdecode(path))
151151

152152
args = args + (b"path:" + path,)
153153
return self.run_command("convert", *args)

test/plugins/test_ipfs.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from beets.test import _common
1919
from beets.test.helper import PluginTestCase
20-
from beets.util import _fsencoding, bytestring_path
20+
from beets.util import bytestring_path
2121
from beetsplug.ipfs import IPFSPlugin
2222

2323

@@ -36,9 +36,7 @@ def test_stored_hashes(self):
3636
for check_item in added_album.items():
3737
try:
3838
if check_item.get("ipfs", with_album=False):
39-
ipfs_item = os.path.basename(want_item.path).decode(
40-
_fsencoding(),
41-
)
39+
ipfs_item = os.fsdecode(os.path.basename(want_item.path))
4240
want_path = "/ipfs/{}/{}".format(test_album.ipfs, ipfs_item)
4341
want_path = bytestring_path(want_path)
4442
assert check_item.path == want_path

0 commit comments

Comments
 (0)