-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"if 0: return" not raising SyntaxError #46183
Comments
Can you guess why importing the attached x.py does nothing, without The real issue shown in that example is that 'return' and 'yield' (Hint about x.py: the yield sets the CO_GENERATOR flag before it gets |
See also issue bpo-1920 |
This was corrected by Benjamin with r69158. |
...which does not really solve anything, as "if 0: yield" at |
Here is a patch that properly raises SyntaxError when 'return' or I did not bother to update the (deprecated) compiler package: it seems >>> dis.dis(compiler.compile("return 1", "module.py", "exec"))
1 0 LOAD_CONST 1 (1)
3 RETURN_VALUE
4 LOAD_CONST 0 (None)
7 RETURN_VALUE |
You should remove the error logic compile.c for "return" and "yield" in The problem with this approach is that every SyntaxError generated |
msg81023 indicates that more work is needed on this. |
With the last 2.7 and 3.7, I can import the x module and the 'hello' is automatically printed. Python 2.7.15 (default, Oct 15 2018, 15:26:09)
[GCC 8.2.1 20180801 (Red Hat 8.2.1-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import x
hello
Python 3.7.3 (default, Apr 2 2019, 06:55:20)
[GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import x
hello
>>> content = open('x.py').read()
>>> import dis
>>> dis.dis(content)
1 0 LOAD_NAME 0 (print)
2 LOAD_CONST 0 ('hello')
4 CALL_FUNCTION 1
6 POP_TOP 3 8 LOAD_CONST 1 (None) but because I am not sure about this issue, I prefer to ask Serhiy. Can we close it? Thank you |
The issue is not fixed. The problem is that this still allows invalid syntax because the code is optimized away: def f():
if 0:
break
print("Hello")
f() |
The drawback of compiling the dead code is adding cells for unneeded constants and local variables. Also it can create less optimal code for jumps. |
Is unlikely that this situation arises often enough that this is a concern. We would be sacrificing correctness for speed and I think that is an error. If there is a more optimal approach I'm happy to implement it. |
The issue is not fixed yet. The compiler now rejects if 0:
return but it still accepts if 1:
pass
else:
return
while 0:
return |
See also discussion in bpo-37500. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: