Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit edb978f

Browse files
committed
#2814: remove deprecated classes from mailbox module. Patch by Humberto Diogenes.
1 parent f7fa63d commit edb978f

3 files changed

Lines changed: 1 addition & 319 deletions

File tree

Doc/library/mailbox.rst

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,133 +1501,6 @@ The following exception classes are defined in the :mod:`mailbox` module:
15011501
instance attempts to read a corrupted :file:`.mh_sequences` file.
15021502

15031503

1504-
.. _mailbox-deprecated:
1505-
1506-
Deprecated classes and methods
1507-
------------------------------
1508-
1509-
Older versions of the :mod:`mailbox` module do not support modification of
1510-
mailboxes, such as adding or removing message, and do not provide classes to
1511-
represent format-specific message properties. For backward compatibility, the
1512-
older mailbox classes are still available, but the newer classes should be used
1513-
in preference to them.
1514-
1515-
Older mailbox objects support only iteration and provide a single public method:
1516-
1517-
1518-
.. method:: oldmailbox.next()
1519-
1520-
Return the next message in the mailbox, created with the optional *factory*
1521-
argument passed into the mailbox object's constructor. By default this is an
1522-
:class:`rfc822.Message` object (see the :mod:`rfc822` module). Depending on the
1523-
mailbox implementation the *fp* attribute of this object may be a true file
1524-
object or a class instance simulating a file object, taking care of things like
1525-
message boundaries if multiple mail messages are contained in a single file,
1526-
etc. If no more messages are available, this method returns ``None``.
1527-
1528-
Most of the older mailbox classes have names that differ from the current
1529-
mailbox class names, except for :class:`Maildir`. For this reason, the new
1530-
:class:`Maildir` class defines a :meth:`next` method and its constructor differs
1531-
slightly from those of the other new mailbox classes.
1532-
1533-
The older mailbox classes whose names are not the same as their newer
1534-
counterparts are as follows:
1535-
1536-
1537-
.. class:: UnixMailbox(fp[, factory])
1538-
1539-
Access to a classic Unix-style mailbox, where all messages are contained in a
1540-
single file and separated by ``From`` (a.k.a. ``From_``) lines. The file object
1541-
*fp* points to the mailbox file. The optional *factory* parameter is a callable
1542-
that should create new message objects. *factory* is called with one argument,
1543-
*fp* by the :meth:`next` method of the mailbox object. The default is the
1544-
:class:`rfc822.Message` class (see the :mod:`rfc822` module -- and the note
1545-
below).
1546-
1547-
.. note::
1548-
1549-
For reasons of this module's internal implementation, you will probably want to
1550-
open the *fp* object in binary mode. This is especially important on Windows.
1551-
1552-
For maximum portability, messages in a Unix-style mailbox are separated by any
1553-
line that begins exactly with the string ``'From '`` (note the trailing space)
1554-
if preceded by exactly two newlines. Because of the wide-range of variations in
1555-
practice, nothing else on the ``From_`` line should be considered. However, the
1556-
current implementation doesn't check for the leading two newlines. This is
1557-
usually fine for most applications.
1558-
1559-
The :class:`UnixMailbox` class implements a more strict version of ``From_``
1560-
line checking, using a regular expression that usually correctly matched
1561-
``From_`` delimiters. It considers delimiter line to be separated by ``From
1562-
name time`` lines. For maximum portability, use the
1563-
:class:`PortableUnixMailbox` class instead. This class is identical to
1564-
:class:`UnixMailbox` except that individual messages are separated by only
1565-
``From`` lines.
1566-
1567-
1568-
.. class:: PortableUnixMailbox(fp[, factory])
1569-
1570-
A less-strict version of :class:`UnixMailbox`, which considers only the ``From``
1571-
at the beginning of the line separating messages. The "*name* *time*" portion
1572-
of the From line is ignored, to protect against some variations that are
1573-
observed in practice. This works since lines in the message which begin with
1574-
``'From '`` are quoted by mail handling software at delivery-time.
1575-
1576-
1577-
.. class:: MmdfMailbox(fp[, factory])
1578-
1579-
Access an MMDF-style mailbox, where all messages are contained in a single file
1580-
and separated by lines consisting of 4 control-A characters. The file object
1581-
*fp* points to the mailbox file. Optional *factory* is as with the
1582-
:class:`UnixMailbox` class.
1583-
1584-
1585-
.. class:: MHMailbox(dirname[, factory])
1586-
1587-
Access an MH mailbox, a directory with each message in a separate file with a
1588-
numeric name. The name of the mailbox directory is passed in *dirname*.
1589-
*factory* is as with the :class:`UnixMailbox` class.
1590-
1591-
1592-
.. class:: BabylMailbox(fp[, factory])
1593-
1594-
Access a Babyl mailbox, which is similar to an MMDF mailbox. In Babyl format,
1595-
each message has two sets of headers, the *original* headers and the *visible*
1596-
headers. The original headers appear before a line containing only ``'*** EOOH
1597-
***'`` (End-Of-Original-Headers) and the visible headers appear after the
1598-
``EOOH`` line. Babyl-compliant mail readers will show you only the visible
1599-
headers, and :class:`BabylMailbox` objects will return messages containing only
1600-
the visible headers. You'll have to do your own parsing of the mailbox file to
1601-
get at the original headers. Mail messages start with the EOOH line and end
1602-
with a line containing only ``'\037\014'``. *factory* is as with the
1603-
:class:`UnixMailbox` class.
1604-
1605-
If you wish to use the older mailbox classes with the :mod:`email` module rather
1606-
than the deprecated :mod:`rfc822` module, you can do so as follows::
1607-
1608-
import email
1609-
import email.Errors
1610-
import mailbox
1611-
1612-
def msgfactory(fp):
1613-
try:
1614-
return email.message_from_file(fp)
1615-
except email.Errors.MessageParseError:
1616-
# Don't return None since that will
1617-
# stop the mailbox iterator
1618-
return ''
1619-
1620-
mbox = mailbox.UnixMailbox(fp, msgfactory)
1621-
1622-
Alternatively, if you know your mailbox contains only well-formed MIME messages,
1623-
you can simplify this to::
1624-
1625-
import email
1626-
import mailbox
1627-
1628-
mbox = mailbox.UnixMailbox(fp, email.message_from_file)
1629-
1630-
16311504
.. _mailbox-examples:
16321505

