Skip to content

Commit bed71f9

Browse files
committed
Use 'polyglot_as_string' for 'PyTruffle_StringToCstr'.
1 parent e8e2359 commit bed71f9

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

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

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,27 +147,18 @@ Object run(Object object,
147147
@Builtin(name = "to_char_pointer", fixedNumOfArguments = 1)
148148
@GenerateNodeFactory
149149
abstract static class TruffleString_AsString extends NativeBuiltin {
150-
@Child private CExtNodes.AsCharPointer asCharPointerNode;
151150

152151
@Specialization
153-
Object run(PString str) {
154-
return run(str.getValue());
155-
}
156-
157-
@Specialization
158-
Object run(String str) {
159-
if (asCharPointerNode == null) {
160-
CompilerDirectives.transferToInterpreterAndInvalidate();
161-
asCharPointerNode = insert(CExtNodes.AsCharPointer.create());
162-
}
163-
return asCharPointerNode.execute(str);
152+
Object run(PythonObjectNativeWrapper str,
153+
@Cached("create()") CExtNodes.AsCharPointer asCharPointerNode,
154+
@Cached("create()") CExtNodes.ToJavaNode toJavaNode) {
155+
return asCharPointerNode.execute(toJavaNode.execute(str));
164156
}
165157

166158
@Fallback
167159
Object run(Object o) {
168160
return raiseNative(PNone.NO_VALUE, PythonErrorType.SystemError, "Cannot convert object of type %p to C string.", o);
169161
}
170-
171162
}
172163

173164
/**

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/CExtNodes.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonNativeWrapper;
5050
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.PythonObjectNativeWrapper;
5151
import com.oracle.graal.python.builtins.objects.cext.NativeWrappers.TruffleObjectNativeWrapper;
52+
import com.oracle.graal.python.builtins.objects.str.PString;
5253
import com.oracle.graal.python.builtins.objects.type.PythonClass;
5354
import com.oracle.graal.python.nodes.PBaseNode;
5455
import com.oracle.graal.python.nodes.PGuards;
@@ -287,11 +288,17 @@ public abstract static class AsCharPointer extends PBaseNode {
287288

288289
public abstract Object execute(Object obj);
289290

291+
@Specialization
292+
Object doPString(PString str,
293+
@Cached("createExecute(1)") Node executeNode) {
294+
return doString(str.getValue(), executeNode);
295+
}
296+
290297
@Specialization
291298
Object doString(String str,
292299
@Cached("createExecute(1)") Node executeNode) {
293300
try {
294-
return ForeignAccess.sendExecute(executeNode, getTruffleStringToCstr(), str);
301+
return ForeignAccess.sendExecute(executeNode, getTruffleStringToCstr(), str, str.length());
295302
} catch (UnsupportedTypeException | ArityException | UnsupportedMessageException e) {
296303
throw e.raise();
297304
}

0 commit comments

Comments
 (0)