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: Inconsistent Exceptions for Readonly Attributes
Type: Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: collinwinter Nosy List: collinwinter, jackdied, rhettinger
Priority: normal Keywords:

Created on 2007-03-23 21:51 by rhettinger, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg31640 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-03-23 21:51
Attempting assignment to a readonly attribute raises an Attribute error for pure Python attributes but raises a TypeError for C readonly attributes.  I think the AttributeError is the correct exception.



>>> class A(object):
...	_x = []
...
...	@property
...	def x(self):
...		return self._x
...	
>>> a = A()
>>> a.x = None

Traceback (most recent call last):
    a.x = None
AttributeError: can't set attribute




>>> def f():
...	yield None
>>> g = f()
>>> g.gi_frame = None

Traceback (most recent call last):
    g.gi_frame = None
TypeError: readonly attribute
msg31641 - (view) Author: Jack Diederich (jackdied) * (Python committer) Date: 2007-03-23 22:12
I think this has something peculiar to frames, for instance:

Python 2.5 (r25:51908, Sep 25 2006, 17:50:07) 
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = 'asdf'
>>> f.strip = 42
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object attribute 'strip' is read-only
>>> 
msg31642 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-03-23 22:16
Readonly methods throw an AttributeError, but readonly members (anything defined in PyMemberDef with an RO or READONLY flag) raise a TypeError.
msg31643 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-03-23 23:52
Here's the one line change:

Index: Python/structmember.c
===================================================================
--- Python/structmember.c       (revision 54557)
+++ Python/structmember.c       (working copy)
@@ -160,7 +160,7 @@

        if ((l->flags & READONLY) || l->type == T_STRING)
        {
-               PyErr_SetString(PyExc_TypeError, "readonly attribute");
+               PyErr_SetString(PyExc_AttributeError, "readonly attribute");



Four of the tests will need to be updated:
    test_csv test_descr test_generators test_os
msg31644 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2007-03-26 20:18
Collin, would you please apply this to the Py3K code.
msg31645 - (view) Author: Collin Winter (collinwinter) * (Python committer) Date: 2007-03-28 21:45
Fixed in r54602.
History
Date User Action Args
2022-04-11 14:56:23adminsetgithub: 44770
2008-01-06 22:29:46adminsetkeywords: - py3k
versions: + Python 3.0
2007-03-23 21:51:18rhettingercreate