@@ -883,8 +883,6 @@ public static void mkdir(CodeContext context, [NotNone] Bytes path, [ParamDictio
883883 public static void mkdir ( CodeContext context , object ? path , [ ParamDictionary , NotNone ] IDictionary < string , object > kwargs , [ NotNone ] params object [ ] args )
884884 => mkdir ( ConvertToFsString ( context , path , nameof ( path ) ) , kwargs , args ) ;
885885
886- private const int DefaultBufferSize = 4096 ;
887-
888886 [ Documentation ( """
889887 open(path, flags, mode=511, *, dir_fd=None)
890888
@@ -926,6 +924,12 @@ public static object open(CodeContext/*!*/ context, [NotNone] string path, int f
926924 }
927925
928926 try {
927+ // FileStream buffer size must be >= 0 on .NET, and >= 1 on .NET Framework and Mono.
928+ // On .NET, buffer size 0 or 1 disables buffering.
929+ // On .NET Framework, buffer size 1 disables buffering.
930+ // On Mono, buffer size 1 makes writes of length >= 2 bypass the buffer.
931+ const int NoBuffering = 1 ;
932+
929933 FileMode fileMode = FileModeFromFlags ( flags ) ;
930934 FileAccess access = FileAccessFromFlags ( flags ) ;
931935 FileOptions options = FileOptionsFromFlags ( flags ) ;
@@ -940,15 +944,15 @@ public static object open(CodeContext/*!*/ context, [NotNone] string path, int f
940944 // open it again w/ just read access.
941945 fs = new FileStream ( path , fileMode , FileAccess . Write , FileShare . None ) ;
942946 fs . Close ( ) ;
943- s = fs = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . ReadWrite , DefaultBufferSize , options ) ;
947+ s = fs = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . ReadWrite , NoBuffering , options ) ;
944948 } else if ( access == FileAccess . ReadWrite && fileMode == FileMode . Append ) {
945949 // .NET doesn't allow Append w/ access != Write, so open the file w/ Write
946950 // and a secondary stream w/ Read, then seek to the end.
947- s = fs = new FileStream ( path , FileMode . Append , FileAccess . Write , FileShare . ReadWrite , DefaultBufferSize , options ) ;
948- rs = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . ReadWrite , DefaultBufferSize , options ) ;
951+ s = fs = new FileStream ( path , FileMode . Append , FileAccess . Write , FileShare . ReadWrite , NoBuffering , options ) ;
952+ rs = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . ReadWrite , NoBuffering , options ) ;
949953 rs . Seek ( 0L , SeekOrigin . End ) ;
950954 } else {
951- s = fs = new FileStream ( path , fileMode , access , FileShare . ReadWrite , DefaultBufferSize , options ) ;
955+ s = fs = new FileStream ( path , fileMode , access , FileShare . ReadWrite , NoBuffering , options ) ;
952956 }
953957 rs ??= s ;
954958
0 commit comments