[ python-Bugs-1687163 ] Inconsistent Exceptions for Readonly Attributes

SourceForge.net noreply at sourceforge.net
Sat Mar 24 00:52:37 CET 2007


Bugs item #1687163, was opened at 2007-03-23 16:51
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1687163&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.5
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Nobody/Anonymous (nobody)
Summary: Inconsistent Exceptions for Readonly Attributes

Initial Comment:
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

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

>Comment By: Raymond Hettinger (rhettinger)
Date: 2007-03-23 18:52

Message:
Logged In: YES 
user_id=80475
Originator: YES

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


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

Comment By: Raymond Hettinger (rhettinger)
Date: 2007-03-23 17:16

Message:
Logged In: YES 
user_id=80475
Originator: YES

Readonly methods throw an AttributeError, but readonly members (anything
defined in PyMemberDef with an RO or READONLY flag) raise a TypeError.

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

Comment By: Jack Diederich (jackdied)
Date: 2007-03-23 17:12

Message:
Logged In: YES 
user_id=591932
Originator: NO

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
>>> 


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

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


More information about the Python-bugs-list mailing list