Skip to content

Commit 93371d6

Browse files
authored
Merge pull request #185 from amyangfei/stream-unicode-bugfix
Fix UnicodeDecocdError when profile result writing to filestream
2 parents cedd238 + c5f79b8 commit 93371d6

2 files changed

Lines changed: 17 additions & 5 deletions

File tree

memory_profiler.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,11 @@
4848

4949
if PY2:
5050
import __builtin__ as builtins
51+
to_str = lambda x: x
5152
from future_builtins import filter
5253
else:
5354
import builtins
54-
55-
56-
def unicode(x, *args):
57-
return str(x)
55+
to_str = lambda x: str(x)
5856

5957
# .. get available packages ..
6058
try:
@@ -778,7 +776,7 @@ def show_results(prof, stream=None, precision=1):
778776
mem = u''
779777
inc = u''
780778
tmp = template.format(lineno, mem, inc, all_lines[lineno - 1])
781-
stream.write(unicode(tmp, 'UTF-8'))
779+
stream.write(to_str(tmp))
782780
stream.write(u'\n\n')
783781

784782

test/test_stream_unicode.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from memory_profiler import profile
4+
5+
f = open('/dev/null', 'w')
6+
@profile(stream=f)
7+
def test_unicode(txt):
8+
# test when unicode is present
9+
txt = txt.replace (u"ی", u"ي") #Arabic Yah = ي
10+
return txt
11+
12+
13+
if __name__ == '__main__':
14+
test_unicode (u"ایست")

0 commit comments

Comments
 (0)