Snapshot of traces of memory blocks allocated by Python.
| 413 | |
| 414 | |
| 415 | class Snapshot: |
| 416 | """ |
| 417 | Snapshot of traces of memory blocks allocated by Python. |
| 418 | """ |
| 419 | |
| 420 | def __init__(self, traces, traceback_limit): |
| 421 | # traces is a tuple of trace tuples: see _Traces constructor for |
| 422 | # the exact format |
| 423 | self.traces = _Traces(traces) |
| 424 | self.traceback_limit = traceback_limit |
| 425 | |
| 426 | def dump(self, filename): |
| 427 | """ |
| 428 | Write the snapshot into a file. |
| 429 | """ |
| 430 | with open(filename, "wb") as fp: |
| 431 | pickle.dump(self, fp, pickle.HIGHEST_PROTOCOL) |
| 432 | |
| 433 | @staticmethod |
| 434 | def load(filename): |
| 435 | """ |
| 436 | Load a snapshot from a file. |
| 437 | """ |
| 438 | with open(filename, "rb") as fp: |
| 439 | return pickle.load(fp) |
| 440 | |
| 441 | def _filter_trace(self, include_filters, exclude_filters, trace): |
| 442 | if include_filters: |
| 443 | if not any(trace_filter._match(trace) |
| 444 | for trace_filter in include_filters): |
| 445 | return False |
| 446 | if exclude_filters: |
| 447 | if any(not trace_filter._match(trace) |
| 448 | for trace_filter in exclude_filters): |
| 449 | return False |
| 450 | return True |
| 451 | |
| 452 | def filter_traces(self, filters): |
| 453 | """ |
| 454 | Create a new Snapshot instance with a filtered traces sequence, filters |
| 455 | is a list of Filter or DomainFilter instances. If filters is an empty |
| 456 | list, return a new Snapshot instance with a copy of the traces. |
| 457 | """ |
| 458 | if not isinstance(filters, Iterable): |
| 459 | raise TypeError("filters must be a list of filters, not %s" |
| 460 | % type(filters).__name__) |
| 461 | if filters: |
| 462 | include_filters = [] |
| 463 | exclude_filters = [] |
| 464 | for trace_filter in filters: |
| 465 | if trace_filter.inclusive: |
| 466 | include_filters.append(trace_filter) |
| 467 | else: |
| 468 | exclude_filters.append(trace_filter) |
| 469 | new_traces = [trace for trace in self.traces._traces |
| 470 | if self._filter_trace(include_filters, |
| 471 | exclude_filters, |
| 472 | trace)] |
no outgoing calls
no test coverage detected
searching dependent graphs…