[pypy-dev] Nonzero

Armin Rigo arigo at tunes.org
Sun Apr 17 11:44:22 CEST 2005


Hi Carl,

(Reply to pypy-dev)

On Sun, Apr 17, 2005 at 12:27:59AM +0200, Carl Friedrich Bolz wrote:
> BTW: I think there's a problem in DescrOperation.is_true. If you have a
> 
> class Monty:
>     def __nonzero__(self):
>         return self
> 
> and you call bool(Monty()) you get an infinite recursion, since
> DescrOperation.is_true calls space.is_true on the result of the __nonzero__
> method, which in turn calls DescrOperation.is_true again. CPython raises a
> "TypeError, __nonzero__ should return an int". I didn't feel confident
> enough to fix it myself. 

Yes, there are two ways to go about it: CPython raises a TypeError basically
to avoid exactly this infinite recursion.  But on other cases it lets the
infinite recursion happen, but bound it, and you get a RuntimeError about the
recursion limit.

For PyPy, the general idea was to have a utility that looks through all loops
of the function call graph of PyPy, and inserts infinite recursion detection
calls automatically.  In this way we can safely reproduce the CPython
approach.  Then your example would give a nice RuntimeError.

Of course in this specific case we can also simply copy the TypeErroring
behavior.  What about doing so if and only if there is a test :-)


Armin



More information about the Pypy-dev mailing list