gpt4 book ai didi

python - emacs `python-shell-send-defun` 跳过缓冲区中的第一行

转载 作者:行者123 更新时间:2023-11-28 19:03:53 25 4
gpt4 key购买 nike

C-M-x 中似乎有一个非常重要的错误,也就是内置 python 模式中的 python-shell-send-defun

  1. 启动emacs
  2. 创建一个新文件,我们称之为test.py
  3. C-c C-prun-python
  4. test.py 缓冲区中,键入一些简单的函数,例如

    def f(x):
    return x+1
  5. C-M-x 用于 python-shell-send-defun

我们得到:

    return x+1
SyntaxError: 'return' outside function

所以这里发生的是 python-shell-send-defun 只发送一行,而不是我们期望的两行(例如 emacs lisp 主要模式中的相应命令有效正确地在光标之前发送整个多行 defun)。这里发生了什么?我在 Mac OS 的 emacs 25 gui 版本和 Ubuntu 的 emacs 24 中测试了这个。两者都表现出相同的错误。

这是一个错误,还是我误解了 python-shell-send-defun 的实际作用?

更新:通过一些进一步的测试,这个错误似乎只发生在从第一行开始的函数中。如果我们在 def f(x): 的正上方插入一个空行,C-M-x 将完美运行。

最佳答案

是的,这看起来像一个错误,应该作为一个错误报告。该函数可能应该被重写以进行如下检查

(defun python-shell-send-defun (&optional arg msg)
"Send the current defun to inferior Python process.
When argument ARG is non-nil do not include decorators. When
optional argument MSG is non-nil, forces display of a
user-friendly message if there's no process running; defaults to
t when called interactively."
(interactive (list current-prefix-arg t))
(save-excursion
(python-shell-send-region
(progn
(end-of-line 1)
(while (and (or (python-nav-beginning-of-defun)
(beginning-of-line 1))
(> (current-indentation) 0)))
(when (not arg)
(while (and (forward-line -1)
(looking-at (python-rx decorator))))
;; add a check here to see if we are on the first line
(and (not (bobp)) (forward-line 1)))
(point-marker))
(progn
(or (python-nav-end-of-defun)
(end-of-line 1))
(point-marker))
nil ;; noop
msg)))

关于python - emacs `python-shell-send-defun` 跳过缓冲区中的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49286811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com