Skip to content

Commit ba3ec2d

Browse files
author
Syam Gadde
committed
Make enable_count a property so that tests don't need to be changed for thread-local enable_count.
1 parent 6c59d87 commit ba3ec2d

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

_line_profiler.pyx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,28 @@ cdef class LineProfiler:
133133
self.code_map[code] = {}
134134
self.functions.append(func)
135135

136+
property enable_count:
137+
def __get__(self):
138+
if not hasattr(self.threaddata, 'enable_count'):
139+
self.threaddata.enable_count = 0
140+
return self.threaddata.enable_count
141+
def __set__(self, value):
142+
self.threaddata.enable_count = value
143+
136144
def enable_by_count(self):
137145
""" Enable the profiler if it hasn't been enabled before.
138146
"""
139-
if not hasattr(self.threaddata, 'enable_count'):
140-
self.threaddata.enable_count = 0
141-
if self.threaddata.enable_count == 0:
147+
if self.enable_count == 0:
142148
self.enable()
143-
self.threaddata.enable_count += 1
149+
self.enable_count += 1
144150

145151
def disable_by_count(self):
146152
""" Disable the profiler if the number of disable requests matches the
147153
number of enable requests.
148154
"""
149-
if self.threaddata.enable_count > 0:
150-
self.threaddata.enable_count -= 1
151-
if self.threaddata.enable_count == 0:
155+
if self.enable_count > 0:
156+
self.enable_count -= 1
157+
if self.enable_count == 0:
152158
self.disable()
153159

154160
def __enter__(self):

0 commit comments

Comments
 (0)