Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stream encoding instead of locale preferred encoding #88

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions better_exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -17,9 +17,8 @@
import sys

from .formatter import THEME, MAX_LENGTH, PIPE_CHAR, CAP_CHAR, ExceptionFormatter
from .encoding import to_byte
from .context import PY3
from .color import SUPPORTS_COLOR, SHOULD_ENCODE, STREAM
from .color import SUPPORTS_COLOR, SHOULD_ENCODE, STREAM, to_byte
from .log import BetExcLogger, patch as patch_logging
from .repl import interact, get_repl

29 changes: 27 additions & 2 deletions better_exceptions/color.py
Original file line number Diff line number Diff line change
@@ -6,20 +6,45 @@

from __future__ import absolute_import

import codecs
import errno
import os
import struct
import sys

from .context import PY3
from .encoding import to_byte as _byte


STREAM = sys.stderr
ENCODING = getattr(STREAM, "encoding", None) or "utf-8"
SHOULD_ENCODE = True
SUPPORTS_COLOR = False


def to_byte(val):
unicode_type = str if PY3 else unicode
if isinstance(val, unicode_type):
try:
return val.encode(ENCODING)
except UnicodeEncodeError:
if PY3:
return codecs.escape_decode(val)[0]
else:
return val.encode("unicode-escape").decode("string-escape")

return val


def to_unicode(val):
if isinstance(val, bytes):
try:
return val.decode(ENCODING)
except UnicodeDecodeError:
return val.decode("unicode-escape")

return val


def get_terminfo_file():
term = os.getenv('TERM', None)

@@ -65,7 +90,7 @@ def __getattr__(self, name):
return getattr(self.__wrapped, name)

def write(self, text):
data = _byte(text)
data = to_byte(text)
self.__wrapped.buffer.write(data)


34 changes: 0 additions & 34 deletions better_exceptions/encoding.py

This file was deleted.

3 changes: 1 addition & 2 deletions better_exceptions/formatter.py
Original file line number Diff line number Diff line change
@@ -9,9 +9,8 @@
import sys
import traceback

from .color import STREAM, SUPPORTS_COLOR
from .color import ENCODING, STREAM, SUPPORTS_COLOR, to_byte, to_unicode
from .context import PY3
from .encoding import ENCODING, to_byte, to_unicode
from .repl import get_repl


5 changes: 1 addition & 4 deletions test_all.sh
Original file line number Diff line number Diff line change
@@ -60,10 +60,7 @@ for encoding in ascii "UTF-8"; do
[[ $color == "1" ]] && color_filename="color" || color_filename="nocolor"
filename="test/output/python3-${term}-${encoding}-${color_filename}.out"

export LANG="en_US.${encoding}"
export LC_ALL="${LANG}"
export PYTHONCOERCECLOCALE=0
export PYTHONUTF8=0
export PYTHONIOENCODING="${encoding}"
export TERM="${term}"
export FORCE_COLOR="${color}"