16331506
Examples

Lib/mailbox.py

Lines changed: 0 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,184 +1925,6 @@ def _sync_close(f):
19251925
_sync_flush(f)
19261926
f.close()
19271927

1928-
## Start: classes from the original module (for backward compatibility).
1929-
1930-
# Note that the Maildir class, whose name is unchanged, itself offers a next()
1931-
# method for backward compatibility.
1932-
1933-
class _Mailbox:
1934-
1935-
def __init__(self, fp, factory=rfc822.Message):
1936-
self.fp = fp
1937-
self.seekp = 0
1938-
self.factory = factory
1939-
1940-
def __iter__(self):
1941-
return iter(self.next, None)
1942-
1943-
def next(self):
1944-
while 1:
1945-
self.fp.seek(self.seekp)
1946-
try:
1947-
self._search_start()
1948-
except EOFError:
1949-
self.seekp = self.fp.tell()
1950-
return None
1951-
start = self.fp.tell()
1952-
self._search_end()
1953-
self.seekp = stop = self.fp.tell()
1954-
if start != stop:
1955-
break
1956-
return self.factory(_PartialFile(self.fp, start, stop))
1957-
1958-
# Recommended to use PortableUnixMailbox instead!
1959-
class UnixMailbox(_Mailbox):
1960-
1961-
def _search_start(self):
1962-
while 1:
1963-
pos = self.fp.tell()
1964-
line = self.fp.readline()
1965-
if not line:
1966-
raise EOFError
1967-
if line[:5] == 'From ' and self._isrealfromline(line):
1968-
self.fp.seek(pos)
1969-
return
1970-
1971-
def _search_end(self):
1972-
self.fp.readline() # Throw away header line
1973-
while 1:
1974-
pos = self.fp.tell()
1975-
line = self.fp.readline()
1976-
if not line:
1977-
return
1978-
if line[:5] == 'From ' and self._isrealfromline(line):
1979-
self.fp.seek(pos)
1980-
return
1981-
1982-
# An overridable mechanism to test for From-line-ness. You can either
1983-
# specify a different regular expression or define a whole new
1984-
# _isrealfromline() method. Note that this only gets called for lines
1985-
# starting with the 5 characters "From ".
1986-
#
1987-
# BAW: According to
1988-
#http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html
1989-
# the only portable, reliable way to find message delimiters in a BSD (i.e
1990-
# Unix mailbox) style folder is to search for "\n\nFrom .*\n", or at the
1991-
# beginning of the file, "^From .*\n". While _fromlinepattern below seems
1992-
# like a good idea, in practice, there are too many variations for more
1993-
# strict parsing of the line to be completely accurate.
1994-
#
1995-
# _strict_isrealfromline() is the old version which tries to do stricter
1996-
# parsing of the From_ line. _portable_isrealfromline() simply returns
1997-
# true, since it's never called if the line doesn't already start with
1998-
# "From ".
1999-
#
2000-
# This algorithm, and the way it interacts with _search_start() and
2001-
# _search_end() may not be completely correct, because it doesn't check
2002-
# that the two characters preceding "From " are \n\n or the beginning of
2003-
# the file. Fixing this would require a more extensive rewrite than is
2004-
# necessary. For convenience, we've added a PortableUnixMailbox class
2005-
# which does no checking of the format of the 'From' line.
2006-
2007-
_fromlinepattern = (r"From \s*[^\s]+\s+\w\w\w\s+\w\w\w\s+\d?\d\s+"
2008-
r"\d?\d:\d\d(:\d\d)?(\s+[^\s]+)?\s+\d\d\d\d\s*"
2009-
r"[^\s]*\s*"
2010-
"$")
2011-
_regexp = None
2012-
2013-
def _strict_isrealfromline(self, line):
2014-
if not self._regexp:
2015-
import re
2016-
self._regexp = re.compile(self._fromlinepattern)
2017-
return self._regexp.match(line)
2018-
2019-
def _portable_isrealfromline(self, line):
2020-
return True
2021-
2022-
_isrealfromline = _strict_isrealfromline
2023-
2024-
2025-
class PortableUnixMailbox(UnixMailbox):
2026-
_isrealfromline = UnixMailbox._portable_isrealfromline
2027-
2028-
2029-
class MmdfMailbox(_Mailbox):
2030-
2031-
def _search_start(self):
2032-
while 1:
2033-
line = self.fp.readline()
2034-
if not line:
2035-
raise EOFError
2036-
if line[:5] == '\001\001\001\001\n':
2037-
return
2038-
2039-
def _search_end(self):
2040-
while 1:
2041-
pos = self.fp.tell()
2042-
line = self.fp.readline()
2043-
if not line:
2044-
return
2045-
if line == '\001\001\001\001\n':
2046-
self.fp.seek(pos)
2047-
return
2048-
2049-
2050-
class MHMailbox:
2051-
2052-
def __init__(self, dirname, factory=rfc822.Message):
2053-
import re
2054-
pat = re.compile('^[1-9][0-9]*$')
2055-
self.dirname = dirname
2056-
# the three following lines could be combined into:
2057-
# list = map(long, filter(pat.match, os.listdir(self.dirname)))
2058-
list = os.listdir(self.dirname)
2059-
list = filter(pat.match, list)
2060-
list = map(int, list)
2061-
list.sort()
2062-
# This only works in Python 1.6 or later;
2063-
# before that str() added 'L':
2064-
self.boxes = map(str, list)
2065-
self.boxes.reverse()
2066-
self.factory = factory
2067-
2068-
def __iter__(self):
2069-
return iter(self.next, None)
2070-
2071-
def next(self):
2072-
if not self.boxes:
2073-
return None
2074-
fn = self.boxes.pop()
2075-
fp = open(os.path.join(self.dirname, fn), newline='')
2076-
msg = self.factory(fp)
2077-
try:
2078-
msg._mh_msgno = fn
2079-
except (AttributeError, TypeError):
2080-
pass
2081-
return msg
2082-
2083-
2084-
class BabylMailbox(_Mailbox):
2085-
2086-
def _search_start(self):
2087-
while 1:
2088-
line = self.fp.readline()
2089-
if not line:
2090-
raise EOFError
2091-
if line == '*** EOOH ***\n':
2092-
return
2093-
2094-
def _search_end(self):
2095-
while 1:
2096-
pos = self.fp.tell()
2097-
line = self.fp.readline()
2098-
if not line:
2099-
return
2100-
if line == '\037\014\n' or line == '\037':
2101-
self.fp.seek(pos)
2102-
return
2103-
2104-
## End: classes from the original module (for backward compatibility).
2105-
21061928

21071929
class Error(Exception):
21081930
"""Raised for module-specific errors."""

Lib/test/test_mailbox.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,20 +1768,7 @@ def test_nonempty_maildir_both(self):
17681768
self.assert_(self.mbox.next() is None)
17691769
self.assert_(self.mbox.next() is None)
17701770

1771-
def test_unix_mbox(self):
1772-
### should be better!
1773-
import email.parser
1774-
fname = self.createMessage("cur", True)
1775-
n = 0
1776-
for msg in mailbox.PortableUnixMailbox(open(fname),
1777-
email.parser.Parser().parse):
1778-
n += 1
1779-
self.assertEqual(msg["subject"], "Simple Test")
1780-
# XXX Disabled until we figure out how to fix this
1781-
##self.assertEqual(len(str(msg)), len(FROM_)+len(DUMMY_MESSAGE))
1782-
self.assertEqual(n, 1)
1783-
1784-
## End: classes from the original module (for backward compatibility).
1771+
## End: tests from the original module (for backward compatibility).
17851772

17861773

17871774
_sample_message = """\

0 commit comments

Comments
 (0)