pdb.set_trace(): 脚本在断点处停止,但没有显示提示符

3

我有一个运行神经网络模拟的脚本。最近,该脚本产生了一些警告,这些警告源自于scipy,在一个名为_dumbdbm_patched的模块中。为了调试此问题,我已经插入了以下代码:

import pdb
pdb.set_trace()

我想把我的脚本放进已经安装在我的计算机上的scipy源代码中。 当我在IPython中运行我的脚本时,它会停在我设置的断点处,但是(Pdb)提示从未显示。只有在我执行了一个键盘中断之后,这个提示才会出现--就在KeyboardInterrupt退出程序之前:

In [2]: run ardid.py --k 1 --g fig_4
^C> /home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/_dumbdbm_patched.py(163)open()
-> return _Database(file)
(Pdb) ---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/home/despo/dbliss/dopa_net/brian/ardid/ardid.py in <module>()
    173     # -------------------------------------------------------------------------
    174 
--> 175     cii_pfc = brian2.Synapses(pi_pfc, pi_pfc, pre='s_gaba += 1', connect=True)
    176     cie_pfc = brian2.Synapses(pi_pfc, pe_pfc, pre='s_gaba += 1', connect=True)
    177 

/home/despo/dbliss/lib/python2.7/site-packages/Brian2-2.0b3+git-py2.7-linux-x86_64.egg/brian2/synapses/synapses.pyc in __init__(self, source, target, model, pre, post, connect, delay, namespace, dtype, codeobj_class, dt, clock, order, method, name)
    756 
    757         if not connect is False:
--> 758             self.connect(connect, level=1)
    759 
    760     def __len__(self):

/home/despo/dbliss/lib/python2.7/site-packages/Brian2-2.0b3+git-py2.7-linux-x86_64.egg/brian2/synapses/synapses.pyc in connect(self, pre_or_cond, post, p, n, namespace, level)
   1064                                 'to an float, is type %s instead.' % type(n))
   1065             self._add_synapses(None, None, n, p, condition=pre_or_cond,
-> 1066                                namespace=namespace, level=level+1)
   1067         else:
   1068             raise TypeError(('First argument has to be an index or a '

/home/despo/dbliss/lib/python2.7/site-packages/Brian2-2.0b3+git-py2.7-linux-x86_64.egg/brian2/synapses/synapses.pyc in _add_synapses(self, sources, targets, n, p, condition, namespace, level)
   1192                                             check_units=False,
   1193                                             run_namespace=namespace,
-> 1194                                             level=level+1)
   1195             codeobj()
   1196 

/home/despo/dbliss/lib/python2.7/site-packages/Brian2-2.0b3+git-py2.7-linux-x86_64.egg/brian2/codegen/codeobject.pyc in create_runner_codeobj(group, code, template_name, user_code, variable_indices, name, check_units, needed_variables, additional_variables, level, run_namespace, template_kwds, override_conditional_write, codeobj_class)
    256 
    257     if codeobj_class is None:
--> 258         codeobj_class = device.code_object_class(group.codeobj_class)
    259     else:
    260         codeobj_class = device.code_object_class(codeobj_class)

/home/despo/dbliss/lib/python2.7/site-packages/Brian2-2.0b3+git-py2.7-linux-x86_64.egg/brian2/devices/device.pyc in code_object_class(self, codeobj_class)
    199     def code_object_class(self, codeobj_class=None):
    200         if codeobj_class is None:
--> 201             codeobj_class = get_default_codeobject_class()
    202         return codeobj_class
    203 

/home/despo/dbliss/lib/python2.7/site-packages/Brian2-2.0b3+git-py2.7-linux-x86_64.egg/brian2/devices/device.pyc in get_default_codeobject_class(pref)
     77     if isinstance(codeobj_class, str):
     78         if codeobj_class == 'auto':
---> 79             return auto_target()
     80         for target in codegen_targets:
     81             if target.class_name == codeobj_class:

/home/despo/dbliss/lib/python2.7/site-packages/Brian2-2.0b3+git-py2.7-linux-x86_64.egg/brian2/devices/device.pyc in auto_target()
     51                            if target.class_name)
     52         using_fallback = False
---> 53         if 'weave' in target_dict and target_dict['weave'].is_available():
     54             _auto_target = target_dict['weave']
     55         elif 'cython' in target_dict and target_dict['cython'].is_available():

/home/despo/dbliss/lib/python2.7/site-packages/Brian2-2.0b3+git-py2.7-linux-x86_64.egg/brian2/codegen/runtime/weave_rt/weave_rt.pyc in is_available()
    135                              headers=['<algorithm>', '<limits>'],
    136                              extra_compile_args=extra_compile_args,
--> 137                              verbose=0)
    138                 return True
    139         except Exception as ex:

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/inline_tools.py in inline(code, arg_names, local_dict, global_dict, force, compiler, verbose, support_code, headers, customize, type_converters, auto_downcast, newarr_converter, **kw)
    350         # 2. try function catalog
    351         try:
