mirror of
https://github.com/python/cpython.git
synced 2025-08-02 08:02:56 +00:00
Change the name of the __getattr__ special method for new-style
classes to __getattribute__, to make it crystal-clear that it doesn't have the same semantics as overriding __getattr__ on classic classes. This is a halfway checkin -- I'll proceed to add a __getattr__ hook that works the way it works in classic classes.
This commit is contained in:
parent
dbb718fa87
commit
867a8d2e26
3 changed files with 11 additions and 11 deletions
|
@ -678,9 +678,9 @@ def pymods():
|
||||||
class MM(MT):
|
class MM(MT):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
MT.__init__(self)
|
MT.__init__(self)
|
||||||
def __getattr__(self, name):
|
def __getattribute__(self, name):
|
||||||
log.append(("getattr", name))
|
log.append(("getattr", name))
|
||||||
return MT.__getattr__(self, name)
|
return MT.__getattribute__(self, name)
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
log.append(("setattr", name, value))
|
log.append(("setattr", name, value))
|
||||||
MT.__setattr__(self, name, value)
|
MT.__setattr__(self, name, value)
|
||||||
|
@ -881,8 +881,8 @@ def dynamics():
|
||||||
if name == "spam":
|
if name == "spam":
|
||||||
return "spam"
|
return "spam"
|
||||||
else:
|
else:
|
||||||
return object.__getattr__(self, name)
|
return object.__getattribute__(self, name)
|
||||||
C.__getattr__ = mygetattr
|
C.__getattribute__ = mygetattr
|
||||||
verify(a.spam == "spam")
|
verify(a.spam == "spam")
|
||||||
a.new = 12
|
a.new = 12
|
||||||
verify(a.new == 12)
|
verify(a.new == 12)
|
||||||
|
@ -1105,11 +1105,11 @@ def overloading():
|
||||||
|
|
||||||
class C(B):
|
class C(B):
|
||||||
|
|
||||||
def __getattr__(self, name):
|
def __getattribute__(self, name):
|
||||||
if name == "foo":
|
if name == "foo":
|
||||||
return ("getattr", name)
|
return ("getattr", name)
|
||||||
else:
|
else:
|
||||||
return B.__getattr__(self, name)
|
return B.__getattribute__(self, name)
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
if name == "foo":
|
if name == "foo":
|
||||||
self.setattr = (name, value)
|
self.setattr = (name, value)
|
||||||
|
|
|
@ -192,7 +192,7 @@ Instead, you can get the same information from the list type:
|
||||||
'__delitem__',
|
'__delitem__',
|
||||||
'__eq__',
|
'__eq__',
|
||||||
'__ge__',
|
'__ge__',
|
||||||
'__getattr__',
|
'__getattribute__',
|
||||||
'__getitem__',
|
'__getitem__',
|
||||||
'__getslice__',
|
'__getslice__',
|
||||||
'__gt__',
|
'__gt__',
|
||||||
|
|
|
@ -2061,8 +2061,8 @@ static struct wrapperbase tab_repr[] = {
|
||||||
};
|
};
|
||||||
|
|
||||||
static struct wrapperbase tab_getattr[] = {
|
static struct wrapperbase tab_getattr[] = {
|
||||||
{"__getattr__", (wrapperfunc)wrap_binaryfunc,
|
{"__getattribute__", (wrapperfunc)wrap_binaryfunc,
|
||||||
"x.__getattr__('name') <==> x.name"},
|
"x.__getattribute__('name') <==> x.name"},
|
||||||
{0}
|
{0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -2877,7 +2877,7 @@ slot_tp_getattro(PyObject *self, PyObject *name)
|
||||||
static PyObject *getattr_str = NULL;
|
static PyObject *getattr_str = NULL;
|
||||||
|
|
||||||
if (getattr_str == NULL) {
|
if (getattr_str == NULL) {
|
||||||
getattr_str = PyString_InternFromString("__getattr__");
|
getattr_str = PyString_InternFromString("__getattribute__");
|
||||||
if (getattr_str == NULL)
|
if (getattr_str == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
@ -3196,7 +3196,7 @@ override_slots(PyTypeObject *type, PyObject *dict)
|
||||||
TPSLOT("__hash__", tp_hash, slot_tp_hash);
|
TPSLOT("__hash__", tp_hash, slot_tp_hash);
|
||||||
TPSLOT("__call__", tp_call, slot_tp_call);
|
TPSLOT("__call__", tp_call, slot_tp_call);
|
||||||
TPSLOT("__str__", tp_str, slot_tp_str);
|
TPSLOT("__str__", tp_str, slot_tp_str);
|
||||||
TPSLOT("__getattr__", tp_getattro, slot_tp_getattro);
|
TPSLOT("__getattribute__", tp_getattro, slot_tp_getattro);
|
||||||
TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
|
TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
|
||||||
TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
|
TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
|
||||||
TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
|
TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue