[Python-bugs-list] [ python-Bugs-667147 ] Segmentation fault printing str subclass

SourceForge.net noreply@sourceforge.net
Mon, 13 Jan 2003 09:03:19 -0800


Bugs item #667147, was opened at 2003-01-13 08:25
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=667147&group_id=5470

Category: Type/class unification
Group: Python 2.2
Status: Open
Resolution: None
Priority: 7
Submitted By: James Henderson (henderj)
>Assigned to: Neal Norwitz (nnorwitz)
Summary: Segmentation fault printing str subclass

Initial Comment:
If I define a subclass of the built-in type str as
follows:

class Letter(str):
    def __new__(cls, letter):
        if letter == 'EPS':
            return str.__new__(cls)
        return str.__new__(cls, letter)
    def __str__(self):
        if not self:
            return 'EPS'
        return self

then the following produces a segmentation fault:

>>> w = Letter('w')
>>> print w

I realize that the last line of the class definition
should be:

return str.__str__(self)

but what I tried should still not cause a crash.  I am
running Red Hat Linux 7.1 and I have got the same
behaviour on Python 2.2, Python 2.2.2 and Python 2.3a0.

James

----------------------------------------------------------------------

>Comment By: Guido van Rossum (gvanrossum)
Date: 2003-01-13 12:03

Message:
Logged In: YES 
user_id=6380

If anything, 10 is too deep already. There should be at most
one extra recursive invocation; anything else is too much.
But I'll accept that in extreme cases there may be more than
one delegation going on, so 10 is fine.

Please go ahead and check it in and do the needful.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-01-13 11:52

Message:
Logged In: YES 
user_id=33168

The patch works for me.  Do you think 10 is a good nesting
level?  100?  I can add the test from this bug report and
doc NEWS.  Is there anything else that needs to be done?  Do
you want to check in the patch or do you want me to?

----------------------------------------------------------------------

Comment By: Guido van Rossum (gvanrossum)
Date: 2003-01-13 11:11

Message:
Logged In: YES 
user_id=6380

How about a simple internal helper that tracks print
recursion? See attached patch.


----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-01-13 10:22

Message:
Logged In: YES 
user_id=33168

This problem is caused in object.c::PyObject_Print(). 
PyObject_Print() recurses infinitely calling itself, with
str(obj).  Since str(obj) == obj, we never complete.  Even
if this condition is fixed there's nothing preventing 2
classes __str__()s to call each other and create infinite
mutual recursion.  The flags could keep a count of the
recurses into PyObject_Print() and refuse to go deeper than
64 or some number, thus limiting the number of flags which
can be used.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-01-13 08:51

Message:
Logged In: YES 
user_id=33168

Ouch!  This affects both 2.2.2 and 2.3.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=667147&group_id=5470