Ticket #3050: linereceiver_depth.diff

File linereceiver_depth.diff, 4.3 KB (added by ghazel, 14 years ago)

diff of the LineReceiver patch and unit test

  • twisted/protocols/basic.py

     
    213213        Translates bytes into lines, and calls lineReceived (or
    214214        rawDataReceived, depending on mode.)
    215215        """
    216         self.__buffer = self.__buffer+data
    217         while self.line_mode and not self.paused:
    218             try:
    219                 line, self.__buffer = self.__buffer.split(self.delimiter, 1)
    220             except ValueError:
    221                 if len(self.__buffer) > self.MAX_LENGTH:
    222                     line, self.__buffer = self.__buffer, ''
    223                     return self.lineLengthExceeded(line)
    224                 break
     216        self.__buffer += data
     217        while self.__buffer and not self.paused:
     218            if self.line_mode:
     219                try:
     220                    line, self.__buffer = self.__buffer.split(self.delimiter, 1)
     221                except ValueError:
     222                    if len(self.__buffer) > self.MAX_LENGTH:
     223                        line, self.__buffer = self.__buffer, ''
     224                        return self.lineLengthExceeded(line)
     225                    return
     226                else:
     227                    linelength = len(line)
     228                    if linelength > self.MAX_LENGTH:
     229                        exceeded = line + self.__buffer
     230                        self.__buffer = ''
     231                        return self.lineLengthExceeded(exceeded)
     232                    why = self.lineReceived(line)
     233                    if why or self.transport and self.transport.disconnecting:
     234                        return why
    225235            else:
    226                 linelength = len(line)
    227                 if linelength > self.MAX_LENGTH:
    228                     exceeded = line + self.__buffer
    229                     self.__buffer = ''
    230                     return self.lineLengthExceeded(exceeded)
    231                 why = self.lineReceived(line)
    232                 if why or self.transport and self.transport.disconnecting:
     236                data = self.__buffer
     237                self.__buffer = ''
     238                why = self.rawDataReceived(data)
     239                if why:
    233240                    return why
    234         else:
    235             if not self.paused:
    236                 data=self.__buffer
    237                 self.__buffer=''
    238                 if data:
    239                     return self.rawDataReceived(data)
    240241
    241242    def setLineMode(self, extra=''):
    242243        """Sets the line-mode of this receiver.
     
    250251        within a lineReceived callback.
    251252        """
    252253        self.line_mode = 1
    253         if extra:
    254             return self.dataReceived(extra)
     254        self.__buffer += extra
    255255
    256256    def setRawMode(self):
    257257        """Sets the raw mode of this receiver.
  • twisted/test/test_protocols.py

     
    1111from twisted.internet import reactor, protocol, defer, task, error
    1212from twisted.test import proto_helpers
    1313
     14import sys
    1415import struct
    1516import StringIO
    1617
     
    2324        Do nothing.
    2425        """
    2526
     27class FlippingLineTester(basic.LineReceiver):
     28    """
     29    A line receiver that flips between line and raw data modes after one byte.
     30    """
     31
     32    delimiter = '\n'
     33   
     34    def lineReceived(self, line):
     35        """
     36        Set the mode to raw.
     37        """
     38        self.setRawMode()
     39
     40    def rawDataReceived(self, data):
     41        """
     42        Set the mode back to line.
     43        """
     44        self.setLineMode(data[1:])
     45
     46
    2647class LineTester(basic.LineReceiver):
    2748    """
    2849    A line receiver that parses data received and make actions on some tokens.
     
    279300        self.assertEquals(a.received,
    280301                          ['produce', 'hello world', 'unproduce', 'goodbye'])
    281302
     303    def testStackRecursion(self):
     304        """
     305        Test switching modes many times on the same data.
     306        """
     307        a = FlippingLineTester()
     308        t = StringIOWithoutClosing()
     309        a.makeConnection(protocol.FileWrapper(t))
     310        a.dataReceived('x\nx' * sys.getrecursionlimit())
    282311
     312
    283313class LineOnlyReceiverTestCase(unittest.TestCase):
    284314    """
    285315    Test line only receiveer.