jorgenschaefer / elpy

Emacs Python Development Environment
GNU General Public License v3.0
1.9k stars 262 forks source link

Cannot interrupt process in inferior python if the process is started from an Elpy buffer #1966

Open novusshu opened 2 years ago

novusshu commented 2 years ago

Summary

This issue is driving me crazy. Simply put, if I start the interactive python with run-python, I can use C-c C-c which binds to comint-interrupt-subjob to interrupt current job (e.g., an infinite loop), and the process is still alive. However, I simply cannot use C-c C-c in an inferior python started from elpy mode python script (e.g., with C-return, or C-c C-z, etc. ). Basically I cannot find a way to interrupt the process without kill the buffer.

I compared an inferior buffer started with run-python and with say, C-return, only to find that they have the exactly same minor modes and major mode (using C-h m). I did realize that Elpy has a slightly different way to start the inferior python based on the documentation for elpy-shell-get-or-create-process, in which it states that

;; We cannot use run-python directly, as it selects the new shell ;; buffer. See https://github.com/jorgenschaefer/elpy/issues/1848

Yet, by comparing the code, I still cannot figure out why C-c C-c works in one way but not another. Someone help!!!

Steps to reproduce

  1. Open any .py file, write a simple infinite loop, e.g.,
    while(1):
    print(1)

    make sure your Elpy mode is enabled for the python buffer

  2. Press Ctrl-: an inferior buffer should start.
  3. Switch to the inferior python buffer, press C-c C-c, and you will find yourself not able to interrupt the infinite loop.

Now, if you change Step 2 to 2: 2. M-x run-python, Then in step 3, C-c C-c will do the job to terminate the loop.

My configuration

OS

MacOS 12.2.1

Result of (elpy-config)

Emacs.............: 28.0.91
Elpy..............: 1.35.0
Virtualenv........: None
Interactive Python: python3 3.9.10 (/opt/homebrew/bin/python3)
RPC virtualenv....: rpc-venv (/Users/XXXX/.emacs.d/elpy/rpc-venv)
 Python...........: python3 3.8.9 (/Users/XXXX/.emacs.d/elpy/rpc-venv/bin/python3)
 Jedi.............: 0.18.1
 Autopep8.........: 1.6.0
 Yapf.............: 0.32.0
 Black............: 22.1.0
Syntax checker....: flake8 (/opt/homebrew/bin/flake8)

Elpy configuration in my init.el

I don't think Elpy configuration matters, because I can reproduce the same problem with no Elpy setup.

  (setq python-shell-interpreter "python3"
    python-shell-interpreter-args "-i"
    python-shell-prompt-detect-failure-warning nil)
  (add-to-list 'python-shell-completion-native-disabled-interpreters
           "python")

    (use-package elpy
      :ensure t
      :defer t
      :init
      (advice-add 'python-mode :before 'elpy-enable)
      :config
      (setq 
       elpy-rpc-virtualenv-path 'default
       elpy-rpc-python-command "python3" 
       ;; elpy-shell-starting-directory 'current-directory
       )
      ;; enable code folding
      (add-hook 'elpy-mode-hook 
'hs-minor-mode)
      ;; use flycheck instead of flymake
      (when (load "flycheck" t t)
    (setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
    (add-hook 'elpy-mode-hook 'flycheck-mode))
      (setq flycheck-python-pycompile-executable "python3")

      ;; ensure auto-complete is disabled for python
      (with-eval-after-load 'auto-complete
    (setq ac-modes (delq 'python-mode ac-modes)))
      )