Fix unicode issues and add test, fix #124

This commit is contained in:
Ram Rachum 2019-05-20 11:42:39 +03:00
parent e21a31162f
commit 56f22f8ffe
4 changed files with 56 additions and 4 deletions

View file

@ -8,6 +8,7 @@ import inspect
import sys
PY3 = (sys.version_info[0] == 3)
PY2 = not PY3
if hasattr(abc, 'ABC'):
ABC = abc.ABC

View file

@ -14,6 +14,8 @@ import traceback
from .variables import CommonVariable, Exploding, BaseVariable
from . import utils, pycompat
if pycompat.PY2:
from io import open
ipython_filename_pattern = re.compile('^<ipython-input-([0-9]+)-.*>$')
@ -84,7 +86,7 @@ def get_source_from_frame(frame):
# apply tokenize.detect_encoding to decode the source into a
# string, then we should do that ourselves.
if isinstance(source[0], bytes):
encoding = 'ascii'
encoding = 'utf-8'
for line in source[:2]:
# File coding may be specified. Match pattern from PEP-263
# (https://www.python.org/dev/peps/pep-0263/)
@ -130,7 +132,8 @@ class FileWriter(object):
self.overwrite = overwrite
def write(self, s):
with open(self.path, 'w' if self.overwrite else 'a') as output_file:
with open(self.path, 'w' if self.overwrite else 'a',
encoding='utf-8') as output_file:
output_file.write(s)
self.overwrite = False

48
tests/test_chinese.py Normal file
View file

@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
# Copyright 2019 Ram Rachum and collaborators.
# This program is distributed under the MIT license.
import io
import textwrap
import threading
import types
import sys
from pysnooper.utils import truncate
from python_toolbox import sys_tools, temp_file_tools
import pytest
import pysnooper
from pysnooper import pycompat
from pysnooper.variables import needs_parentheses
from .utils import (assert_output, assert_sample_output, VariableEntry,
CallEntry, LineEntry, ReturnEntry, OpcodeEntry,
ReturnValueEntry, ExceptionEntry)
def test_chinese():
with temp_file_tools.create_temp_folder(prefix='pysnooper') as folder:
path = folder / 'foo.log'
@pysnooper.snoop(path)
def foo():
a = 1
x = '失败'
return 7
foo()
with path.open(encoding='utf-8') as file:
output = file.read()
assert_output(
output,
(
CallEntry(),
LineEntry(),
VariableEntry('a'),
LineEntry(u"x = '失败'"),
VariableEntry(u'x', (u"'失败'" if pycompat.PY3 else None)),
LineEntry(),
ReturnEntry(),
ReturnValueEntry('7')
),
)

View file

@ -254,7 +254,7 @@ def assert_output(output, expected_entries, prefix=None):
any_mismatch = False
result = ''
template = '\n{line!s:%s} {expected_entry} {arrow}' % max(map(len, lines))
template = u'\n{line!s:%s} {expected_entry} {arrow}' % max(map(len, lines))
for expected_entry, line in zip_longest(expected_entries, lines, fillvalue=""):
mismatch = not (expected_entry and expected_entry.check(line))
any_mismatch |= mismatch
@ -273,7 +273,7 @@ def assert_sample_output(module):
with sys_tools.OutputCapturer(stdout=False,
stderr=True) as output_capturer:
module.main()
time = '21:10:42.298924'
time_pattern = re.sub(r'\d', r'\\d', time)