Skip to content

Commit 7f3dcea

Browse files
committed
Support 'bytes' in 'eval'.
1 parent db07de6 commit 7f3dcea

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
import com.oracle.graal.python.builtins.objects.PNotImplemented;
8484
import com.oracle.graal.python.builtins.objects.bytes.BytesNodes;
8585
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
86+
import com.oracle.graal.python.builtins.objects.bytes.PIBytesLike;
8687
import com.oracle.graal.python.builtins.objects.cell.PCell;
8788
import com.oracle.graal.python.builtins.objects.code.PCode;
8889
import com.oracle.graal.python.builtins.objects.common.HashingCollectionNodes;
@@ -456,6 +457,7 @@ private static BigInteger[] divideAndRemainder(PInt a, PInt b) {
456457
public abstract static class EvalNode extends PythonBuiltinNode {
457458
@Child private GetItemNode getNameNode = GetItemNode.create();
458459
@Child private ReadCallerFrameNode readCallerFrameNode = ReadCallerFrameNode.create();
460+
@Child private SequenceStorageNodes.ToByteArrayNode toByteArrayNode;
459461

460462
@Specialization
461463
public Object eval(VirtualFrame frame, String expression, @SuppressWarnings("unused") PNone globals, @SuppressWarnings("unused") PNone locals) {
@@ -515,6 +517,39 @@ public Object eval(VirtualFrame frame, PCode code, @SuppressWarnings("unused") P
515517
return evalExpression(code, callerGlobals, locals, callerClosure);
516518
}
517519

520+
@Specialization
521+
public Object eval(VirtualFrame frame, PBytes expression, PNone globals, PNone locals) {
522+
return eval(frame, getString(expression), globals, locals);
523+
}
524+
525+
@Specialization
526+
public Object eval(VirtualFrame frame, PBytes expression, PythonObject globals, PNone locals) {
527+
return eval(frame, getString(expression), globals, locals);
528+
}
529+
530+
@Specialization
531+
public Object eval(VirtualFrame frame, PBytes expression, PythonObject globals, PythonObject locals) {
532+
return eval(frame, getString(expression), globals, locals);
533+
}
534+
535+
@Specialization
536+
public Object eval(VirtualFrame frame, PBytes expression, PNone globals, PythonObject locals) {
537+
return eval(frame, getString(expression), globals, locals);
538+
}
539+
540+
@TruffleBoundary
541+
private String getString(PBytes expression) {
542+
return new String(getByteArray(expression));
543+
}
544+
545+
private byte[] getByteArray(PIBytesLike pByteArray) {
546+
if (toByteArrayNode == null) {
547+
CompilerDirectives.transferToInterpreterAndInvalidate();
548+
toByteArrayNode = insert(SequenceStorageNodes.ToByteArrayNode.create());
549+
}
550+
return toByteArrayNode.execute(pByteArray.getSequenceStorage());
551+
}
552+
518553
@TruffleBoundary
519554
private static Object evalExpression(PCode code, PythonObject globals, PythonObject locals, PCell[] closure) {
520555
RootNode root = code.getRootNode();

0 commit comments

Comments
 (0)