3535import java .io .IOException ;
3636import java .io .InputStream ;
3737import java .io .OutputStream ;
38+ import java .io .UnsupportedEncodingException ;
3839import java .lang .ProcessBuilder .Redirect ;
3940import java .math .BigInteger ;
4041import 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