--> 352             results = attempt_function_call(code,local_dict,global_dict)
    353         # 3. build the function
    354         except ValueError:

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/inline_tools.py in attempt_function_call(code, local_dict, global_dict)
    419     # 3. try persistent catalog
    420     module_dir = global_dict.get('__file__',None)
--> 421     function_list = function_catalog.get_functions(code,module_dir)
    422     for func in function_list:
    423         try:

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/catalog.py in get_functions(self, code, module_dir)
    793         try:
    794             self.set_module_directory(module_dir)
--> 795             function_list = self.get_cataloged_functions(code)
    796             # put function_list in cache to save future lookups.
    797             if function_list:

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/catalog.py in get_cataloged_functions(self, code)
    708         function_list = []
    709         for path in self.build_search_order():
--> 710             cat = get_catalog(path,mode)
    711             if cat is not None and code in cat:
    712                 # set up the python path so that modules for this

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/catalog.py in get_catalog(module_path, mode)
    479            and ((dumb and os.path.exists(catalog_file+'.dat'))
    480                 or os.path.exists(catalog_file)):
--> 481         sh = shelve.open(catalog_file,mode)
    482     else:
    483         if mode == 'r':

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/_dumb_shelve.py in open(filename, flag)
     49     """
     50 
---> 51     return DbfilenameShelf(filename, flag)

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/_dumb_shelve.py in __init__(self, filename, flag)
     20     def __init__(self, filename, flag='c'):
     21         from . import _dumbdbm_patched
---> 22         Shelf.__init__(self, _dumbdbm_patched.open(filename, flag))
     23 
     24     def __getitem__(self, key):

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/_dumbdbm_patched.py in open(file, flag, mode)
    161     pdb.set_trace()
    162 
--> 163     return _Database(file)

/home/despo/dbliss/lib/python2.7/site-packages/scipy/weave/_dumbdbm_patched.py in open(file, flag, mode)
    161     pdb.set_trace()
    162 
--> 163     return _Database(file)

/usr/local/anaconda-1.9.2/lib/python2.7/bdb.pyc in trace_dispatch(self, frame, event, arg)
     47             return # None
     48         if event == 'line':
---> 49             return self.dispatch_line(frame)
     50         if event == 'call':
     51             return self.dispatch_call(frame, arg)

/usr/local/anaconda-1.9.2/lib/python2.7/bdb.pyc in dispatch_line(self, frame)
     65     def dispatch_line(self, frame):
     66         if self.stop_here(frame) or self.break_here(frame):
---> 67             self.user_line(frame)
     68             if self.quitting: raise BdbQuit
     69         return self.trace_dispatch

/usr/local/anaconda-1.9.2/lib/python2.7/pdb.pyc in user_line(self, frame)
    156             self._wait_for_mainpyfile = 0
    157         if self.bp_commands(frame):
--> 158             self.interaction(frame, None)
    159 
    160     def bp_commands(self,frame):

/usr/local/anaconda-1.9.2/lib/python2.7/pdb.pyc in interaction(self, frame, traceback)
    208         self.setup(frame, traceback)
    209         self.print_stack_entry(self.stack[self.curindex])
--> 210         self.cmdloop()
    211         self.forget()
    212 

/usr/local/anaconda-1.9.2/lib/python2.7/cmd.pyc in cmdloop(self, intro)
    128                     if self.use_rawinput:
    129                         try:
--> 130                             line = raw_input(self.prompt)
    131                         except EOFError:
    132                             line = 'EOF'

KeyboardInterrupt: 

当我直接从 shell 中进行调用时,情况也是一样的,如下所示:

$ python ardid.py --k 1 --g fig_4

为什么会发生这种情况?我该如何使得scipy中的pdb正常工作?
注意:我的脚本没有使用线程。
2个回答

3
您的标准输出被重定向到其他地方了吗?当我从shell(bash)运行Python脚本时,我也遇到了同样的问题。我正在运行类似于以下内容的命令:
python script_with_pdb_trace.py > output.py

我刚刚运行了程序,直到此时仍未出现PDB提示。

python script_with_pdb_trace.py

我并没有刻意重定向它,但我猜我的系统管理员可能在我不知情的情况下这样做了。 - abcd

1

我将尝试两种解决方案。我是python的初学者。

1-- 尝试不给任何参数,即 python ardid.py

2-- 有时候会发生没有提示符的情况,但我可以输入pdb命令s、n()。 3-- 注释掉keyboardinterrupt异常。


#2 是个好建议,我会试一下。 #3 不太合理,据我所知。 KeyboardInterrupt 并没有被写入我的代码中;它是在脚本执行期间输入 ctrl-C 时出现的。#1 对这个脚本不起作用;脚本需要命令行参数。我可以重写脚本,使其不需要这些参数,但我的目标是让 pdb 在当前的脚本中正常工作。 - abcd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接