]> git.eshelyaron.com Git - emacs.git/commitdiff
Don't send "if __name__ == '__main__':" to interpreter
authorDan Davison <dandavison7@gmail.com>
Thu, 17 May 2012 03:03:39 +0000 (00:03 -0300)
committerFabián Ezequiel Gallina <fgallina@gnu.org>
Thu, 17 May 2012 03:03:39 +0000 (00:03 -0300)
lisp/progmodes/python.el

index 59802cca6239e80601a64382561ce694ba43d49a..16a3befc3f29ea269889875369b463d4c0761c68 100644 (file)
      `(decorator            . ,(rx line-start (* space) ?@ (any letter ?_)
                                    (* (any word ?_))))
      `(defun                . ,(rx symbol-start (or "def" "class") symbol-end))
+     `(if-name-main         . ,(rx line-start "if" (+ space) "__name__"
+                                  (+ space) "==" (+ space)
+                                  (any ?' ?\") "__main__" (any ?' ?\")
+                                  (* space) ?:))
      `(symbol-name          . ,(rx (any letter ?_) (* (any word ?_))))
      `(open-paren           . ,(rx (or "{" "[" "(")))
      `(close-paren          . ,(rx (or "}" "]" ")")))
@@ -1593,12 +1597,21 @@ Returns the output.  See `python-shell-send-string-no-output'."
   (interactive "r")
   (python-shell-send-string (buffer-substring start end) nil t))
 
-(defun python-shell-send-buffer ()
-  "Send the entire buffer to inferior Python process."
-  (interactive)
+(defun python-shell-send-buffer (&optional arg)
+  "Send the entire buffer to inferior Python process.
+
+With prefix arg include lines protected by \"if __name__ == '__main__':\""
+  (interactive "P")
   (save-restriction
     (widen)
-    (python-shell-send-region (point-min) (point-max))))
+    (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)))))
 
 (defun python-shell-send-defun (arg)
   "Send the current defun to inferior Python process.