Posts Tagged ‘programming’

Improved Python Traceback Module

Published January 27th, 2010, updated January 28th, 2010.

Like any modern language, Python comes along with a nice traceback module. This module gives you stack traces from the line of code where an exception is raised up to the next try-except clause. So, you can easily catch exceptions and write stack traces into a debug log. This debugging technique is pretty handy to drill down bugs and I use it a lot in prototyping.

Using the traceback module is straight forward for evident programming mistakes. However, real bugs are context-sensitive and they can hardly be reproduced without the actual data that was processed when an exception was raised. If you can reproduce a specific bug, you can add some logging code in front and inspect the variables the next time the bug is triggered. But if a bug occurs once in a blue moon, you’d be better in logging the data the first time an exception raises.

import traceback

def erroneous_function():
    ham = u"unicode string with umlauts äöü."
    eggs = "binary string with umlauts äöü."
    i = 23
    if i>5:
        raise Exception("it's true!")

try:
    erroneous_function()
except:
    print traceback.format_exc(with_vars=True)

Here’s my solution; an improved Python traceback module the logs variables from the local scope next to the affected code. You can find a working copy in our Mercirual repository (see the below).

Traceback (most recent call last):
 File "test.py", line 16, in <module>
   Local variables:
     erroneous_function = <function erroneous_function at 0x7ff6d82b...
     __builtins__ = <module '__builtin__' (built-in)>
     __file__ = "test.py"
     traceback = <module 'traceback' from '/srv/www/vhosts/dev.teamr...
     __name__ = "__main__"
     __doc__ = None
   erroneous_function()
 File "test.py", line 13, in erroneous_function
   Local variables:
     i = 23
     eggs = "binary string with umlauts \xc3\xa4\xc3\xb6\xc3\xbc."
     ham = u"unicode string with umlauts ???."
   raise Exception("it's true!")
Exception: it's true!

I am not sure if it is the “right” solution as sensitive information might be logged. This might have security implications for some real-world scenarios where webapps report stack traces to the end user (e.g. by using cgitb in production).

Credit: this code was inspired by format_exc_plus by Bryn Keller.

2010-01-28: there’s an active discussion on python-dev.

get latest source code
visit mercurial repository

Today 16oo: Sickos Hack Nacht

Published October 24th, 2009.

We are going to have a Hack Nacht tonight. Sickos and other nerds are invited to exchange ideas, write code and to get to know each other. I’ve some ideas what to do and I’m looking forward to seeing you hackers tonight. I’ll update this article when the event is over, stay tuned (our join us on SickosNet).

Exploiting Python’s Class Dispatcher

Published May 16th, 2008.

In object oriented programming, the class dispatcher is a built-in function that looks up member functions and executes them in the context of a given class. In Python, those lookups are conducted dynamically, enabling one to modify the behaviour of a class without the need of subclassing. Here are some unusual but yet useful examples.

# class-based programming style
class Foo:
    pass

class Bar(Foo):
    def bar(self):
        print "bar"

bar = Bar()

bar.bar() # prints "bar"

So what? If you write all code on your own, you are fine. You can subclass Foo and invoke all methods from the new class Bar. But what, if the instantiation is done in code sections that you cannot modify? Imagine you are writing a plugin and you do not want to touch the code of others. They decided to instantiate Foo and you do not want to change this, nor you want to change Foo.

# prototype-based programming style
class Foo:
    pass

foo = Foo()

def bar(self):
    print "bar"

foo.__class__.bar = bar

foo.bar() # prints "bar"

This second example shows how to “inject” a method into an already instantiated object. In fact, this works because Python uses dynamic delegation. Objects and classes are inspected at runtime and so, the dispatcher finds attributes even if they are added after object instantiation.