Ticket #5412: 5412.2.patch

File 5412.2.patch, 3.7 KB (added by moijes12, 8 years ago)
  • twisted/topfiles/5412.bugfix

     
     1twisted.internet._pollingfile._PollableWritePipe now checks for outgoing unicode data in write() and writeSequence() instead of checkWork().
  • twisted/internet/_pollingfile.py

     
    234234            pass
    235235        self.lostCallback()
    236236
     237
    237238    def writeSequence(self, seq):
     239        """
     240        Append a sequence to the output buffer.
     241
     242        @param seq: Sequence of bytes to be appended to the output buffer.
     243
     244        @raise TypeError: If seq contains C{unicode}.
     245        """
     246        if unicode in map(type, seq):
     247            raise TypeError("Unicode not allowed in output buffer.")
    238248        self.outQueue.extend(seq)
    239249
     250
    240251    def write(self, data):
     252        """
     253        Append data to the output buffer.
     254
     255        @param data: C{str} to be appended to the output buffer.
     256        @type data: C{str}.
     257
     258        @raise TypeError: If the type of data is C{unicode} instead of C{str}.
     259        """
     260        if isinstance(data, unicode):
     261            raise TypeError("Unicode not allowed in output buffer.")
    241262        if self.disconnecting:
    242263            return
    243264        self.outQueue.append(data)
    244265        if sum(map(len, self.outQueue)) > FULL_BUFFER_SIZE:
    245266            self.bufferFull()
    246267
     268
    247269    def checkWork(self):
    248270        numBytesWritten = 0
    249271        if not self.outQueue:
     
    258280        while self.outQueue:
    259281            data = self.outQueue.pop(0)
    260282            errCode = 0
    261             if isinstance(data, unicode):
    262                 raise TypeError("unicode not allowed")
    263283            try:
    264284                errCode, nBytesWritten = win32file.WriteFile(self.writePipe,
    265285                                                             data, None)
  • twisted/internet/test/test_pollingfile.py

     
    2020    Tests for L{_pollingfile._PollableWritePipe}.
    2121    """
    2222
    23     def test_checkWorkUnicode(self):
     23    def test_writeUnicode(self):
    2424        """
    25         When one tries to pass unicode to L{_pollingfile._PollableWritePipe}, a
    26         C{TypeError} is raised instead of passing the data to C{WriteFile}
    27         call which is going to mangle it.
     25        L{_pollingfile._PollableWritePipe.write} raises a C{TypeError} if an
     26        attempt is made to append unicode data to the output buffer.
    2827        """
    2928        p = _pollingfile._PollableWritePipe(1, lambda: None)
    30         p.write("test")
    31         p.checkWork()
     29        self.assertRaises(TypeError, p.write, u"test")
    3230
    33         p.write(u"test")
    34         self.assertRaises(TypeError, p.checkWork)
    3531
     32    def test_writeSequenceUnicode(self):
     33        """
     34        L{_pollingfile._PollableWritePipe.writeSequence} raises a C{TypeError}
     35        if unicode data is part of the data sequence to be appended to the
     36        output buffer.
     37        """
     38        p = _pollingfile._PollableWritePipe(1, lambda: None)
     39        self.assertRaises(TypeError, p.writeSequence, [u"test"])
     40        self.assertRaises(TypeError, p.writeSequence, (u"test", ))
     41        self.assertRaises(TypeError, p.writeSequence, {u"test":"foo", 1:"bar"})
     42        self.assertRaises(TypeError, p.writeSequence, buffer(u"foo"))
    3643
    3744
     45
    3846if _pollingfile is None:
    3947    TestPollableWritePipe.skip = "Test will run only on Windows."