|
3 | 3 | # .. we'll use this to pass it to the child script .. |
4 | 4 | _CLEAN_GLOBALS = globals().copy() |
5 | 5 |
|
6 | | -__version__ = '0.40' |
| 6 | +__version__ = '0.41' |
7 | 7 |
|
8 | 8 | _CMD_USAGE = "python -m memory_profiler script_file.py" |
9 | 9 |
|
|
31 | 31 | line_cell_magic = lambda func: func |
32 | 32 | magics_class = lambda cls: cls |
33 | 33 |
|
34 | | -PY3 = sys.version_info[0] == 3 |
| 34 | +PY2 = sys.version_info[0] == 2 |
35 | 35 |
|
36 | 36 | _TWO_20 = float(2 ** 20) |
37 | 37 |
|
38 | | -if PY3: |
39 | | - import builtins |
40 | | -else: |
| 38 | +if PY2: |
41 | 39 | import __builtin__ as builtins |
| 40 | +else: |
| 41 | + import builtins |
| 42 | + def unicode(x): return str(x) |
42 | 43 |
|
43 | 44 | # .. get available packages .. |
44 | 45 | try: |
@@ -599,25 +600,26 @@ def show_results(prof, stream=None, precision=1): |
599 | 600 | header = template.format('Line #', 'Mem usage', 'Increment', |
600 | 601 | 'Line Contents') |
601 | 602 |
|
602 | | - stream.write('Filename: ' + filename + '\n\n') |
603 | | - stream.write(header + '\n') |
604 | | - stream.write('=' * len(header) + '\n') |
| 603 | + stream.write(u'Filename: ' + filename + '\n\n') |
| 604 | + stream.write(header + u'\n') |
| 605 | + stream.write(u'=' * len(header) + '\n') |
605 | 606 |
|
606 | 607 | all_lines = linecache.getlines(filename) |
607 | 608 | mem_old = None |
608 | | - float_format = '{0}.{1}f'.format(precision + 4, precision) |
609 | | - template_mem = '{0:' + float_format + '} MiB' |
| 609 | + float_format = u'{0}.{1}f'.format(precision + 4, precision) |
| 610 | + template_mem = u'{0:' + float_format + '} MiB' |
610 | 611 | for (lineno, mem) in lines: |
611 | 612 | if mem: |
612 | 613 | inc = (mem - mem_old) if mem_old else 0 |
613 | 614 | mem_old = mem |
614 | 615 | mem = template_mem.format(mem) |
615 | 616 | inc = template_mem.format(inc) |
616 | 617 | else: |
617 | | - mem = '' |
618 | | - inc = '' |
619 | | - stream.write(template.format(lineno, mem, inc, all_lines[lineno - 1])) |
620 | | - stream.write('\n\n') |
| 618 | + mem = u'' |
| 619 | + inc = u'' |
| 620 | + tmp = template.format(lineno, mem, inc, all_lines[lineno - 1]) |
| 621 | + stream.write(unicode(tmp)) |
| 622 | + stream.write(u'\n\n') |
621 | 623 |
|
622 | 624 |
|
623 | 625 | def _func_exec(stmt, ns): |
@@ -901,18 +903,18 @@ def inner_wrapper(f): |
901 | 903 | # globally defined (global variables is not enough |
902 | 904 | # for all cases, e.g. a script that imports another |
903 | 905 | # script where @profile is used) |
904 | | -if PY3: |
| 906 | +if PY2: |
905 | 907 | def exec_with_profiler(filename, profiler): |
906 | 908 | builtins.__dict__['profile'] = profiler |
907 | | - # shadow the profile decorator defined above |
908 | 909 | ns = dict(_CLEAN_GLOBALS, profile=profiler) |
909 | | - with open(filename) as f: |
910 | | - exec(compile(f.read(), filename, 'exec'), ns, ns) |
| 910 | + execfile(filename, ns, ns) |
911 | 911 | else: |
912 | 912 | def exec_with_profiler(filename, profiler): |
913 | 913 | builtins.__dict__['profile'] = profiler |
| 914 | + # shadow the profile decorator defined above |
914 | 915 | ns = dict(_CLEAN_GLOBALS, profile=profiler) |
915 | | - execfile(filename, ns, ns) |
| 916 | + with open(filename) as f: |
| 917 | + exec(compile(f.read(), filename, 'exec'), ns, ns) |
916 | 918 |
|
917 | 919 |
|
918 | 920 | class LogFile(object): |
|
0 commit comments