emacs`pythonshell send defun`跳过缓冲区中的第一行

emacs`pythonshell send defun`跳过缓冲区中的第一行,python,emacs,Python,Emacs,在内置python模式下的C-M-x,即pythonshellsend-defun中似乎存在一个相当严重的错误 启动emacs 创建一个新文件,我们称之为test.py C-C-p到运行python 在test.pybuffer中,键入一些简单的函数,如 def f(x): return x+1 C-M-xforpythonshell发送defun 我们得到: return x+1 SyntaxError: 'return' outside function 因此,这里发生

在内置python模式下的
C-M-x
,即
pythonshellsend-defun
中似乎存在一个相当严重的错误

  • 启动emacs
  • 创建一个新文件,我们称之为
    test.py
  • C-C-p
    运行python
  • test.py
    buffer中,键入一些简单的函数,如

    def f(x):
        return x+1
    
  • C-M-x
    for
    pythonshell发送defun

  • 我们得到:

        return x+1
    SyntaxError: 'return' outside function
    
    因此,这里发生的事情是,
    pythonshell send defun
    只发送一行,而不是像我们预期的那样发送两行(例如,emacs lisp主模式中的相应命令可以正确地在光标之前发送整个多行defun)。这是怎么回事?我在Mac OS的emacs 25 gui版本和Ubuntu的emacs 24中测试了这一点。两者都表现出相同的错误

    这是一个bug,还是我误解了
    pythonshellsendefun
    的实际功能


    更新:经过进一步测试,似乎只有在第一行开始的函数才会出现此错误。如果我们在
    def(x):
    的正上方插入一个空行,
    C-M-x
    工作正常。

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

    (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)))
    

    我已经向emacs邮件列表提交了一份错误报告。