Skip to content

Commit 4026838

Browse files
committed
Don't set __weakref__ descriptor if the attribute exists
1 parent ee64b1c commit 4026838

File tree

2 files changed

+14
-6
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type

2 files changed

+14
-6
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_descr.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,10 @@ def __getattribute__(self, name):
5151

5252
obj = EvilGetattribute()
5353
assert getattr(obj, "bar") == "original"
54-
assert getattr(obj, "bar") == "bar"
54+
assert getattr(obj, "bar") == "bar"
55+
56+
57+
def test_overwrite___weakref__():
58+
class C:
59+
__weakref__ = 1
60+
assert C.__weakref__ == 1

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/TypeNodes.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,11 +2287,13 @@ private static void addDictDescrAttribute(PythonAbstractClass[] basesArray, Pyth
22872287

22882288
@TruffleBoundary
22892289
private static void addWeakrefDescrAttribute(PythonClass pythonClass, PythonObjectFactory factory) {
2290-
Builtin builtin = GetWeakRefsNode.class.getAnnotation(Builtin.class);
2291-
RootCallTarget callTarget = PythonLanguage.get(null).createCachedCallTarget(
2292-
l -> new BuiltinFunctionRootNode(l, builtin, WeakRefModuleBuiltinsFactory.GetWeakRefsNodeFactory.getInstance(), true), GetWeakRefsNode.class,
2293-
WeakRefModuleBuiltinsFactory.class);
2294-
setAttribute(T___WEAKREF__, builtin, callTarget, pythonClass, factory);
2290+
if (LookupAttributeInMRONode.lookupSlowPath(pythonClass, T___WEAKREF__) == PNone.NO_VALUE) {
2291+
Builtin builtin = GetWeakRefsNode.class.getAnnotation(Builtin.class);
2292+
RootCallTarget callTarget = PythonLanguage.get(null).createCachedCallTarget(
2293+
l -> new BuiltinFunctionRootNode(l, builtin, WeakRefModuleBuiltinsFactory.GetWeakRefsNodeFactory.getInstance(), true), GetWeakRefsNode.class,
2294+
WeakRefModuleBuiltinsFactory.class);
2295+
setAttribute(T___WEAKREF__, builtin, callTarget, pythonClass, factory);
2296+
}
22952297
}
22962298

22972299
private static void setAttribute(TruffleString name, Builtin builtin, RootCallTarget callTarget, PythonClass pythonClass, PythonObjectFactory factory) {

0 commit comments

Comments
 (0)