fix(shell): keep else/elif/except/finally inside multi-line REPL block - #381
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
现象
通过串口(CDC)REPL 逐行执行含 if/else 的多行脚本时,条件分支失效:if 分支执行完,else 分支也被无条件执行。
实际输出(两个分支都打印):
原因
_inner_do_obj_runChar()(1.11.8 中为_do_obj_runChar())用「下一行是否以空格/Tab 开头」来判断是否仍处于缩进块内(PikaObj.c的“go out from block”):问题在于:顶层
else: / elif是顶格无缩进的,所以被判定为“dedent、块结束”。执行顺序变成:读到
else:时,它先被追加进块缓冲,随后立刻触发结束 →obj_run()执行了if...else:(此时else后面还没有 body),于是if分支跑完打印succss;下一行
print('connect fail')已不在块状态,被当成一条独立语句执行 →else的 body 被无条件执行,打印fail。本质上 REPL 把 Python 中“同级的续行关键字”误判成了“块结束”。
修复
新增一个判定函数:缩进行、以及顶格的块续行关键字(
else / elif / except / finally)都算作“仍在当前块内”,其它顶格语句才结束块。这样整段if/for/else会一次性交给obj_run()编译执行。