Ticket #5974: fdesc.diff

File fdesc.diff, 6.5 KB (added by itamar, 3 years ago)

Port fdesc; depends on #5973

  • twisted/internet/fdesc.py

     
    1616
    1717# twisted imports
    1818from twisted.internet.main import CONNECTION_LOST, CONNECTION_DONE
    19 from twisted.python.runtime import platformType
    2019
     20
    2121def setNonBlocking(fd):
    2222    """
    2323    Make a file descriptor non-blocking.
     
    8484    """
    8585    try:
    8686        output = os.read(fd, 8192)
    87     except (OSError, IOError), ioe:
     87    except (OSError, IOError) as ioe:
    8888        if ioe.args[0] in (errno.EAGAIN, errno.EINTR):
    8989            return
    9090        else:
     
    109109    """
    110110    try:
    111111        return os.write(fd, data)
    112     except (OSError, IOError), io:
     112    except (OSError, IOError) as io:
    113113        if io.errno in (errno.EAGAIN, errno.EINTR):
    114114            return 0
    115115        return CONNECTION_LOST
  • twisted/test/test_fdesc.py

     
    1515else:
    1616    from twisted.internet import fdesc
    1717
    18 from twisted.python.util import untilConcludes
     18from twisted.python._utilpy3 import untilConcludes
    1919from twisted.trial import unittest
    2020
    2121
    22 class ReadWriteTestCase(unittest.TestCase):
     22
     23class NonBlockingTestCase(unittest.SynchronousTestCase):
    2324    """
     25    Tests for L{fdesc.setNonBlocking} and L{fdesc.setBlocking}.
     26    """
     27
     28    def test_setNonBlocking(self):
     29        """
     30        L{fdesc.setNonBlocking} sets a file descriptor to non-blocking.
     31        """
     32        r, w = os.pipe()
     33        self.addCleanup(os.close, r)
     34        self.addCleanup(os.close, w)
     35        self.assertFalse(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK)
     36        fdesc.setNonBlocking(r)
     37        self.assertTrue(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK)
     38
     39
     40    def test_setBlocking(self):
     41        """
     42        L{fdesc.setBlocking} sets a file descriptor to blocking.
     43        """
     44        r, w = os.pipe()
     45        self.addCleanup(os.close, r)
     46        self.addCleanup(os.close, w)
     47        fdesc.setNonBlocking(r)
     48        fdesc.setBlocking(r)
     49        self.assertFalse(fcntl.fcntl(r, fcntl.F_GETFL) & os.O_NONBLOCK)
     50
     51
     52
     53class ReadWriteTestCase(unittest.SynchronousTestCase):
     54    """
    2455    Tests for fdesc.readFromFD, fdesc.writeToFD.
    2556    """
    2657
     
    6495            if l:
    6596                return l[0]
    6697            else:
    67                 return ""
     98                return b""
    6899        else:
    69100            return res
    70101
     
    74105        Test that the number of bytes L{fdesc.writeToFD} reports as written
    75106        with its return value are seen by L{fdesc.readFromFD}.
    76107        """
    77         n = self.write("hello")
     108        n = self.write(b"hello")
    78109        self.failUnless(n > 0)
    79110        s = self.read()
    80111        self.assertEqual(len(s), n)
    81         self.assertEqual("hello"[:n], s)
     112        self.assertEqual(b"hello"[:n], s)
    82113
    83114
    84115    def test_writeAndReadLarge(self):
     
    86117        Similar to L{test_writeAndRead}, but use a much larger string to verify
    87118        the behavior for that case.
    88119        """
    89         orig = "0123456879" * 10000
     120        orig = b"0123456879" * 10000
    90121        written = self.write(orig)
    91122        self.failUnless(written > 0)
    92123        result = []
     
    97128            resultlength += len(result[-1])
    98129            # Increment a counter to be sure we'll exit at some point
    99130            i += 1
    100         result = "".join(result)
     131        result = b"".join(result)
    101132        self.assertEqual(len(result), written)
    102133        self.assertEqual(orig[:written], result)
    103134
     
    128159        results in a connection lost indicator.
    129160        """
    130161        os.close(self.r)
    131         self.assertEqual(self.write("s"), fdesc.CONNECTION_LOST)
     162        self.assertEqual(self.write(b"s"), fdesc.CONNECTION_LOST)
    132163
    133164
    134165    def test_readFromInvalid(self):
     
    146177        closed results in a connection lost indicator.
    147178        """
    148179        os.close(self.w)
    149         self.assertEqual(self.write("s"), fdesc.CONNECTION_LOST)
     180        self.assertEqual(self.write(b"s"), fdesc.CONNECTION_LOST)
    150181
    151182
    152183    def test_writeErrors(self):
     
    160191            raise err
    161192        os.write = eagainWrite
    162193        try:
    163             self.assertEqual(self.write("s"), 0)
     194            self.assertEqual(self.write(b"s"), 0)
    164195        finally:
    165196            os.write = oldOsWrite
    166197
     
    170201            raise err
    171202        os.write = eintrWrite
    172203        try:
    173             self.assertEqual(self.write("s"), 0)
     204            self.assertEqual(self.write(b"s"), 0)
    174205        finally:
    175206            os.write = oldOsWrite
    176207
    177208
    178209
    179 class CloseOnExecTests(unittest.TestCase):
     210class CloseOnExecTests(unittest.SynchronousTestCase):
    180211    """
    181212    Tests for L{fdesc._setCloseOnExec} and L{fdesc._unsetCloseOnExec}.
    182213    """
    183214    program = '''
    184215import os, errno
    185216try:
    186     os.write(%d, 'lul')
    187 except OSError, e:
     217    os.write(%d, b'lul')
     218except OSError as e:
    188219    if e.errno == errno.EBADF:
    189220        os._exit(0)
    190221    os._exit(5)
     
    215246        by a new process image created with one of the exec family of
    216247        functions.
    217248        """
    218         fObj = file(self.mktemp(), 'w')
     249        fObj = open(self.mktemp(), 'wb')
    219250        fdesc._setCloseOnExec(fObj.fileno())
    220251        status = self._execWithFileDescriptor(fObj)
    221252        self.assertTrue(os.WIFEXITED(status))
     
    227258        A file descriptor passed to L{fdesc._unsetCloseOnExec} is inherited by
    228259        a new process image created with one of the exec family of functions.
    229260        """
    230         fObj = file(self.mktemp(), 'w')
     261        fObj = open(self.mktemp(), 'wb')
    231262        fdesc._setCloseOnExec(fObj.fileno())
    232263        fdesc._unsetCloseOnExec(fObj.fileno())
    233264        status = self._execWithFileDescriptor(fObj)
  • admin/_twistedpython3.py

     
    2222    "twisted.internet",
    2323    "twisted.internet.defer",
    2424    "twisted.internet.interfaces",
     25    "twisted.internet.fdesc",
    2526    "twisted.internet.main",
    2627    "twisted.internet.test",
    2728    "twisted.internet.test.reactormixins",
     
    6768    "twisted.test.test_context",
    6869    "twisted.test.test_defer",
    6970    "twisted.test.test_failure",
     71    "twisted.test.test_fdesc",
    7072    "twisted.test.test_log",
    7173    "twisted.test.test_monkey",
    7274    "twisted.test.test_paths",