This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: crash recursive __getattr__
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: duplicate
Dependencies: Superseder: a bunch of infinite C recursions
View: 1202533
Assigned To: Nosy List: arigo, rodrigo_rc, terry.reedy
Priority: normal Keywords:

Created on 2005-08-24 10:22 by rodrigo_rc, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg26111 - (view) Author: rodrigo (rodrigo_rc) Date: 2005-08-24 10:22
The following code eats all stack space and crashes, 
both in Windows and Linux:

------------8<-------------
class C:
    def __getattr__(self, a):
        return object.__getattribute__(self, a)

c = C()
str(c)
------------8<-------------

It shoud probably raise "RuntimeError: maximum 
recursion depth exceeded" or similar instead.
msg26112 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2005-09-01 20:35
Logged In: YES 
user_id=593130

I believe this is essentially the same as open bug
[ 1202533 ] a bunch of infinite C recursions
Closing as duplicate and adding note to 1202533.  Reopen if a 
fix to the above, if and when there is one, does not fix this.
msg26113 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2005-09-01 20:37
Logged In: YES 
user_id=593130

I believe this is essentially the same as open bug
[ 1202533 ] a bunch of infinite C recursions
Closing as duplicate and adding a cross-reference note to 
1202533.  Reopen if a fix to the above, if and when there is one, 
does not fix this.
msg26114 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2005-09-02 09:40
Logged In: YES 
user_id=4771

This is indeed a bug similar to 1202533: the infinite
recursion is in C, and doesn't go through the user-defined
__getattr__().

The sample code snippet is definitely strange: it mixes
object.__getattribute__() with an old-style class, i.e. one
that doesn't inherit from 'object'.  This code would work as
expected if C were inheriting from 'object'.

Instead, what occurs in this crash is that the __str__ of
InstanceType calls __getattr__, which calls
object.__getattribute__(c, '__str__'); the latter returns a
so-called method wrapper object, which means essentially a
method object bound to a C function.  In this case the C
function is again the __str__ of InstanceType.  So this
__str__ ends up calling itself infinitely.

A more direct way to expose the bug is:

from types import *
class C:
    __str__ = InstanceType.__str__
str(C())

Clearly, all special methods are affected:

class C:
    __add__ = InstanceType.__add__
C()+1

It should be fixed together with [ 1202533 ], if the latter
is ever fixed.
History
Date User Action Args
2022-04-11 14:56:12adminsetgithub: 42301
2007-09-07 03:38:05brett.cannonsetsuperseder: a bunch of infinite C recursions
2005-08-24 10:22:04rodrigo_rccreate