Skip to content

Commit de62a3e

Browse files
author
Forest
committed
imaplib: warn on use of undocumented 'file' attr
1 parent be2d2b0 commit de62a3e

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Lib/imaplib.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import time
3535
from datetime import datetime, timezone, timedelta
3636
from io import DEFAULT_BUFFER_SIZE
37+
import warnings
3738

3839
try:
3940
import ssl
@@ -323,7 +324,22 @@ def open(self, host='', port=IMAP4_PORT, timeout=None):
323324
self.host = host
324325
self.port = port
325326
self.sock = self._create_socket(timeout)
326-
self.file = self.sock.makefile('rb')
327+
self._file = self.sock.makefile('rb')
328+
329+
330+
@property
331+
def file(self):
332+
# The old 'file' attribute is no longer used now that we do our own
333+
# read() and readline() buffering, with which it conflicts.
334+
# As an undocumented interface, it should never have been accessed by
335+
# external code, and therefore does not warrant deprecation.
336+
# Nevertheless, we provide this property for now, to avoid suddenly
337+
# breaking any code in the wild that might have been using it in a
338+
# harmless way.
339+
warnings.warn(
340+
'IMAP4.file is unsupported, can cause errors, and may be removed.',
341+
RuntimeWarning)
342+
return self._file
327343

328344

329345
def read(self, size):
@@ -383,7 +399,7 @@ def send(self, data):
383399

384400
def shutdown(self):
385401
"""Close I/O established in "open"."""
386-
self.file.close()
402+
self._file.close()
387403
try:
388404
self.sock.shutdown(socket.SHUT_RDWR)
389405
except OSError as exc:
@@ -883,7 +899,7 @@ def starttls(self, ssl_context=None):
883899
if typ == 'OK':
884900
self.sock = ssl_context.wrap_socket(self.sock,
885901
server_hostname=self.host)
886-
self.file = self.sock.makefile('rb')
902+
self._file = self.sock.makefile('rb')
887903
self._tls_established = True
888904
self._get_capabilities()
889905
else:
@@ -1629,7 +1645,7 @@ def open(self, host=None, port=None, timeout=None):
16291645
self.host = None # For compatibility with parent class
16301646
self.port = None
16311647
self.sock = None
1632-
self.file = None
1648+
self._file = None
16331649
self.process = subprocess.Popen(self.command,
16341650
bufsize=DEFAULT_BUFFER_SIZE,
16351651
stdin=subprocess.PIPE, stdout=subprocess.PIPE,

0 commit comments

Comments
 (0)