/usr/lib/python2.7/dist-packages/scrapy/utils/trackref.py is in python-scrapy 1.0.3-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | """This module provides some functions and classes to record and report
references to live object instances.
If you want live objects for a particular class to be tracked, you only have to
subclass from object_ref (instead of object).
About performance: This library has a minimal performance impact when enabled,
and no performance penalty at all when disabled (as object_ref becomes just an
alias to object in that case).
"""
from __future__ import print_function
import weakref, os, six
from collections import defaultdict
from time import time
from operator import itemgetter
NoneType = type(None)
live_refs = defaultdict(weakref.WeakKeyDictionary)
class object_ref(object):
"""Inherit from this class (instead of object) to a keep a record of live
instances"""
__slots__ = ()
def __new__(cls, *args, **kwargs):
obj = object.__new__(cls)
live_refs[cls][obj] = time()
return obj
def format_live_refs(ignore=NoneType):
s = "Live References" + os.linesep + os.linesep
now = time()
for cls, wdict in six.iteritems(live_refs):
if not wdict:
continue
if issubclass(cls, ignore):
continue
oldest = min(wdict.itervalues())
s += "%-30s %6d oldest: %ds ago" % (cls.__name__, len(wdict), \
now-oldest) + os.linesep
return s
def print_live_refs(*a, **kw):
print(format_live_refs(*a, **kw))
def get_oldest(class_name):
for cls, wdict in six.iteritems(live_refs):
if cls.__name__ == class_name:
if wdict:
return min(six.iteritems(wdict), key=itemgetter(1))[0]
def iter_all(class_name):
for cls, wdict in six.iteritems(live_refs):
if cls.__name__ == class_name:
return six.iterkeys(wdict)
|