@@ -340,6 +340,7 @@ def seek(self, pos: int, whence: int = 0) -> int:
340
340
341
341
def tell (self ) -> int :
342
342
"""Return current stream position."""
343
+ self ._checkClosed ()
343
344
return self .seek (0 , 1 )
344
345
345
346
def truncate (self , pos : int = None ) -> int :
@@ -358,6 +359,8 @@ def flush(self) -> None:
358
359
This is not implemented for read-only and non-blocking streams.
359
360
"""
360
361
# XXX Should this return the number of bytes written???
362
+ if self .__closed :
363
+ raise ValueError ("I/O operation on closed file." )
361
364
362
365
__closed = False
363
366
@@ -530,6 +533,7 @@ def readlines(self, hint=None):
530
533
lines will be read if the total size (in bytes/characters) of all
531
534
lines so far exceeds hint.
532
535
"""
536
+ self ._checkClosed ()
533
537
if hint is None or hint <= 0 :
534
538
return list (self )
535
539
n = 0
@@ -567,6 +571,7 @@ def read(self, n: int = -1) -> bytes:
567
571
Returns an empty bytes object on EOF, or None if the object is
568
572
set not to block and has no data to read.
569
573
"""
574
+ self ._checkClosed ()
570
575
if n is None :
571
576
n = - 1
572
577
if n < 0 :
@@ -578,6 +583,7 @@ def read(self, n: int = -1) -> bytes:
578
583
579
584
def readall (self ):
580
585
"""Read until EOF, using multiple read() call."""
586
+ self ._checkClosed ()
581
587
res = bytearray ()
582
588
while True :
583
589
data = self .read (DEFAULT_BUFFER_SIZE )
@@ -673,6 +679,7 @@ def readinto(self, b: bytearray) -> int:
673
679
data at the moment.
674
680
"""
675
681
# XXX This ought to work with anything that supports the buffer API
682
+ self ._checkClosed ()
676
683
data = self .read (len (b ))
677
684
n = len (data )
678
685
try :
@@ -787,13 +794,11 @@ def __init__(self, initial_bytes=None):
787
794
def getvalue (self ):
788
795
"""Return the bytes value (contents) of the buffer
789
796
"""
790
- if self .closed :
791
- raise ValueError ("getvalue on closed file" )
797
+ self ._checkClosed ()
792
798
return bytes (self ._buffer )
793
799
794
800
def read (self , n = None ):
795
- if self .closed :
796
- raise ValueError ("read from closed file" )
801
+ self ._checkClosed ()
797
802
if n is None :
798
803
n = - 1
799
804
if n < 0 :
@@ -811,8 +816,7 @@ def read1(self, n):
811
816
return self .read (n )
812
817
813
818
def write (self , b ):
814
- if self .closed :
815
- raise ValueError ("write to closed file" )
819
+ self ._checkClosed ()
816
820
if isinstance (b , str ):
817
821
raise TypeError ("can't write str to binary stream" )
818
822
n = len (b )
@@ -829,8 +833,7 @@ def write(self, b):
829
833
return n
830
834
831
835
def seek (self , pos , whence = 0 ):
832
- if self .closed :
833
- raise ValueError ("seek on closed file" )
836
+ self ._checkClosed ()
834
837
try :
835
838
pos = pos .__index__ ()
836
839
except AttributeError as err :
@@ -848,13 +851,11 @@ def seek(self, pos, whence=0):
848
851
return self ._pos
849
852
850
853
def tell (self ):
851
- if self .closed :
852
- raise ValueError ("tell on closed file" )
854
+ self ._checkClosed ()
853
855
return self ._pos
854
856
855
857
def truncate (self , pos = None ):
856
- if self .closed :
857
- raise ValueError ("truncate on closed file" )
858
+ self ._checkClosed ()
858
859
if pos is None :
859
860
pos = self ._pos
860
861
elif pos < 0 :
@@ -914,6 +915,7 @@ def read(self, n=None):
914
915
mode. If n is negative, read until EOF or until read() would
915
916
block.
916
917
"""
918
+ self ._checkClosed ()
917
919
with self ._read_lock :
918
920
return self ._read_unlocked (n )
919
921
@@ -970,6 +972,7 @@ def peek(self, n=0):
970
972
do at most one raw read to satisfy it. We never return more
971
973
than self.buffer_size.
972
974
"""
975
+ self ._checkClosed ()
973
976
with self ._read_lock :
974
977
return self ._peek_unlocked (n )
975
978
@@ -988,6 +991,7 @@ def read1(self, n):
988
991
"""Reads up to n bytes, with at most one read() system call."""
989
992
# Returns up to n bytes. If at least one byte is buffered, we
990
993
# only return buffered bytes. Otherwise, we do one raw read.
994
+ self ._checkClosed ()
991
995
if n <= 0 :
992
996
return b""
993
997
with self ._read_lock :
@@ -996,9 +1000,11 @@ def read1(self, n):
996
1000
min (n , len (self ._read_buf ) - self ._read_pos ))
997
1001
998
1002
def tell (self ):
1003
+ self ._checkClosed ()
999
1004
return self .raw .tell () - len (self ._read_buf ) + self ._read_pos
1000
1005
1001
1006
def seek (self , pos , whence = 0 ):
1007
+ self ._checkClosed ()
1002
1008
with self ._read_lock :
1003
1009
if whence == 1 :
1004
1010
pos -= len (self ._read_buf ) - self ._read_pos
@@ -1029,8 +1035,7 @@ def __init__(self, raw,
1029
1035
self ._write_lock = Lock ()
1030
1036
1031
1037
def write (self , b ):
1032
- if self .closed :
1033
- raise ValueError ("write to closed file" )
1038
+ self ._checkClosed ()
1034
1039
if isinstance (b , str ):
1035
1040
raise TypeError ("can't write str to binary stream" )
1036
1041
with self ._write_lock :
@@ -1060,19 +1065,19 @@ def write(self, b):
1060
1065
return written
1061
1066
1062
1067
def truncate (self , pos = None ):
1068
+ self ._checkClosed ()
1063
1069
with self ._write_lock :
1064
1070
self ._flush_unlocked ()
1065
1071
if pos is None :
1066
1072
pos = self .raw .tell ()
1067
1073
return self .raw .truncate (pos )
1068
1074
1069
1075
def flush (self ):
1076
+ self ._checkClosed ()
1070
1077
with self ._write_lock :
1071
1078
self ._flush_unlocked ()
1072
1079
1073
1080
def _flush_unlocked (self ):
1074
- if self .closed :
1075
- raise ValueError ("flush of closed file" )
1076
1081
written = 0
1077
1082
try :
1078
1083
while self ._write_buf :
@@ -1086,9 +1091,11 @@ def _flush_unlocked(self):
1086
1091
raise BlockingIOError (e .errno , e .strerror , written )
1087
1092
1088
1093
def tell (self ):
1094
+ self ._checkClosed ()
1089
1095
return self .raw .tell () + len (self ._write_buf )
1090
1096
1091
1097
def seek (self , pos , whence = 0 ):
1098
+ self ._checkClosed ()
1092
1099
with self ._write_lock :
1093
1100
self ._flush_unlocked ()
1094
1101
return self .raw .seek (pos , whence )
@@ -1186,6 +1193,7 @@ def seek(self, pos, whence=0):
1186
1193
return pos
1187
1194
1188
1195
def tell (self ):
1196
+ self ._checkClosed ()
1189
1197
if self ._write_buf :
1190
1198
return self .raw .tell () + len (self ._write_buf )
1191
1199
else :
@@ -1217,6 +1225,7 @@ def read1(self, n):
1217
1225
return BufferedReader .read1 (self , n )
1218
1226
1219
1227
def write (self , b ):
1228
+ self ._checkClosed ()
1220
1229
if self ._read_buf :
1221
1230
# Undo readahead
1222
1231
with self ._read_lock :
@@ -1474,8 +1483,7 @@ def isatty(self):
1474
1483
return self .buffer .isatty ()
1475
1484
1476
1485
def write (self , s : str ):
1477
- if self .closed :
1478
- raise ValueError ("write to closed file" )
1486
+ self ._checkClosed ()
1479
1487
if not isinstance (s , str ):
1480
1488
raise TypeError ("can't write %s to text stream" %
1481
1489
s .__class__ .__name__ )
@@ -1583,6 +1591,7 @@ def _unpack_cookie(self, bigint):
1583
1591
return position , dec_flags , bytes_to_feed , need_eof , chars_to_skip
1584
1592
1585
1593
def tell (self ):
1594
+ self ._checkClosed ()
1586
1595
if not self ._seekable :
1587
1596
raise IOError ("underlying stream is not seekable" )
1588
1597
if not self ._telling :
@@ -1653,8 +1662,7 @@ def truncate(self, pos=None):
1653
1662
return self .buffer .truncate ()
1654
1663
1655
1664
def seek (self , cookie , whence = 0 ):
1656
- if self .closed :
1657
- raise ValueError ("tell on closed file" )
1665
+ self ._checkClosed ()
1658
1666
if not self ._seekable :
1659
1667
raise IOError ("underlying stream is not seekable" )
1660
1668
if whence == 1 : # seek relative to current position
@@ -1712,6 +1720,7 @@ def seek(self, cookie, whence=0):
1712
1720
return cookie
1713
1721
1714
1722
def read (self , n = None ):
1723
+ self ._checkClosed ()
1715
1724
if n is None :
1716
1725
n = - 1
1717
1726
decoder = self ._decoder or self ._get_decoder ()
@@ -1732,6 +1741,7 @@ def read(self, n=None):
1732
1741
return result
1733
1742
1734
1743
def __next__ (self ):
1744
+ self ._checkClosed ()
1735
1745
self ._telling = False
1736
1746
line = self .readline ()
1737
1747
if not line :
@@ -1741,8 +1751,7 @@ def __next__(self):
1741
1751
return line
1742
1752
1743
1753
def readline (self , limit = None ):
1744
- if self .closed :
1745
- raise ValueError ("read from closed file" )
1754
+ self ._checkClosed ()
1746
1755
if limit is None :
1747
1756
limit = - 1
1748
1757
@@ -1963,17 +1972,15 @@ def seekable(self):
1963
1972
1964
1973
def getvalue (self ) -> str :
1965
1974
"""Retrieve the entire contents of the object."""
1966
- if self .closed :
1967
- raise ValueError ("read on closed file" )
1975
+ self ._checkClosed ()
1968
1976
return self ._getvalue ()
1969
1977
1970
1978
def write (self , s : str ) -> int :
1971
1979
"""Write string s to file.
1972
1980
1973
1981
Returns the number of characters written.
1974
1982
"""
1975
- if self .closed :
1976
- raise ValueError ("write to closed file" )
1983
+ self ._checkClosed ()
1977
1984
if not isinstance (s , str ):
1978
1985
raise TypeError ("can't write %s to text stream" %
1979
1986
s .__class__ .__name__ )
@@ -1990,8 +1997,7 @@ def read(self, n: int = None) -> str:
1990
1997
If the argument is negative or omitted, read until EOF
1991
1998
is reached. Return an empty string at EOF.
1992
1999
"""
1993
- if self .closed :
1994
- raise ValueError ("read to closed file" )
2000
+ self ._checkClosed ()
1995
2001
if n is None :
1996
2002
n = - 1
1997
2003
res = self ._pending
@@ -2006,8 +2012,7 @@ def read(self, n: int = None) -> str:
2006
2012
2007
2013
def tell (self ) -> int :
2008
2014
"""Tell the current file position."""
2009
- if self .closed :
2010
- raise ValueError ("tell from closed file" )
2015
+ self ._checkClosed ()
2011
2016
if self ._pending :
2012
2017
return self ._tell () - len (self ._pending )
2013
2018
else :
@@ -2022,8 +2027,7 @@ def seek(self, pos: int = None, whence: int = 0) -> int:
2022
2027
2 End of stream - pos must be 0.
2023
2028
Returns the new absolute position.
2024
2029
"""
2025
- if self .closed :
2026
- raise ValueError ("seek from closed file" )
2030
+ self ._checkClosed ()
2027
2031
self ._pending = ""
2028
2032
return self ._seek (pos , whence )
2029
2033
@@ -2034,14 +2038,12 @@ def truncate(self, pos: int = None) -> int:
2034
2038
returned by tell(). Imply an absolute seek to pos.
2035
2039
Returns the new absolute position.
2036
2040
"""
2037
- if self .closed :
2038
- raise ValueError ("truncate from closed file" )
2041
+ self ._checkClosed ()
2039
2042
self ._pending = ""
2040
2043
return self ._truncate (pos )
2041
2044
2042
2045
def readline (self , limit : int = None ) -> str :
2043
- if self .closed :
2044
- raise ValueError ("read from closed file" )
2046
+ self ._checkClosed ()
2045
2047
if limit is None :
2046
2048
limit = - 1
2047
2049
if limit >= 0 :
0 commit comments