* bug#39070: python interpreter within Emacs fails on simple test (syntax error)
@ 2020-01-10 11:29 Pierre ALBARÈDE
2020-02-04 0:03 ` Federico Tedin
0 siblings, 1 reply; 3+ messages in thread
From: Pierre ALBARÈDE @ 2020-01-10 11:29 UTC (permalink / raw)
To: 39070
[-- Attachment #1: Type: text/plain, Size: 1388 bytes --]
This small program
mac2011% cat bug.py
print(__name__ == "__main__")
#if True:
if __name__ == "__main__":
print("main")
else:
print("not main")
works well in Terminal :
mac2011% python bug.py
True
main
mac2011% python --version
Python 3.6.9 :: Anaconda, Inc.
but it does not work in Emacs python mode :
mac2011% Emacs -Q bug.py
(version)
"GNU Emacs 28.0.50 (build 1, x86_64-apple-darwin15.6.0, NS appkit-1404.47
Version 10.11.6 (Build 15G22010))
of 2020-01-09"
Files 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 more error.
Same with Python 3.7.6.
'print("main") if __name__ == '__main__' else print("not main")' works.
Best regards.
--
Pierre ALBARÈDE
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4044 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* bug#39070: python interpreter within Emacs fails on simple test (syntax error)
2020-01-10 11:29 bug#39070: python interpreter within Emacs fails on simple test (syntax error) Pierre ALBARÈDE
@ 2020-02-04 0:03 ` Federico Tedin
2021-01-30 6:53 ` Lars Ingebrigtsen
0 siblings, 1 reply; 3+ messages in thread
From: Federico Tedin @ 2020-02-04 0:03 UTC (permalink / raw)
To: Pierre ALBARÈDE; +Cc: 39070
> 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
^ permalink raw reply [flat|nested] 3+ messages in thread
* bug#39070: python interpreter within Emacs fails on simple test (syntax error)
2020-02-04 0:03 ` Federico Tedin
@ 2021-01-30 6:53 ` Lars Ingebrigtsen
0 siblings, 0 replies; 3+ messages in thread
From: Lars Ingebrigtsen @ 2021-01-30 6:53 UTC (permalink / raw)
To: Federico Tedin; +Cc: Pierre ALBARÈDE, 39070
Federico Tedin <federicotedin@gmail.com> writes:
> 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".
So I guess this works as designed, and I'm closing this bug report.
--
(domestic pets only, the antidote for overdose, milk.)
bloggy blog: http://lars.ingebrigtsen.no
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-01-30 6:53 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-01-10 11:29 bug#39070: python interpreter within Emacs fails on simple test (syntax error) Pierre ALBARÈDE
2020-02-04 0:03 ` Federico Tedin
2021-01-30 6:53 ` Lars Ingebrigtsen
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.