@@ -159,21 +159,18 @@ def _check_not_closed(self):
159159 raise ValueError ("I/O operation on closed file" )
160160
161161 def _check_can_read (self ):
162- if self .closed :
163- raise ValueError ("I/O operation on closed file" )
164162 if self ._mode not in (_MODE_READ , _MODE_READ_EOF ):
163+ self ._check_not_closed ()
165164 raise io .UnsupportedOperation ("File not open for reading" )
166165
167166 def _check_can_write (self ):
168- if self .closed :
169- raise ValueError ("I/O operation on closed file" )
170167 if self ._mode != _MODE_WRITE :
168+ self ._check_not_closed ()
171169 raise io .UnsupportedOperation ("File not open for writing" )
172170
173171 def _check_can_seek (self ):
174- if self .closed :
175- raise ValueError ("I/O operation on closed file" )
176172 if self ._mode not in (_MODE_READ , _MODE_READ_EOF ):
173+ self ._check_not_closed ()
177174 raise io .UnsupportedOperation ("Seeking is only supported "
178175 "on files open for reading" )
179176 if not self ._fp .seekable ():
@@ -322,10 +319,12 @@ def readline(self, size=-1):
322319 non-negative, no more than size bytes will be read (in which
323320 case the line may be incomplete). Returns b'' if already at EOF.
324321 """
325- if not hasattr (size , "__index__" ):
326- raise TypeError ("Integer argument expected" )
327- size = size .__index__ ()
322+ if not isinstance (size , int ):
323+ if not hasattr (size , "__index__" ):
324+ raise TypeError ("Integer argument expected" )
325+ size = size .__index__ ()
328326 with self ._lock :
327+ self ._check_can_read ()
329328 # Shortcut for the common case - the whole line is in the buffer.
330329 if size < 0 :
331330 end = self ._buffer .find (b"\n " , self ._buffer_offset ) + 1
@@ -343,9 +342,10 @@ def readlines(self, size=-1):
343342 further lines will be read once the total size of the lines read
344343 so far equals or exceeds size.
345344 """
346- if not hasattr (size , "__index__" ):
347- raise TypeError ("Integer argument expected" )
348- size = size .__index__ ()
345+ if not isinstance (size , int ):
346+ if not hasattr (size , "__index__" ):
347+ raise TypeError ("Integer argument expected" )
348+ size = size .__index__ ()
349349 with self ._lock :
350350 return io .BufferedIOBase .readlines (self , size )
351351
0 commit comments