Ticket #2491: 2491-1.patch

File 2491-1.patch, 4.0 KB (added by Michael, 6 years ago)
  • twisted/internet/tcp.py

    diff --git twisted/internet/tcp.py twisted/internet/tcp.py
    index 222ae9b..9fefe6b 100644
    import types 
    1717import socket
    1818import sys
    1919import operator
     20import warnings
    2021
    2122from zope.interface import implements, classImplements
    2223
    except ImportError: 
    2627    SSL = None
    2728
    2829from twisted.python.runtime import platformType
    29 
     30from twisted.python import versions, deprecate
    3031
    3132if platformType == 'win32':
    3233    # no such thing as WSAEPERM or error code 10001 according to winsock.h or MSDN
    class Connection(abstract.FileDescriptor, _SocketCloser): 
    457458                return main.CONNECTION_LOST
    458459        if not data:
    459460            return main.CONNECTION_DONE
    460         return self.protocol.dataReceived(data)
    461 
     461        rval = self.protocol.dataReceived(data)
     462        if rval:
     463            warningFormat = 'Returning a value other than None from %(fqpn)s was deprecated in %(version)s'
     464            warningString = deprecate.getDeprecationWarningString(self.protocol.dataReceived,
     465                                                                  versions.Version('twisted', 11, 0, 0),
     466                                                                  format = warningFormat)
     467            warnings.warn(warningString, category=DeprecationWarning, stacklevel=2)
     468        return rval
    462469
    463470    def writeSomeData(self, data):
    464471        """
  • twisted/internet/test/test_tcp.py

    diff --git twisted/internet/test/test_tcp.py twisted/internet/test/test_tcp.py
    index 19f861d..1d162aa 100644
    from twisted.internet.protocol import ServerFactory, ClientFactory, Protocol 
    2222from twisted.python.runtime import platform
    2323from twisted.python.failure import Failure
    2424from twisted.python import log
    25 from twisted.trial.unittest import SkipTest
     25from twisted.trial.unittest import SkipTest, TestCase
     26from twisted.internet.tcp import Connection
    2627
    2728from twisted.test.test_tcp import ClosingProtocol
    2829from twisted.internet.test.test_core import ObjectModelIntegrationMixin
    def serverFactoryFor(protocol): 
    102103    return factory
    103104
    104105
     106class FakeSocket(object):
     107    """
     108    A Fake Socket object
     109    """
     110    fileno = 1
     111   
     112    def __init__(self, data):
     113        self.data = data
     114
     115    def setblocking(self, blocking):
     116        self.blocking = blocking
     117
     118    def recv(self, size):
     119        return self.data
     120
     121
     122
     123class TestFakeSocket(TestCase):
     124    """
     125    Test that the FakeSocket can be used by the doRead method of L{Connection}
     126    """
     127
     128    def test_blocking(self):
     129        skt = FakeSocket("someData")
     130        skt.setblocking(0)
     131        self.assertEquals(skt.blocking, 0)
     132
     133
     134    def test_recv(self):
     135        skt = FakeSocket("someData")
     136        self.assertEquals(skt.recv(10), "someData")
     137
     138
     139
     140class FakeProtocol(Protocol):
     141    """
     142    An L{IProtocol} that returns a value from its dataReceived method.
     143    """
     144    def dataReceived(self, data):
     145        return "Returning something I shouldn't"
     146
     147
     148
     149class TestTCPConnection(TestCase):
     150    """
     151    Test that an L{IProtocol} that returns a value from its dataReceived method raises a deprecated warning
     152    """
     153    def test_doReadWarningIsRaised(self):
     154        skt = FakeSocket("someData")
     155        protocol = FakeProtocol()
     156        conn = Connection(skt, protocol)
     157        conn.doRead()
     158        warnings = self.flushWarnings([self.test_doReadWarningIsRaised])
     159        self.assertEquals(warnings[0]['category'], DeprecationWarning)
     160        self.assertEquals(
     161            warnings[0]["message"],
     162            "Returning a value other than None from twisted.internet.test.test_tcp.FakeProtocol.dataReceived was deprecated in twisted 11.0.0")
     163        self.assertEquals(len(warnings), 1)
     164
     165
     166
    105167
    106168class TCPClientTestsBuilder(ReactorBuilder):
    107169    """
  • new file twisted/topfiles/2491.removal

    diff --git twisted/topfiles/2491.removal twisted/topfiles/2491.removal
    new file mode 100644
    index 0000000..cee4baf
    - +  
     1returning a value other than None from IProtocol.dataReceived was deprecated