1. 交互式调试器

1.1. 交互式应用程序调试

事情总会出错, 而当它们出错的时候, 快速地查清楚什么和为什么出错就显得很重要了. PyLons 默认的调试器是使用一个 Ian Bicking 编写地 EvalException 中间件的定制版本, 它还加入了完整的 Myghty Traceback 信息.

关于如何启动交互性调试请看 getting_started.html 文档的 Enabling Debugging 一节.

1.2. 调试界面

调试界面的顶端包括三个标签:

  • Traceback

    • 使用交互式调试器提供原始的异常跟踪信息
  • Extra Data

    • 显示异常时刻的 CGI, WSGI 变量, 和配置信息.
  • Myghty

    • Myghty 模板的人性化的 traceback 信息, 和 Myghty 模板以返回值的形式抛出的错误.

既然 Myghty 会把模板编译成 Python 模块, 那么要精确地找出错误出在模板的哪一行是比较困难的. Myghty 标签提供了完整的 Myghty traceback, 它包含了精确的模板行号和错误起源. 如果你的错误是在模板渲染之前触发的, 那么这一节就没用任何 Myghty 信息了.

1.3. 示例: 研究 Traceback

使用交互调试器还有一个好处就是可以对那些只在请求过程中才存在的对象--比如 Myghty 的 m 对象, 或者是 sessionrequest 对象--有更深入的认识. 对于有些对象需要注意的是, 除非你先调用一个 Controller 内置的方法把它们绑定到你的控制器, 你将没法在交互式调试中研究它们.

只要在你感兴趣的 action 里抛出一个异常, 就能触发一个错误, 这样我们就可以来研究研究里面究竟发生了什么. 在这个例子中, 我们会在一个用来显示你正在阅读的这个页面的 action 中抛出一个错误. 这个 docs 控制器内容如下:

   1     class DocsController(BaseController):
   2         def view(self, url):
   3             if request.path_info.endswith('docs'):
   4                 redirect_to('/docs/')
   5             return render_response('/docs/' + url)

既然我们要研究 sessionrequest, 我们需要绑定它们先. 加上绑定和抛出异常的代码后我们的 action 是这个样子:

   1     def view(self, url):
   2         self._attach_locals()
   3         raise "hi"
   4         if request.path_info.endswith('docs'):
   5             redirect_to('/docs/')
   6         return render_response('/docs/' + url)

下面这个图是在上面这个例子中研究 Traceback 的一个截图 (只截取相关部分):

http://PyLonshq.com/img/screenshots/doctraceback.gif

1.4. Email 选项

You can make all sorts of changes to how the debugging works. For example if you disable the debug variable in the config file PyLons will email you an error report instead of displaying it as long as you provide your email address at the top of the config file:

    error_email_from = [email protected]

This is very useful for a production site. Emails are sent via SMTP so you need to specify a valid SMTP server too.

1.5. 修改调试器主题

If you are using PyLons in a commercial company it is useful to be able to change the theme of the debugger so that if an error occurs, a page with your company logo appears. You might also decide to remove the PyLons logo if you use the debugger a lot so that there is more space to view the traceback.

You can change the theme by creating a new template. For example, a very simple template might look like this:

   1     my_error_template = '''
   2         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   3           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   4         <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   5         <head>
   6         <title>Server Error</title>
   7         %(head)s
   8         <body id="documentation">
   9             %(extra_data)s
  10             %(myghty_data)s
  11             %(traceback_data)s
  12         </body>
  13         </html>
  14     '''

The values are automatically substituted by the error middleware. You can also add %(prefix)s which is replaced by the path to your application so you can include CSS files or images. For example if your application had a file called style.css in a directory called css within your public directory, you could add the following line to your template to ensure that the CSS file was always correctly found:

    <link rel="stylesheet" href="%(prefix)s/css/style.css" type="text/css" media="screen" />

If you want to retain the ability to switch between the different error displays you need a slightly more complicated example:

   1     my_error_template = '''
   2         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   3           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   4         <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   5         <head>
   6         <title>Server Error</title>
   7         %(head)s
   8         <body id="documentation" onload="switch_display('%(set_tab)s')">
   9         <ul id="navlist">
  10           <li id='traceback_data_tab' class="active">
  11             <a href="javascript:switch_display('traceback_data')" id='traceback_data_link'>Traceback</a>
  12           </li>
  13           <li id='extra_data_tab' class="">
  14             <a href="javascript:switch_display('extra_data')" id='extra_data_link'>Extra Data</a>
  15           </li>
  16           <li id='myghty_data_tab'>
  17             <a href="javascript:switch_display('myghty_data')" id='myghty_data_link'>Myghty</a>
  18           </li>
  19         </ul>
  20         <div id="extra_data" class="hidden-data">
  21             %(extra_data)s
  22         </div>
  23         <div id="myghty_data" class="hidden-data">
  24             %(myghty_data)s
  25         </div>
  26         <div id="traceback_data">
  27             %(traceback_data)s
  28         </div>
  29         </body>
  30         </html>
  31     '''

In this case when you click on a link the relevant tab is displayed. As long as you keep the same IDs and class names, you can specify your own styles and create a theme like the one used by PyLons by default.

Now that you have a template you need to use it in your application. In config/middleware.py change the following lines:

   1     # @@@ Error Handling @@@
   2     app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware)

to use your template:

   1     # @@@ Error Handling @@@
   2     my_error_template = '''
   3         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
   4           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   5         <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   6         <head>
   7         <title>Server Error</title>
   8         %(head)s
   9         <body id="documentation">
  10             %(extra_data)s
  11             %(myghty_data)s
  12             %(traceback_data)s
  13         </body>
  14         </html>
  15     '''
  16     app = ErrorHandler(app, global_conf, error_template=my_error_template, **config.errorware)

Your interactive debugger will now be themed with the new template.