spacemacs精神不稳定?,python运行间歇性出错

一段简单的代码

def c2f(celsius):
    fahrenheit=celsius*9.0/5+32
    return fahrenheit

celsius=float(input("Enter a temprature in Celsius:"))
fahrenheit=c2f(celsius)
print("That's",fahrenheit,"degrees Fahrenheit")

在spacemacs里面按M-c-C,shell运行,输入整数比如25后返回:

ValueError: could not convert string to float: ‘25\t\x08\x08\x08\x08\x0825\t\x08\x08\x08\x08\x0825\t\x08\x08\x08\x08\x0825’

在系统终端和vim中用ipython和python验证都没有问题,在spacemacs中使用ipython或者python都是同样的错误。

*发现是间歇性抽搐,就是启动emancs后运行出错,运行2,3次后等会儿再运行,能正常得到结果。再等一段时间后再运行,又出现这个问题。spacemacs精神不稳定?

两次运行结果,正常的:

-*- mode: compilation; default-directory: "~/temp/test/" -*-
Comint started at Fri Dec 18 13:59:41

/usr/bin/ipython test2.py
Enter a temprature in Celsius:24
That's 75.2 degrees Fahrenheit

Inferior Python finished at Fri Dec 18 13:59:45

错误的:

-*- mode: compilation; default-directory: "~/temp/test/" -*-
Comint started at Fri Dec 18 11:47:43

/usr/bin/ipython test2.py
Enter a temprature in Celsius:34
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/temp/test/test2.py in <module>
      4     return fahrenheit
      5 
----> 6 celsius=float(input("Enter a temprature in Celsius:"))
      7 fahrenheit=c2f(celsius)
      8 print("That's",fahrenheit,"degrees Fahrenheit")

ValueError: could not convert string to float: '34\t\x08\x08\x08\x08\x0834\t\x08\x08\x08\x08\x0834\t\x08\x08\x08\x08\x0834'

Inferior Python exited abnormally with code 1 at Fri Dec 18 11:48:02
'34\t\x08\x08\x08\x08\x0834\t\x08\x08\x08\x08\x0834\t\x08\x08\x08\x08\x0834'

\x08 是 backspace,感觉像是什么插件,导致在获取 stdin 时,多了些不可见字符。

关键是间歇性,等一会再运行可能正常。 另装了论坛里的 Centaur Emacs测试了一下,干脆就运行超过10秒没反应,time out了。 看来这是hacker版emacs的通病啊,不知道是哪个插件的问题。

我用spacemacs python 有一段时间了,都比较正常,但没有尝试过input这个函数。

你是否可以先定位,如果不用input这种交互,python都能正常运行。那剩下的问题,是input 输入后,python-mode 可能insert 什么内容了,这个在网络上找找看这个特定问题?

另外,你说的harker版本emacs 有问题,是说vanilla emacs 正常运行么?

----------- 更新

我尝试了你的脚本,在我的emacs里面运行,可以正常拿到结果。我的运行方式是在emacs 里面开启一个ipython console,和spacemacs应该是一致的(调用这个spacemacs/python-start-or-switch-repl, 我把它也抄写到了我的配置里). 我不知道你说的 M-c-C shell 这个对应的是什么命令? 是开启一个shell ternimal 么? 在我这里也都正常。

打错漏了,是SPC m c C 在spacemacs里怎么定位这个问题?

  • 你能利用C-h k,然后 SPC m c C 来看看是调用什么函数么?
  • 你可否尝试利用M-x spacemas/python-start-or-switch-repl (应该binding to ,' in python-mode) , 看看在里面运行是否正常?
  • 你尝试过在spacemacs 里面运行其他python脚本么?(我没有理解你的运行方式,看起来像是你打开了一个terminal in spacemacs?)

SPC m c C是python-execute-file-focus,C-h k 返回信息:

SPC m c C runs the command spacemacs/python-execute-file-focus, which is an
interactive Lisp function in ‘../../funcs.el’.

It is bound to <M-return> c C, M-m m c C, and many ordinary text characters.

(spacemacs/python-execute-file-focus ARG)

Execute a python script in a shell and switch to the shell buffer in
 ‘insert state’.

对了, 我忘记问你了, 你的spacemacs 用的是master 分支还是develop分支? 一般大家都用develop分支。

没有用过这个方式来运行python,我的建议是你可以按照我之前提到的两种方式来测试下python 运行; 另外,就是你阅读下这个spacemacs/python-execute-file-focus 的源码,看看它是怎么做的,是否这个函数有问题。

你在console里怎么做,’,'快捷键调出来的命令也是c C:python-execute-file-focus vim直接F5的方式,我在emacs里还没找到

  • 你看看这个链接, 应该是利用这个调用python console: SPC m s i start inferior REPL process. 我之前用spacemacs的时候,我记得是binding 在了 , ' 上。

  • again double check if you’re using the develop branch.

  • 我一点也不知道vim的事情。按哪个键都是定义的,主要还是看运行函数,以及函数本身在执行什么。我自己没办法测试你那个调用函数,因为我现在不用spacemacs…

SPC m c C对应的~/.emacs.d/layers/+lang/python/funcs.el的lisp源码,高手帮忙鉴定下有没有问题: :handshake:

   (defun spacemacs/python-execute-file (arg)
  "Execute a python script in a shell."
  (interactive "P")
  ;; set compile command to buffer-file-name
  ;; universal argument put compile buffer in comint mode
  (let ((universal-argument t)
        (compile-command (format "%s %s"
                                 (spacemacs/pyenv-executable-find python-shell-interpreter)
                                 (shell-quote-argument (file-name-nondirectory buffer-file-name)))))
    (if arg
        (call-interactively 'compile)
      (compile compile-command t)
      (with-current-buffer (get-buffer "*compilation*")
        (inferior-python-mode)))))

(defun spacemacs/python-execute-file-focus (arg)
  "Execute a python script in a shell and switch to the shell buffer in
 `insert state'."
  (interactive "P")
  (spacemacs/python-execute-file arg)
  (switch-to-buffer-other-window "*compilation*")
  (end-of-buffer)
  (evil-insert-state))

在spacemans github上提交issue,已解决。 关闭auto-completion,运行正常。