diff --git a/Lib/cgitb.py b/Lib/cgitb.py --- a/Lib/cgitb.py +++ b/Lib/cgitb.py @@ -125,9 +125,19 @@ args, varargs, varkw, locals = inspect.getargvalues(frame) call = '' if func != '?': - call = 'in ' + strong(func) + \ + try: + call = 'in ' + strong(func) + \ inspect.formatargvalues(args, varargs, varkw, locals, formatvalue=lambda value: '=' + pydoc.html.repr(value)) + except Exception as e: + import traceback + print '-'*80 + print repr(e) + traceback.print_exc() + print 'args', args + print 'varargs', varargs + print '-'*80 + return '' highlight = {} def reader(lnum=[lnum]): diff --git a/Lib/inspect.py b/Lib/inspect.py --- a/Lib/inspect.py +++ b/Lib/inspect.py @@ -867,6 +867,14 @@ specs.append(formatvarkw(varkw)) return '(' + string.join(specs, ', ') + ')' +# a placeholder with as repr (using a string would have '' +# with quotes). This is used in formatargvalues +class _Deleted(object): + def __repr__(self): + return '' +_deleted = _Deleted() +del _Deleted + def formatargvalues(args, varargs, varkw, locals, formatarg=str, formatvarargs=lambda name: '*' + name, @@ -881,7 +889,7 @@ argument is an optional function to format the sequence of arguments.""" def convert(name, locals=locals, formatarg=formatarg, formatvalue=formatvalue): - return formatarg(name) + formatvalue(locals[name]) + return formatarg(name) + formatvalue(locals.get(name, _deleted)) specs = [] for i in range(len(args)): specs.append(strseq(args[i], convert, join)) diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py --- a/Lib/test/test_inspect.py +++ b/Lib/test/test_inspect.py @@ -193,6 +193,14 @@ self.assertEqual(inspect.formatargvalues(args, varargs, varkw, locals), '(a=7, b=8, c=9, d=3, (e=4, (f=5,)), *g=(), **h={})') + def test_formatargvalues_deleted_args(self): + def fun(a, b, x, y): + del b, y + return inspect.currentframe() + args = inspect.getargvalues(fun(2, 4, 8, 16)) + self.assertEqual(inspect.formatargvalues(*args), + '(a=2, b=, x=8, y=)') + class GetSourceBase(unittest.TestCase): # Subclasses must override. fodderFile = None