mirror of https://github.com/python/cpython
Relocating file to Lib/lib-old.
This commit is contained in:
parent
a351f89a94
commit
bdc61b1c7b
|
@ -1,81 +0,0 @@
|
|||
# A subroutine for extracting a function name from a code object
|
||||
# (with cache)
|
||||
|
||||
import sys
|
||||
from stat import *
|
||||
import string
|
||||
import os
|
||||
import linecache
|
||||
|
||||
# XXX The functions getcodename() and getfuncname() are now obsolete
|
||||
# XXX as code and function objects now have a name attribute --
|
||||
# XXX co.co_name and f.func_name.
|
||||
# XXX getlineno() is now also obsolete because of the new attribute
|
||||
# XXX of code objects, co.co_firstlineno.
|
||||
|
||||
# Extract the function or class name from a code object.
|
||||
# This is a bit of a hack, since a code object doesn't contain
|
||||
# the name directly. So what do we do:
|
||||
# - get the filename (which *is* in the code object)
|
||||
# - look in the code string to find the first SET_LINENO instruction
|
||||
# (this must be the first instruction)
|
||||
# - get the line from the file
|
||||
# - if the line starts with 'class' or 'def' (after possible whitespace),
|
||||
# extract the following identifier
|
||||
#
|
||||
# This breaks apart when the function was read from <stdin>
|
||||
# or constructed by exec(), when the file is not accessible,
|
||||
# and also when the file has been modified or when a line is
|
||||
# continued with a backslash before the function or class name.
|
||||
#
|
||||
# Because this is a pretty expensive hack, a cache is kept.
|
||||
|
||||
SET_LINENO = 127 # The opcode (see "opcode.h" in the Python source)
|
||||
identchars = string.letters + string.digits + '_' # Identifier characters
|
||||
|
||||
_namecache = {} # The cache
|
||||
|
||||
def getcodename(co):
|
||||
try:
|
||||
return co.co_name
|
||||
except AttributeError:
|
||||
pass
|
||||
key = `co` # arbitrary but uniquely identifying string
|
||||
if _namecache.has_key(key): return _namecache[key]
|
||||
filename = co.co_filename
|
||||
code = co.co_code
|
||||
name = ''
|
||||
if ord(code[0]) == SET_LINENO:
|
||||
lineno = ord(code[1]) | ord(code[2]) << 8
|
||||
line = linecache.getline(filename, lineno)
|
||||
words = string.split(line)
|
||||
if len(words) >= 2 and words[0] in ('def', 'class'):
|
||||
name = words[1]
|
||||
for i in range(len(name)):
|
||||
if name[i] not in identchars:
|
||||
name = name[:i]
|
||||
break
|
||||
_namecache[key] = name
|
||||
return name
|
||||
|
||||
# Use the above routine to find a function's name.
|
||||
|
||||
def getfuncname(func):
|
||||
try:
|
||||
return func.func_name
|
||||
except AttributeError:
|
||||
pass
|
||||
return getcodename(func.func_code)
|
||||
|
||||
# A part of the above code to extract just the line number from a code object.
|
||||
|
||||
def getlineno(co):
|
||||
try:
|
||||
return co.co_firstlineno
|
||||
except AttributeError:
|
||||
pass
|
||||
code = co.co_code
|
||||
if ord(code[0]) == SET_LINENO:
|
||||
return ord(code[1]) | ord(code[2]) << 8
|
||||
else:
|
||||
return -1
|
|
@ -1,15 +0,0 @@
|
|||
import struct, fcntl, FCNTL
|
||||
|
||||
def writelock(f):
|
||||
_lock(f, FCNTL.F_WRLCK)
|
||||
|
||||
def readlock(f):
|
||||
_lock(f, FCNTL.F_RDLCK)
|
||||
|
||||
def unlock(f):
|
||||
_lock(f, FCNTL.F_UNLCK)
|
||||
|
||||
def _lock(f, op):
|
||||
dummy = fcntl.fcntl(f.fileno(), FCNTL.F_SETLKW,
|
||||
struct.pack('2h8l', op,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0))
|
178
Lib/tb.py
178
Lib/tb.py
|
@ -1,178 +0,0 @@
|
|||
# Print tracebacks, with a dump of local variables.
|
||||
# Also an interactive stack trace browser.
|
||||
# Note -- this module is obsolete -- use pdb.pm() instead.
|
||||
|
||||
import sys
|
||||
import os
|
||||
from stat import *
|
||||
import string
|
||||
import linecache
|
||||
|
||||
def br(): browser(sys.last_traceback)
|
||||
|
||||
def tb(): printtb(sys.last_traceback)
|
||||
|
||||
def browser(tb):
|
||||
if not tb:
|
||||
print 'No traceback.'
|
||||
return
|
||||
tblist = []
|
||||
while tb:
|
||||
tblist.append(tb)
|
||||
tb = tb.tb_next
|
||||
ptr = len(tblist)-1
|
||||
tb = tblist[ptr]
|
||||
while 1:
|
||||
if tb <> tblist[ptr]:
|
||||
tb = tblist[ptr]
|
||||
print `ptr` + ':',
|
||||
printtbheader(tb)
|
||||
try:
|
||||
line = raw_input('TB: ')
|
||||
except KeyboardInterrupt:
|
||||
print '\n[Interrupted]'
|
||||
break
|
||||
except EOFError:
|
||||
print '\n[EOF]'
|
||||
break
|
||||
cmd = string.strip(line)
|
||||
if cmd:
|
||||
if cmd == 'quit':
|
||||
break
|
||||
elif cmd == 'list':
|
||||
browserlist(tb)
|
||||
elif cmd == 'up':
|
||||
if ptr-1 >= 0: ptr = ptr-1
|
||||
else: print 'Bottom of stack.'
|
||||
elif cmd == 'down':
|
||||
if ptr+1 < len(tblist): ptr = ptr+1
|
||||
else: print 'Top of stack.'
|
||||
elif cmd == 'locals':
|
||||
printsymbols(tb.tb_frame.f_locals)
|
||||
elif cmd == 'globals':
|
||||
printsymbols(tb.tb_frame.f_globals)
|
||||
elif cmd in ('?', 'help'):
|
||||
browserhelp()
|
||||
else:
|
||||
browserexec(tb, cmd)
|
||||
|
||||
def browserlist(tb):
|
||||
filename = tb.tb_frame.f_code.co_filename
|
||||
lineno = tb.tb_lineno
|
||||
last = lineno
|
||||
first = max(1, last-10)
|
||||
for i in range(first, last+1):
|
||||
if i == lineno: prefix = '***' + string.rjust(`i`, 4) + ':'
|
||||
else: prefix = string.rjust(`i`, 7) + ':'
|
||||
line = linecache.getline(filename, i)
|
||||
if line[-1:] == '\n': line = line[:-1]
|
||||
print prefix + line
|
||||
|
||||
def browserexec(tb, cmd):
|
||||
locals = tb.tb_frame.f_locals
|
||||
globals = tb.tb_frame.f_globals
|
||||
try:
|
||||
exec cmd+'\n' in globals, locals
|
||||
except:
|
||||
t, v = sys.exc_info()[:2]
|
||||
print '*** Exception:',
|
||||
if type(t) == type(''):
|
||||
print t,
|
||||
else:
|
||||
print t.__name__,
|
||||
if v <> None:
|
||||
print ':', v,
|
||||
print
|
||||
print 'Type help to get help.'
|
||||
|
||||
def browserhelp():
|
||||
print
|
||||
print ' This is the traceback browser. Commands are:'
|
||||
print ' up : move one level up in the call stack'
|
||||
print ' down : move one level down in the call stack'
|
||||
print ' locals : print all local variables at this level'
|
||||
print ' globals : print all global variables at this level'
|
||||
print ' list : list source code around the failure'
|
||||
print ' help : print help (what you are reading now)'
|
||||
print ' quit : back to command interpreter'
|
||||
print ' Typing any other 1-line statement will execute it'
|
||||
print ' using the current level\'s symbol tables'
|
||||
print
|
||||
|
||||
def printtb(tb):
|
||||
while tb:
|
||||
print1tb(tb)
|
||||
tb = tb.tb_next
|
||||
|
||||
def print1tb(tb):
|
||||
printtbheader(tb)
|
||||
if tb.tb_frame.f_locals is not tb.tb_frame.f_globals:
|
||||
printsymbols(tb.tb_frame.f_locals)
|
||||
|
||||
def printtbheader(tb):
|
||||
filename = tb.tb_frame.f_code.co_filename
|
||||
lineno = tb.tb_lineno
|
||||
info = '"' + filename + '"(' + `lineno` + ')'
|
||||
line = linecache.getline(filename, lineno)
|
||||
if line:
|
||||
info = info + ': ' + string.strip(line)
|
||||
print info
|
||||
|
||||
def printsymbols(d):
|
||||
keys = d.keys()
|
||||
keys.sort()
|
||||
for name in keys:
|
||||
print ' ' + string.ljust(name, 12) + ':',
|
||||
printobject(d[name], 4)
|
||||
print
|
||||
|
||||
def printobject(v, maxlevel):
|
||||
if v == None:
|
||||
print 'None',
|
||||
elif type(v) in (type(0), type(0.0)):
|
||||
print v,
|
||||
elif type(v) == type(''):
|
||||
if len(v) > 20:
|
||||
print `v[:17] + '...'`,
|
||||
else:
|
||||
print `v`,
|
||||
elif type(v) == type(()):
|
||||
print '(',
|
||||
printlist(v, maxlevel)
|
||||
print ')',
|
||||
elif type(v) == type([]):
|
||||
print '[',
|
||||
printlist(v, maxlevel)
|
||||
print ']',
|
||||
elif type(v) == type({}):
|
||||
print '{',
|
||||
printdict(v, maxlevel)
|
||||
print '}',
|
||||
else:
|
||||
print v,
|
||||
|
||||
def printlist(v, maxlevel):
|
||||
n = len(v)
|
||||
if n == 0: return
|
||||
if maxlevel <= 0:
|
||||
print '...',
|
||||
return
|
||||
for i in range(min(6, n)):
|
||||
printobject(v[i], maxlevel-1)
|
||||
if i+1 < n: print ',',
|
||||
if n > 6: print '...',
|
||||
|
||||
def printdict(v, maxlevel):
|
||||
keys = v.keys()
|
||||
n = len(keys)
|
||||
if n == 0: return
|
||||
if maxlevel <= 0:
|
||||
print '...',
|
||||
return
|
||||
keys.sort()
|
||||
for i in range(min(6, n)):
|
||||
key = keys[i]
|
||||
print `key` + ':',
|
||||
printobject(v[key], maxlevel-1)
|
||||
if i+1 < n: print ',',
|
||||
if n > 6: print '...',
|
Loading…
Reference in New Issue