Skip to content

Commit

Permalink
Send elpy setup code after first prompt is received
Browse files Browse the repository at this point in the history
This prevent the shell from hanging in some situations
  • Loading branch information
galaunay committed Nov 10, 2019
1 parent ee0f5c9 commit f679a8a
Show file tree
Hide file tree
Showing 39 changed files with 134 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ install:
- virtualenv ~/.virtualenvs/elpy-test-venv
script:
- nosetests
- PYTHONPATH="`pwd`" cask exec ert-runner
- PYTHONPATH="`pwd`" cask exec ert-runner --reporter ert+duration
after_success:
- coverage run -m nose.__main__
- coveralls
12 changes: 9 additions & 3 deletions elpy-shell.el
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,17 @@ Python process. This allows the process to start up."
(error "Wrong value for `elpy-shell-starting-directory', please check this variable documentation and set it to a proper value")))))
(run-python (python-shell-parse-command) nil t))
(when sit (sit-for sit))
(when (elpy-project-root)
(python-shell-send-string
(format "import sys;sys.path.append('%s')" (elpy-project-root))))
(get-buffer-process bufname))))

(defun elpy-shell--send-setup-code ()
"Send setup code for the shell."
(let ((process (python-shell-get-process)))
(when (elpy-project-root)
(python-shell-send-string-no-output
(format "import sys;sys.path.append('%s');del sys"
(elpy-project-root))
process))))

