Skip to content

Commit 6bb856b

Browse files
committed
Merge branch 'master' of github.com:fabianp/memory_profiler
Conflicts: memory_profiler.py
2 parents 8fb44f1 + 796d0be commit 6bb856b

4 files changed

Lines changed: 53 additions & 19 deletions

File tree

.travis.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
language: python
2+
python:
3+
- "2.7"
4+
- "3.3"
5+
install:
6+
- sudo apt-get update
7+
# We do this conditionally because it saves us some downloading if the
8+
# version is the same.
9+
- if [[ "$TRAVIS_PYTHON_VERSION" == "2.7" ]]; then
10+
wget https://repo.continuum.io/miniconda/Miniconda-latest-Linux-x86_64.sh -O miniconda.sh;
11+
else
12+
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
13+
fi
14+
- bash miniconda.sh -b -p $HOME/miniconda
15+
- export PATH="$HOME/miniconda/bin:$PATH"
16+
- hash -r
17+
- conda config --set always_yes yes --set changeps1 no
18+
- conda update -q conda
19+
# Useful for debugging any issues with conda
20+
- conda info -a
21+
22+
# Replace dep1 dep2 ... with your dependencies
23+
- conda create -q -n test-environment python=$TRAVIS_PYTHON_VERSION numpy nose ipython
24+
- source activate test-environment
25+
- python setup.py install
26+
script:
27+
- make test

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
PYTHON ?= python
2+
IPYTHON ?= ipython
23

34
.PHONY: test
45

@@ -13,3 +14,4 @@ test:
1314
$(PYTHON) test/test_import.py
1415
$(PYTHON) test/test_memory_usage.py
1516
$(PYTHON) test/test_precision_import.py
17+
$(IPYTHON) test/test_ipython.py

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
.. image:: https://travis-ci.org/fabianp/memory_profiler.svg?branch=master
2+
:target: https://travis-ci.org/fabianp/memory_profiler
3+
14
=================
25
Memory Profiler
36
=================

memory_profiler.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# .. we'll use this to pass it to the child script ..
44
_CLEAN_GLOBALS = globals().copy()
55

6-
__version__ = '0.40'
6+
__version__ = '0.41'
77

88
_CMD_USAGE = "python -m memory_profiler script_file.py"
99

@@ -31,14 +31,15 @@
3131
line_cell_magic = lambda func: func
3232
magics_class = lambda cls: cls
3333

34-
PY3 = sys.version_info[0] == 3
34+
PY2 = sys.version_info[0] == 2
3535

3636
_TWO_20 = float(2 ** 20)
3737

38-
if PY3:
39-
import builtins
40-
else:
38+
if PY2:
4139
import __builtin__ as builtins
40+
else:
41+
import builtins
42+
def unicode(x): return str(x)
4243

4344
# .. get available packages ..
4445
try:
@@ -599,25 +600,26 @@ def show_results(prof, stream=None, precision=1):
599600
header = template.format('Line #', 'Mem usage', 'Increment',
600601
'Line Contents')
601602

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')
605606

606607
all_lines = linecache.getlines(filename)
607608
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'
610611
for (lineno, mem) in lines:
611612
if mem:
612613
inc = (mem - mem_old) if mem_old else 0
613614
mem_old = mem
614615
mem = template_mem.format(mem)
615616
inc = template_mem.format(inc)
616617
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')
621623

622624

623625
def _func_exec(stmt, ns):
@@ -901,18 +903,18 @@ def inner_wrapper(f):
901903
# globally defined (global variables is not enough
902904
# for all cases, e.g. a script that imports another
903905
# script where @profile is used)
904-
if PY3:
906+
if PY2:
905907
def exec_with_profiler(filename, profiler):
906908
builtins.__dict__['profile'] = profiler
907-
# shadow the profile decorator defined above
908909
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)
911911
else:
912912
def exec_with_profiler(filename, profiler):
913913
builtins.__dict__['profile'] = profiler
914+
# shadow the profile decorator defined above
914915
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)
916918

917919

918920
class LogFile(object):

0 commit comments

Comments
 (0)