简体   繁体   English

在外壳中逐行运行Python脚本vs原子运行

[英]Run Python script line-by-line in shell vs atomically

I'm integrating MicroPython into a microcontroller and I want to add a debug step-by-step execution mode to my product (via a connection to a PC). 我正在将MicroPython集成到微控制器中,我想向产品中添加调试逐步执行模式(通过与PC的连接)。

Thankfully, MicroPython includes a REPL aka Python shell functionality: I can feed it one line at a time and execute. 值得庆幸的是,MicroPython包含了REPL或 Python Shell功能:我可以一次输入一行并执行。

I want to use this feature to single-step on the PC-side and send in the lines in the Python script one-by-one. 我想使用此功能在PC端单步执行,并一步一步地发送Python脚本中的行。

Is there ANY difference, besides possibly timing, between running a Python script one line at a time vs python my_script.py ? 除了可能的计时之外,一次运行一行Python脚本与python my_script.py之间是否有任何区别?

Passing one line of code at a time on stdin is a completely unacceptable alternative to a proper debugger. 在标准输入上一次传递一行代码对于适当的调试器来说是完全不可接受的。

Let's say that you want to debug the following: 假设您要调试以下内容:

def foo():                            # 1
    for i in range(10):               # 2
        if i == 5:                    # 3
            raise Exception("Argh!")  # 4
                                      # 5
foo()                                 # 6

...in a proper step-by-step debugger, the user could use it like so: ...在适当的逐步调试器中,用户可以像这样使用它:

break 4
run

Now, how are you going to do that? 现在,你要怎么做? If you enter the function in a REPL, the function is defined as one operation, and it runs as one operation. 如果在REPL中输入该功能,则该功能将定义为一项操作,并且将其作为一项操作运行。 It doesn't stop at line 6. It doesn't let you proceed line-by-line. 它不会在第6行停止。它不会让您逐行进行。 The same is true of the for loop: Entering the text of the for loop one line at a time doesn't let you step it before the exception is thrown. 同样是真正for循环:输入文本for一条线路在同一时间不会让你踩它抛出异常之前。

If you eliminate the function, and eliminate the loop (generating the code _something = iter(range(10)); i=_something.next() , maybe?), then you need to emulate the effects of scoping. 如果您消除了函数并消除了循环(生成代码_something = iter(range(10)); i=_something.next() ,也许?),那么您需要模拟作用域的效果。 It means you have a hugely different language than the one you're purportedly "debugging". 这意味着您使用的语言与您所谓的“调试”语言截然不同。

I don't know whether MicroPython has compile() and exec() built-in. 我不知道MicroPython是否具有内置的compile()和exec()。 But when embeded Python has them and when MCU has enough RAM, then I do the following: 但是,当嵌入式Python拥有它们并且MCU具有足够的RAM时,我将执行以下操作:

  1. Send a line to embeded shell to start a creation of variable with multiline string. 将行发送到嵌入式外壳程序以使用多行字符串开始创建变量。

    '_code = """\\' '_code =“”“ \\'

  2. Send the code I wish executed (line by line or however) 发送我希望执行的代码(逐行或任意行)

  3. Close the multiline string with """ 用“”关闭多行字符串

  4. Send exec command to run the transfered code stored in the variable on MCU and pick up the output. 发送exec命令以运行存储在MCU变量中的已传输代码并获取输出。

If your RAM is small and you cannot transfer whole code at once, you should transfer it in blocks that would be executed. 如果您的RAM很小,并且您无法一次传输全部代码,则应按将要执行的块进行传输。 Like functions, loops, etc. 像函数,循环等。

If you can compile bytecode for MicroPython on a PC, then you should be able to transfer it and prepare it for execution. 如果您可以在PC上为MicroPython编译字节码,那么您应该能够传输它并准备执行。 This would use a lot less of RAM. 这将使用更少的RAM。 But whether you can inject the raw bytecode into shell and run it depends on how much MicroPython resembles CPython. 但是是否可以将原始字节码注入shell并运行它取决于MicroPython与CPython相似的程度。

And yep, there are differences. 是的,有差异。 As explained in another answer line by line execution can be tricky. 如另一回答所述,逐行执行可能很棘手。 So blocks of code is your best bet. 因此,代码块是您最好的选择。

Is there ANY difference ... 什么不同?

Yes. 是。

The code below, for example, works in .py file, but is a SyntaxError in the interactive interpreter: 例如,下面的代码在.py文件中有效,但在交互式解释器中为SyntaxError

x = 1
if x == 1:
    pass
x = 2

There are many other differences, but this alone should be enough to scare you away from the idea. 还有许多其他差异,但是仅此一项就足以吓you您脱离想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM