Skip to content

Commit 4565b8f

Browse files
committed
FIX global variable _backend_choosen removed
1 parent e0b37a5 commit 4565b8f

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

memory_profiler.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def unicode(x, *args):
6161
except ImportError:
6262
has_tracemalloc = False
6363

64-
_backend_chosen = False
65-
_backend = 'psutil'
64+
_backend = None
6665

6766

6867
class MemitResult(object):
@@ -995,10 +994,7 @@ def profile(func=None, stream=None, precision=1, backend='psutil'):
995994
"""
996995
Decorator that will run the function and print a line-by-line profile
997996
"""
998-
global _backend
999-
if not _backend_chosen:
1000-
_backend = backend
1001-
choose_backend()
997+
choose_backend(backend)
1002998
if _backend == 'tracemalloc' and not tracemalloc.is_tracing():
1003999
tracemalloc.start()
10041000
if func is not None:
@@ -1017,13 +1013,12 @@ def inner_wrapper(f):
10171013
return inner_wrapper
10181014

10191015

1020-
def choose_backend():
1016+
def choose_backend(new_backend=None):
10211017
"""
10221018
Function that tries to setup backend, chosen by user, and if failed,
10231019
setup one of the allowable backends
10241020
"""
1025-
global _backend
1026-
old_backend = _backend
1021+
10271022
backends = OrderedDict([
10281023
('psutil', has_psutil),
10291024
('posix', os.name == 'posix'),
@@ -1040,19 +1035,20 @@ def move_to_start(d, key):
10401035
if _key != key:
10411036
items.append((_key, _value))
10421037
return OrderedDict(items)
1043-
1044-
backends = move_to_start(backends, _backend)
1038+
if new_backend is not None:
1039+
backends = move_to_start(backends, new_backend)
10451040

10461041
for n_backend, is_available in backends.items():
10471042
if is_available:
1043+
global _backend
10481044
_backend = n_backend
10491045
break
10501046
if _backend == 'no_backend':
10511047
raise NotImplementedError(
10521048
'Tracemalloc or psutil module is required for non-unix '
10531049
'platforms')
1054-
if _backend != old_backend:
1055-
print('{} can not be used, {} used instead'.format(old_backend,
1050+
if _backend != new_backend and new_backend is not None:
1051+
print('{} can not be used, {} used instead'.format(new_backend,
10561052
_backend))
10571053
global _backend_chosen
10581054
_backend_chosen = True
@@ -1063,19 +1059,19 @@ def move_to_start(d, key):
10631059
# for all cases, e.g. a script that imports another
10641060
# script where @profile is used)
10651061
if PY2:
1066-
def exec_with_profiler(filename, profiler):
1062+
def exec_with_profiler(filename, profiler, backend):
10671063
builtins.__dict__['profile'] = profiler
10681064
ns = dict(_CLEAN_GLOBALS, profile=profiler)
1069-
choose_backend()
1065+
choose_backend(backend)
10701066
execfile(filename, ns, ns)
10711067
else:
1072-
def exec_with_profiler(filename, profiler):
1068+
def exec_with_profiler(filename, profiler, backend):
1069+
choose_backend(backend)
10731070
if _backend == 'tracemalloc' and has_tracemalloc:
10741071
tracemalloc.start()
10751072
builtins.__dict__['profile'] = profiler
10761073
# shadow the profile decorator defined above
10771074
ns = dict(_CLEAN_GLOBALS, profile=profiler)
1078-
choose_backend()
10791075
try:
10801076
with open(filename) as f:
10811077
exec(compile(f.read(), filename, 'exec'), ns, ns)
@@ -1149,7 +1145,6 @@ def flush(self):
11491145

11501146
(options, args) = parser.parse_args()
11511147
sys.argv[:] = args # Remove every memory_profiler arguments
1152-
_backend = options.backend
11531148

11541149
script_filename = _find_script(args[0])
11551150
if options.timestamp:
@@ -1158,7 +1153,7 @@ def flush(self):
11581153
prof = LineProfiler(max_mem=options.max_mem)
11591154

11601155
try:
1161-
exec_with_profiler(script_filename, prof)
1156+
exec_with_profiler(script_filename, prof, options.backend)
11621157
finally:
11631158
if options.out_filename is not None:
11641159
out_file = open(options.out_filename, "a")

0 commit comments

Comments
 (0)