Skip to content

Commit cd1f7bc

Browse files
committed
fix guard on setting and getting __dict__ for exact builtin objects - but allow setting and getting the dict for extensions of builtins
1 parent 7a87904 commit cd1f7bc

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/ObjectBuiltins.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,16 @@ protected PNone doIt(Object object, Object key,
479479
@Builtin(name = __DICT__, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true)
480480
@GenerateNodeFactory
481481
static abstract class DictNode extends PythonBinaryBuiltinNode {
482-
private final IsBuiltinClassProfile isBuiltinClassProfile = IsBuiltinClassProfile.create();
482+
private final IsBuiltinClassProfile exactObjInstanceProfile = IsBuiltinClassProfile.create();
483+
private final IsBuiltinClassProfile exactBuiltinInstanceProfile = IsBuiltinClassProfile.create();
483484

484485
protected boolean isExactObjectInstance(PythonObject self) {
485-
return isBuiltinClassProfile.profileObject(self, PythonBuiltinClassType.PythonObject);
486+
return exactObjInstanceProfile.profileObject(self, PythonBuiltinClassType.PythonObject);
487+
}
488+
489+
protected boolean isBuiltinObjectExact(PythonObject self) {
490+
// any builtin class except Modules
491+
return exactBuiltinInstanceProfile.profileIsOtherBuiltinObject(self, PythonBuiltinClassType.PythonModule);
486492
}
487493

488494
@SuppressWarnings("unused")
@@ -492,7 +498,7 @@ Object dict(PythonClass self, PNone none) {
492498
throw new AssertionError();
493499
}
494500

495-
@Specialization(guards = {"!isBuiltinObject(self)", "!isClass(self)", "!isExactObjectInstance(self)", "isNoValue(none)"})
501+
@Specialization(guards = {"!isBuiltinObjectExact(self)", "!isClass(self)", "!isExactObjectInstance(self)", "isNoValue(none)"})
496502
Object dict(PythonObject self, @SuppressWarnings("unused") PNone none) {
497503
PHashingCollection dict = self.getDict();
498504
if (dict == null) {
@@ -502,7 +508,7 @@ Object dict(PythonObject self, @SuppressWarnings("unused") PNone none) {
502508
return dict;
503509
}
504510

505-
@Specialization(guards = {"!isBuiltinObject(self)", "!isClass(self)", "!isExactObjectInstance(self)"})
511+
@Specialization(guards = {"!isBuiltinObjectExact(self)", "!isClass(self)", "!isExactObjectInstance(self)"})
506512
Object dict(PythonObject self, PDict dict) {
507513
self.getDictStableAssumption().invalidate();
508514
self.setDict(dict);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/object/IsBuiltinClassProfile.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ public static IsBuiltinClassProfile create() {
6565
return new IsBuiltinClassProfile();
6666
}
6767

68+
public boolean profileIsAnyBuiltinException(PException object) {
69+
return profileIsAnyBuiltinClass(object.getExceptionObject().getLazyPythonClass());
70+
}
71+
72+
public boolean profileIsAnyBuiltinObject(PythonObject object) {
73+
return profileIsAnyBuiltinClass(object.getLazyPythonClass());
74+
}
75+
76+
public boolean profileIsOtherBuiltinObject(PythonObject object, PythonBuiltinClassType type) {
77+
return profileIsOtherBuiltinClass(object.getLazyPythonClass(), type);
78+
}
79+
6880
public boolean profileException(PException object, PythonBuiltinClassType type) {
6981
return profileClass(object.getExceptionObject().getLazyPythonClass(), type);
7082
}
@@ -74,6 +86,76 @@ public boolean profileObject(PythonObject object, PythonBuiltinClassType type) {
7486

7587
}
7688

89+
public boolean profileIsAnyBuiltinClass(LazyPythonClass clazz) {
90+
if (clazz instanceof PythonBuiltinClassType) {
91+
if (!isBuiltinType) {
92+
CompilerDirectives.transferToInterpreterAndInvalidate();
93+
isBuiltinType = true;
94+
}
95+
return true;
96+
} else {
97+
if (clazz instanceof PythonBuiltinClass) {
98+
if (!isBuiltinClass) {
99+
CompilerDirectives.transferToInterpreterAndInvalidate();
100+
isBuiltinClass = true;
101+
}
102+
return true;
103+
}
104+
if (!isOtherClass) {
105+
CompilerDirectives.transferToInterpreterAndInvalidate();
106+
isOtherClass = true;
107+
}
108+
return false;
109+
}
110+
}
111+
112+
public boolean profileIsOtherBuiltinClass(LazyPythonClass clazz, PythonBuiltinClassType type) {
113+
if (clazz instanceof PythonBuiltinClassType) {
114+
if (!isBuiltinType) {
115+
CompilerDirectives.transferToInterpreterAndInvalidate();
116+
isBuiltinType = true;
117+
}
118+
if (clazz == type) {
119+
if (!match) {
120+
CompilerDirectives.transferToInterpreterAndInvalidate();
121+
match = true;
122+
}
123+
return false;
124+
} else {
125+
if (!noMatch) {
126+
CompilerDirectives.transferToInterpreterAndInvalidate();
127+
noMatch = true;
128+
}
129+
return true;
130+
}
131+
} else {
132+
if (clazz instanceof PythonBuiltinClass) {
133+
if (!isBuiltinClass) {
134+
CompilerDirectives.transferToInterpreterAndInvalidate();
135+
isBuiltinClass = true;
136+
}
137+
if (((PythonBuiltinClass) clazz).getType() == type) {
138+
if (!match) {
139+
CompilerDirectives.transferToInterpreterAndInvalidate();
140+
match = true;
141+
}
142+
return false;
143+
} else {
144+
if (!noMatch) {
145+
CompilerDirectives.transferToInterpreterAndInvalidate();
146+
noMatch = true;
147+
}
148+
return true;
149+
}
150+
}
151+
if (!isOtherClass) {
152+
CompilerDirectives.transferToInterpreterAndInvalidate();
153+
isOtherClass = true;
154+
}
155+
return false;
156+
}
157+
}
158+
77159
public boolean profileClass(LazyPythonClass clazz, PythonBuiltinClassType type) {
78160
if (clazz instanceof PythonBuiltinClassType) {
79161
if (!isBuiltinType) {

0 commit comments

Comments
 (0)