Commit c8db2919 authored by Andrey Golovizin's avatar Andrey Golovizin

Fix displaying non-ASCII filenames in exceptions in Python 2

parent cbf4161e
......@@ -214,8 +214,10 @@ class CommandLine(object):
return [self._replace_legacy_option(arg) for arg in args]
def _replace_legacy_option(self, arg):
# sys.argv contains byte strings in Python 2 and unicode strings in Python 3
try:
# legacy options are ASCII
# all legacy options are ASCII-only
unicode_arg = arg if isinstance(arg, six.text_type) else arg.decode('ASCII')
except UnicodeDecodeError:
return arg
......
......@@ -21,6 +21,8 @@
from __future__ import absolute_import, unicode_literals
import sys
import six
......@@ -35,7 +37,11 @@ class PybtexError(Exception):
def get_filename(self):
"""Return filename, if relevant."""
return self.filename
if isinstance(self.filename, six.text_type):
return self.filename
else:
from .io import _decode_filename
return _decode_filename(self.filename, errors='replace')
def __eq__(self, other):
return six.text_type(self) == six.text_type(other)
......
......@@ -41,6 +41,13 @@ def get_stream_encoding(stream):
return stream_encoding or get_default_encoding()
def _decode_filename(filename, errors='strict'):
"""Decode byte-encoded filename."""
encoding = sys.getfilesystemencoding() or self.get_default_encoding()
return filename.decode(encoding, errors=errors)
def _open_existing(opener, filename, mode, locate, **kwargs):
if not path.isfile(filename):
found = locate(filename)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment