Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行pdb.set_trace()时,将保留自定义上下文管理器_Python_Pdb_Contextmanager - Fatal编程技术网

Python 运行pdb.set_trace()时,将保留自定义上下文管理器

Python 运行pdb.set_trace()时,将保留自定义上下文管理器,python,pdb,contextmanager,Python,Pdb,Contextmanager,我注意到,运行pdb.set_trace()或ipdb.set_trace()作为上下文的最后一行,其上下文管理器及其分配的变量已经被销毁 特别是,当对Python的内置open()执行相同操作时,情况并非如此 import pdb import contextlib @contextlib.contextmanager def my_open(filename): with open(filename) as f: yield f class my_open_c

我注意到,运行
pdb.set_trace()
ipdb.set_trace()
作为上下文的最后一行,其上下文管理器及其分配的变量已经被销毁

特别是,当对Python的内置
open()
执行相同操作时,情况并非如此

import pdb
import contextlib


@contextlib.contextmanager
def my_open(filename):
    with open(filename) as f:
        yield f


class my_open_class():
    def __init__(self, filename):
        self.filename = filename

    def __enter__(self):
        self.f = open(self.filename)
        return self.f

    def __exit__(self, exc_type, exc_value, exc_traceback):
        self.f.close()


with open("/dev/null") as f:
    # f will be available
    pdb.set_trace()


with my_open("/dev/null") as f:
    # f will not be available!
    pdb.set_trace()


with my_open_class("/dev/null") as f:
    # f will not be available!
    pdb.set_trace()


with my_open("/dev/null") as f:
    # f will be available!
    pdb.set_trace()
    pass


with my_open_class("/dev/null") as f:
    # f will be available!
    pdb.set_trace()
    pass
运行和单步执行代码时(使用
c
从第一个断点继续到第二个断点),提示也不同:

open(…)

第四个和第五个街区停在
关口

(Pdb) c
> /Users/me/test.py(41)<module>()
-> pass
(Pdb) c
> /Users/me/test.py(47)<module>()
-> pass
(Pdb) c
(Pdb)c
>/Users/me/test.py(41)()
->通过
(Pdb)c
>/Users/me/test.py(47)()
->通过
(Pdb)c
我找不到这种行为的原因,也不相信这是故意的


这是Python还是pdb中的错误?有没有一种方法可以绕过这个问题而不添加
通行证

您想要实现什么?在
\uuu\u输入\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuoself)
中,您应该返回
self.f
。我正在尝试在。我已经在
\uuuu enter\uuuu
中修复了返回值。我尝试了类方式。它对我来说运行良好…我可以在退出前访问
self.f
,我正在尝试访问
f
,在我调用
pdb.set\u trace()的范围内。是的,我能够做到这一点
(Pdb) c
--Call--
> /usr/local/Cellar/python3/3.6.0/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py(86)__exit__()
-> def __exit__(self, type, value, traceback):
(Pdb) c
--Call--
> /Users/me/test.py(19)__exit__()
-> def __exit__(self, exc_type, exc_value, exc_traceback):
(Pdb) c
> /Users/me/test.py(41)<module>()
-> pass
(Pdb) c
> /Users/me/test.py(47)<module>()
-> pass
(Pdb) c