Skip to content

Commit f39f227

Browse files
committed
Fix: 'os.open' should accept 'bytes' object.
1 parent be17134 commit f39f227

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.io.IOException;
3636
import java.io.InputStream;
3737
import java.io.OutputStream;
38+
import java.io.UnsupportedEncodingException;
3839
import java.lang.ProcessBuilder.Redirect;
3940
import java.math.BigInteger;
4041
import java.net.InetAddress;
@@ -673,6 +674,8 @@ int dupOvf(PInt fd) {
673674
@GenerateNodeFactory
674675
@TypeSystemReference(PythonArithmeticTypes.class)
675676
public abstract static class OpenNode extends PythonFileNode {
677+
@Child private SequenceStorageNodes.ToByteArrayNode toByteArrayNode;
678+
676679
private final BranchProfile gotException = BranchProfile.create();
677680

678681
@Specialization(guards = {"isNoValue(mode)", "isNoValue(dir_fd)"})
@@ -694,6 +697,28 @@ Object open(String pathname, int flags, int fileMode, @SuppressWarnings("unused"
694697
}
695698
}
696699

700+
@Specialization(guards = {"isNoValue(dir_fd)"})
701+
Object open(PBytes pathname, int flags, int fileMode, PNone dir_fd) {
702+
return open(decode(getByteArray(pathname)), flags, fileMode, dir_fd);
703+
}
704+
705+
private byte[] getByteArray(PIBytesLike pByteArray) {
706+
if (toByteArrayNode == null) {
707+
CompilerDirectives.transferToInterpreterAndInvalidate();
708+
toByteArrayNode = insert(ToByteArrayNode.create());
709+
}
710+
return toByteArrayNode.execute(pByteArray.getSequenceStorage());
711+
}
712+
713+
@TruffleBoundary
714+
private String decode(byte[] raw) {
715+
try {
716+
return new String(raw, "ascii");
717+
} catch (UnsupportedEncodingException e) {
718+
throw raise(PythonBuiltinClassType.UnicodeDecodeError, e.getMessage());
719+
}
720+
}
721+
697722
@SuppressWarnings({"unchecked", "rawtypes"})
698723
@TruffleBoundary(allowInlining = true)
699724
private static FileAttribute<Set<PosixFilePermission>>[] modeToAttributes(int fileMode) {

0 commit comments

Comments
 (0)