Skip to content

Commit 9530029

Browse files
author
Juan Luis Cano Rodríguez
authored
Merge pull request #198 from spacether/master
Adds wraps decorator to profile, fixes issue #186
2 parents 0b6e2bf + 0cecc69 commit 9530029

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ MANIFEST
88

99
# Ignore mprof generated files
1010
mprofile_*.dat
11+
12+
# virtual environment
13+
venv/

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PYTHON ?= python
55
test:
66
$(PYTHON) -m memory_profiler test/test_func.py
77
$(PYTHON) -m memory_profiler test/test_loop.py
8+
$(PYTHON) -m memory_profiler test/test_mprofile.py
89
$(PYTHON) -m memory_profiler test/test_as.py
910
$(PYTHON) -m memory_profiler test/test_global.py
1011
$(PYTHON) -m memory_profiler test/test_precision_command_line.py
@@ -15,3 +16,6 @@ test:
1516
$(PYTHON) test/test_memory_usage.py
1617
$(PYTHON) test/test_precision_import.py
1718
$(PYTHON) test/test_exception.py
19+
20+
develop:
21+
pip install -e .

memory_profiler.py

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

6-
__version__ = '0.50.0'
6+
__version__ = '0.53.0'
77

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

10-
import time
11-
import sys
10+
from functools import wraps
11+
import inspect
12+
import linecache
13+
import logging
1214
import os
1315
import pdb
14-
import warnings
15-
import linecache
16-
import inspect
1716
import subprocess
18-
import logging
17+
import sys
18+
import time
1919
import traceback
20+
import warnings
21+
2022
if sys.platform == "win32":
2123
# any value except signal.CTRL_C_EVENT and signal.CTRL_BREAK_EVENT
2224
# can be used to kill a process unconditionally in Windows
@@ -1067,6 +1069,7 @@ def profile(func=None, stream=None, precision=1, backend='psutil'):
10671069
if not tracemalloc.is_tracing():
10681070
tracemalloc.start()
10691071
if func is not None:
1072+
@wraps(func)
10701073
def wrapper(*args, **kwargs):
10711074
prof = LineProfiler(backend=backend)
10721075
val = prof(func)(*args, **kwargs)

test/test_mprofile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
@profile
66
def test1(l):
7+
"""test1 docstring"""
78
a = [1] * l
89
time.sleep(1)
910
return a
@@ -14,8 +15,22 @@ def test2(l):
1415
time.sleep(1)
1516
return b
1617

18+
def test3(l):
19+
"""test3 docstring"""
20+
return l
21+
1722
if __name__ == "__main__":
1823
l = 100000
1924
test1(l)
2025
test2(2 * l)
2126

27+
# make sure that the function name and docstring are set
28+
# by functools.wraps
29+
# memory_profile.py def profile func is not None case
30+
assert (test1.__name__ == 'test1'), 'function name is incorrect'
31+
assert (test1.__doc__ == 'test1 docstring'), 'function docstring is incorrect'
32+
# memory_profile.py def profile func is None case
33+
profile_maker = profile()
34+
profiled_test3 = profile_maker(test3)
35+
assert (profiled_test3.__name__ == 'test3'), 'function name is incorrect'
36+
assert (profiled_test3.__doc__ == 'test3 docstring'), 'function docstring is incorrect'

0 commit comments

Comments
 (0)