/usr/lib/python2.7/dist-packages/dill/pointers.py is in python-dill 0.2.7.1-1.
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | #!/usr/bin/env python
#
# Author: Mike McKerns (mmckerns @caltech and @uqfoundation)
# Copyright (c) 2008-2016 California Institute of Technology.
# Copyright (c) 2016-2017 The Uncertainty Quantification Foundation.
# License: 3-clause BSD. The full license text is available at:
# - http://trac.mystic.cacr.caltech.edu/project/pathos/browser/dill/LICENSE
from __future__ import absolute_import
__all__ = ['parent', 'reference', 'at', 'parents', 'children']
import gc
import sys
from .dill import _proxy_helper as reference
from .dill import _locate_object as at
def parent(obj, objtype, ignore=()):
"""
>>> listiter = iter([4,5,6,7])
>>> obj = parent(listiter, list)
>>> obj == [4,5,6,7] # actually 'is', but don't have handle any longer
True
NOTE: objtype can be a single type (e.g. int or list) or a tuple of types.
WARNING: if obj is a sequence (e.g. list), may produce unexpected results.
Parent finds *one* parent (e.g. the last member of the sequence).
"""
depth = 1 #XXX: always looking for the parent (only, right?)
chain = parents(obj, objtype, depth, ignore)
parent = chain.pop()
if parent is obj:
return None
return parent
def parents(obj, objtype, depth=1, ignore=()): #XXX: objtype=object ?
"""Find the chain of referents for obj. Chain will end with obj.
objtype: an object type or tuple of types to search for
depth: search depth (e.g. depth=2 is 'grandparents')
ignore: an object or tuple of objects to ignore in the search
"""
edge_func = gc.get_referents # looking for refs, not back_refs
predicate = lambda x: isinstance(x, objtype) # looking for parent type
#if objtype is None: predicate = lambda x: True #XXX: in obj.mro() ?
ignore = (ignore,) if not hasattr(ignore, '__len__') else ignore
ignore = (id(obj) for obj in ignore)
chain = find_chain(obj, predicate, edge_func, depth)[::-1]
#XXX: should pop off obj... ?
return chain
def children(obj, objtype, depth=1, ignore=()): #XXX: objtype=object ?
"""Find the chain of referrers for obj. Chain will start with obj.
objtype: an object type or tuple of types to search for
depth: search depth (e.g. depth=2 is 'grandchildren')
ignore: an object or tuple of objects to ignore in the search
NOTE: a common thing to ignore is all globals, 'ignore=(globals(),)'
NOTE: repeated calls may yield different results, as python stores
the last value in the special variable '_'; thus, it is often good
to execute something to replace '_' (e.g. >>> 1+1).
"""
edge_func = gc.get_referrers # looking for back_refs, not refs
predicate = lambda x: isinstance(x, objtype) # looking for child type
#if objtype is None: predicate = lambda x: True #XXX: in obj.mro() ?
ignore = (ignore,) if not hasattr(ignore, '__len__') else ignore
ignore = (id(obj) for obj in ignore)
chain = find_chain(obj, predicate, edge_func, depth, ignore)
#XXX: should pop off obj... ?
return chain
# more generic helper function (cut-n-paste from objgraph)
# Source at http://mg.pov.lt/objgraph/
# Copyright (c) 2008-2010 Marius Gedminas <marius@pov.lt>
# Copyright (c) 2010 Stefano Rivera <stefano@rivera.za.net>
# Released under the MIT licence (see objgraph/objgrah.py)
def find_chain(obj, predicate, edge_func, max_depth=20, extra_ignore=()):
queue = [obj]
depth = {id(obj): 0}
parent = {id(obj): None}
ignore = set(extra_ignore)
ignore.add(id(extra_ignore))
ignore.add(id(queue))
ignore.add(id(depth))
ignore.add(id(parent))
ignore.add(id(ignore))
ignore.add(id(sys._getframe())) # this function
ignore.add(id(sys._getframe(1))) # find_chain/find_backref_chain, likely
gc.collect()
while queue:
target = queue.pop(0)
if predicate(target):
chain = [target]
while parent[id(target)] is not None:
target = parent[id(target)]
chain.append(target)
return chain
tdepth = depth[id(target)]
if tdepth < max_depth:
referrers = edge_func(target)
ignore.add(id(referrers))
for source in referrers:
if id(source) in ignore:
continue
if id(source) not in depth:
depth[id(source)] = tdepth + 1
parent[id(source)] = target
queue.append(source)
return [obj] # not found
# backward compatability
refobject = at
# EOF
|