English 中文(简体)
从一个方案而不是从奥塞罗省开办一场 p会议
原标题:Running a python debug session from a program, not from the console

I m writing a little python IDE, and I want to add simple debugging. I don t need all the features of winpdb. How do I launch a python program (by file name) with a breakpoint set at a line number so that it runs until that line number and halts? Note that I don t want to do this from the command-line, and I don t want to edit the source (by inserting set_trace, for example). And I don t want it to stop at the first line so I have to run the debugger from there. I ve tried all the obvious ways with pdb and bdb, but I must be missing something.

问题回答

这样做的唯一可行办法(如我所知)是作为从民主选举学会内部的一个分过程进行 Python。 这避免了从现任口译中进行的“污染”,因此,方案的实施很可能与独立启动方案一样。 (如果存在问题,检查分处理环境。) 这样,你就可以使用“假肢”的方式操作文字。

p = subprocess.Popen(args=[sys.executable,  -m ,  pdb ,  scriptname.py ,  arg1 ],
                     stdin=subprocess.PIPE,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)

这将在夸张的迅速的时候启动。 你们需要掌握一些毫不夸张的指挥,以设置突破点。

o,e = p.communicate( break scriptname.py:lineno )

If this works, o should be the normal output of the Python interpreter after it sets a breakpoint, and e should be empty. I d suggest you play around with this and add some checks in your code to ensure whether the breakpoints were properly set.

之后,你可以启动该方案。

p.communicate( continue )

此时此刻,你可能要看一下你们重新植根于其发展信息网的投入、产出和错误。 也许,你需要做这样的事:

while p.returncode is None:
    o,e = p.communicate(console.read())
    console.write(o)
    console.write(e)

You should consider that snippet to be effectively pseudocode, since depending on how exactly your console works, it ll probably take some tinkering to get it right.

如果这似乎过于夸张,你可能利用Avry s pdbbdb模块(I m guessing “Python debugger”和“基本 de”)的特征简化程序。 如何做到这一点的最佳参考是<代码>pdb模块本身的来源代码。 基本上,这些模块的责任分工方式是:bdb处理“属于”的变迁功能,如设置断点或停止和重新启动执行;pdb是处理用户互动,即阅读指挥和显示产出的包装。

您的IDE-Integrated debugger认为,调整pdb的行为是有意义的。 模块以两种方式我认为:

  1. have it automatically set breakpoints during initialization, without you having to explicity send the textual commands to do so
  2. make it take input from and send output to your IDE s console

只有这两项改动才能通过分类(<代码>pdb)执行。 Pdb。 您可以设立一个分级,其初始编制者将一个分级名单作为另一个论点:

class MyPDB(pdb.Pdb):
    def __init__(self, breakpoints, completekey= tab ,
                 stdin=None, stdout=None, skip=None):
        pdb.Pdb.__init__(self, completekey, stdin, stdout, skip)
        self._breakpoints = breakpoints

实际确定破碎点的逻辑位置,是在被夸大者读到其<条码>后。 为实际安装,使用<代码>_break <代码>b Bdb:

    def setInitialBreakpoints(self):
        _breakpoints = self._breakpoints
        self._breakpoints = None  # to avoid setting breaks twice
        for bp in _breakpoints:
            self.set_break(filename=bp.filename, line=bp.line,
                           temporary=bp.temporary, conditional=bp.conditional,
                           funcname=bp.funcname)

    def setup(self, f, t):
        pdb.Pdb.setup(self, f, t)
        self.setInitialBreakpoints()

这部法典将规定每个休息点都通过,例如一个名人。 你还可以尝试直接建造<条码>db.Breakpoint<>/条码>,但我不敢肯定,既然<条码>b. Bdb保持了自己关于休息点的信息。

其次,你需要创建新的<代码>main。 采用<代码>pdb的模块方法。 在一定程度上,您可复制<代码>main。 缩略语 (和if name__= __main__ statement of course),但你需要以某种方式加以扩充,以掌握关于你额外休息点的信息。 我所建议的是,把左点写成从民主选举学会获得的临时档案,并将该档案的名称作为第二点:

tmpfilename = ...
# write breakpoint info
p = subprocess.Popen(args=[sys.executable,  -m ,  mypdb , tmpfilename, ...], ...)
# delete the temporary file

之后,请在<条码>上添加如下内容:

def main():
    # code excerpted from pdb.main()
    ...
    del sys.argv[0]

    # add this
    bpfilename = sys.argv[0]
    with open(bpfilename) as f:
        # read breakpoint info
        breakpoints = ...
    del sys.argv[0]
    # back to excerpt from pdb.main()

    sys.path[0] = os.path.dirname(mainpyfile)

    pdb = Pdb(breakpoints) # modified

Now you can use your new debugger module just like you would use pdb, except that you don t have to explicitly send break commands before the process starts. This has the advantage that you can directly hook the standard input and output of the Python subprocess to your console, if it allows you to do that.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签