Skip to content

Commit 6d61714

Browse files
committed
add missing str specialization for native classes
1 parent 7cb359b commit 6d61714

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinConstructors.java

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,16 +2293,16 @@ static Object strOneArg(VirtualFrame frame, Object cls, Object obj, @SuppressWar
22932293
@Specialization(guards = {"!needsNativeAllocationNode.execute(inliningTarget, cls)", "!isNoValue(encoding) || !isNoValue(errors)"}, limit = "3")
22942294
static Object doBuffer(VirtualFrame frame, Object cls, Object obj, Object encoding, Object errors,
22952295
@Bind("this") Node inliningTarget,
2296-
@Cached("createFor(this)") IndirectCallData indirectCallData,
2296+
@Exclusive @Cached("createFor(this)") IndirectCallData indirectCallData,
22972297
@SuppressWarnings("unused") @Exclusive @Cached TypeNodes.NeedsNativeAllocationNode needsNativeAllocationNode,
22982298
@Exclusive @Cached IsBuiltinClassExactProfile isPrimitiveProfile,
22992299
@Exclusive @Cached InlinedConditionProfile isStringProfile,
23002300
@Exclusive @Cached InlinedConditionProfile isPStringProfile,
2301-
@CachedLibrary("obj") PythonBufferAcquireLibrary acquireLib,
2302-
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
2303-
@Cached("create(T_DECODE)") LookupAndCallTernaryNode callDecodeNode,
2301+
@Exclusive @CachedLibrary("obj") PythonBufferAcquireLibrary acquireLib,
2302+
@Exclusive @CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
2303+
@Exclusive @Cached("create(T_DECODE)") LookupAndCallTernaryNode callDecodeNode,
23042304
@Shared @Cached PythonObjectFactory factory,
2305-
@Cached PRaiseNode.Lazy raiseNode) {
2305+
@Exclusive @Cached PRaiseNode.Lazy raiseNode) {
23062306
Object buffer;
23072307
try {
23082308
buffer = acquireLib.acquireReadonly(obj, frame, indirectCallData);
@@ -2337,16 +2337,53 @@ static Object doBuffer(VirtualFrame frame, Object cls, Object obj, Object encodi
23372337
static Object doNativeSubclass(VirtualFrame frame, Object cls, Object obj, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors,
23382338
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
23392339
@SuppressWarnings("unused") @Exclusive @Cached TypeNodes.NeedsNativeAllocationNode needsNativeAllocationNode,
2340-
@Cached @SuppressWarnings("unused") IsSubtypeNode isSubtype,
2340+
@Shared @Cached @SuppressWarnings("unused") IsSubtypeNode isSubtype,
23412341
@Exclusive @Cached PyObjectStrAsObjectNode strNode,
2342-
@Cached CExtNodes.StringSubtypeNew subtypeNew) {
2342+
@Shared @Cached(neverDefault = true) CExtNodes.StringSubtypeNew subtypeNew) {
23432343
if (obj == PNone.NO_VALUE) {
23442344
return subtypeNew.call(cls, T_EMPTY_STRING);
23452345
} else {
23462346
return subtypeNew.call(cls, strNode.execute(frame, inliningTarget, obj));
23472347
}
23482348
}
23492349

2350+
@Specialization(guards = {"needsNativeAllocationNode.execute(inliningTarget, cls)", "isSubtypeOfString(frame, isSubtype, cls)", //
2351+
"!isNoValue(encoding) || !isNoValue(errors)"}, limit = "1")
2352+
static Object doNativeSubclassEncodeErr(VirtualFrame frame, Object cls, Object obj, @SuppressWarnings("unused") Object encoding, @SuppressWarnings("unused") Object errors,
2353+
@SuppressWarnings("unused") @Bind("this") Node inliningTarget,
2354+
@Exclusive @Cached("createFor(this)") IndirectCallData indirectCallData,
2355+
@SuppressWarnings("unused") @Exclusive @Cached TypeNodes.NeedsNativeAllocationNode needsNativeAllocationNode,
2356+
@Shared @Cached @SuppressWarnings("unused") IsSubtypeNode isSubtype,
2357+
@Exclusive @Cached IsBuiltinClassExactProfile isPrimitiveProfile,
2358+
@Exclusive @Cached InlinedConditionProfile isStringProfile,
2359+
@Exclusive @Cached InlinedConditionProfile isPStringProfile,
2360+
@Exclusive @CachedLibrary("obj") PythonBufferAcquireLibrary acquireLib,
2361+
@Exclusive @CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
2362+
@Exclusive @Cached("create(T_DECODE)") LookupAndCallTernaryNode callDecodeNode,
2363+
@Shared @Cached(neverDefault = true) CExtNodes.StringSubtypeNew subtypeNew,
2364+
@Shared @Cached PythonObjectFactory factory,
2365+
@Exclusive @Cached PRaiseNode.Lazy raiseNode) {
2366+
Object buffer;
2367+
try {
2368+
buffer = acquireLib.acquireReadonly(obj, frame, indirectCallData);
2369+
} catch (PException e) {
2370+
throw raiseNode.get(inliningTarget).raise(TypeError, ErrorMessages.NEED_BYTELIKE_OBJ, obj);
2371+
}
2372+
try {
2373+
PBytes bytesObj = factory.createBytes(bufferLib.getCopiedByteArray(buffer));
2374+
Object en = encoding == PNone.NO_VALUE ? T_UTF8 : encoding;
2375+
Object result = assertNoJavaString(callDecodeNode.execute(frame, bytesObj, en, errors));
2376+
if (isStringProfile.profile(inliningTarget, result instanceof TruffleString)) {
2377+
return subtypeNew.call(cls, asPString(cls, (TruffleString) result, inliningTarget, isPrimitiveProfile, factory));
2378+
} else if (isPStringProfile.profile(inliningTarget, result instanceof PString)) {
2379+
return subtypeNew.call(cls, result);
2380+
}
2381+
throw raiseNode.get(inliningTarget).raise(TypeError, ErrorMessages.P_S_RETURNED_NON_STRING, bytesObj, "decode", result);
2382+
} finally {
2383+
bufferLib.release(buffer, frame, indirectCallData);
2384+
}
2385+
}
2386+
23502387
protected static boolean isSubtypeOfString(VirtualFrame frame, IsSubtypeNode isSubtypeNode, Object cls) {
23512388
return isSubtypeNode.execute(frame, cls, PythonBuiltinClassType.PString);
23522389
}

0 commit comments

Comments
 (0)