@@ -4209,7 +4209,7 @@ def __init__(self, file_object, file_path, update=False, read=False,
4209
4209
append = False , delete_on_close = False , filesystem = None ,
4210
4210
newline = None , binary = True , closefd = True , encoding = None ,
4211
4211
errors = None , raw_io = False , is_stream = False , use_io = True ):
4212
- self ._file_object = file_object
4212
+ self .file_object = file_object
4213
4213
self ._file_path = file_path
4214
4214
self ._append = append
4215
4215
self ._read = read
@@ -4286,7 +4286,7 @@ def _raise(self, message):
4286
4286
4287
4287
def get_object (self ):
4288
4288
"""Return the FakeFile object that is wrapped by the current instance."""
4289
- return self ._file_object
4289
+ return self .file_object
4290
4290
4291
4291
def fileno (self ):
4292
4292
"""Return the file descriptor of the file object."""
@@ -4300,7 +4300,7 @@ def close(self):
4300
4300
4301
4301
# for raw io, all writes are flushed immediately
4302
4302
if self .allow_update and not self .raw_io :
4303
- self ._file_object .set_contents (self ._io .getvalue (), self ._encoding )
4303
+ self .file_object .set_contents (self ._io .getvalue (), self ._encoding )
4304
4304
if self ._closefd :
4305
4305
self ._filesystem ._close_open_file (self .filedes )
4306
4306
else :
@@ -4313,8 +4313,17 @@ def flush(self):
4313
4313
self ._check_open_file ()
4314
4314
if self .allow_update :
4315
4315
self ._io .flush ()
4316
- self ._file_object .set_contents (self ._io .getvalue (), self ._encoding )
4317
- self ._file_epoch = self ._file_object .epoch
4316
+ self .file_object .set_contents (self ._io .getvalue (), self ._encoding )
4317
+ self ._file_epoch = self .file_object .epoch
4318
+
4319
+ if not self .is_stream :
4320
+ self ._flush_related_files ()
4321
+
4322
+ def _flush_related_files (self ):
4323
+ for open_files in self ._filesystem .open_files [3 :]:
4324
+ for open_file in open_files :
4325
+ if open_file is not self and self .file_object == open_file .file_object :
4326
+ open_file ._sync_io ()
4318
4327
4319
4328
def seek (self , offset , whence = 0 ):
4320
4329
"""Move read/write pointer in 'file'."""
@@ -4358,13 +4367,13 @@ def _flushes_after_tell(self):
4358
4367
4359
4368
def _sync_io (self ):
4360
4369
"""Update the stream with changes to the file object contents."""
4361
- if self ._file_epoch == self ._file_object .epoch :
4370
+ if self ._file_epoch == self .file_object .epoch :
4362
4371
return
4363
4372
4364
4373
if isinstance (self ._io , io .BytesIO ):
4365
- contents = self ._file_object .byte_contents
4374
+ contents = self .file_object .byte_contents
4366
4375
else :
4367
- contents = self ._file_object .contents
4376
+ contents = self .file_object .contents
4368
4377
4369
4378
is_stream_reader_writer = isinstance (self ._io , codecs .StreamReaderWriter )
4370
4379
if is_stream_reader_writer :
@@ -4380,7 +4389,7 @@ def _sync_io(self):
4380
4389
4381
4390
if is_stream_reader_writer :
4382
4391
self ._io .stream .allow_update = False
4383
- self ._file_epoch = self ._file_object .epoch
4392
+ self ._file_epoch = self .file_object .epoch
4384
4393
4385
4394
def _read_wrappers (self , name ):
4386
4395
"""Wrap a stream attribute in a read wrapper.
@@ -4468,12 +4477,12 @@ def truncate_wrapper(*args, **kwargs):
4468
4477
size = io_attr (* args , ** kwargs )
4469
4478
self .flush ()
4470
4479
if not self .is_stream :
4471
- self ._file_object .SetSize (size )
4480
+ self .file_object .SetSize (size )
4472
4481
buffer_size = len (self ._io .getvalue ())
4473
4482
if buffer_size < size :
4474
4483
self ._io .seek (buffer_size )
4475
4484
self ._io .write ('\0 ' * (size - buffer_size ))
4476
- self ._file_object .SetContents (self ._io .getvalue (), self ._encoding )
4485
+ self .file_object .SetContents (self ._io .getvalue (), self ._encoding )
4477
4486
if sys .version_info [0 ] > 2 :
4478
4487
return size
4479
4488
@@ -4497,10 +4506,10 @@ def write_wrapper(*args, **kwargs):
4497
4506
4498
4507
def size (self ):
4499
4508
"""Return the content size in bytes of the wrapped file."""
4500
- return self ._file_object .st_size
4509
+ return self .file_object .st_size
4501
4510
4502
4511
def __getattr__ (self , name ):
4503
- if self ._file_object .is_large_file ():
4512
+ if self .file_object .is_large_file ():
4504
4513
raise FakeLargeFileIoException (self ._file_path )
4505
4514
4506
4515
reading = name .startswith ('read' ) or name == 'next'
@@ -4579,20 +4588,23 @@ def close(self):
4579
4588
"""We do not support closing standard streams."""
4580
4589
pass
4581
4590
4591
+ def is_stream (self ):
4592
+ return True
4593
+
4582
4594
4583
4595
class FakeDirWrapper (object ):
4584
4596
"""Wrapper for a FakeDirectory object to be used in open files list.
4585
4597
"""
4586
4598
4587
4599
def __init__ (self , file_object , file_path , filesystem ):
4588
- self ._file_object = file_object
4600
+ self .file_object = file_object
4589
4601
self ._file_path = file_path
4590
4602
self ._filesystem = filesystem
4591
4603
self .filedes = None
4592
4604
4593
4605
def get_object (self ):
4594
4606
"""Return the FakeFile object that is wrapped by the current instance."""
4595
- return self ._file_object
4607
+ return self .file_object
4596
4608
4597
4609
def fileno (self ):
4598
4610
"""Return the file descriptor of the file object."""
0 commit comments