本文整理汇总了Python中gc.get_referents方法的典型用法代码示例。如果您正苦于以下问题:Python gc.get_referents方法的具体用法?Python gc.get_referents怎么用?Python gc.get_referents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gc
的用法示例。
在下文中一共展示了gc.get_referents方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _getr
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def _getr(slist, olist, first=True):
i = 0
for e in slist:
oid = id(e)
typ = type(e)
if oid in olist or typ is int: ## or e in olist: ## since we're excluding all ints, there is no longer a need to check for olist keys
continue
olist[oid] = e
if first and (i%1000) == 0:
gc.collect()
tl = gc.get_referents(e)
if tl:
_getr(tl, olist, first=False)
i += 1
# The public function.
示例2: get_leaking_objects
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def get_leaking_objects(objects=None):
"""Return objects that do not have any referents.
These could indicate reference-counting bugs in C code. Or they could
be legitimate.
Note that the GC does not track simple objects like int or str.
.. versionadded:: 1.7
"""
if objects is None:
gc.collect()
objects = gc.get_objects()
try:
ids = set(id(i) for i in objects)
for i in objects:
ids.difference_update(id(j) for j in gc.get_referents(i))
# this then is our set of objects without referrers
return [i for i in objects if id(i) in ids]
finally:
objects = i = j = None # clear cyclic references to frame
示例3: find_ref_chain
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def find_ref_chain(obj, predicate, max_depth=20, extra_ignore=()):
"""Find a shortest chain of references leading from obj.
The end of the chain will be some object that matches your predicate.
``predicate`` is a function taking one argument and returning a boolean.
``max_depth`` limits the search depth.
``extra_ignore`` can be a list of object IDs to exclude those objects from
your search.
Example:
>>> find_chain(obj, lambda x: isinstance(x, MyClass))
[obj, ..., <MyClass object at ...>]
Returns ``[obj]`` if such a chain could not be found.
.. versionadded:: 1.7
"""
return find_chain(obj, predicate, gc.get_referents,
max_depth=max_depth, extra_ignore=extra_ignore)[::-1]
示例4: test_get_referents
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def test_get_referents(self):
alist = [1, 3, 5]
got = gc.get_referents(alist)
got.sort()
self.assertEqual(got, alist)
atuple = tuple(alist)
got = gc.get_referents(atuple)
got.sort()
self.assertEqual(got, alist)
adict = {1: 3, 5: 7}
expected = [1, 3, 5, 7]
got = gc.get_referents(adict)
got.sort()
self.assertEqual(got, expected)
got = gc.get_referents([1, 2], {3: 4}, (0, 0, 0))
got.sort()
self.assertEqual(got, [0, 0] + range(5))
self.assertEqual(gc.get_referents(1, 'a', 4j), [])
示例5: test_get_referents
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def test_get_referents(self):
alist = [1, 3, 5]
got = gc.get_referents(alist)
got.sort()
self.assertEqual(got, alist)
atuple = tuple(alist)
got = gc.get_referents(atuple)
got.sort()
self.assertEqual(got, alist)
adict = {1: 3, 5: 7}
expected = [1, 3, 5, 7]
got = gc.get_referents(adict)
got.sort()
self.assertEqual(got, expected)
got = gc.get_referents([1, 2], {3: 4}, (0, 0, 0))
got.sort()
self.assertEqual(got, [0, 0] + list(range(5)))
self.assertEqual(gc.get_referents(1, 'a', 4j), [])
示例6: _get_edges
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def _get_edges(self):
"""
Compute the edges for the reference graph.
The function returns a set of tuples (id(a), id(b), ref) if a
references b with the referent 'ref'.
"""
idset = set([id(x) for x in self.objects])
self.edges = set([])
for n in self.objects:
refset = set([id(x) for x in get_referents(n)])
for ref in refset.intersection(idset):
label = ''
members = None
if isinstance(n, dict):
members = n.items()
if not members:
members = named_refs(n)
for (k, v) in members:
if id(v) == ref:
label = k
break
self.edges.add(_Edge(id(n), ref, label))
示例7: getsize
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def getsize(obj):
"""sum size of object & members."""
if isinstance(obj, BLACKLIST):
raise TypeError('getsize() does not take argument of type: '+ str(type(obj)))
seen_ids = set()
size = 0
objects = [obj]
while objects:
need_referents = []
for obj in objects:
if not isinstance(obj, BLACKLIST) and id(obj) not in seen_ids:
seen_ids.add(id(obj))
size += sys.getsizeof(obj)
need_referents.append(obj)
print(size, type(obj), obj)
objects = get_referents(*need_referents)
return size
示例8: getsize
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def getsize(obj):
"""Sum size of object & members."""
if isinstance(obj, BLACKLIST):
raise TypeError('getsize() does not take argument of type: ' +
str(type(obj)))
seen_ids = set()
size = 0
objects = [obj]
while objects:
need_referents = []
for obj in objects:
if not isinstance(obj, BLACKLIST) and id(obj) not in seen_ids:
seen_ids.add(id(obj))
size += sys.getsizeof(obj)
need_referents.append(obj)
objects = get_referents(*need_referents)
return size
示例9: CollectGarbage
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def CollectGarbage():
import gc
#gc.set_debug(gc.DEBUG_SAVEALL)
#gc.set_debug(gc.DEBUG_UNCOLLECTABLE)
from pprint import pprint
print "threshold:", gc.get_threshold()
print "unreachable object count:", gc.collect()
garbageList = gc.garbage[:]
for i, obj in enumerate(garbageList):
print "Object Num %d:" % i
pprint(obj)
#print "Referrers:"
#print(gc.get_referrers(o))
#print "Referents:"
#print(gc.get_referents(o))
print "Done."
#print "unreachable object count:", gc.collect()
#from pprint import pprint
#pprint(gc.garbage)
示例10: show_refs
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def show_refs(objs, max_depth=3, extra_ignore=(), filter=None, too_many=10,
highlight=None):
"""Generate an object reference graph starting at ``objs``
The graph will show you what objects are reachable from ``objs``, directly
and indirectly.
``objs`` can be a single object, or it can be a list of objects.
Produces a Graphviz .dot file and spawns a viewer (xdot) if one is
installed, otherwise converts the graph to a .png image.
Use ``max_depth`` and ``too_many`` to limit the depth and breadth of the
graph.
Use ``filter`` (a predicate) and ``extra_ignore`` (a list of object IDs) to
remove undesired objects from the graph.
Use ``highlight`` (a predicate) to highlight certain graph nodes in blue.
Examples:
>>> show_refs(obj)
>>> show_refs([obj1, obj2])
>>> show_refs(obj, max_depth=5)
>>> show_refs(obj, filter=lambda x: not inspect.isclass(x))
>>> show_refs(obj, highlight=inspect.isclass)
>>> show_refs(obj, extra_ignore=[id(locals())])
"""
show_graph(objs, max_depth=max_depth, extra_ignore=extra_ignore,
filter=filter, too_many=too_many, highlight=highlight,
edge_func=gc.get_referents, swap_source_target=True)
#
# Internal helpers
#
示例11: test_get_referents
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def test_get_referents(self):
if is_cli:
self.assertRaises(NotImplementedError, gc.get_referents,1,"hello",True)
self.assertRaises(NotImplementedError, gc.get_referents)
else:
gc.get_referents(1,"hello",True)
gc.get_referents()
class TempClass: pass
self.assertEqual(gc.get_referents(TempClass).count('TempClass'), 1)
示例12: __init__
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def __init__(self, obj):
self.obj = obj
self.referents = [id(ref) for ref in gc.get_referents(obj)]
self.referrers = set()
示例13: _check_setitem_copy
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def _check_setitem_copy(self, stacklevel=4, t='setting'):
""" validate if we are doing a settitem on a chained copy.
If you call this function, be sure to set the stacklevel such that the
user will see the error *at the level of setting*"""
if self.is_copy:
value = config.get_option('mode.chained_assignment')
if value is None:
return
# see if the copy is not actually refererd; if so, then disolve
# the copy weakref
try:
gc.collect(2)
if not gc.get_referents(self.is_copy()):
self.is_copy = None
return
except:
pass
if t == 'referant':
t = ("A value is trying to be set on a copy of a slice from a "
"DataFrame")
else:
t = ("A value is trying to be set on a copy of a slice from a "
"DataFrame.\nTry using .loc[row_index,col_indexer] = value "
"instead")
if value == 'raise':
raise SettingWithCopyError(t)
elif value == 'warn':
warnings.warn(t, SettingWithCopyWarning, stacklevel=stacklevel)
示例14: _getreferents
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def _getreferents(unused):
return () # sorry, no refs
# sys.getsizeof() new in Python 2.6
示例15: _iter_refs
# 需要导入模块: import gc [as 别名]
# 或者: from gc import get_referents [as 别名]
def _iter_refs(obj, named):
'''Return the referent(s) of an iterator object.
'''
r = _getreferents(obj) # special case
return _refs(r, named, itor=_nameof(obj) or 'iteref')