syl20bnr / spacemacs

A community-driven Emacs distribution - The best editor is neither Emacs nor Vim, it's Emacs *and* Vim!
http://spacemacs.org
GNU General Public License v3.0
23.7k stars 4.9k forks source link

Python run error, while tested ok with python and ipython outside the spacemacs. #14219

Closed fratcn closed 2 years ago

fratcn commented 3 years ago

Description :octocat:

Python run error, while tested ok with python and ipython outside the spacemacs.

Reproduction guide :beetle:

The python code:

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

Observed behaviour: :eyes: :broken_heart:

-*- 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

Expected behaviour: :heart: :smile:

Enter a temprature in Celsius:34
That's 93.2 degrees Fahrenheit

System Info :computer:

Backtrace :paw_prints:

<<BACKTRACE IF RELEVANT>>
lebensterben commented 3 years ago

Not able to replicate your error.

-*- mode: compilation; default-directory: "/tmp/" -*-
Comint started at Fri Dec 18 07:45:06

/home/lucius/.pyenv/shims/python test.py
Enter a temprature in Celsius:37
That's 98.6 degrees Fahrenheit

Inferior Python finished at Fri Dec 18 07:45:12
fratcn commented 3 years ago

The trick is: The error happens when emacs start edit the code,after a while, the error may goes away and output the right answer,after a while,this python-running error comes back again. Really don't know why the spacemacs act like a unstable human. The spacemacs is installed clean from official github,and the python layer is added following the dotspacemancs instruction.

fratcn commented 3 years ago
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’.

guess something wrong with the ‘interactive Lisp function in ‘../../funcs.el’’

fratcn commented 3 years ago

Today I backup emacs.d and turn from master to delevop branch of Spacemacs,this error replicate again.

fratcn commented 3 years ago

I find out the lisp code of “python-execute-file” in ~/.emacs.d/layers/+lang/python/funcs.el:

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

Anything wrong with it?

lebensterben commented 3 years ago

Since I'm not able to replicate the error, I cannot debug this.

alexey0308 commented 3 years ago

python-execute-file creates "compilation-mode" buffer, which looks like not playing well with an interactive input. Probably python-shell-send-buffer-switch is more suitable for this case (, s B)

fratcn commented 3 years ago

python-shell-send-buffer-switch work like this:

Python 3.9.1 (default, Dec 13 2020, 11:55:53) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: Enter a temprature in Celsius:-10
Out[4]: -10

In [5]: Enter a temprature in Celsius:25
Out[8]: 25

In [9]: 

In [9]: Enter a temprature in Celsius:0
That's 32.0 degrees Fahrenheit

In [12]: Enter a temprature in Celsius:-10
Out[15]: -10

In [16]: Enter a temprature in Celsius:-10
Out[19]: -10

In [20]: Enter a temprature in Celsius:-10
Out[23]: -10

In [24]: Enter a temprature in Celsius:25
That's 77.0 degrees Fahrenheit

In [25]: Enter a temprature in Celsius:-10
Out[28]: -10

the output isn't right all the time,but is right just for twice.so this works like just like python-execute-file

alexey0308 commented 3 years ago

Does disabling company-mode in either one (inferior buffer or compilation buffer) solve the problem?

tshu-w commented 3 years ago

Maybe you could try a clean configuration with just the Python layer.

fratcn commented 3 years ago

well,it works when I clear the auto-completion layer. Thank you.

alexey0308 commented 3 years ago

@fratcn emacs sends commands to get completions meanwhile you are typing the input, hence everything breaks. But I think you should not remove the layer, it should be enough to do M-x company-mode in the inferior buffer to make sure it is disabled only for this buffer.

lebensterben commented 3 years ago

@fratcn emacs sends commands to get completions meanwhile you are typing the input, hence everything breaks. ...

@alexey0308 How's this possible? Executing the current script should be instantaneous, and how is it possible to break it by keep typing?

alexey0308 commented 3 years ago

@lebensterben the input command makes script waiting for the user input => user starts typing => completion query sent to the interpreter before user press "enter". You can see the error msg in the first post: can not convert "34\t\x08...", so additional string was sent after the user typed 34. But this is only a guess.

lebensterben commented 3 years ago

@lebensterben the input command makes script waiting for the user input => user starts typing => completion query sent to the interpreter before user press "enter". You can see the error msg in the first post: can not convert "34\t\x08...", so additional string was sent after the user typed 34. ...

@alexey0308 I also had tried to type input while the focus is not in the inferior shell, but I don't see this error. Have you replicated this?

alexey0308 commented 3 years ago

But if you send the buffer to the inferior shell, you have to type the input within the inferior shell, I am confused now, how do you type "while the focus is not in the inferior shell"?

In my case, there is no error output for inferior buffer, same as here. If I enter fast, so completion's not started yet, I get the Fahrenheit. If I enter with some pause before pressing enter, no Fahrenheit in output.

lebensterben commented 3 years ago

But if you send the buffer to the inferior shell, you have to type the input within the inferior shell, I am confused now, ...

@alexey0308 I kept typing while the focus is not in inferior shell, and therefore nothing is actually input there. Once I switch to the inferior shell, auto-completion is not in effect, as it shouldn't be.

alexey0308 commented 3 years ago

@lebensterben As I understand, with lsp-mode, completion in the file buffer comes from the lsp server, but in the inferior buffer from the inferior buffer process, hence they are independent and typing in the file buffer has no effect.

github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please let us know if this issue is still valid!