I've encountered the error RuntimeError: maximum recursion depth exceeded in cmp when working with lists. More precisely, p0 in points, the points.index(p0) method call as well as the points.remove(p0) method call on the points list have been raising the error for a specific dictionary p0 at a specific index of my points list. The pointslist contains 4700 dictionaries at the time of the error, reduced one dictionary by one from a list of 12000 objects until the error is raised. The p0 dictionary contains a reference to another dictionary in the list, which in returns contains a reference to the p0 object. The p0 dictionary as well as the dictionary it contains a reference to appear twice in the points list before the error is raised by any of the three method call.
Where does this error come from?
EDIT: Here's the code that raises the error.
for roadType in roadTypes:
    points = roadPoints[roadType][:]
    while len(roadTails[roadType]) > 0:
        p0 = roadTails[roadType].pop()
        p1 = p0['next']
        points.remove(p0) # Where the error occurs
        points.remove(p1)
        while True:
            p2 = find(p1, points, 0.01)
            if p2:
                points.remove(p2)
                p3 = p2['next']
                points.remove(p3)
                if p3 in roadTails[roadType]:
                    roadTails[roadType].remove(p3)
                    break
                else:
                    p0, p1 = p2, p3
                    continue
            else: break
Here's the definition of find, where dist calculates the distance between two points.
def find(p1, points, tolerance = 0.01):
    for p2 in points:
        if dist(p2['coords'], p1['coords']) <= tolerance:
            return p2
    return False
Here's the full traceback of the error:
Traceback (most recent call last):
  File "app.py", line 314, in <module>
    points.remove(p0) # Where the error occurs
RuntimeError: maximum recursion depth exceeded in cmp
                Probably you have a circular structure where one of your dicts refers to itself through a chain of 'next's, like this:
>>> a = {}
>>> b = {}
>>> a['next'] = b
>>> b['next'] = a
>>> a == b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: maximum recursion depth exceeded in cmp
If you print out the dict, a circular reference would show up as ...:
>>> a
{'next': {'next': {...}}}
Maybe this can help to find the problematic part of the dict.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With