Ticket #5412: 5412.patch

File 5412.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 twisted.intenet._pollingfile._PollableWritePipe.write and twisted.internet._pollingfile._PollableWritePipe.writeSequence instead of twisted.internet._pollingfile._PollableWritePipe.checkWork.
  • twisted/internet/_pollingfile.py

     
    235235        self.lostCallback()
    236236
    237237    def writeSequence(self, seq):
     238        """
     239        Append a data sequence to the output buffer.
     240
     241        @param seq: C{sequence} containing the data to be appended to the
     242                    output buffer.
     243        @type seq: C{sequence}.
     244
     245        @raise TypeError: If seq contains C{unicode}.
     246        """
     247        if unicode in map(type, seq):
     248            raise TypeError("Unicode not allowed in output buffer.")
    238249        self.outQueue.extend(seq)
    239250
    240251    def write(self, data):
     252        """
     253        Append data to the output buffer.
     254
     255        @param data: C{str} containing data to be appended to the output buffer
     256                     .
     257        @type data: C{str}.
     258
     259        @raise TypeError: If the type of data is C{unicode} instead of C{str}.
     260        """
     261        if isinstance(data, unicode):
     262            raise TypeError("Unicode not allowed in output buffer.")
    241263        if self.disconnecting:
    242264            return
    243265        self.outQueue.append(data)
     
    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."