From dc4f818b02a02ec6f93977885e2ff75f8210c14b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fabi=C3=A1n=20Ezequiel=20Gallina?= Date: Mon, 24 Sep 2012 20:15:40 -0300 Subject: [PATCH] * progmodes/python.el: (python-shell-send-buffer): Better handling of "if __name__ == '__main__':" conditionals when sending the buffer. --- lisp/ChangeLog | 6 ++++++ lisp/progmodes/python.el | 19 +++++++++---------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 270d548b71..5539ee41b2 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,9 @@ +2012-09-24 Fabián Ezequiel Gallina + + * progmodes/python.el: + (python-shell-send-buffer): Better handling of "if __name__ == + '__main__':" conditionals when sending the buffer. + 2012-09-24 Glenn Morris * eshell/esh-cmd.el (eshell-find-alias-function): diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index cc835ca79a..e99e6bda4b 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1919,19 +1919,18 @@ Returns the output. See `python-shell-send-string-no-output'." (defun python-shell-send-buffer (&optional arg) "Send the entire buffer to inferior Python process. - -With prefix ARG include lines surrounded by \"if __name__ == '__main__':\"" +With prefix ARG allow execution of code inside blocks delimited +by \"if __name__== '__main__':\"" (interactive "P") (save-restriction (widen) - (python-shell-send-region - (point-min) - (or (and - (not arg) - (save-excursion - (re-search-forward (python-rx if-name-main) nil t)) - (match-beginning 0)) - (point-max))))) + (let ((str (buffer-substring (point-min) (point-max)))) + (and + (not arg) + (setq str (replace-regexp-in-string + (python-rx if-name-main) + "if __name__ == '__main__ ':" str))) + (python-shell-send-string str)))) (defun python-shell-send-defun (arg) "Send the current defun to inferior Python process. -- 2.20.1