Ticket #5852: 5852.9.patch

File 5852.9.patch, 4.3 KB (added by Richard Wall, 9 years ago)

Now with a getattr method as suggested by tomprince

  • twisted/mail/smtp.py

    === modified file 'twisted/mail/smtp.py'
     
    88
    99import time, re, base64, types, socket, os, random, rfc822
    1010import binascii
     11import warnings
    1112from email.base64MIME import encode as encode_base64
    1213
    1314from zope.interface import implements, Interface
     
    984985class SMTPClient(basic.LineReceiver, policies.TimeoutMixin):
    985986    """
    986987    SMTP client for sending emails.
    987    
     988
    988989    After the client has connected to the SMTP server, it repeatedly calls
    989990    L{SMTPClient.getMailFrom}, L{SMTPClient.getMailTo} and
    990991    L{SMTPClient.getMailData} and uses this information to send an email.
     
    12341235    requireTransportSecurity = False
    12351236
    12361237    # Indicate whether or not our transport can be considered secure.
    1237     tlsMode = False
     1238    _tlsMode = False
    12381239
    12391240    # ClientContextFactory to use for STARTTLS
    12401241    context = None
     
    12461247        self.context = contextFactory
    12471248
    12481249
     1250    def __getattr__(self, name):
     1251        if name == "tlsMode":
     1252            warnings.warn(
     1253                "tlsMode attribute of twisted.mail.smtp.ESMTPClient "
     1254                "is deprecated since Twisted 13.0",
     1255                category=DeprecationWarning, stacklevel=2)
     1256            return self._tlsMode
     1257        else:
     1258            raise AttributeError(
     1259                'ESMTPClient instance has no attribute %r' % (name,))
     1260
     1261
     1262    def __setattr__(self, name, value):
     1263        if name == "tlsMode":
     1264            warnings.warn(
     1265                "tlsMode attribute of twisted.mail.smtp.ESMTPClient "
     1266                "is deprecated since Twisted 13.0",
     1267                category=DeprecationWarning, stacklevel=2)
     1268            self._tlsMode = value
     1269        else:
     1270            self.__dict__[name] = value
     1271
     1272
    12491273    def esmtpEHLORequired(self, code=-1, resp=None):
    12501274        self.sendError(EHLORequiredError(502, "Server does not support ESMTP Authentication", self.log.str()))
    12511275
     
    16841708
    16851709class SMTPSender(SenderMixin, SMTPClient):
    16861710    """
    1687     SMTP protocol that sends a single email based on information it 
     1711    SMTP protocol that sends a single email based on information it
    16881712    gets from its factory, a L{SMTPSenderFactory}.
    16891713    """
    16901714
  • twisted/mail/test/test_smtp.py

    === modified file 'twisted/mail/test/test_smtp.py'
     
    16111611
    16121612        # The client give up
    16131613        self.assertEqual("QUIT\r\n", transport.value())
     1614
     1615
     1616    def test_esmtpClientTlsModeDeprecationGet(self):
     1617        """
     1618        L{smtp.ESMTPClient.tlsMode} is deprecated.
     1619        """
     1620        val = self.clientProtocol.tlsMode
     1621        del val
     1622        warningsShown = self.flushWarnings(
     1623            offendingFunctions=[self.test_esmtpClientTlsModeDeprecationGet])
     1624        self.assertEqual(len(warningsShown), 1)
     1625        self.assertIdentical(
     1626            warningsShown[0]['category'], DeprecationWarning)
     1627        self.assertEqual(
     1628            warningsShown[0]['message'],
     1629            "tlsMode attribute of twisted.mail.smtp.ESMTPClient "
     1630            "is deprecated since Twisted 13.0")
     1631
     1632
     1633    def test_esmtpClientTlsModeDeprecationGetAttributeError(self):
     1634        """
     1635        L{smtp.ESMTPClient.__getattr__} raises an attribute error for other
     1636        attribute names which do not exist.
     1637        """
     1638        self.assertRaises(
     1639            AttributeError, lambda: self.clientProtocol.doesNotExist)
     1640
     1641
     1642    def test_esmtpClientTlsModeDeprecationSet(self):
     1643        """
     1644        L{smtp.ESMTPClient.tlsMode} is deprecated.
     1645        """
     1646        self.clientProtocol.tlsMode = False
     1647        warningsShown = self.flushWarnings(
     1648            offendingFunctions=[self.test_esmtpClientTlsModeDeprecationSet])
     1649        self.assertEqual(len(warningsShown), 1)
     1650        self.assertIdentical(
     1651            warningsShown[0]['category'], DeprecationWarning)
     1652        self.assertEqual(
     1653            warningsShown[0]['message'],
     1654            "tlsMode attribute of twisted.mail.smtp.ESMTPClient "
     1655            "is deprecated since Twisted 13.0")
  • twisted/mail/topfiles/5852.removal

    === added file 'twisted/mail/topfiles/5852.removal'
     
     1tlsMode attribute of twisted.mail.smtp.ESMTPClient is deprecated.