@@ -334,8 +334,8 @@ public static void chmod(CodeContext context, object? path, int mode, [ParamDict
334334 public static void close ( CodeContext /*!*/ context , int fd ) {
335335 PythonFileManager fileManager = context . LanguageContext . FileManager ;
336336 if ( fileManager . TryGetFileFromId ( fd , out PythonIOModule . FileIO ? file ) ) {
337- file . closefd = true ;
338- file . close ( context ) ;
337+ file . CloseStreams ( fileManager ) ;
338+ fileManager . RemoveObjectOnId ( fd ) ;
339339 } else {
340340 Stream stream = fileManager . GetStreamFromId ( fd ) ;
341341 fileManager . RemoveObjectOnId ( fd ) ;
@@ -358,7 +358,7 @@ public static int dup(CodeContext/*!*/ context, int fd) {
358358
359359 object obj = fileManager . GetObjectFromId ( fd ) ; // OSError if fd not valid
360360 if ( obj is PythonIOModule . FileIO file ) {
361- var file2 = new PythonIOModule . FileIO ( context , file . fileno ( context ) ) ;
361+ var file2 = new PythonIOModule . FileIO ( context , file . fileno ( context ) ) { closefd = false } ;
362362 int fd2 = fileManager . AddFile ( file2 ) ;
363363 fileManager . EnsureRef ( file . _readStream ) ;
364364 fileManager . AddRef ( file2 . _readStream ) ;
@@ -391,7 +391,7 @@ public static int dup2(CodeContext/*!*/ context, int fd, int fd2) {
391391 // TODO: race condition: `open` or `dup` on another thread may occupy fd2
392392
393393 if ( obj is PythonIOModule . FileIO file ) {
394- var file2 = new PythonIOModule . FileIO ( context , file . fileno ( context ) ) ;
394+ var file2 = new PythonIOModule . FileIO ( context , file . fileno ( context ) ) { closefd = false } ;
395395 fileManager . AddFile ( fd2 , file2 ) ;
396396 fileManager . EnsureRef ( file . _readStream ) ;
397397 fileManager . AddRef ( file2 . _readStream ) ;
@@ -909,8 +909,8 @@ static Tuple<Stream, Stream> CreatePipeStreamsUnix() {
909909 public static PythonTuple pipe ( CodeContext context ) {
910910 var pipeStreams = CreatePipeStreams ( ) ;
911911
912- var inFile = new PythonIOModule . FileIO ( context , pipeStreams . Item1 ) ;
913- var outFile = new PythonIOModule . FileIO ( context , pipeStreams . Item2 ) ;
912+ var inFile = new PythonIOModule . FileIO ( context , pipeStreams . Item1 ) { closefd = false } ;
913+ var outFile = new PythonIOModule . FileIO ( context , pipeStreams . Item2 ) { closefd = false } ;
914914
915915 return PythonTuple . MakeTuple (
916916 context . LanguageContext . FileManager . AddFile ( inFile ) ,
0 commit comments