Ramesh Sahoo <rameshsaho...@gmail.com> added the comment:

Hi all,

Thanks for your valuable response. I would like to inform you that I found this 
issue while playing with the following code. This is reproducible with Python 
3.8. 

Please find the following details and let me know if I am doing something 
wrong. 


[Using for loop popping elements]

import sys
stack,braces = [],{'(':')','{':'}','[':']'}
stack = [s for s in "(([])"]
c = 0
print(f"Python version: {sys.version}")
print(f"Before for loop:\nbraces.keys = {braces.keys()}\nbraces.items = 
{braces.items()}\nstack = {stack}\n\n")

for i in stack:
    c += 1
    print(f"{'='*10} loop {c} {'='*10}")
    print("just after for loop stack =",stack)
    print(f"Just after for loop i =",i)
    print(f"Just after for loop: Is i'{i}' found in braces.keys:{i in 
braces.keys()}")

    if i in braces.keys():
        print(f"inside if i = {i}")
        print("inside if stack =",stack)
        print("inside if Popped =",stack.pop(stack.index(i)))
        print(f"inside if after popped stack = {stack}\n")
        continue

    else:
        print(f"in else: Is i'{i}' found in braces.keys:{i in braces.keys()}")
        print(f"in else stack = {stack}\n")


[Program Output]

$ python3 test1.py 

Python version: 3.8.0 (default, Nov 14 2019, 22:29:45) 
[GCC 5.4.0 20160609]

Before for loop:
braces.keys = dict_keys(['(', '{', '['])
braces.items = dict_items([('(', ')'), ('{', '}'), ('[', ']')])
stack = ['(', '(', '[', ']', ')']

========== loop 1 ==========
just after for loop stack = ['(', '(', '[', ']', ')']
Just after for loop i = (
Just after for loop: Is i'(' found in braces.keys:True
inside if i = (
inside if stack = ['(', '(', '[', ']', ')']
inside if Popped = (
inside if after popped stack = ['(', '[', ']', ')']

========== loop 2 ==========
just after for loop stack = ['(', '[', ']', ')']
Just after for loop i = [
Just after for loop: Is i'[' found in braces.keys:True
inside if i = [
inside if stack = ['(', '[', ']', ')']
inside if Popped = [
inside if after popped stack = ['(', ']', ')']

========== loop 3 ==========
just after for loop stack = ['(', ']', ')']
Just after for loop i = )
Just after for loop: Is i')' found in braces.keys:False
in else: Is i')' found in braces.keys:False
in else stack = ['(', ']', ')']


[Summary]

stack = ['(', '(', '[', ']', ')']
=> [loop 1] is correct where the right element "(" has been popped.

['(', '[', ']', ')']
=> [loop 2] "(" element should have been popped but "[" chosen to be popped. 
This seems to be incorrect.

=> There must be 5 iterations(3 in if block and 2 in else block) but loop 
completed with 3 iterations(2 in if and one in else).  


I am not sure if I am doing something wrong or it is a buggy behavior. 



[Popping elements manually gives correct behavior]

import sys
print(f"Python Version: {sys.version}")

stack,braces = [],{'(':')','{':'}','[':']'}
stack = [s for s in "(([])"]

print(f"Original:\nbraces.keys = {braces.keys()}\nbraces.items = 
{braces.items()}\nstack = {stack}\n\n")

# Popping '('
print(f"Popped {stack.pop(stack.index('('))}")
print(f"stack after popping '(' = {stack}")

# Popping '('
print(f"Popped {stack.pop(stack.index('('))}")
print(f"stack after popping '(' = {stack}")

# Popping '['
print(f"Popped {stack.pop(stack.index('['))}")
print(f"stack after popping '[' = {stack}")


[Program Output]
Python Version: 3.8.0 (default, Nov 14 2019, 22:29:45) 
[GCC 5.4.0 20160609]
Original:
braces.keys = dict_keys(['(', '{', '['])
braces.items = dict_items([('(', ')'), ('{', '}'), ('[', ']')])
stack = ['(', '(', '[', ']', ')']
Popped (
stack after popping '(' = ['(', '[', ']', ')']
Popped (
stack after popping '(' = ['[', ']', ')']
Popped [
stack after popping '[' = [']', ')']

----------
status: closed -> open
versions: +Python 3.8 -Python 3.6

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41518>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to