Ticket #3050: linereceiver_depth.2.diff

File linereceiver_depth.2.diff, 4.7 KB (added by ghazel, 11 years ago)

Fixed unittest, and added News file

  • twisted/test/test_protocols.py

     
    55Test cases for twisted.protocols package.
    66"""
    77
     8import sys
    89import struct
    910
    1011from twisted.trial import unittest
     
    1314from twisted.test import proto_helpers
    1415
    1516
     17class FlippingLineTester(basic.LineReceiver):
     18    """
     19    A line receiver that flips between line and raw data modes after one byte.
     20    """
     21
     22    delimiter = '\n'
     23   
     24    def lineReceived(self, line):
     25        """
     26        Set the mode to raw.
     27        """
     28        self.setRawMode()
     29
     30    def rawDataReceived(self, data):
     31        """
     32        Set the mode back to line.
     33        """
     34        self.setLineMode(data[1:])
     35
     36
    1637class LineTester(basic.LineReceiver):
    1738    """
    1839    A line receiver that parses data received and make actions on some tokens.
     
    195216              'len 20', 'foo 123', '0123456789\n012345678',
    196217              'len 0', 'foo 5', '', '67890', 'len 1', 'a']
    197218
    198     def testBuffer(self):
     219    def test_buffer(self):
    199220        """
    200221        Test buffering for different packet size, checking received matches
    201222        expected data.
     
    309330        self.assertEqual(protocol.rest, '')
    310331
    311332
     333    def test_stackRecursion(self):
     334        """
     335        Test switching modes many times on the same data.
     336        """
     337        a = FlippingLineTester()
     338        t = proto_helpers.StringIOWithoutClosing()
     339        a.makeConnection(protocol.FileWrapper(t))
     340        a.dataReceived('x\nx' * sys.getrecursionlimit())
    312341
     342
    313343class LineOnlyReceiverTestCase(unittest.TestCase):
    314344    """
    315345    Test line only receiveer.
  • twisted/protocols/basic.py

     
    537537        Translates bytes into lines, and calls lineReceived (or
    538538        rawDataReceived, depending on mode.)
    539539        """
    540         self.__buffer = self.__buffer+data
    541         while self.line_mode and not self.paused:
    542             try:
    543                 line, self.__buffer = self.__buffer.split(self.delimiter, 1)
    544             except ValueError:
    545                 if len(self.__buffer) > self.MAX_LENGTH:
    546                     line, self.__buffer = self.__buffer, ''
    547                     return self.lineLengthExceeded(line)
    548                 break
     540        self.__buffer += data
     541        while self.__buffer and not self.paused:
     542            if self.line_mode:
     543                try:
     544                    line, self.__buffer = self.__buffer.split(self.delimiter, 1)
     545                except ValueError:
     546                    if len(self.__buffer) > self.MAX_LENGTH:
     547                        line, self.__buffer = self.__buffer, ''
     548                        return self.lineLengthExceeded(line)
     549                    return
     550                else:
     551                    linelength = len(line)
     552                    if linelength > self.MAX_LENGTH:
     553                        exceeded = line + self.__buffer
     554                        self.__buffer = ''
     555                        return self.lineLengthExceeded(exceeded)
     556                    why = self.lineReceived(line)
     557                    if why or self.transport and self.transport.disconnecting:
     558                        return why
    549559            else:
    550                 linelength = len(line)
    551                 if linelength > self.MAX_LENGTH:
    552                     exceeded = line + self.__buffer
    553                     self.__buffer = ''
    554                     return self.lineLengthExceeded(exceeded)
    555                 why = self.lineReceived(line)
    556                 if why or self.transport and self.transport.disconnecting:
     560                data = self.__buffer
     561                self.__buffer = ''
     562                why = self.rawDataReceived(data)
     563                if why:
    557564                    return why
    558         else:
    559             if not self.paused:
    560                 data=self.__buffer
    561                 self.__buffer=''
    562                 if data:
    563                     return self.rawDataReceived(data)
    564565
    565566
    566567    def setLineMode(self, extra=''):
     
    576577        within a lineReceived callback.
    577578        """
    578579        self.line_mode = 1
    579         if extra:
    580             return self.dataReceived(extra)
     580        self.__buffer += extra
    581581
    582582
    583583    def setRawMode(self):
  • twisted/topfiles/3050.bugfix

     
     1twisted.protocols.basic.LineReceiver now does not hit the maximum stack recursion depth when the line and data mode is switched many times.