Issue1985

classification
Title: "TypeError: readonly attribute" occurs instead of an AttributeError
Type: behaviour Severity: minor
Components: Core Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: amak Nosy List: amak, fwierzbicki, mete0r
Priority: Keywords:

Created on 2012-11-07.04:14:43 by mete0r, last changed 2013-02-26.18:37:57 by fwierzbicki.

Messages
msg7510 (view) Author: mete0r (mete0r) Date: 2012-11-07.04:14:42
Jython 2.5.3 behaves differently than CPython (2.5, 2.6, 2.7) with following code:

----

class ReadonlyDescriptor(object):
    def __get__(self, instance, owner):
        return 123

class A(object):
    __slots__ = []
    attr = ReadonlyDescriptor()

A().attr = 2  # Jython 2.5.3: TypeError: readonly attribute in Jython 2.5.3
              # CPython 2.5:  AttributeError: 'A' object attribute 'attr' is read-only
msg7650 (view) Author: Alan Kennedy (amak) Date: 2013-02-09.14:36:37
Fixing this would require breaking some tests we take from cpython.

The code reads

/////////
    public void readonlyAttributeError(String name) {
        // XXX: Should be an AttributeError but CPython throws TypeError for read only
        // member descriptors (in structmember.c::PyMember_SetOne), which is expected by a
        // few tests. fixed in py3k: http://bugs.python.org/issue1687163
        throw Py.TypeError("readonly attribute");
    }
/////

The problem is described in detail in this bug

Inconsistent Exceptions for Readonly Attributes
http://bugs.python.org/issue1687163

So we won't be able to fix this in 2.x without breaking tests.

What problems does this create for you?

Can you change your exception handling slightly to work around it? i.e.

try:
    A().attr = 2
except (AttributeError, TypeError):
    print "That was a read-only attribute"

or even

try:
    A().attr = 2
except TypeError, t:
    if t.message == "readonly attribute":
        raise AttributeError(t)
msg7803 (view) Author: Frank Wierzbicki (fwierzbicki) Date: 2013-02-26.18:37:57
TypeError vs AttributeError is one of those grey areas where implementations can differ. Alan's workaround is the right way to go.
History
Date User Action Args
2013-02-26 18:37:57fwierzbickisetstatus: open -> closed
nosy: + fwierzbicki
resolution: wont fix
messages: + msg7803
2013-02-09 14:36:37amaksetassignee: amak
messages: + msg7650
nosy: + amak
2012-11-07 04:14:43mete0rcreate