3

This small program runs well in Terminal :

mac2011% cat bug.py
print(__name__ == "__main__")
#if True:
if __name__ == "__main__":
    print("main")
else:
    print("not main")
mac2011% python bug.py
True
main
mac2011% python --version
Python 3.6.9 :: Anaconda, Inc.

In Emacs:

mac2011% Emacs --version
GNU Emacs 26.3
mac2011% Emacs -q bug.py

the file opens in (Python ElDoc) mode.

Python menu

Start interpreter (C-c C-p)

Eval buffer (C-c C-c)

Python 3.6.9 |Anaconda, Inc.| (default,     Jul 30 2019, 13:42:17) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or     "license" for more information.
>>> python.el: native completion setup loaded
>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/alba/Documents/projets/plot time series/bug.py", line 5
    else:
       ^
SyntaxError: invalid syntax
>>> 

Replacing if __name__ == "__main__": by if True:, there is no more error.

Replacing "__main__" by "__main_", there is no error.

Can you reproduce the facts?

Do you have any explanation?

How to repair?

Same with Python 3.7.6.

My emacs comes from Emacs for Mac OS X.

Same with emacs 28.0.50 that I have just built.

5
  • May reproduce it with GNU Emacs 26.3 (build 1, i686-pc-linux-gnu, GTK+ Version 3.14.5) of 2019-09-04. Please consider a bug-report. Jan 10, 2020 at 8:23
  • I have filed a bug report with GNU. Meanwhile, I have noticed this: even the first line of the script, that should print True ou False, is not executed. Indeed, the error will be the same, whatever the python file content. Jan 10, 2020 at 10:46
  • Unless the if line is commented out. Jan 10, 2020 at 10:57
  • This equivalent syntax works : print("main") if __name__ == '__main__' else print("not main") Jan 10, 2020 at 11:11
  • print("main") if __name__ == "__main__" else print("not main") does not work today with Emacs 27.1
    – mankoff
    May 13, 2021 at 14:25

1 Answer 1

6

Use C-u C-c C-c.

See python-shell-buffer-substring function docstring:

When optional argument NOMAIN is non-nil everything under an if __name__ == '__main__' block will be removed.

And python-shell-send-buffer:

When optional argument SEND-MAIN is non-nil, allow execution of code inside blocks delimited by if __name__== '__main__':. When called interactively SEND-MAIN defaults to nil, unless it’s called with prefix argument.

So the code actually sent by C-c C-c is:

print(__name__ == "__main__")
#if True:


else:
    print("not main")
6
  • 2
    Nice find, but the feature is pretty nasty IMO.
    – NickD
    Jan 10, 2020 at 15:28
  • @NickD It allows to surpass some stuff if intended. Currently, the bug seems causing the opposite. Jan 10, 2020 at 15:38
  • @NickD This behavior was added by git.savannah.gnu.org/cgit/emacs.git/commit/… with really brief comment. Maybe it should do the opposite - strip if-name-main block when invoked with prefix argument.
    – muffinmad
    Jan 10, 2020 at 15:44
  • True. Admitting that Emacs has to modify the code, for a reason that is not yet completely clear to me, at least it should do it cleanly, i. e., remove the full block if [elif...] else and maybe issue a warning. This might finally be a bug. Jan 10, 2020 at 18:51
  • Reading the Git page, I notice a loop hole in the filtering, where the alternative syntax print("main") if __name__ == '__main__' else print("not main") goes through. Jan 10, 2020 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.