@@ -313,25 +313,34 @@ def open(self, host='', port=IMAP4_PORT, timeout=None):
313313 self .host = host
314314 self .port = port
315315 self .sock = self ._create_socket (timeout )
316- self ._file = self .sock .makefile ('rb' )
317-
316+ # Since IMAP4 implements its own read() and readline() buffering,
317+ # the '_imaplib_file' attribute is unused. Nonetheless it is kept
318+ # and exposed solely for backward compatibility purposes.
319+ self ._imaplib_file = self .sock .makefile ('rb' )
318320
319321 @property
320322 def file (self ):
321- # The old 'file' attribute is no longer used now that we do our own
322- # read() and readline() buffering, with which it conflicts.
323- # As an undocumented interface, it should never have been accessed by
324- # external code, and therefore does not warrant deprecation.
325- # Nevertheless, we provide this property for now, to avoid suddenly
326- # breaking any code in the wild that might have been using it in a
327- # harmless way.
328323 import warnings
329- warnings .warn (
330- 'IMAP4.file is unsupported, can cause errors, and may be removed.' ,
331- RuntimeWarning ,
332- stacklevel = 2 )
333- return self ._file
324+ warnings ._deprecated ("IMAP4.file" , remove = (3 , 19 ))
325+ return self ._imaplib_file
334326
327+ @file .setter
328+ def file (self , value ):
329+ import warnings
330+ warnings ._deprecated ("IMAP4.file" , remove = (3 , 19 ))
331+ # Ideally, we would want to close the previous file,
332+ # but since we do not know how subclasses will use
333+ # that setter, it is probably better to leave it to
334+ # the caller.
335+ self ._imaplib_file = value
336+
337+ def _close_imaplib_file (self ):
338+ file = self ._imaplib_file
339+ if file is not None :
340+ try :
341+ file .close ()
342+ except OSError :
343+ pass
335344
336345 def read (self , size ):
337346 """Read 'size' bytes from remote."""
@@ -417,7 +426,7 @@ def send(self, data):
417426
418427 def shutdown (self ):
419428 """Close I/O established in "open"."""
420- self ._file . close ()
429+ self ._close_imaplib_file ()
421430 try :
422431 self .sock .shutdown (socket .SHUT_RDWR )
423432 except OSError as exc :
@@ -921,9 +930,10 @@ def starttls(self, ssl_context=None):
921930 ssl_context = ssl ._create_stdlib_context ()
922931 typ , dat = self ._simple_command (name )
923932 if typ == 'OK' :
933+ self ._close_imaplib_file ()
924934 self .sock = ssl_context .wrap_socket (self .sock ,
925935 server_hostname = self .host )
926- self ._file = self .sock .makefile ('rb' )
936+ self ._imaplib_file = self .sock .makefile ('rb' )
927937 self ._tls_established = True
928938 self ._get_capabilities ()
929939 else :
@@ -1678,7 +1688,7 @@ def open(self, host=None, port=None, timeout=None):
16781688 self .host = None # For compatibility with parent class
16791689 self .port = None
16801690 self .sock = None
1681- self ._file = None
1691+ self ._imaplib_file = None
16821692 self .process = subprocess .Popen (self .command ,
16831693 bufsize = DEFAULT_BUFFER_SIZE ,
16841694 stdin = subprocess .PIPE , stdout = subprocess .PIPE ,
0 commit comments