GNU bug report logs -
#39070
python interpreter within Emacs fails on simple test (syntax error)
Previous Next
Reported by: Pierre ALBARÈDE <pa.com <at> free.fr>
Date: Fri, 10 Jan 2020 15:34:01 UTC
Severity: normal
Tags: notabug
Done: Lars Ingebrigtsen <larsi <at> gnus.org>
Bug is archived. No further changes may be made.
Full log
View this message in rfc822 format
> 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
This bug report was last modified 4 years and 109 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.