开发者

problem with pdb

开发者 https://www.devze.com 2023-02-17 15:42 出处:网络
on a django shell i tried from django import forms class A(forms.Form): x = forms.CharField() ao = A() import pdb

on a django shell i tried

from django import forms
class A(forms.Form):
    x = forms.CharField()
ao = A()
import pdb 
pdb.run('ao.as_table') 

but on the last statement , after hitting continue for the first time i am getting a stacktrace as

/usr/lib/python2.6/pdb.pyc in run(statement, globals, locals)
   1218 
   1219 def run(statement, globals=None, locals=None):
-> 1220     Pdb().run(statement, globals, locals)
   1221 
   1222 def runeval(expression, globals=None,开发者_运维百科 locals=None):

/usr/lib/python2.6/bdb.pyc in run(self, cmd, globals, locals)
    370             cmd = cmd+'\n'
    371         try:
--> 372             exec cmd in globals, locals
    373         except BdbQuit:
    374             pass

/usr/lib/pymodules/python2.6/IPython/FakeModule.pyc in <module>()

NameError: name 'ao' is not defined

what went wrong ? :(


pdb.run('print ao.as_table()', locals())

Basically pass on the locals() dictionary


Two things.

  • First, use the statement argument as a call. i.e "ao.as_table()" with () as suffix.
  • Second, pass a keyword argument locals=locals() to the function. You can also pass globals=globals(). Don't use locals() as positional argument, as it will get assigned to globals as run(statement[, globals[, locals]]) takes globals as first argument. So if the first positional argument is locals() it will be mistaken as globals while running your code.

It should be

pdb.run('print ao.as_table()' locals=locals())

0

精彩评论

暂无评论...
验证码 换一张
取 消