Commit c8db2919 authored by Andrey Golovizin's avatar Andrey Golovizin
Browse files

Fix displaying non-ASCII filenames in exceptions in Python 2

parent cbf4161e
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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
+7 −1
Original line number Diff line number Diff line
@@ -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."""
        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)
+7 −0
Original line number Diff line number Diff line
@@ -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)