Skip to content

Commit cf9bf13

Browse files
committed
[GR-28986] Fix ClassCastException caused by specialization arg eval order
PullRequest: graalpython/1566
2 parents a723fec + 9595ed7 commit cf9bf13

File tree

2 files changed

+23
-9
lines changed
  • graalpython
    • com.oracle.graal.python.test/src/tests/cpyext
    • com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str

2 files changed

+23
-9
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_object.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,24 @@ def test_str_subclass(self):
573573
assert tester.replace("o", "uff") == "helluff\nwuffrld"
574574
assert tester.replace("o", "uff", 1) == "helluff\nworld"
575575

576+
def test_doc(self):
577+
TestDoc = CPyExtType("TestDoc",
578+
'''
579+
Py_ssize_t global_basicsize = -1;
580+
581+
static PyObject* some_member(PyObject* self) {
582+
return PyLong_FromLong(42);
583+
}
584+
''',
585+
tp_methods='{"some_member", (PyCFunction)some_member, METH_NOARGS, "This is some member that returns some value."}',
586+
)
587+
obj = TestDoc()
588+
expected_doc = "This is some member that returns some value."
589+
assert obj.some_member() == 42
590+
assert len(obj.some_member.__doc__) == len(expected_doc)
591+
assert obj.some_member.__doc__ == expected_doc
592+
593+
576594
class TestObjectFunctions(CPyExtTestCase):
577595
def compile_module(self, name):
578596
type(self).mro()[1].__dict__["test_%s" % name].create_module(name)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/PString.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
4848
import com.oracle.truffle.api.CompilerDirectives;
4949
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
50-
import com.oracle.truffle.api.dsl.Bind;
5150
import com.oracle.truffle.api.dsl.Cached;
5251
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5352
import com.oracle.truffle.api.dsl.Cached.Shared;
@@ -152,15 +151,16 @@ static int nativeString(PString self, @SuppressWarnings("unused") ThreadState st
152151
"isBuiltin(self, profile) || hasBuiltinLen(self, lookupSelf, lookupString)"
153152
}, replaces = "nativeString", limit = "1")
154153
static int nativeStringMat(PString self, @SuppressWarnings("unused") ThreadState state,
155-
@Bind("getNativeCharSequence(self)") NativeCharSequence nativeCharSequence,
156154
@SuppressWarnings("unused") @Shared("builtinProfile") @Cached IsBuiltinClassProfile profile,
157155
@SuppressWarnings("unused") @Shared("lookupSelf") @Cached LookupInheritedAttributeNode.Dynamic lookupSelf,
158156
@SuppressWarnings("unused") @Shared("lookupString") @Cached LookupAttributeInMRONode.Dynamic lookupString,
159-
@CachedLibrary("nativeCharSequence.getPtr()") InteropLibrary lib,
157+
@CachedLibrary(limit = "1") InteropLibrary lib,
160158
@Cached CastToJavaIntExactNode castToJavaIntNode,
161159
@Shared("stringMaterializeNode") @Cached StringMaterializeNode materializeNode) {
162-
if (lib.hasArrayElements(nativeCharSequence.getPtr())) {
163-
return nativeCharSequence.length(lib, castToJavaIntNode);
160+
// this cast is guaranteed by cast 'isNativeString(self.getCharSequence())'
161+
NativeCharSequence value = (NativeCharSequence) self.value;
162+
if (lib.hasArrayElements(value.getPtr())) {
163+
return value.length(lib, castToJavaIntNode);
164164
} else {
165165
return materializeNode.execute(self).length();
166166
}
@@ -180,10 +180,6 @@ static int subclassedString(PString self, ThreadState state,
180180
// call the generic implementation in the superclass
181181
return self.lengthWithState(state, plib, methodLib, hasLen, ltZero, raiseNode, lib, toLong, ignoreOverflow, overflow);
182182
}
183-
184-
static NativeCharSequence getNativeCharSequence(PString self) {
185-
return (NativeCharSequence) self.value;
186-
}
187183
}
188184

189185
@ExportMessage

0 commit comments

Comments
 (0)