Skip to content

Commit 7b92098

Browse files
committed
Fix UnicodeDecocdError when profile result writing to filestream
1 parent 5106293 commit 7b92098

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
@@ -46,12 +46,10 @@
4646

4747
if PY2:
4848
import __builtin__ as builtins
49+
to_str = lambda x: x
4950
else:
5051
import builtins
51-
52-
53-
def unicode(x, *args):
54-
return str(x)
52+
to_str = lambda x: str(x)
5553

5654
# .. get available packages ..
5755
try:
@@ -777,7 +775,7 @@ def show_results(prof, stream=None, precision=1):
777775
mem = u''
778776
inc = u''
779777
tmp = template.format(lineno, mem, inc, all_lines[lineno - 1])
780-
stream.write(unicode(tmp, 'UTF-8'))
778+
stream.write(to_str(tmp))
781779
stream.write(u'\n\n')
782780

783781

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)