classification
Title: codeop possible flow error
Type: behavior Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Wilberto, r.david.murray, serhiy.storchaka
Priority: normal Keywords:

Created on 2013-09-06 18:59 by Wilberto, last changed 2013-09-06 22:14 by serhiy.storchaka. This issue is now closed.

Messages (4)
msg197091 - (view) Author: Wilberto Morales (Wilberto) Date: 2013-09-06 18:59
I think the way this loop in /Lib/codeop.py is wrong

def _maybe_compile(compiler, source, filename, symbol):
    # Check for source consisting of only blank lines and comments
    for line in source.split("\n"):
        line = line.strip()
        if line and line[0] != '#':
            break               # Leave it alone
    else:
        if symbol != "eval":
            source = "pass"     # Replace it with a 'pass' statement

I think the else statement shouldn't be there.
msg197099 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-09-06 19:21
Why do you think that?  Can you provide a test case that fails?
msg197102 - (view) Author: Wilberto Morales (Wilberto) Date: 2013-09-06 20:13
I can't provide a example but reading the source comments it seems wrong.

'First, check if the source consists entirely of blank lines and
comments; if so, replace it with 'pass', because the built-in
parser doesn't always do the right thing for these.'

So the way I understand this is.

pure_comments_or_blank = True

for line in source:
    line = line.strip()
    if line and line[0] != '#':
        pure_comments_or_blank = False
        break

if pure_comments_or_blank:
    if symbol != "eval":
        source = "pass"

So check that atleast one line is actual code and not comments or blank. If none is then replace it with a 'pass'

Instead the loop seems a little weird. 

for line in source.split("\n"):
    line = line.strip()
    if line and line[0] != '#':
        break               # Leave it alone
else:
    if symbol != "eval":
        source = "pass"   

If it finds a something that is not a comment in a line it breaks. But then right after the for loop it contains an else statement. I'm not even sure when this else statement is executed. I'm sorry if I'm misinterpreting this.
msg197115 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-06 22:14
> I'm not even sure when this else statement is executed.

It executed when no breaks were happen. I.e. both variants of code are equivalent besides the fact that variant with "else" doesn't require named boolean variable. It is a feature of Python syntax.
History
Date User Action Args
2013-09-06 22:14:46serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg197115

resolution: not a bug
stage: resolved
2013-09-06 20:13:09Wilbertosetmessages: + msg197102
2013-09-06 19:21:21r.david.murraysetnosy: + r.david.murray
messages: + msg197099
2013-09-06 19:06:11Wilbertosettitle: codeop possible flow erro -> codeop possible flow error
2013-09-06 18:59:45Wilbertocreate