Mailing List Archive

[issue1875] "if 0: return" not raising SyntaxError
Amaury Forgeot d'Arc <amauryfa@gmail.com> added the comment:

This was corrected by Benjamin with r69158.
Now the yield statement is still optimized away, but at least the
CO_GENERATOR flag is set only when compiling a function.

----------
nosy: +amaury.forgeotdarc, benjamin.peterson
resolution: -> fixed
status: open -> closed

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue1875>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue1875] "if 0: return" not raising SyntaxError [ In reply to ]
Armin Rigo <arigo@users.sourceforge.net> added the comment:

...which does not really solve anything, as "if 0: yield" at
module-level is now just ignored instead of raising a SyntaxError (e.g.
like "if False: yield").

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue1875>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue1875] "if 0: return" not raising SyntaxError [ In reply to ]
Amaury Forgeot d'Arc <amauryfa@gmail.com> added the comment:

Here is a patch that properly raises SyntaxError when 'return' or
'yield' statements appear outside a function.

I did not bother to update the (deprecated) compiler package: it seems
to be completely foreign to this kind of checks...

>>> 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

----------
assignee: -> amaury.forgeotdarc
keywords: +needs review, patch
resolution: fixed ->
status: closed -> open
Added file: http://bugs.python.org/file12926/return_outside_func.patch

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue1875>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com
[issue1875] "if 0: return" not raising SyntaxError [ In reply to ]
Benjamin Peterson <benjamin@python.org> added the comment:

You should remove the error logic compile.c for "return" and "yield" in
function calls.

The problem with this approach is that every SyntaxError generated
during bytecode compilation must be moved to earlier. For example, I can
still use "break" and "continue" outside loops with your patch. The only
way I can think of around this is to compile the block anyway and remove
the extra code later. Maybe this optimization could be moved to the peep
holer?

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue1875>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com