(defun elpy-shell-toggle-dedicated-shell (&optional arg)
"Toggle the use of a dedicated python shell for the current buffer.
Expand Down
22 changes: 21 additions & 1 deletion elpy.el
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ This option need to bet set through `customize' or `customize-set-variable' to b
(add-hook 'pyvenv-post-activate-hooks 'elpy-rpc--disconnect)
(add-hook 'pyvenv-post-deactivate-hooks 'elpy-rpc--disconnect)
(add-hook 'inferior-python-mode-hook 'elpy-shell--enable-output-filter)
(add-hook 'python-shell-first-prompt-hook 'elpy-shell--send-setup-code t)
;; Enable Elpy-mode in the opened python buffer
(dolist (buffer (buffer-list))
(and (not (string-match "^ ?\\*" (buffer-name buffer)))
Expand All @@ -529,9 +530,13 @@ This option need to bet set through `customize' or `customize-set-variable' to b
(defun elpy-disable ()
"Disable Elpy in all future Python buffers."
(interactive)
(elpy-modules-global-stop)
(define-key inferior-python-mode-map (kbd "C-c C-z") nil)
(remove-hook 'python-mode-hook 'elpy-mode)
(remove-hook 'pyvenv-post-activate-hooks 'elpy-rpc--disconnect)
(remove-hook 'pyvenv-post-deactivate-hooks 'elpy-rpc--disconnect)
(remove-hook 'inferior-python-mode-hook 'elpy-shell--enable-output-filter)
(elpy-modules-global-stop)
(remove-hook 'python-shell-first-prompt-hook 'elpy-shell--send-setup-code)
(setq elpy-enabled-p nil))

;;;###autoload
Expand Down Expand Up @@ -3501,6 +3506,21 @@ which we're looking."
(defun python-info-encoding ()
'utf-8))

;; first-prompt-hook has been added in emacs 25.
;; for earlier versions, make sure Elpy's setup code is
;; still send to the python shell.
(unless (boundp 'python-shell-first-prompt-hook)
(add-hook 'inferior-python-mode-hook
(lambda ()
(when (elpy-project-root)
(let ((process (get-buffer-process (current-buffer))))
(python-shell-send-string
(format "import sys;sys.path.append('%s');del sys"
(elpy-project-root))
process))))))



;; Added in Emacs 25
(unless (fboundp 'python-shell-comint-end-of-output-p)
(defun python-shell-comint-end-of-output-p (output)
Expand Down
15 changes: 5 additions & 10 deletions test/elpy-company-backend-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@

(ert-deftest elpy-company-backend-should-find-no-prefix-in-string ()
(elpy-testcase ()
(elpy-modules-run 'global-init)
(elpy-enable)
(python-mode)
(elpy-mode)
(insert "# hello")
(should-not (elpy-company-backend 'prefix))))

(ert-deftest elpy-company-backend-should-find-simple-prefix-string ()
(elpy-testcase ()
(elpy-modules-run 'global-init)
(elpy-enable)
(python-mode)
(elpy-mode)
(insert "hello")
(should (equal (elpy-company-backend 'prefix)
"hello"))))

(ert-deftest elpy-company-backend-should-find-full-prefix-string ()
(elpy-testcase ()
(elpy-modules-run 'global-init)
(elpy-enable)
(python-mode)
(elpy-mode)
(insert "hello.world")
(should (equal (elpy-company-backend 'prefix)
'("world" . t)))))
Expand Down Expand Up @@ -120,9 +117,8 @@

(ert-deftest elpy-company-backend-should-add-shell-candidates ()
(elpy-testcase ()
(elpy-modules-run 'global-init)
(elpy-enable)
(python-mode)
(elpy-mode)
(let ((elpy-get-info-from-shell t)
(elpy-get-info-from-shell-timeout 10))
(insert "variable_script = 3\n")
Expand Down Expand Up @@ -154,9 +150,8 @@

(ert-deftest elpy-company-backend-should-not-add-shell-candidates ()
(elpy-testcase ()
(elpy-modules-run 'global-init)
(elpy-enable)
(python-mode)
(elpy-mode)
(insert "variable_script = 3\n")
(insert "def function_script(a): print(a)\n")
(elpy-shell-get-or-create-process)
Expand Down
2 changes: 1 addition & 1 deletion test/elpy-config-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
(ert-deftest elpy-config-should-show-flake8-pip-button-when-no-syntax-checker-available ()
(elpy-testcase ()
(mletf* ((elpy-syntax-check-command "/foo/bar/flake8"))
(elpy-enable)
(python-mode)
(elpy-mode)
(elpy-config)
(let ((output (with-current-buffer "*Elpy Config*"
(elpy/wait-for-output "Options")
Expand Down
4 changes: 2 additions & 2 deletions test/elpy-doc-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

(ert-deftest elpy-doc ()
(elpy-testcase ()
(elpy-enable)
(python-mode)
(elpy-mode)
(insert "sys")
(elpy-doc)
(with-current-buffer "*Python Doc*"
(should (re-search-forward "This module provides access")))))

(ert-deftest elpy-doc-should-find-documentation-from-inside-arguments ()
(elpy-testcase ()
(elpy-enable)
(python-mode)
(elpy-mode)
(insert "import socket\n"
"socket.getaddrinfo(socket.gethostname(")
(save-excursion
Expand Down
2 changes: 1 addition & 1 deletion test/elpy-keymap-test.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ert-deftest elpy-shell-prefix-key-should-be-changed-when-customized ()
(elpy-testcase ()
(elpy-enable)
(python-mode)
(elpy-mode)
(customize-set-variable 'elpy-shell-command-prefix-key "C-c C-a")
(should (equal elpy-shell-command-prefix-key "C-c C-a"))
(should (equal (key-binding (kbd "C-c C-a")) 'elpy-shell-map))
Expand Down
5 changes: 3 additions & 2 deletions test/elpy-module-autodoc-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def fun2():
(elpy-testcase ()
(let ((elpy-autodoc-delay 0.01))
(add-to-list 'elpy-modules 'elpy-module-autodoc)
(elpy-enable)
(python-mode)
(elpy-mode)
(elpy-doc-test-insert-functions)
(insert "fun1")
(elpy-doc)
Expand All @@ -32,6 +32,7 @@ def fun2():
(ert-deftest elpy-doc-should-not-be-updated-if-doc-not-visible ()
(elpy-testcase ()
(let ((elpy-autodoc-delay 0.01))
(elpy-enable)
(add-to-list 'elpy-modules 'elpy-module-autodoc)
(python-mode)
(elpy-doc-test-insert-functions)
Expand All @@ -49,9 +50,9 @@ def fun2():
(ert-deftest elpy-doc-should-not-be-updated-if-deactivated ()
(elpy-testcase ()
(let ((elpy-autodoc-delay 0.01))
(elpy-enable)
(add-to-list 'elpy-modules 'elpy-module-autodoc)
(python-mode)
(elpy-mode)
(setq elpy-autodoc-delay nil)
(elpy-doc-test-insert-functions)
(insert "fun1")
Expand Down
4 changes: 1 addition & 3 deletions test/elpy-module-company-test.el
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
(ert-deftest elpy-module-company ()
(elpy-testcase ()
(elpy-module-company 'global-init)
(elpy-enable)
(python-mode)
(elpy-mode)
(elpy-module-company 'buffer-init)

(should company-mode)
(should (< (abs (- company-idle-delay 0.1))
Expand Down
4 changes: 1 addition & 3 deletions test/elpy-module-sane-defaults-test.el
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
(ert-deftest elpy-module-sane-defaults-buffer-init ()
(elpy-testcase ()
(elpy-enable)
(python-mode)
(elpy-mode)

(elpy-module-sane-defaults 'buffer-init)

(should (eq forward-sexp-function nil))
(should (= comment-inline-offset 2))))
6 changes: 2 additions & 4 deletions test/elpy-module-yasnippet-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@

(ert-deftest elpy-module-yasnippet-buffer-init ()
(elpy-testcase ()
(elpy-enable)
(python-mode)
(elpy-mode)

(elpy-module-yasnippet 'buffer-init)

(should yas-minor-mode)))

(ert-deftest elpy-module-yasnippet-buffer-stop ()
(elpy-testcase ()
(elpy-enable)
(python-mode)
(elpy-mode)
(yas-minor-mode 1)

(elpy-module-yasnippet 'buffer-stop)
Expand Down
2 changes: 1 addition & 1 deletion test/elpy-pdb-clear-breakpoints-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/B\n"
Expand Down
4 changes: 2 additions & 2 deletions test/elpy-pdb-debug-buffer-and-break-at-point-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand All @@ -22,8 +22,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand Down
6 changes: 3 additions & 3 deletions test/elpy-pdb-debug-buffer-from-beginning-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand All @@ -20,8 +20,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand All @@ -41,8 +41,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand Down
8 changes: 4 additions & 4 deletions test/elpy-pdb-debug-buffer-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand All @@ -20,8 +20,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand All @@ -41,8 +41,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand Down Expand Up @@ -70,8 +70,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/b\n"
Expand Down
4 changes: 2 additions & 2 deletions test/elpy-pdb-debug-last-exception-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/B\n"
Expand All @@ -24,8 +24,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/B\n"
Expand Down
4 changes: 2 additions & 2 deletions test/elpy-pdb-toggle-breakpoint-at-point-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/B\n"
Expand All @@ -24,8 +24,8 @@
(elpy-testcase ((:project project-root "test.py")
(:emacs-required "25.0"))
(find-file (f-join project-root "test.py"))
(elpy-enable)
(python-mode)
(elpy-mode 1)
(insert "def foo(a):\n"
" b = 1 + 2\n"
" c = a/B\n"
Expand Down
Loading

0 comments on commit f679a8a

Please sign in to comment.