[ python-Bugs-942706 ] Python crash on __init__/__getattr__/__setattr__ interaction

SourceForge.net noreply at sourceforge.net
Mon Feb 20 22:54:43 CET 2006


Bugs item #942706, was opened at 2004-04-26 23:39
Message generated for change (Comment added) made by gbrandl
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=942706&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.3
>Status: Pending
Resolution: None
Priority: 5
Submitted By: has (hhas)
Assigned to: Nobody/Anonymous (nobody)
Summary: Python crash on __init__/__getattr__/__setattr__ interaction

Initial Comment:
The following code causes [Mac]Python 2.3 process to crash 
(Bad!) rather than raise an error (good) when creating a new 
instance of Foo:

class Foo:
	def __init__(self):
		self.x = 1
	
	def __getattr__(self, name):
		if self.x:
			pass # etc...
	
	def __setattr__(self, name, val):
		if self.x:
			pass # etc...


(See <http://freespace.virgin.net/hamish.sanderson/
HTMLTemplate-0.4.0.tar.gz> for a working example plus general 
solution to the referencing-instance-var-before-it's-created 
paradox that threw up this bug in the first place.)

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

>Comment By: Georg Brandl (gbrandl)
Date: 2006-02-20 21:54

Message:
Logged In: YES 
user_id=849994

OP: Any further insights?

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

Comment By: Georg Brandl (birkenfeld)
Date: 2006-01-10 21:56

Message:
Logged In: YES 
user_id=1188172

To the OP: Is there still a crash with newest Python 2.4?

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

Comment By: Walter Dörwald (doerwalter)
Date: 2004-06-02 19:17

Message:
Logged In: YES 
user_id=89016

Assigning to self.x in __init__() calls __setattr__(), which 
checks self.x, which calls __getattr__() which checks self.x, 
which leads to endless recursion. This usually leads to 
a "RuntimeError: maximum recursion depth exceeded". In what 
way does Python 2.3 crash?

To avoid the recursion access the instance dictionary directly:
class Foo:
   def __init__(self):
      self.x = 1

   def __getattr__(self, name):
      if "x" in self.__dict__ and self.__dict__["x"]:
         pass # etc...

   def __setattr__(self, name, val):
      if self.x:
         pass # etc...


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

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


More information about the Python-bugs-list mailing list