Question

Ok, this is maybe a difficult question. And I don't want you to do all the hard work for me. I'm just tring to get some good advices and points where I should start.

I'm writing a couple of python programs and i'm having a bad time debugging those. So i'd like to create a simple debug function that records a couple of things.

This is how I'd use it:

# Defined in random_function_definitios.py
def some_random_function():
  a = 1 + 1
  debug(a)
  # More stuff

I'd like to display in debug this information:

  • The function that invoked it: some_random_function
  • The filename where that function is defined: random_function_definitios.py
  • The line number: 4
  • Some context: Global variables defined and local variables defined.

I've been taking a look at the inspect module and the frame builtin object. But i'm not completely sure if i'm in the right direction.

Thank you!

Was it helpful?

Solution

I wrote my own debugger.py with info / warning and debug messages.

see: https://github.com/unixunion/toolbox/blob/master/python/debugger.py

I also use a traceback to go through the stack like:

except Exception, e:
    # capture the traceback data
    exc_type, exc_value, exc_traceback = sys.exc_info()
    warningMsg("-14 %s" % str(e))
    warningMsg("Exec Traceback")
    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)
    warningMsg("Exception")
    traceback.print_exception(exc_type, exc_value, exc_traceback, limit=2, file=sys.stdout)
    raise

Gives results like:

WARNING fabfile.py:76 glassfish_deploy()-14 cannot concatenate 'str' and 'float' objects
WARNING fabfile.py:77 glassfish_deploy()Exec Traceback
File "/home/marzubus/go-agent-scripts/fabfile.py", line 52, in glassfish_deploy    oiled =     gfOil(**kwargs)
WARNING fabfile.py:79 glassfish_deploy()Exception
Traceback (most recent call last):  File "/home/marzubus/go-agent-scripts/fabfile.py",    line 52, in glassfish_deploy
oiled = gfOil(**kwargs)
File "/home/marzubus/go-agent-scripts/oilon/gfdeployer.py", line 37, in __init__
self.tmpDirectory = '/tmp/' + self.domainName + self.time
TypeError: cannot concatenate 'str' and 'float' objects

Kegan

OTHER TIPS

You can use Python's standard logging facilities. Plenty of data come with a log record - full list of them is available in the documentation: http://docs.python.org/library/logging.html#logrecord-attributes . Ones you need are funcName, filename, lineno . To have dump of local variables logged you can convert to string output of built-in function locals() and use it as a log message. All you need do is user a logger like:

import logging

logger = logging.getLogger('some_module')

def some_random_function():
    a = 1 + 1
    logger.debug(str(locals()))

And configure it with proper format using previously mentioned formatting attributes. All of this is well-documented in Basic Logging Tutorial: http://docs.python.org/howto/logging.html#logging-basic-tutorial

edit

But if I were you I would just start python debugger in the debugged context to check all you wanna check interactively:

def some_random_function():
    a = 1 + 1
    import pdb; pdb.set_trace()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top