Skip to content

while else loop seems to behave incorrectly #46215

Closed

Description

@mark-summerfield
BPO 1920
Nosy @amauryfa, @mdickinson, @mark-summerfield
Files
  • while.patch
  • 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:

    assignee = None
    closed_at = <Date 2008-01-25.02:27:52.023>
    created_at = <Date 2008-01-24.13:22:47.346>
    labels = ['interpreter-core', 'type-bug']
    title = 'while else loop seems to behave incorrectly'
    updated_at = <Date 2008-01-25.03:59:32.836>
    user = 'https://github.com/mark-summerfield'

    bugs.python.org fields:

    activity = <Date 2008-01-25.03:59:32.836>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2008-01-25.02:27:52.023>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2008-01-24.13:22:47.346>
    creator = 'mark'
    dependencies = []
    files = ['9279']
    hgrepos = []
    issue_num = 1920
    keywords = []
    message_count = 6.0
    messages = ['61629', '61632', '61634', '61639', '61667', '61670']
    nosy_count = 3.0
    nosy_names = ['amaury.forgeotdarc', 'mark.dickinson', 'mark']
    pr_nums = []
    priority = 'critical'
    resolution = 'accepted'
    stage = None
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue1920'
    versions = ['Python 2.6', 'Python 2.5', 'Python 3.0']

    Activity

    1. mark-summerfield commented on Jan 24, 2008

      mark-summerfieldmannequin
      MannequinAuthor

      I am using:

      Python 3.0a2 (r30a2:59382, Dec 17 2007, 08:47:22)
      [GCC 4.1.2 20070626 (Red Hat 4.1.2-13)] on linux2
      IDLE 3.0a1

      This seems wrong:

      >>> while False:
      	print("no")
      else:
      	print("yes")

      >>

      I expected it to print "yes" because the docs say that the else suite is
      executed if present and if the loop terminated normally (no break), and
      this is the case here.

      This works though:

      >>> x = False
      >>> while x:
      	print("no")
      else:
      	print("yes")

      yes

      >>

      So it seems that "while False" and "while variable" are giving different
      behaviour.

    2. amauryfa commented on Jan 24, 2008

      @amauryfa
      Contributor

      python 2.5 has the same behaviour, if you use "while 0:" instead.
      In compiler.c, there is code that optimizes away blocks like "if 0",
      "while 0". 'if' correctly emit the else clause, 'while' does not...

    3. mdickinson commented on Jan 24, 2008

      @mdickinson
      Member

      This bug is also present in the trunk, with while 0 instead of while
      False.

      This appears closely related to issue bpo-1875.

      In my opinion this is a serious bug in the core language. I'm not sure
      whether it's serious enough to be considered a showstopper for 2.5.2.
      In any case, I'm raising the priority to get more eyes on this before
      2.5.2.

    4. amauryfa commented on Jan 24, 2008

      @amauryfa
      Contributor

      Here is a patch, made against py3k; it should apply cleanly to the trunk.

      On the 2.5 branch, compile.c seems identical, but test_grammar.py looks
      very different; the conversion should be easy.

      Can someone review and apply it? I don't have svn access at the moment.

    5. mdickinson commented on Jan 25, 2008

      @mdickinson
      Member

      Looks like Amaury found his svn access. Is it okay to close this now?

    6. mdickinson commented on Jan 25, 2008

      @mdickinson
      Member

      Forgot to say: Amaury checked his fix in in revision bpo-60265 (trunk) and
      revision bpo-60268 (Python 2.5).

      Thanks for the bug report, Mark!

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions