> -----Original Message-----
> From: python-dev-bounces+kristjan=ccpgames....@python.org
> [mailto:python-dev-bounces+kristjan=ccpgames....@python.org] On Behalf
> Of Guido van Rossum
> 
> This is a nice idea, but to be 100% robust is very hard. I assume
> you've already added something to clip large values. But still...
> sometimes you find variables with a broken __repr__, and you must
> catch various exceptions, and even be prepared for __repr__ not
> returning a string. Sometimes there are too many variables to print,
> or their names are excessively long. Sometimes __repr__ takes a very
> long time. How many of these have you encountered yet? And dealt with?

We have been using this approach in EVE online for seven years at least and 
encountered most possible problems :)
Only recently did I refactor it into a traceback2.py module.
Here is the value printing code:

def _format_locals(f_locals, format):
    lines = []
    if f_locals is None:
        return lines
    for key, value in f_locals.iteritems():
        if format & FORMAT_LOGSRV:
            extra = '        %s = ' % (key,)
        else:
            extra = '%20s = ' % (key,)
        try:
            width = 253-len(extra) #limit according to log server
            val = pprint.pformat(value, depth=1, width=width)
            #truncate each var's dump to 1024 bytes
            if len(val) > 1024:
                val = val[:1024] + "..."
            vlines = val.splitlines()
            #show at most 4 lines
            if len(vlines) > 4:
                vlines[4:] = ["..."]
            #indent lines
            for i in xrange(1, len(vlines)):
                vlines[i] = " "*23+vlines[i]
            extra += "\n".join(vlines)+"\n"
        except Exception, e:
            try:
                extra += "<error printing value: %r>"%(e,)
            except Exception:
                extra += "<error printing value>"
        lines.append(extra)
    return lines

You'll notice the pprint thingie to format output.  There is also truncation of 
long data as well as double exception handling on the output.  There is no 
unicode encoding at this level, we pass on the any unicode objects directly (in 
our framework, that only happens at the final output stream if necessary)

K
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to