gh-93243: Make smtpd private before porting its users (GH-93246)

gh-93243

This PR is required to reduce diffs of the following porting (no need to either maintain documentation and tests consistent with each porting step, or try to port everything and remove smtpd in a single PR).

Automerge-Triggered-By: GH:warsaw
This commit is contained in:
Oleg Iarygin 2022-08-06 03:41:29 +03:00 committed by GitHub
parent 29650fea96
commit 56d16e8cb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 23 additions and 1312 deletions

View file

@ -147,6 +147,3 @@ Legacy API:
Module :mod:`mailbox`
Tools for creating, reading, and managing collections of messages on disk
using a variety standard formats.
Module :mod:`smtpd`
SMTP server framework (primarily useful for testing)

View file

@ -1,268 +0,0 @@
:mod:`smtpd` --- SMTP Server
============================
.. module:: smtpd
:synopsis: A SMTP server implementation in Python.
:deprecated:
.. moduleauthor:: Barry Warsaw <barry@python.org>
.. sectionauthor:: Moshe Zadka <moshez@moshez.org>
**Source code:** :source:`Lib/smtpd.py`
--------------
This module offers several classes to implement SMTP (email) servers.
.. deprecated-removed:: 3.6 3.12
The :mod:`smtpd` module is deprecated
(see :pep:`PEP 594 <594#smtpd>` for details).
The `aiosmtpd <https://aiosmtpd.readthedocs.io/>`_ package is a recommended
replacement for this module. It is based on :mod:`asyncio` and provides a
more straightforward API.
Several server implementations are present; one is a generic
do-nothing implementation, which can be overridden, while the other two offer
specific mail-sending strategies.
Additionally the SMTPChannel may be extended to implement very specific
interaction behaviour with SMTP clients.
The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE and :rfc:`6531`
SMTPUTF8 extensions.
.. include:: ../includes/wasm-notavail.rst
SMTPServer Objects
------------------
.. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432,\
map=None, enable_SMTPUTF8=False, decode_data=False)
Create a new :class:`SMTPServer` object, which binds to local address
*localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. Both
*localaddr* and *remoteaddr* should be a :ref:`(host, port) <host_port>`
tuple. The object inherits from :class:`asyncore.dispatcher`, and so will
insert itself into :mod:`asyncore`'s event loop on instantiation.
*data_size_limit* specifies the maximum number of bytes that will be
accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no
limit.
*map* is the socket map to use for connections (an initially empty
dictionary is a suitable value). If not specified the :mod:`asyncore`
global socket map is used.
*enable_SMTPUTF8* determines whether the ``SMTPUTF8`` extension (as defined
in :RFC:`6531`) should be enabled. The default is ``False``.
When ``True``, ``SMTPUTF8`` is accepted as a parameter to the ``MAIL``
command and when present is passed to :meth:`process_message` in the
``kwargs['mail_options']`` list. *decode_data* and *enable_SMTPUTF8*
cannot be set to ``True`` at the same time.
*decode_data* specifies whether the data portion of the SMTP transaction
should be decoded using UTF-8. When *decode_data* is ``False`` (the
default), the server advertises the ``8BITMIME``
extension (:rfc:`6152`), accepts the ``BODY=8BITMIME`` parameter to
the ``MAIL`` command, and when present passes it to :meth:`process_message`
in the ``kwargs['mail_options']`` list. *decode_data* and *enable_SMTPUTF8*
cannot be set to ``True`` at the same time.
.. method:: process_message(peer, mailfrom, rcpttos, data, **kwargs)
Raise a :exc:`NotImplementedError` exception. Override this in subclasses to
do something useful with this message. Whatever was passed in the
constructor as *remoteaddr* will be available as the :attr:`_remoteaddr`
attribute. *peer* is the remote host's address, *mailfrom* is the envelope
originator, *rcpttos* are the envelope recipients and *data* is a string
containing the contents of the e-mail (which should be in :rfc:`5321`
format).
If the *decode_data* constructor keyword is set to ``True``, the *data*
argument will be a unicode string. If it is set to ``False``, it
will be a bytes object.
*kwargs* is a dictionary containing additional information. It is empty
if ``decode_data=True`` was given as an init argument, otherwise
it contains the following keys:
*mail_options*:
a list of all received parameters to the ``MAIL``
command (the elements are uppercase strings; example:
``['BODY=8BITMIME', 'SMTPUTF8']``).
*rcpt_options*:
same as *mail_options* but for the ``RCPT`` command.
Currently no ``RCPT TO`` options are supported, so for now
this will always be an empty list.
Implementations of ``process_message`` should use the ``**kwargs``
signature to accept arbitrary keyword arguments, since future feature
enhancements may add keys to the kwargs dictionary.
Return ``None`` to request a normal ``250 Ok`` response; otherwise
return the desired response string in :RFC:`5321` format.
.. attribute:: channel_class
Override this in subclasses to use a custom :class:`SMTPChannel` for
managing SMTP clients.
.. versionadded:: 3.4
The *map* constructor argument.
.. versionchanged:: 3.5
*localaddr* and *remoteaddr* may now contain IPv6 addresses.
.. versionadded:: 3.5
The *decode_data* and *enable_SMTPUTF8* constructor parameters, and the
*kwargs* parameter to :meth:`process_message` when *decode_data* is
``False``.
.. versionchanged:: 3.6
*decode_data* is now ``False`` by default.
DebuggingServer Objects
-----------------------
.. class:: DebuggingServer(localaddr, remoteaddr)
Create a new debugging server. Arguments are as per :class:`SMTPServer`.
Messages will be discarded, and printed on stdout.
PureProxy Objects
-----------------
.. class:: PureProxy(localaddr, remoteaddr)
Create a new pure proxy server. Arguments are as per :class:`SMTPServer`.
Everything will be relayed to *remoteaddr*. Note that running this has a good
chance to make you into an open relay, so please be careful.
SMTPChannel Objects
-------------------
.. class:: SMTPChannel(server, conn, addr, data_size_limit=33554432,\
map=None, enable_SMTPUTF8=False, decode_data=False)
Create a new :class:`SMTPChannel` object which manages the communication
between the server and a single SMTP client.
*conn* and *addr* are as per the instance variables described below.
*data_size_limit* specifies the maximum number of bytes that will be
accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no
limit.
*enable_SMTPUTF8* determines whether the ``SMTPUTF8`` extension (as defined
in :RFC:`6531`) should be enabled. The default is ``False``.
*decode_data* and *enable_SMTPUTF8* cannot be set to ``True`` at the same
time.
A dictionary can be specified in *map* to avoid using a global socket map.
*decode_data* specifies whether the data portion of the SMTP transaction
should be decoded using UTF-8. The default is ``False``.
*decode_data* and *enable_SMTPUTF8* cannot be set to ``True`` at the same
time.
To use a custom SMTPChannel implementation you need to override the
:attr:`SMTPServer.channel_class` of your :class:`SMTPServer`.
.. versionchanged:: 3.5
The *decode_data* and *enable_SMTPUTF8* parameters were added.
.. versionchanged:: 3.6
*decode_data* is now ``False`` by default.
The :class:`SMTPChannel` has the following instance variables:
.. attribute:: smtp_server
Holds the :class:`SMTPServer` that spawned this channel.
.. attribute:: conn
Holds the socket object connecting to the client.
.. attribute:: addr
Holds the address of the client, the second value returned by
:func:`socket.accept <socket.socket.accept>`
.. attribute:: received_lines
Holds a list of the line strings (decoded using UTF-8) received from
the client. The lines have their ``"\r\n"`` line ending translated to
``"\n"``.
.. attribute:: smtp_state
Holds the current state of the channel. This will be either
:attr:`COMMAND` initially and then :attr:`DATA` after the client sends
a "DATA" line.
.. attribute:: seen_greeting
Holds a string containing the greeting sent by the client in its "HELO".
.. attribute:: mailfrom
Holds a string containing the address identified in the "MAIL FROM:" line
from the client.
.. attribute:: rcpttos
Holds a list of strings containing the addresses identified in the
"RCPT TO:" lines from the client.
.. attribute:: received_data
Holds a string containing all of the data sent by the client during the
DATA state, up to but not including the terminating ``"\r\n.\r\n"``.
.. attribute:: fqdn
Holds the fully qualified domain name of the server as returned by
:func:`socket.getfqdn`.
.. attribute:: peer
Holds the name of the client peer as returned by ``conn.getpeername()``
where ``conn`` is :attr:`conn`.
The :class:`SMTPChannel` operates by invoking methods named ``smtp_<command>``
upon reception of a command line from the client. Built into the base
:class:`SMTPChannel` class are methods for handling the following commands
(and responding to them appropriately):
======== ===================================================================
Command Action taken
======== ===================================================================
HELO Accepts the greeting from the client and stores it in
:attr:`seen_greeting`. Sets server to base command mode.
EHLO Accepts the greeting from the client and stores it in
:attr:`seen_greeting`. Sets server to extended command mode.
NOOP Takes no action.
QUIT Closes the connection cleanly.
MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as
:attr:`mailfrom`. In extended command mode, accepts the
:rfc:`1870` SIZE attribute and responds appropriately based on the
value of *data_size_limit*.
RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in
the :attr:`rcpttos` list.
RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and
:attr:`received_data`, but not the greeting.
DATA Sets the internal state to :attr:`DATA` and stores remaining lines
from the client in :attr:`received_data` until the terminator
``"\r\n.\r\n"`` is received.
HELP Returns minimal information on command syntax
VRFY Returns code 252 (the server doesn't know if the address is valid)
EXPN Reports that the command is not implemented.
======== ===================================================================

View file

@ -27,7 +27,6 @@ backwards compatibility. They have been superseded by other modules.
optparse.rst
ossaudiodev.rst
pipes.rst
smtpd.rst
sndhdr.rst
spwd.rst
sunau.rst

View file

@ -299,6 +299,14 @@ Removed
(and corresponding ``EXPERIMENTAL_ISOLATED_SUBINTERPRETERS``)
have been removed.
* ``smtpd`` has been removed according to the schedule in :pep:`594`,
having been deprecated in Python 3.4.7 and 3.5.4.
Use aiosmtpd_ PyPI module or any other
:mod:`asyncio`-based server instead.
(Contributed by Oleg Iarygin in :gh:`93243`.)
.. _aiosmtpd: https://pypi.org/project/aiosmtpd/
* Remove ``io.OpenWrapper`` and ``_pyio.OpenWrapper``, deprecated in Python
3.10: just use :func:`open` instead. The :func:`open` (:func:`io.open`)
function is a built-in function. Since Python 3.10, :func:`_pyio.open` is
@ -382,6 +390,10 @@ Changes in the Python API
to :term:`filesystem encoding and error handler`.
Argument files should be encoded in UTF-8 instead of ANSI Codepage on Windows.
* Removed the ``asyncore``-based ``smtpd`` module deprecated in Python 3.4.7
and 3.5.4. A recommended replacement is the
:mod:`asyncio`-based aiosmtpd_ PyPI module.
* :func:`shlex.split`: Passing ``None`` for *s* argument now raises an
exception, rather than reading :data:`sys.stdin`. The feature was deprecated
in Python 3.9.

View file

@ -1,4 +1,4 @@
"""Mock socket module used by the smtpd and smtplib tests.
"""Mock socket module used by the smtplib tests.
"""
# imported for _GLOBAL_DEFAULT_TIMEOUT
@ -33,7 +33,7 @@ def close(self):
class MockSocket:
"""Mock socket object used by smtpd and smtplib tests.
"""Mock socket object used by the smtplib tests.
"""
def __init__(self, family=None):
global _reply_data

View file

@ -77,25 +77,16 @@
import time
import socket
import collections
from warnings import _deprecated, warn
from test.support.import_helper import import_module
from warnings import warn
from email._header_value_parser import get_addr_spec, get_angle_addr
__all__ = [
"SMTPChannel", "SMTPServer", "DebuggingServer", "PureProxy",
]
_DEPRECATION_MSG = ('The {name} module is deprecated and unmaintained and will '
'be removed in Python {remove}. Please see aiosmtpd '
'(https://aiosmtpd.readthedocs.io/) for the recommended '
'replacement.')
_deprecated(__name__, _DEPRECATION_MSG, remove=(3, 12))
# These are imported after the above warning so that users get the correct
# deprecation warning.
import asyncore
import asynchat
asyncore = import_module('asyncore', deprecated=True)
asynchat = import_module('asynchat', deprecated=True)
program = sys.argv[0]
__version__ = 'Python SMTP proxy version 0.3'

View file

@ -61,9 +61,10 @@
from socketserver import (ThreadingUDPServer, DatagramRequestHandler,
ThreadingTCPServer, StreamRequestHandler)
with warnings.catch_warnings():
from . import smtpd
asyncore = warnings_helper.import_deprecated('asyncore')
smtpd = warnings_helper.import_deprecated('smtpd')
try:

File diff suppressed because it is too large Load diff

View file

@ -24,9 +24,9 @@
from test.support import warnings_helper
from unittest.mock import Mock
from . import smtpd
asyncore = warnings_helper.import_deprecated('asyncore')
smtpd = warnings_helper.import_deprecated('smtpd')
support.requires_working_socket(module=True)

View file

@ -0,0 +1 @@
The :mod:`smtpd` module was removed per the schedule in :pep:`594`.

View file

@ -642,7 +642,6 @@
<Compile Include="shutil.py" />
<Compile Include="signal.py" />
<Compile Include="site.py" />
<Compile Include="smtpd.py" />
<Compile Include="smtplib.py" />
<Compile Include="sndhdr.py" />
<Compile Include="socket.py" />
@ -760,6 +759,7 @@
<Compile Include="test\sample_doctest_no_doctests.py" />
<Compile Include="test\seq_tests.py" />
<Compile Include="test\signalinterproctester.py" />
<Compile Include="test\smtpd.py" />
<Compile Include="test\sortperf.py" />
<Compile Include="test\ssltests.py" />
<Compile Include="test\ssl_servers.py" />
@ -1261,7 +1261,6 @@
<Compile Include="test\test_signal.py" />
<Compile Include="test\test_site.py" />
<Compile Include="test\test_slice.py" />
<Compile Include="test\test_smtpd.py" />
<Compile Include="test\test_smtplib.py" />
<Compile Include="test\test_smtpnet.py" />
<Compile Include="test\test_sndhdr.py" />

View file

@ -241,7 +241,6 @@ static const char* _Py_stdlib_module_names[] = {
"shutil",
"signal",
"site",
"smtpd",
"smtplib",
"sndhdr",
"socket",

View file

@ -77,7 +77,6 @@
"mailcap.py",
"nntplib.py",
"poplib.py",
"smtpd.py",
"smtplib.py",
"socketserver.py",
"telnetlib.py",