Skip to content

Commit

Permalink
Explicitely call the __dunder__ methods instead of builtins (ie,
Browse files Browse the repository at this point in the history
obj.__repr__() instead of repr(obj).

This is ugly, but for some reason the cleaner way triggers
MaxRecursionErrors on some big trees, which we do *not* get when calling
the method directly... (And it's not an InfiniteRecursion bug, we just
hit python's limit when traversing the tree, and get no trouble if we
raise this limit by fooling around with sys.setrecursionlimit(n))

This occurs on the __repr__, __eq__ and __str__ methods.

Note that this doesn't prevent errors if we *do* hit the maximum recursion depth,
but at least we get some usable margin.

Hopefully this fix is temporary, or at least we'll understand why it
works soon. The bug might still be caused by something else, but i don't
get why this switch would seem to fix it ?
  • Loading branch information
raphigaziano committed Apr 14, 2016
1 parent 43227c5 commit a25f779
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions luqum/tree.py
Expand Up @@ -20,7 +20,14 @@ def children(self):
return []

def __repr__(self):
children = ", ".join(repr(c) for c in self.children)
# /!\
# Calling c.__repr__ rather than repr(c) to avoid recursion trouble...
# Using the builtin seems to trigger the max recursion sooner, can't
# figure out why...
# We have the same problem with other __methods__, like __eq__ below or
# __str__.
# /!\
children = ", ".join(c.__repr__() for c in self.children)
return "%s(%s)" % (self.__class__.__name__, children)

def __eq__(self, other):
Expand All @@ -29,7 +36,7 @@ def __eq__(self, other):
return (self.__class__ == other.__class__ and
all(getattr(self, a, _MARKER) == getattr(other, a, _MARKER)
for a in self._equality_attrs) and
all(c == d for c, d in zip(self.children, other.children)))
all(c.__eq__(d) for c, d in zip(self.children, other.children)))


class SearchField(Item):
Expand All @@ -44,7 +51,7 @@ def __init__(self, name, expr):
self.expr = expr

def __str__(self):
return str(self.name) + ":" + str(self.expr)
return self.name + ":" + self.expr.__str__()

@property
def children(self):
Expand All @@ -58,7 +65,7 @@ def __init__(self, expr):
self.expr = expr

def __str__(self):
return "(%s)" % str(self.expr)
return "(%s)" % self.expr.__str__()

@property
def children(self):
Expand Down Expand Up @@ -99,8 +106,8 @@ def children(self):
def __str__(self):
return "%s%s TO %s%s" % (
self.LOW_CHAR[self.include_low],
self.low,
self.high,
self.low.__str__(),
self.high.__str__(),
self.HIGH_CHAR[self.include_high])


Expand All @@ -124,7 +131,7 @@ def __str__(self):
return self.value

def __repr__(self):
return "%s(%s)" % (self.__class__.__name__, str(self))
return "%s(%s)" % (self.__class__.__name__, self.value)


class Word(Term):
Expand Down Expand Up @@ -185,7 +192,7 @@ def children(self):
return [self.expr]

def __str__(self):
return "%s^%s" % (self.expr, self.force)
return "%s^%s" % (self.expr.__str__(), self.force)


class Operation(Item):
Expand All @@ -197,7 +204,7 @@ def __init__(self, a, b):
self.b = b

def __str__(self):
return "%s %s %s" % (self.a, self.op, self.b)
return "%s %s %s" % (self.a.__str__(), self.op, self.b.__str__())

@property
def children(self):
Expand Down

0 comments on commit a25f779

Please sign in to comment.