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: Python crash on __init__/__getattr__/__setattr__ interaction
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: doerwalter, georg.brandl, georg.brandl, hhas
Priority: normal Keywords:

Created on 2004-04-26 23:39 by hhas, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg20587 - (view) Author: (hhas) Date: 2004-04-26 23:39
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.)
msg20588 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2004-06-02 19:17
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...
msg20589 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-01-10 21:56
Logged In: YES 
user_id=1188172

To the OP: Is there still a crash with newest Python 2.4?
msg20590 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-02-20 21:54
Logged In: YES 
user_id=849994

OP: Any further insights?
msg20591 - (view) Author: (hhas) Date: 2006-02-20 23:48
Logged In: YES 
user_id=996627

Original test used a framework build of Python 2.3.x on Mac OS X 10.2.8; I've 
upgraded to OS 10.4.4 since and can't reproduce the problem on that - I get the 
standard recursion error as expected. I've no further insights into why I had 
problems originally and no-one else seems to have reproduced it, so probably 
best just to close it. Sorry not to be of more help.
msg20592 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-02-21 16:03
Logged In: YES 
user_id=849994

Closing then.
History
Date User Action Args
2022-04-11 14:56:03adminsetgithub: 40189
2004-04-26 23:39:19hhascreate