class SlotsFunctionContainer(object): """ A function container using slots for the class attributes. """ __slots__ = ('index', '_recorder', 'record') def __init__(self): self.index = 0 self._recorder = RecordCounter() self.record = self._recorder.record def step_eight(self, frame, event, arg): """ Eigthth step -- single char check for c events. """ if '_' == event[1]: record = ( self.index, event, arg.__name__, frame.f_lineno, frame.f_code.co_filename) else: code = frame.f_code record = ( self.index, event, code.co_name, frame.f_lineno, code.co_filename) self._recorder.record(record) self.index += 1 def step_nine(self, frame, event, arg): """ Nineth step -- store the record method into a private attribute. """ if '_' == event[1]: record = ( self.index, event, arg.__name__, frame.f_lineno, frame.f_code.co_filename) else: code = frame.f_code record = ( self.index, event, code.co_name, frame.f_lineno, code.co_filename) self.record(record) self.index += 1
def __init__(self): self._index = 0 self._recorder = RecordCounter() self.namedtuple_record = FunctionRecord self.tuple_record = lambda *x: x
class FunctionContainer(object): """ The event function container. Each method implements a stage in the function event (i.e. set_profile) compatible function. The current """ def __init__(self): self._index = 0 self._recorder = RecordCounter() self.namedtuple_record = FunctionRecord self.tuple_record = lambda *x: x def original_method(self, frame, event, arg): """ The original method for the function event monitor. """ filename, lineno, function, _, _ = \ inspect.getframeinfo(frame, context=0) if event.startswith('c_'): function = arg.__name__ record = FunctionRecord(self._index, event, function, lineno, filename) self._recorder.record(record) self._index += 1 def step_one(self, frame, event, arg): """ First step -- remove the use of inspect. """ lineno = frame.f_lineno code = frame.f_code filename = code.co_filename if event.startswith('c_'): function = arg.__name__ else: function = code.co_name record = FunctionRecord(self._index, event, function, lineno, filename) self._recorder.record(record) self._index += 1 def step_two(self, frame, event, arg): """ Second step -- simplify check of c function related events. """ lineno = frame.f_lineno code = frame.f_code filename = code.co_filename if 'c_' == event[:2]: function = arg.__name__ else: function = code.co_name record = FunctionRecord( self._index, event, function, lineno, filename) self._recorder.record(record) self._index += 1 def step_three(self, frame, event, arg): """ Third step -- do not create the `function` variable. """ code = frame.f_code if 'c_' == event[:2]: record = FunctionRecord( self._index, event, arg.__name__, frame.f_lineno, code.co_filename) else: record = FunctionRecord( self._index, event, code.co_name, frame.f_lineno, code.co_filename) self._recorder.record(record) self._index += 1 def step_four(self, frame, event, arg): """ Forth step -- do not create `code` variable on some events. """ if 'c_' == event[:2]: record = FunctionRecord( self._index, event, arg.__name__, frame.f_lineno, frame.f_code.co_filename) else: code = frame.f_code record = FunctionRecord( self._index, event, code.co_name, frame.f_lineno, code.co_filename) self._recorder.record(record) self._index += 1 def step_five(self, frame, event, arg): """ Fifth step -- store the record type on an instance attribute. .. note :: This a marginally slower than before but we need to support custom records. """ if 'c_' == event[:2]: record = self.namedtuple_record( self._index, event, arg.__name__, frame.f_lineno, frame.f_code.co_filename) else: code = frame.f_code record = self.namedtuple_record( self._index, event, code.co_name, frame.f_lineno, code.co_filename) self._recorder.record(record) self._index += 1 def step_six(self, frame, event, arg): """ Sixth step -- use a tuple creating lambda instead of a named tuple. """ if 'c_' == event[:2]: record = self.tuple_record( self._index, event, arg.__name__, frame.f_lineno, frame.f_code.co_filename) else: code = frame.f_code record = self.tuple_record( self._index, event, code.co_name, frame.f_lineno, code.co_filename) self._recorder.record(record) self._index += 1 def step_seven(self, frame, event, arg): """ Seventh step -- use a tuple instead of a lambda. """ if 'c_' == event[:2]: record = ( self._index, event, arg.__name__, frame.f_lineno, frame.f_code.co_filename) else: code = frame.f_code record = ( self._index, event, code.co_name, frame.f_lineno, code.co_filename) self._recorder.record(record) self._index += 1
def __init__(self): self.index = 0 self._recorder = RecordCounter() self.record = self._recorder.record