From 3985526c054f7d5a8c944b3af1a38a6d4c4cc88e Mon Sep 17 00:00:00 2001 From: Antonio Larrosa Date: Thu, 2 Aug 2018 17:38:25 +0200 Subject: [PATCH] PICARD-1306: Fix crash opening the options dialog if cwd doesn't exist Handle FileNotFoundError exceptions raised from os.path.abspath and os.path.realpath used by the options dialog for the examples in the file naming tab. Fixes PICARD-1306 --- picard/file.py | 8 +++++++- picard/util/filenaming.py | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/picard/file.py b/picard/file.py index 410c630d3..53a1e1ec5 100644 --- a/picard/file.py +++ b/picard/file.py @@ -369,7 +369,13 @@ class File(QtCore.QObject, Item): if sys.platform == "darwin": new_filename = unicodedata.normalize("NFD", new_filename) - return os.path.realpath(os.path.join(new_dirname, new_filename)) + new_path = os.path.join(new_dirname, new_filename) + try: + return os.path.realpath(new_path) + except FileNotFoundError: + # os.path.realpath can fail if cwd doesn't exist + return new_path + def _rename(self, old_filename, metadata): new_filename, ext = os.path.splitext( diff --git a/picard/util/filenaming.py b/picard/util/filenaming.py index 187bcfac8..6ae23affc 100644 --- a/picard/util/filenaming.py +++ b/picard/util/filenaming.py @@ -23,6 +23,7 @@ import struct import sys import unicodedata from picard.util import _io_encoding, decode_filename, encode_filename +from PyQt5.QtCore import QStandardPaths def _get_utf16_length(text): @@ -301,7 +302,12 @@ def make_short_filename(basedir, relpath, win_compat=False, relative_to=""): """ # only deal with absolute paths. it saves a lot of grief, # and is the right thing to do, even for renames. - basedir = os.path.abspath(basedir) + try: + basedir = os.path.abspath(basedir) + except FileNotFoundError: + # os.path.abspath raises an exception if basedir is a relative path and + # cwd doesn't exist anymore + basedir = QStandardPaths.writableLocation(QStandardPaths.MusicLocation) # also, make sure the relative path is clean relpath = os.path.normpath(relpath) if win_compat and relative_to: