Movatterモバイル変換


[0]ホーム

URL:


homepage

Issue1875

This issue trackerhas been migrated toGitHub, and is currentlyread-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title:"if 0: return" not raising SyntaxError
Type:behaviorStage:resolved
Components:Interpreter CoreVersions:Python 3.8, Python 3.7
process
Status:closedResolution:fixed
Dependencies:Superseder:
Assigned To: amaury.forgeotdarcNosy List: Arfrever, Carl.Friedrich.Bolz, amaury.forgeotdarc, benjamin.peterson, fijal, mark.dickinson, mastrodomenico, matrixise, miss-islington, ned.deily, pablogsal, serhiy.storchaka
Priority:lowKeywords:needs review, patch

Created on2008-01-19 18:55 byarigo, last changed2022-04-11 14:56 byadmin. This issue is nowclosed.

Files
File nameUploadedDescriptionEdit
x.pyarigo,2008-01-19 18:55
return_outside_func.patchamaury.forgeotdarc,2009-02-02 23:48
Pull Requests
URLStatusLinkedEdit
PR 13332mergedpablogsal,2019-05-15 00:27
PR 13382mergedmiss-islington,2019-05-17 10:37
PR 14116closedserhiy.storchaka,2019-06-15 16:09
Messages (15)
msg60213 -(view)Author: Armin Rigo (arigo)*(Python committer)Date: 2008-01-19 18:55
Can you guess why importing the attached x.py does nothing, withoutprinting "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 theyare optimized away by 'if 0:'.  (Hint about x.py: the yield sets the CO_GENERATOR flag before it getsoptimized 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)*(Python committer)Date: 2008-01-24 16:24
See also issue#1920
msg80955 -(view)Author: Amaury Forgeot d'Arc (amaury.forgeotdarc)*(Python committer)Date: 2009-02-02 17:00
This was corrected by Benjamin withr69158.Now the yield statement is still optimized away, but at least theCO_GENERATOR flag is set only when compiling a function.
msg80961 -(view)Author: Armin Rigo (arigo)*(Python committer)Date: 2009-02-02 17:11
...which does not really solve anything, as "if 0: yield" atmodule-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)*(Python committer)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)*(Python committer)Date: 2009-02-03 01:56
You should remove the error logic compile.c for "return" and "yield" infunction calls.The problem with this approach is that every SyntaxError generatedduring bytecode compilation must be moved to earlier. For example, I canstill use "break" and "continue" outside loops with your patch. The onlyway I can think of around this is to compile the block anyway and removethe extra code later. Maybe this optimization could be moved to the peepholer?
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)*(Python committer)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 linux2Type "help", "copyright", "credits" or "license" for more information.>>> import xhelloPython 3.7.3 (default, Apr  2 2019, 06:55:20) [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linuxType "help", "copyright", "credits" or "license" for more information.>>> import xhello>>> 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_VALUEbut 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)*(Python committer)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)*(Python committer)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)*(Python committer)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)*(Python committer)Date: 2019-05-17 10:37
New changesetaf8646c8054d0f4180a2013383039b6a472f9698 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 changeset85ed1712e428f93408f56fc684816f9a85b0ebc0 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)*(Python committer)Date: 2019-06-15 15:19
The issue is not fixed yet. The compiler now rejectsif 0:    returnbut it still acceptsif 1:    passelse:    returnwhile 0:    return
msg347350 -(view)Author: Ned Deily (ned.deily)*(Python committer)Date: 2019-07-05 14:22
See also discussion inIssue37500.
History
DateUserActionArgs
2022-04-11 14:56:29adminsetgithub: 46183
2019-12-02 22:41:47pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-07-05 14:22:15ned.deilysetnosy: +ned.deily
messages: +msg347350
2019-06-15 16:09:49serhiy.storchakasetstage: patch review
pull_requests: +pull_request13965
2019-06-15 15:19:58serhiy.storchakasetstatus: closed -> open
resolution: fixed -> (no value)
messages: +msg345684

stage: resolved -> (no value)
2019-05-17 11:22:44pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-17 10:59:53miss-islingtonsetnosy: +miss-islington
messages: +msg342706
2019-05-17 10:37:55miss-islingtonsetpull_requests: +pull_request13293
2019-05-17 10:37:13pablogsalsetmessages: +msg342705
2019-05-15 10:00:46pablogsalsetmessages: +msg342559
2019-05-15 09:20:22arigosetnosy: -arigo
2019-05-15 09:02:02serhiy.storchakasetmessages: +msg342555
2019-05-15 00:27:25pablogsalsetstage: needs patch -> patch review
pull_requests: +pull_request13244
2019-05-14 23:27:14pablogsalsetnosy: +pablogsal
messages: +msg342525
2019-05-14 14:22:36matrixisesetnosy: +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:44Arfreversetnosy: +Arfrever
2014-02-03 18:34:04BreamoreBoysetnosy: -BreamoreBoy
2013-04-07 15:24:44terry.reedysetversions: + Python 3.3, Python 3.4, - Python 3.1, Python 3.2
2010-09-20 07:55:39BreamoreBoysetversions: + 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:05mastrodomenicosetnosy: +mastrodomenico
2009-02-03 01:56:05benjamin.petersonsetmessages: +msg81023
2009-02-02 23:48:25amaury.forgeotdarcsetstatus: 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:58arigosetmessages: +msg80961
2009-02-02 17:00:52amaury.forgeotdarcsetstatus: open -> closed
nosy: +amaury.forgeotdarc,benjamin.peterson
resolution: fixed
messages: +msg80955
2008-01-24 16:24:25mark.dickinsonsetnosy: +mark.dickinson
messages: +msg61638
2008-01-19 18:59:32fijalsetnosy: +fijal,Carl.Friedrich.Bolz
2008-01-19 18:55:49arigocreate
Supported byThe Python Software Foundation,
Powered byRoundup
Copyright © 1990-2022,Python Software Foundation
Legal Statements

[8]ページ先頭

©2009-2026 Movatter.jp