Skip to content

Commit 9a82f61

Browse files
Copilotyouknowone
andauthored
Fix subclass __getattribute__ being dropped after del Base.__getattr__ (#9800)
* Initial plan * Apply remaining changes Co-authored-by: youknowone <[email protected]> * Resolve TpGetattro from both __getattribute__ and __getattr__ update_slot for TpGetattro now looks up __getattribute__ and __getattr__ through the MRO on both add and remove, and picks the slot from the pair the same way TpSetattro does for __setattr__/__delattr__. Assisted-by: Claude:claude-opus-5-5 --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: youknowone <[email protected]> Co-authored-by: Jeong, YunWon <[email protected]>
1 parent 354f673 commit 9a82f61

2 files changed

Lines changed: 46 additions & 35 deletions

File tree

‎crates/vm/src/types/slot.rs‎

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,44 +1228,40 @@ impl PyType {
12281228
}
12291229
SlotAccessor::TpDel => update_main_slot!(del, del_wrapper, Del),
12301230
SlotAccessor::TpGetattro => {
1231-
// __getattribute__ and __getattr__ both map to TpGetattro.
1232-
// If __getattr__ is defined anywhere in MRO, we must use the wrapper
1233-
// because the native slot won't call __getattr__.
1234-
let __getattr__ = identifier!(ctx, __getattr__);
1235-
let has_getattr = {
1236-
// mro[0] is self, so skip it
1237-
self.attributes.contains(__getattr__)
1238-
|| self
1239-
.mro
1240-
.read()
1241-
.iter()
1242-
.skip(1)
1243-
.any(|cls| cls.attributes.contains(__getattr__))
1231+
// __getattribute__ and __getattr__ share the same slot, so both
1232+
// names must be resolved together: a Python-level definition of
1233+
// either one forces the dispatching wrapper, and the native
1234+
// slot is only usable when every resolved name agrees on it.
1235+
// __getattr__ has no native wrapper, so finding it anywhere in
1236+
// the MRO always selects the wrapper.
1237+
// This resolution reads the current attribute dicts, so it
1238+
// applies the same whether a name was just added or removed.
1239+
let extract = |sf: &SlotFunc| match sf {
1240+
SlotFunc::GetAttro(f) => Some(*f),
1241+
_ => None,
12441242
};
1245-
1246-
if has_getattr {
1247-
// Must use wrapper to handle __getattr__
1248-
self.slots.getattro.store(Some(getattro_wrapper));
1249-
} else if ADD {
1250-
match self.lookup_slot_in_mro(name, ctx, |sf| {
1251-
if let SlotFunc::GetAttro(f) = sf {
1252-
Some(*f)
1243+
let getattribute =
1244+
self.lookup_slot_in_mro(identifier!(ctx, __getattribute__), ctx, extract);
1245+
let getattr = self.lookup_slot_in_mro(identifier!(ctx, __getattr__), ctx, extract);
1246+
use SlotLookupResult::{NativeSlot, NotFound, PythonMethod};
1247+
match (getattribute, getattr) {
1248+
(PythonMethod, _) | (_, PythonMethod) => {
1249+
self.slots.getattro.store(Some(getattro_wrapper));
1250+
}
1251+
(NativeSlot(getattribute), NativeSlot(getattr)) => {
1252+
let func = if fn_addr(getattribute) == fn_addr(getattr) {
1253+
getattribute
12531254
} else {
1254-
None
1255-
}
1256-
}) {
1257-
SlotLookupResult::NativeSlot(func) => {
1258-
self.slots.getattro.store(Some(func));
1259-
}
1260-
SlotLookupResult::PythonMethod => {
1261-
self.slots.getattro.store(Some(getattro_wrapper));
1262-
}
1263-
SlotLookupResult::NotFound => {
1264-
accessor.inherit_from_mro(self);
1265-
}
1255+
getattro_wrapper
1256+
};
1257+
self.slots.getattro.store(Some(func));
1258+
}
1259+
(NativeSlot(func), NotFound) | (NotFound, NativeSlot(func)) => {
1260+
self.slots.getattro.store(Some(func));
1261+
}
1262+
(NotFound, NotFound) => {
1263+
accessor.inherit_from_mro(self);
12661264
}
1267-
} else {
1268-
accessor.inherit_from_mro(self);
12691265
}
12701266
}
12711267
SlotAccessor::TpSetattro => {

‎extra_tests/snippets/builtin_object.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,18 @@ class MissingSlot:
236236

237237

238238
assert MissingSlot().__getstate__() == (None, {"y": 2})
239+
240+
241+
# deleting __getattr__ from a base must not drop a subclass's __getattribute__
242+
class _GABase:
243+
pass
244+
245+
246+
class _GAProxy(_GABase):
247+
def __getattribute__(self, name):
248+
return "hooked"
249+
250+
251+
_GABase.__getattr__ = lambda self, name: "fallback"
252+
del _GABase.__getattr__
253+
assert _GAProxy().x == "hooked"

0 commit comments

Comments
 (0)