1 new commit in py:
https://bitbucket.org/hpk42/py/changeset/22bf546e4878/ changeset: 22bf546e4878 user: Manuel Jacob date: 2012-11-05 23:49:21 summary: Include * and ** arguments in py.code.FormattedExcinfo. affected #: 3 files diff -r 88c67bb0484990537681b03840cc488690719311 -r 22bf546e487897b1b88fa075f45753bc1a4eb538 py/_code/code.py --- a/py/_code/code.py +++ b/py/_code/code.py @@ -1,5 +1,6 @@ import py import sys, os.path +from inspect import CO_VARARGS, CO_VARKEYWORDS builtin_repr = repr @@ -49,12 +50,19 @@ # return source only for that part of code return py.code.Source(self.raw) - def getargs(self): + def getargs(self, var=False): """ return a tuple with the argument names for the code object + + if 'var' is set True also return the names of the variable and + keyword arguments when present """ # handfull shortcut for getting args raw = self.raw - return raw.co_varnames[:raw.co_argcount] + argcount = raw.co_argcount + if var: + argcount += raw.co_flags & CO_VARARGS + argcount += raw.co_flags & CO_VARKEYWORDS + return raw.co_varnames[:argcount] class Frame(object): """Wrapper around a Python frame holding f_locals and f_globals @@ -102,11 +110,14 @@ def is_true(self, object): return object - def getargs(self): + def getargs(self, var=False): """ return a list of tuples (name, value) for all arguments + + if 'var' is set True also include the variable and keyword + arguments when present """ retval = [] - for arg in self.code.getargs(): + for arg in self.code.getargs(var): try: retval.append((arg, self.f_locals[arg])) except KeyError: @@ -432,7 +443,7 @@ def repr_args(self, entry): if self.funcargs: args = [] - for argname, argvalue in entry.frame.getargs(): + for argname, argvalue in entry.frame.getargs(var=True): args.append((argname, self._saferepr(argvalue))) return ReprFuncArgs(args) diff -r 88c67bb0484990537681b03840cc488690719311 -r 22bf546e487897b1b88fa075f45753bc1a4eb538 testing/code/test_code.py --- a/testing/code/test_code.py +++ b/testing/code/test_code.py @@ -104,3 +104,48 @@ s = unicode_or_repr(A()) assert 'print-error' in s assert 'ValueError' in s + + +def test_code_getargs(): + def f1(x): + pass + c1 = py.code.Code(f1) + assert c1.getargs(var=True) == ('x',) + + def f2(x, *y): + pass + c2 = py.code.Code(f2) + assert c2.getargs(var=True) == ('x', 'y') + + def f3(x, **z): + pass + c3 = py.code.Code(f3) + assert c3.getargs(var=True) == ('x', 'z') + + def f4(x, *y, **z): + pass + c4 = py.code.Code(f4) + assert c4.getargs(var=True) == ('x', 'y', 'z') + + +def test_frame_getargs(): + def f1(x): + return sys._getframe(0) + fr1 = py.code.Frame(f1('a')) + assert fr1.getargs(var=True) == [('x', 'a')] + + def f2(x, *y): + return sys._getframe(0) + fr2 = py.code.Frame(f2('a', 'b', 'c')) + assert fr2.getargs(var=True) == [('x', 'a'), ('y', ('b', 'c'))] + + def f3(x, **z): + return sys._getframe(0) + fr3 = py.code.Frame(f3('a', b='c')) + assert fr3.getargs(var=True) == [('x', 'a'), ('z', {'b': 'c'})] + + def f4(x, *y, **z): + return sys._getframe(0) + fr4 = py.code.Frame(f4('a', 'b', c='d')) + assert fr4.getargs(var=True) == [('x', 'a'), ('y', ('b',)), + ('z', {'c': 'd'})] diff -r 88c67bb0484990537681b03840cc488690719311 -r 22bf546e487897b1b88fa075f45753bc1a4eb538 testing/code/test_excinfo.py --- a/testing/code/test_excinfo.py +++ b/testing/code/test_excinfo.py @@ -494,6 +494,27 @@ assert tw.lines[1] == "x = 5, y = 13" assert tw.lines[2] == "z = " + repr('z' * 120) + def test_repr_tracebackentry_lines_var_kw_args(self, importasmod): + mod = importasmod(""" + def func1(x, *y, **z): + raise ValueError("hello\\nworld") + """) + excinfo = py.test.raises(ValueError, mod.func1, 'a', 'b', c='d') + excinfo.traceback = excinfo.traceback.filter() + entry = excinfo.traceback[-1] + p = FormattedExcinfo(funcargs=True) + reprfuncargs = p.repr_args(entry) + assert reprfuncargs.args[0] == ('x', repr('a')) + assert reprfuncargs.args[1] == ('y', repr(('b',))) + assert reprfuncargs.args[2] == ('z', repr({'c': 'd'})) + + p = FormattedExcinfo(funcargs=True) + repr_entry = p.repr_traceback_entry(entry) + assert repr_entry.reprfuncargs.args == reprfuncargs.args + tw = TWMock() + repr_entry.toterminal(tw) + assert tw.lines[0] == "x = 'a', y = ('b',), z = {'c': 'd'}" + def test_repr_tracebackentry_short(self, importasmod): mod = importasmod(""" def func1(): Repository URL: https://bitbucket.org/hpk42/py/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. _______________________________________________ py-svn mailing list py-svn@codespeak.net http://codespeak.net/mailman/listinfo/py-svn