Maximum recursion in `_descendants` function
The inkex.elements._base.BaseElement._descendants
function is currently very recursive, this can hit pythons maximum recursion limit.
This is a piece of example code written by David Burghoff which attempts to avoid this problem¹
To fix this issue, a test case should be written with more than 1000 descendants which fails with our existing code. Then the code should be fixed. It doesn't have to use the same pattern as the example code here.
def descendants2(el):
# Like descendants(), but avoids recursion to avoid recursion depth issues
cel = el;
keepgoing = True; childrendone = False;
descendants = [];
while keepgoing:
keepgoing = False;
if not(childrendone):
descendants.append(cel);
# if isinstance(cel, (str)):
# debug(type(cel))
ks = cel.getchildren();
if len(ks)>0: # try children
cel = ks[0];
keepgoing = True; childrendone = False; continue;
if cel==el:
keepgoing = False; continue;
else:
sibs = cel.getparent().getchildren();
myi = [ii for ii in range(len(sibs)) if sibs[ii]==cel][0];
if myi!=len(sibs)-1: # try younger siblings
cel = sibs[myi+1];
keepgoing = True; childrendone = False; continue;
else:
cel = cel.getparent();
keepgoing = True; childrendone = True; continue;
descendants = [v for v in descendants if isinstance(v, (BaseElement, str))]
return descendants;