msg60213 - (view) |
Author: Armin Rigo (arigo) *  |
Date: 2008-01-19 18:55 |
Can you guess why importing the attached x.py does nothing, without
printing "hello" at all?
The real issue shown in that example is that 'return' and 'yield'
outside a function are ignored instead of giving a SyntaxError if they
are optimized away by 'if 0:'.
(Hint about x.py: the yield sets the CO_GENERATOR flag before it gets
optimized away. So clearly, that's a way to comment out a whole module
-- just put "if 0: yield" anywhere at top-level...)
|
msg61638 - (view) |
Author: Mark Dickinson (mark.dickinson) *  |
Date: 2008-01-24 16:24 |
See also issue #1920
|
msg80955 - (view) |
Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) *  |
Date: 2009-02-02 17:00 |
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.
|
msg80961 - (view) |
Author: Armin Rigo (arigo) *  |
Date: 2009-02-02 17:11 |
...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").
|
msg81019 - (view) |
Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) *  |
Date: 2009-02-02 23:48 |
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
|
msg81023 - (view) |
Author: Benjamin Peterson (benjamin.peterson) *  |
Date: 2009-02-03 01:56 |
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?
|
msg116917 - (view) |
Author: Mark Lawrence (BreamoreBoy) * |
Date: 2010-09-20 07:55 |
msg81023 indicates that more work is needed on this.
|
msg342464 - (view) |
Author: Stéphane Wirtel (matrixise) *  |
Date: 2019-05-14 14:22 |
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)
10 RETURN_VALUE
but because I am not sure about this issue, I prefer to ask Serhiy.
Can we close it?
Thank you
|
msg342525 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2019-05-14 23:27 |
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()
|
msg342555 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2019-05-15 09:02 |
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.
|
msg342559 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2019-05-15 10:00 |
>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.
|
msg342705 - (view) |
Author: Pablo Galindo Salgado (pablogsal) *  |
Date: 2019-05-17 10:37 |
New changeset af8646c8054d0f4180a2013383039b6a472f9698 by Pablo Galindo in branch 'master':
bpo-1875: Raise SyntaxError in invalid blocks that will be optimised away (GH-13332)
https://github.com/python/cpython/commit/af8646c8054d0f4180a2013383039b6a472f9698
|
msg342706 - (view) |
Author: miss-islington (miss-islington) |
Date: 2019-05-17 10:59 |
New changeset 85ed1712e428f93408f56fc684816f9a85b0ebc0 by Miss Islington (bot) in branch '3.7':
bpo-1875: Raise SyntaxError in invalid blocks that will be optimised away (GH-13332)
https://github.com/python/cpython/commit/85ed1712e428f93408f56fc684816f9a85b0ebc0
|
msg345684 - (view) |
Author: Serhiy Storchaka (serhiy.storchaka) *  |
Date: 2019-06-15 15:19 |
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
|
msg347350 - (view) |
Author: Ned Deily (ned.deily) *  |
Date: 2019-07-05 14:22 |
See also discussion in Issue37500.
|
|
Date |
User |
Action |
Args |
2022-04-11 14:56:29 | admin | set | github: 46183 |
2019-12-02 22:41:47 | pablogsal | set | status: open -> closed resolution: fixed stage: patch review -> resolved |
2019-07-05 14:22:15 | ned.deily | set | nosy:
+ ned.deily messages:
+ msg347350
|
2019-06-15 16:09:49 | serhiy.storchaka | set | stage: patch review pull_requests:
+ pull_request13965 |
2019-06-15 15:19:58 | serhiy.storchaka | set | status: closed -> open resolution: fixed -> (no value) messages:
+ msg345684
stage: resolved -> (no value) |
2019-05-17 11:22:44 | pablogsal | set | status: open -> closed resolution: fixed stage: patch review -> resolved |
2019-05-17 10:59:53 | miss-islington | set | nosy:
+ miss-islington messages:
+ msg342706
|
2019-05-17 10:37:55 | miss-islington | set | pull_requests:
+ pull_request13293 |
2019-05-17 10:37:13 | pablogsal | set | messages:
+ msg342705 |
2019-05-15 10:00:46 | pablogsal | set | messages:
+ msg342559 |
2019-05-15 09:20:22 | arigo | set | nosy:
- arigo
|
2019-05-15 09:02:02 | serhiy.storchaka | set | messages:
+ msg342555 |
2019-05-15 00:27:25 | pablogsal | set | stage: needs patch -> patch review pull_requests:
+ pull_request13244 |
2019-05-14 23:27:14 | pablogsal | set | nosy:
+ pablogsal messages:
+ msg342525
|
2019-05-14 14:22:36 | matrixise | set | nosy:
+ serhiy.storchaka, matrixise
messages:
+ msg342464 versions:
+ Python 3.7, Python 3.8, - Python 2.7, Python 3.3, Python 3.4 |
2014-02-03 19:35:44 | Arfrever | set | nosy:
+ Arfrever
|
2014-02-03 18:34:04 | BreamoreBoy | set | nosy:
- BreamoreBoy
|
2013-04-07 15:24:44 | terry.reedy | set | versions:
+ Python 3.3, Python 3.4, - Python 3.1, Python 3.2 |
2010-09-20 07:55:39 | BreamoreBoy | set | versions:
+ Python 3.1, Python 2.7, Python 3.2, - Python 2.6 nosy:
+ BreamoreBoy
messages:
+ msg116917
type: behavior stage: needs patch |
2009-08-23 21:42:05 | mastrodomenico | set | nosy:
+ mastrodomenico
|
2009-02-03 01:56:05 | benjamin.peterson | set | messages:
+ msg81023 |
2009-02-02 23:48:25 | amaury.forgeotdarc | set | status: closed -> open files:
+ return_outside_func.patch messages:
+ msg81019 assignee: amaury.forgeotdarc keywords:
+ needs review, patch resolution: fixed -> (no value) |
2009-02-02 17:11:58 | arigo | set | messages:
+ msg80961 |
2009-02-02 17:00:52 | amaury.forgeotdarc | set | status: open -> closed nosy:
+ amaury.forgeotdarc, benjamin.peterson resolution: fixed messages:
+ msg80955 |
2008-01-24 16:24:25 | mark.dickinson | set | nosy:
+ mark.dickinson messages:
+ msg61638 |
2008-01-19 18:59:32 | fijal | set | nosy:
+ fijal, Carl.Friedrich.Bolz |
2008-01-19 18:55:49 | arigo | create | |