无论变量是否存在于Python Debugger中,变量都可以交替使用 [英] Variable alternates whether or not it exists in Python Debugger

查看:139
本文介绍了无论变量是否存在于Python Debugger中,变量都可以交替使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下吗(Python 2.7,Django 1.7)

  foo = data ['selected_items'] 
(Pdb)foo
(Pdb)*** NameError:name'foo'未定义
foo
(Pdb)u'1,2'
foo
(Pdb)*** NameError :没有定义名称'foo'
foo
(Pdb)u'1,2'
foo
(Pdb)*** NameError:name'foo'未定义
foo
(Pdb)u'1,2'

这是查看功能触发它:

  def process_form(request,model_name):
form = BulkEditForm(request.POST)
如果form.is_valid():
data = form.clean()

如果data ['select_all']:
pass
else:
import pdb; pdb.set_trace()

request.POST:

  {'select_all':False,'primary_tech':< Person:Bob>,primary_biz':< Person:Mary>,selected_items':u'1,2 ','backup_tech':无,'backup_biz':无} 


解决方案

你有两个线程在同一个断点停止。



所以基本上,有两个pdb的实例,竞争你的提示,即你的提示是在不同的线程。您首先分配一个名称 foo ,而不是另一个有机会运行,不知道 foo ,那么当你按回车键,另一个回来,定义了 foo



这是最好的指标情况是您的提示与您的命令及其输出不一致。而不是:

 < PROMPT> COMMAND 
OUTPUT
< PROMPT> COMMAND
OUTPUT

你看到:

  COMMAND 
< PROMPT> OUTPUT
命令
< PROMPT> OUTPUT

每次按Enter键,其他线程在第一线程之前的提示中潜行设法写出其输出。






编辑



一个简单的方式再现在独立的,没有django:

 从线程导入线程

def f(x ):
import pdb; pdb.set_trace()
while True:pass

t1 = Thread(target = lambda:f(1))
t2 = Thread(target = lambda:f(2) )
t1.start(); t2.start()

(Pdb)x
2
(Pdb)x
1


Can anyone explain this? (Python 2.7, Django 1.7)

foo = data['selected_items']
(Pdb) foo
(Pdb) *** NameError: name 'foo' is not defined
foo
(Pdb) u'1,2'
foo
(Pdb) *** NameError: name 'foo' is not defined
foo
(Pdb) u'1,2'
foo
(Pdb) *** NameError: name 'foo' is not defined
foo
(Pdb) u'1,2'

Here is the view function that triggered it:

def process_form(request, model_name):
    form = BulkEditForm(request.POST)
    if form.is_valid():
        data = form.clean()

        if data['select_all']:
            pass
        else:
            import pdb; pdb.set_trace()

request.POST:

{'select_all': False, 'primary_tech': <Person: Bob>, 'primary_biz': <Person: Mary>, 'selected_items': u'1,2', 'backup_tech': None, 'backup_biz': None}

解决方案

You have two threads which stopped at the same breakpoint.

So basically, there are two instances of pdb, competing for your prompt, i.e. your prompts are alternating between the different threads. You first assign to the name foo in one, than the other one gets a chance to run, knowing nothing about foo, then when you press enter, the other is back, with foo defined.

The best indicator that this is the case is that your prompt is not aligned with your commands and their outputs. Instead of:

<PROMPT> COMMAND
OUTPUT
<PROMPT> COMMAND
OUTPUT

You see:

COMMAND
<PROMPT> OUTPUT
COMMAND
<PROMPT> OUTPUT

Everytime you press enter, the "other" thread sneaks in its prompt before the "first" thread manages to write its output.


EDIT

A simple way to reproduce in a standalone, without django:

from threading import Thread

def f(x):
    import pdb; pdb.set_trace()
    while True: pass

t1 = Thread(target=lambda: f(1))
t2 = Thread(target=lambda: f(2))
t1.start(); t2.start()

(Pdb) x
2
(Pdb) x
1

这篇关于无论变量是否存在于Python Debugger中,变量都可以交替使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