[issue24590] Customized attribute access causes infinite loop

Zorceta report at bugs.python.org
Wed Jul 8 11:07:22 CEST 2015


New submission from Zorceta:

Code and result:

```
>>> class A:
	def __init__(self):
		self.data = {'a': 1, 'b': 2, 'c': 3}
	def __getattr__(self, name):
		print('in __getattr__, getting %s' % name)
		if name in self.data:
			return self.data[name]
		else:
			raise AttributeError
	def __setattr__(self, name, value):
		print('in __setattr__, setting %s to %s' % (name, value))
		if name in self.data:
			self.data[name] = value
		else:
			self.__class__.__setattr__(self, name, value)

			
>>> a = A()
in __setattr__, setting data to {'a': 1, 'b': 2, 'c': 3}
in __getattr__, getting data
in __getattr__, getting data
in __getattr__, getting data
......
```

>From above you can see it stuck on

>>> if name in self.data:

in `__setattr__`.

But, the result indicates that `__setattr__` uses `__getattr__` to read the `data` attribute, which is against docs:

"Note that if the attribute is found through the normal mechanism, __getattr__() is not called."

If it acts as described, `data` should be read "through the normal mechanism", not through `__getattr__`.

----------
messages: 246453
nosy: zorceta
priority: normal
severity: normal
status: open
title: Customized attribute access causes infinite loop
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue24590>
_______________________________________


More information about the Python-bugs-list mailing list