Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_mro.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,46 @@ class Z():
else:
assert False


def test_class_assignment_keeps_new_lookup():
class Meta(type):
pass

class Base:
def __new__(cls, *args):
return object.__new__(cls)

# __class__ assignment looks up __new__ in the native and built-in bases only;
# that must not change what normal lookups of __new__ find
class C(Base, metaclass=Meta):
pass

def make(cls, *args):
return cls(*args)

def get_new(cls):
return cls.__new__

obj = Base()
obj.__class__ = C
assert type(obj) is C
for i in range(3):
assert type.__getattribute__(C, "__new__") is Base.__new__
assert get_new(C) is Base.__new__
assert get_new(Base) is Base.__new__
assert type(make(Base, "arg")) is Base
assert type(make(C, "arg")) is C

# and a normal lookup of __new__ before the assignment must not make it fail
class D(Base, metaclass=Meta):
pass

assert type.__getattribute__(D, "__new__") is Base.__new__
obj = Base()
obj.__class__ = D
assert type(obj) is D


def test_mro_change_on_attr_access():
eq_called = []
class MyKey(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public static LookupAttributeInMRONode.Dynamic getUncached() {
}
}

private final boolean skipNonStaticBases;
final boolean skipNonStaticBases;

/**
* The suffix lookup used by super. Its result depends on the starting type as well as the
Expand Down Expand Up @@ -376,7 +376,9 @@ static MroSequenceStorage.FinalAttributeAssumptionPair findAttrAndAssumptionInMR
return assumptionPair;
}

@Specialization(guards = {"isSingleContext()", "isSameTypeNode.execute(inliningTarget, cachedKlass, klass)", "cachedAttrInMROInfo.getAssumption() != null"}, //
// The final attribute assumptions stored in the MRO are shared by all lookups of the key, so
// they must not hold the result of a lookup that skips the non-static bases
@Specialization(guards = {"isSingleContext()", "!skipNonStaticBases", "isSameTypeNode.execute(inliningTarget, cachedKlass, klass)", "cachedAttrInMROInfo.getAssumption() != null"}, //
limit = "getAttributeAccessInlineCacheMaxDepth()", //
assumptions = "cachedAttrInMROInfo.getAssumption()")
static Object lookupConstantMROCached(Object klass,
Expand Down