problem with 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 passglobals=globals()
. Don't use locals() as positional argument, as it will get assigned to globals asrun(statement[, globals[, locals]])
takes globals as first argument. So if the first positional argument islocals()
it will be mistaken asglobals
while running your code.
It should be
pdb.run('print ao.as_table()' locals=locals())
精彩评论