Subject: Re: bug#39070: python interpreter within Emacs fails on simple test (syntax error)

From: Federico Tedin <[email-masked]>
Date: Tue, 04 Feb 2020 01:03:49 +0100

> Replacing 'if __name__ == "__main__":' by 'if True:', there is no more error.
>
> Replacing "__main__" by "__main_", there is no more error.
>
> Same with Python 3.7.6.
>
> 'print("main") if __name__ == '__main__' else  print("not main")' works.
>
> Best regards.

Hello Pierre,

This behavior is due to `python-shell-send-buffer's SEND-MAIN
argument:

"When optional argument SEND-MAIN is non-nil, allow execution of
code inside blocks delimited by "if __name__== '__main__'" "

So when this argument is nil (the default), the whole block under "if
__name__== '__main__'" " will be removed (along the line containing the
"if" itself).
Your example breaks because you added an "else:" to your "if", so when
Emacs removes that part, a syntax error is raised.

Try the following test file:

print("1")

if __name__ == "__main__":
    print("2")

print("3")

Using C-c C-c on it should yield "1 3", but using C-u C-c C-c
(i.e. SEND-MAIN set to t) should yield "1 2 3".

Hope that helps.
- Fede