PyCQA / redbaron

Bottom-up approach to refactoring in python
http://redbaron.pycqa.org/
694 stars 74 forks source link

parent of IfelseblockNode doesn't work correctly #99

Closed b5y closed 8 years ago

b5y commented 8 years ago

I have the current test case:

import urllib, sys, string
from string import maketrans

bbb = 0

f = urllib.urlopen("http://www.pythonchallenge.com/pc/def/equality.html")
while 1:
    buf = f.read(200000)
    if not len(buf):
        break
    for x in range(len(buf)):
        if buf[x] in string.ascii_lowercase:
           if buf[x+1] in string.ascii_uppercase:
               if buf[x-1] in string.ascii_uppercase:
                   if buf[x+2] in string.ascii_uppercase:
                       if buf[x-2] in string.ascii_uppercase:
                           if buf[x+3] in string.ascii_uppercase:
                               if buf[x-3] in string.ascii_uppercase:
                                   if buf[x+4] in string.ascii_lowercase:
                                       if buf[x-4] in string.ascii_lowercase:
                                           bbb = x
    sys.stdout.write(buf)
    print(buf[bbb-3:bbb+4])

And I am trying to iterate through IfelseblockNode to the parent node to count nesting depth. I know that this node is unaffected.

When I get all IfelseblockNodes from code and jump to parent every time, something go wrong on step where if buf[x+3] in string.ascii_uppercase: is defined. I don't know why (yet), but it counts twice current parent and only after that goes higher. It happens when it starts from if buf[x-4] in string.ascii_lowercase: node.

Here how I took all nodes: if_else_node = red.find_all('IfelseblockNode')

And how I iterate through nodes to the parent node (this is piece of code - may be changed by request):

while parent:
    if isinstance(parent.parent, redbaron.RedBaron) or isinstance(parent, redbaron.RedBaron):
        break
    else:
        parent = parent.parent
        k += parent.index_on_parent_raw