/usr/lib/python2.7/dist-packages/pytools/debug.py is in python-pytools 2014.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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | from pytools import memoize
# {{{ debug files -------------------------------------------------------------
def make_unique_filesystem_object(stem, extension="", directory="",
creator=None):
"""
:param extension: needs a leading dot.
:param directory: must not have a trailing slash.
"""
from os.path import join
import os
if creator is None:
def creator(name):
return os.fdopen(os.open(name,
os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0444), "w")
i = 0
while True:
fname = join(directory, "%s-%d%s" % (stem, i, extension))
try:
return creator(fname), fname
except OSError:
i += 1
@memoize
def get_run_debug_directory():
def creator(name):
from os import mkdir
mkdir(name)
return name
return make_unique_filesystem_object("run-debug", creator=creator)[0]
def open_unique_debug_file(stem, extension=""):
"""
:param extension: needs a leading dot.
"""
return make_unique_filesystem_object(
stem, extension, get_run_debug_directory())
# }}}
# {{{ refcount debugging ------------------------------------------------------
class RefDebugQuit(Exception):
pass
def refdebug(obj, top_level=True, exclude=[]):
from types import FrameType
def is_excluded(o):
for ex in exclude:
if o is ex:
return True
from sys import _getframe
if isinstance(o, FrameType) and \
o.f_code.co_filename == _getframe().f_code.co_filename:
return True
return False
if top_level:
try:
refdebug(obj, top_level=False, exclude=exclude)
except RefDebugQuit:
pass
else:
import gc
print_head = True
print "-------------->"
try:
reflist = [x for x in gc.get_referrers(obj)
if not is_excluded(x)]
idx = 0
while True:
if print_head:
print "referring to", id(obj), type(obj), obj
print "----------------------"
print_head = False
r = reflist[idx]
if isinstance(r, FrameType):
s = str(r.f_code)
else:
s = str(r)
print "%d/%d: " % (idx, len(reflist)), id(r), type(r), s
if isinstance(r, dict):
for k, v in r.iteritems():
if v is obj:
print "...referred to from key", k
print "[d]ig, [n]ext, [p]rev, [e]val, [r]eturn, [q]uit?"
response = raw_input()
if response == "d":
refdebug(r, top_level=False, exclude=exclude+[reflist])
print_head = True
elif response == "n":
if idx + 1 < len(reflist):
idx += 1
elif response == "p":
if idx - 1 >= 0:
idx -= 1
elif response == "e":
print "type expression, obj is your object:"
expr_str = raw_input()
try:
res = eval(expr_str, {"obj": r})
except:
from traceback import print_exc
print_exc()
print res
elif response == "r":
return
elif response == "q":
raise RefDebugQuit()
else:
print "WHAT YOU SAY!!! (invalid choice)"
finally:
print "<--------------"
# }}}
# {{{ interactive shell
def get_shell_hist_filename():
import os
_home = os.environ.get('HOME', '/')
return os.path.join(_home, ".pytools-debug-shell-history")
def setup_readline():
from os.path import exists
hist_filename = get_shell_hist_filename()
if exists(hist_filename):
try:
readline.read_history_file(hist_filename)
except Exception:
# http://docs.python.org/3/howto/pyporting.html#capturing-the-currently-raised-exception # noqa
import sys
e = sys.exc_info()[1]
from warnings import warn
warn("Error opening readline history file: %s" % e)
readline.parse_and_bind("tab: complete")
try:
import readline
import rlcompleter
HAVE_READLINE = True
except ImportError:
HAVE_READLINE = False
else:
setup_readline()
class SetPropagatingDict(dict):
def __init__(self, source_dicts, target_dict):
dict.__init__(self)
for s in source_dicts[::-1]:
self.update(s)
self.target_dict = target_dict
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
self.target_dict[key] = value
def __delitem__(self, key):
dict.__delitem__(self, key)
del self.target_dict[key]
def shell(locals=None, globals=None):
from inspect import currentframe, getouterframes
calling_frame = getouterframes(currentframe())[1][0]
if locals is None:
locals = calling_frame.f_locals
if globals is None:
globals = calling_frame.f_globals
ns = SetPropagatingDict([locals, globals], locals)
if HAVE_READLINE:
readline.set_completer(
rlcompleter.Completer(ns).complete)
from code import InteractiveConsole
cons = InteractiveConsole(ns)
cons.interact("")
readline.write_history_file(get_shell_hist_filename())
# }}}
# vim: foldmethod=marker
|