Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

getargspec, formatargspec Debugging Since Python's development cycle is so fast, the most effective way to debug is often to edit your code to make it output relevant information at key points. Python has many ways to let your code explore its own state in order to extract information that may be relevant for debugging. The inspect and traceback modules specifically support such exploration, which is also known as reflection or introspection. Once you have obtained debugging-relevant information, statement print is often the simplest way to display it. You can also log debugging information to files. Log- ging is particularly useful for programs that run unattended for a long time, as is typically the case for server programs. Displaying debugging information is like dis- playing other kinds of information, as covered in Chapter 10 and Chapter 16, and similarly for logging it, as covered in Chapter 10 and Chapter 11. Python 2.3 will also include a module specifically dedicated to logging. As covered in Chapter 8, rebinding attribute excepthook of module sys lets your program log detailed error information just before your program is terminated by a propagating exception. Python also offers hooks enabling interactive debugging. Module pdb supplies a simple text-mode interactive debugger. Other interactive debuggers for Python are part of integrated development environments (IDEs), such as IDLE and various commercial offerings. However, I do not cover IDEs in this book. The inspect Module The inspect module supplies functions to extract information from all kinds of ob- jects, including the Python call stack (which records all function calls currently ex- ecuting) and source files. At the time of this writing, module inspect is not yet avail- able for Jython. The most frequently used functions of module inspect are as fol- lows. getargspec, formatargspec getargspec(f) f is a function object. getargspec returns a tuple with four items ( arg_names , extra_args , extra_kwds , arg_defaults ) . arg_names is the sequence of names of f 's formal arguments. extra_args is the name of the special formal argument of the form * args , or None if f has no such special argument. extra_kwds is the name of the special formal argument of the form ** kwds , or None if f has no such special argument. arg_defaults is the tuple of default values for f 's arguments. You can deduce other details about f 's signature from getarg spec 's results. For example, f has len( arg_names )-len( arg_defaults ) mandatory ar- guments, and the names of f 's optional arguments are the strings that are the items of the list slice arg_names [-len( arg_defaults ):] . formatargspec accepts one to four arguments that are the same as the items of the tuple that getargspec returns, and returns a formatted string that displays this information. Thus, formatargspec(*getargspec( f )) returns a formatted string with f 's formal argu- ments (i.e., f 's signature) in parentheses, as used in the def statement that created f . Testing, Debug- ging, and Optimiz- ing Chapter 17:Testing, Debugging, and Optimizing | 435