Merge pull request #319 from Freso/issue-315-fix-unicode-error-in-truncate_filename

Explicitly encode path as UTF-8 in truncate_filename()
This commit is contained in:
JoeLametta
2018-10-29 12:28:18 +00:00
committed by GitHub

View File

@@ -157,11 +157,11 @@ class MissingFrames(Exception):
def truncate_filename(path): def truncate_filename(path):
""" """
Truncate filename to the max. len. allowed by the path's filesystem Truncate filename to the max. len. allowed by the path's filesystem
Hopefully it handles Unicode strings correctly
""" """
p, f = os.path.split(os.path.normpath(path)) p, f = os.path.split(os.path.normpath(path))
f, e = os.path.splitext(f) f, e = os.path.splitext(f)
fn_lim = os.pathconf(p, 'PC_NAME_MAX') # max.filename length in bytes # Get the filename length limit in bytes
fn_lim = os.pathconf(p.encode('utf-8'), 'PC_NAME_MAX')
f_max = fn_lim - len(e.encode('utf-8')) f_max = fn_lim - len(e.encode('utf-8'))
f = unicodedata.normalize('NFC', f) f = unicodedata.normalize('NFC', f)
f_trunc = unicode(f.encode('utf-8')[:f_max], 'utf-8', errors='ignore') f_trunc = unicode(f.encode('utf-8')[:f_max], 'utf-8', errors='ignore')