35
35
import java .io .IOException ;
36
36
import java .io .InputStream ;
37
37
import java .io .OutputStream ;
38
+ import java .io .UnsupportedEncodingException ;
38
39
import java .lang .ProcessBuilder .Redirect ;
39
40
import java .math .BigInteger ;
40
41
import java .net .InetAddress ;
@@ -673,6 +674,8 @@ int dupOvf(PInt fd) {
673
674
@ GenerateNodeFactory
674
675
@ TypeSystemReference (PythonArithmeticTypes .class )
675
676
public abstract static class OpenNode extends PythonFileNode {
677
+ @ Child private SequenceStorageNodes .ToByteArrayNode toByteArrayNode ;
678
+
676
679
private final BranchProfile gotException = BranchProfile .create ();
677
680
678
681
@ Specialization (guards = {"isNoValue(mode)" , "isNoValue(dir_fd)" })
@@ -694,6 +697,28 @@ Object open(String pathname, int flags, int fileMode, @SuppressWarnings("unused"
694
697
}
695
698
}
696
699
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
+
697
722
@ SuppressWarnings ({"unchecked" , "rawtypes" })
698
723
@ TruffleBoundary (allowInlining = true )
699
724
private static FileAttribute <Set <PosixFilePermission >>[] modeToAttributes (int fileMode ) {
0 commit comments