mirror of
https://github.com/cool-RR/PySnooper.git
synced 2026-01-23 02:14:04 +00:00
Fix unicode issues and add test, fix #124
This commit is contained in:
parent
e21a31162f
commit
56f22f8ffe
4 changed files with 56 additions and 4 deletions
|
|
@ -8,6 +8,7 @@ import inspect
|
|||
import sys
|
||||
|
||||
PY3 = (sys.version_info[0] == 3)
|
||||
PY2 = not PY3
|
||||
|
||||
if hasattr(abc, 'ABC'):
|
||||
ABC = abc.ABC
|
||||
|
|
|
|||
|
|
@ -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
48
tests/test_chinese.py
Normal 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')
|
||||
),
|
||||
)
|
||||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue