Ticket #5412: patch_5412.patch

File patch_5412.patch, 3.4 KB (added by moijes12, 8 years ago)

The check for unicode has been removed from _PollableWrite.checkWork. Checks for unicode have been added to _PollableWritePipe.write and _PollableWritePipe.writeSequence. The unit tests have been added to test_pollingfile.TestPollableWritePipe and the test involving checkWork has been removed. Do note that I've created this patch using Eclipse as I'm trying to setup a work environment and if this causes any issues, please let me know. I'll make the required changes.

  • twisted/internet/_pollingfile.py

    ### Eclipse Workspace Patch 1.0
    #P Twisted_5412
     
    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 output buffer.
     242        @type seq:  : A C{sequence}
     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
    240250    def write(self, data):
     251        """
     252        Append data to the output buffer
     253       
     254        @param data: C{str} containing data to be appended to the output buffer
     255        @type data: A C{str}
     256       
     257        @raise TypeError: If the type of data is C{unicode} instead of C{str}
     258        """
     259        if isinstance(data, unicode):
     260            raise TypeError("Unicode not allowed in output buffer")
    241261        if self.disconnecting:
    242262            return
    243263        self.outQueue.append(data)
     
    257277                return numBytesWritten
    258278        while self.outQueue:
    259279            data = self.outQueue.pop(0)
    260             errCode = 0
    261             if isinstance(data, unicode):
    262                 raise TypeError("unicode not allowed")
     280            errCode = 0           
    263281            try:
    264282                errCode, nBytesWritten = win32file.WriteFile(self.writePipe,
    265283                                                             data, None)
  • twisted/internet/test/test_pollingfile.py

     
    1919    """
    2020    Tests for L{_pollingfile._PollableWritePipe}.
    2121    """
    22 
    23     def test_checkWorkUnicode(self):
     22   
     23   
     24    def test_writeUnicode(self):
    2425        """
    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.
     26        L{_pollingfile._PollableWritePipe.write} raises a C{TypeError} if
     27        an attempt is made to append unicode data to the output buffer
    2828        """
    2929        p = _pollingfile._PollableWritePipe(1, lambda: None)
    30         p.write("test")
    31         p.checkWork()
     30        self.assertRaises(TypeError, p.write, u"test")
     31       
     32       
     33    def test_writeSequenceUnicode(self):
     34        """
     35        L{_pollingfile._PollableWritePipe.writeSequence} raises a C{TypeError} if
     36        unicode data is part of the data sequence to be appended to the output
     37        buffer         
     38        """
     39        p = _pollingfile._PollableWritePipe(1, lambda: None)
     40        self.assertRaises(TypeError, p.writeSequence, [u"test"])
     41        self.assertRaises(TypeError, p.writeSequence, (u"test",))
     42        self.assertRaises(TypeError, p.writeSequence, {u"test":"foo",1:"bar"})
    3243
    33         p.write(u"test")
    34         self.assertRaises(TypeError, p.checkWork)
    3544
    36 
    37 
    3845if _pollingfile is None:
    3946    TestPollableWritePipe.skip = "_pollingfile is only avalable under Windows."