From b8dec67a4f968b02e9ebfd771a27f5168444d501 Mon Sep 17 00:00:00 2001 From: Ram Rachum Date: Thu, 2 May 2019 09:36:21 +0300 Subject: [PATCH] Workaround for Unicode display on Py2.7, fix #67 --- pysnooper/__init__.py | 2 +- pysnooper/pysnooper.py | 6 +++++- pysnooper/tracer.py | 4 ++-- pysnooper/utils.py | 6 ++++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pysnooper/__init__.py b/pysnooper/__init__.py index 2135595..bf8590a 100644 --- a/pysnooper/__init__.py +++ b/pysnooper/__init__.py @@ -7,7 +7,7 @@ import collections __VersionInfo = collections.namedtuple('VersionInfo', ('major', 'minor', 'micro')) -__version__ = '0.0.24' +__version__ = '0.0.25' __version_info__ = __VersionInfo(*(map(int, __version__.split('.')))) del collections, __VersionInfo # Avoid polluting the namespace diff --git a/pysnooper/pysnooper.py b/pysnooper/pysnooper.py index ad6e689..bc17403 100644 --- a/pysnooper/pysnooper.py +++ b/pysnooper/pysnooper.py @@ -14,7 +14,11 @@ def get_write_and_truncate_functions(output): if output is None: def write(s): stderr = sys.stderr - stderr.write(s) + try: + stderr.write(s) + except UnicodeEncodeError: + # God damn Python 2 + stderr.write(utils.shitcode(s)) truncate = None elif isinstance(output, (pycompat.PathLike, str)): def write(s): diff --git a/pysnooper/tracer.py b/pysnooper/tracer.py index c4f4407..f029535 100644 --- a/pysnooper/tracer.py +++ b/pysnooper/tracer.py @@ -251,8 +251,8 @@ class Tracer: # # ### Finished dealing with misplaced function definition. ############## - self.write('{indent}{now_string} {event:9} ' - '{line_no:4} {source_line}'.format(**locals())) + self.write(u'{indent}{now_string} {event:9} ' + u'{line_no:4} {source_line}'.format(**locals())) if event == 'return': return_value_repr = get_shortish_repr(arg) diff --git a/pysnooper/utils.py b/pysnooper/utils.py index 07d8814..2de6948 100644 --- a/pysnooper/utils.py +++ b/pysnooper/utils.py @@ -36,3 +36,9 @@ file_reading_errors = ( OSError, ValueError # IronPython weirdness. ) + + +def shitcode(s): + return ''.join( + (c if (0 < ord(c) < 256) else '?') for c in s + )