[Tutor] Tutor Digest, Vol 33, Issue 48

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Sun Nov 12 03:45:28 CET 2006


>>> I should, though, rephrase the signature as:
>>>
>>>    register: object str_function -> string or None
>>>
>>> to be more readable.  Separately, I can then define that a 'str_function' is
>>> a callable that takes anything and turns it into a string.
>>
>> Ok, I made those changes.  I've also fixed a very silly bug.  (Basically,
>> it's the same exact bug that the SOAPpy folks did in traversing shallow
>> structures.)
>
> What was the bug?

If the stringifier saw two instances of the same object, it'd think that 
recursion was going on.  Unfortunately, since the numbers between 1-100 
are typically interned (as well as strings), it'd also think that if I 
visited "0" twice,that I was in a recursive loop too.

I got around it by special casing the "shallow types":

########################################################################
def make_shallow_recursive_str(recursion_label):
     shallow_types = ((bool, int, float, long, complex, types.NoneType) +
                      types.StringTypes)
     def f(obj, deep_str):
         if isinstance(obj, shallow_types):
             return deep_str(obj)
         return recursion_label
     return f
########################################################################

But this is a kludge, though, and I feel bad because the code now is very 
tricky.

The real problem is design: I really should be monitoring recursive loops 
only when I'm visiting container types.  I could rearrange the code a bit 
so that it limits recursion tests to just that.  I'll think about it a bit 
more to make sure it doesn't look too ugly.


More information about the Tutor mailing list