the ^C^R (eval region) command may generate a lot of blank "In [X]:" from to the interpreter.
Here is what I have been able to understand about the issue. What you are seeing is a side effect of how code is evaluated in comint-mode
(this is a general mode on top of which python-mode's repl is written).
Basically it sends the string you selected to the inferior process as it is (including the newlines etc), then it receives the output from the inferior process and prints it in the emacs comint buffer (the repl you opened in emacs). The python code
spam = "spam "
spam = spam + spam
spam = spam + spam
do not produce any output that is why you see the blank "In [X]:" lines.
This is all fine. But then I highlight the final line print spam and then type ^C^R. The output:
After doing C-cC-r, if you switch to the python repl and press enter you will actually get the output printed. The issue I guess is that the python-mode is NOT adding the final newline
(which triggers the evaluation) to the string sent to command interpreter. I was able to trace this issue to the function python-shell-send-string
in python.el
(do C-h fpython-shell-send-string
RET, you will see a link to python.el
in the help buffer, click on it and emacs will take you to definition of function). Towards the end of the function, we have these lines
(when (or (not (string-match "\n$" string))
(string-match "\n[ \t].*\n?$" string))
(comint-send-string process "\n"))
It seems this checks whether the string just sent had a final newline
, if not it explicitly sends a newline
, this check fails for single line strings and python mode never inserts the last newline. This seems to be a bug, maybe this is fixed in emacs trunk will need to check.
UPDATE
I can confirm that this has been fixed in the emacs trunk. In fact both the issues you have reported have been fixed.
UPDATE
The python.el's github repository, which is now deprecated, has the instructions for getting the latest version of the library.
1) Download the latest version from this url
2) Add the directory, where you have downloaded python.el
, to your load path by adding something like this to your init file
(add-to-list 'load-path "<name_of_the_directory>")
3) Finally require python-mode
(require 'python)