Ticket #5974: fdesc.diff
| File fdesc.diff, 6.5 KB (added by itamar, 8 months ago) |
|---|
-
twisted/internet/fdesc.py
16 16 17 17 # twisted imports 18 18 from twisted.internet.main import CONNECTION_LOST, CONNECTION_DONE 19 from twisted.python.runtime import platformType20 19 20 21 21 def setNonBlocking(fd): 22 22 """ 23 23 Make a file descriptor non-blocking. … … 84 84 """ 85 85 try: 86 86 output = os.read(fd, 8192) 87 except (OSError, IOError) ,ioe:87 except (OSError, IOError) as ioe: 88 88 if ioe.args[0] in (errno.EAGAIN, errno.EINTR): 89 89 return 90 90 else: … … 109 109 """ 110 110 try: 111 111 return os.write(fd, data) 112 except (OSError, IOError) ,io:112 except (OSError, IOError) as io: 113 113 if io.errno in (errno.EAGAIN, errno.EINTR): 114 114 return 0 115 115 return CONNECTION_LOST -
twisted/test/test_fdesc.py
15 15 else: 16 16 from twisted.internet import fdesc 17 17 18 from twisted.python. utilimport untilConcludes18 from twisted.python._utilpy3 import untilConcludes 19 19 from twisted.trial import unittest 20 20 21 21 22 class ReadWriteTestCase(unittest.TestCase): 22 23 class NonBlockingTestCase(unittest.SynchronousTestCase): 23 24 """ 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 53 class ReadWriteTestCase(unittest.SynchronousTestCase): 54 """ 24 55 Tests for fdesc.readFromFD, fdesc.writeToFD. 25 56 """ 26 57 … … 64 95 if l: 65 96 return l[0] 66 97 else: 67 return ""98 return b"" 68 99 else: 69 100 return res 70 101 … … 74 105 Test that the number of bytes L{fdesc.writeToFD} reports as written 75 106 with its return value are seen by L{fdesc.readFromFD}. 76 107 """ 77 n = self.write( "hello")108 n = self.write(b"hello") 78 109 self.failUnless(n > 0) 79 110 s = self.read() 80 111 self.assertEqual(len(s), n) 81 self.assertEqual( "hello"[:n], s)112 self.assertEqual(b"hello"[:n], s) 82 113 83 114 84 115 def test_writeAndReadLarge(self): … … 86 117 Similar to L{test_writeAndRead}, but use a much larger string to verify 87 118 the behavior for that case. 88 119 """ 89 orig = "0123456879" * 10000120 orig = b"0123456879" * 10000 90 121 written = self.write(orig) 91 122 self.failUnless(written > 0) 92 123 result = [] … … 97 128 resultlength += len(result[-1]) 98 129 # Increment a counter to be sure we'll exit at some point 99 130 i += 1 100 result = "".join(result)131 result = b"".join(result) 101 132 self.assertEqual(len(result), written) 102 133 self.assertEqual(orig[:written], result) 103 134 … … 128 159 results in a connection lost indicator. 129 160 """ 130 161 os.close(self.r) 131 self.assertEqual(self.write( "s"), fdesc.CONNECTION_LOST)162 self.assertEqual(self.write(b"s"), fdesc.CONNECTION_LOST) 132 163 133 164 134 165 def test_readFromInvalid(self): … … 146 177 closed results in a connection lost indicator. 147 178 """ 148 179 os.close(self.w) 149 self.assertEqual(self.write( "s"), fdesc.CONNECTION_LOST)180 self.assertEqual(self.write(b"s"), fdesc.CONNECTION_LOST) 150 181 151 182 152 183 def test_writeErrors(self): … … 160 191 raise err 161 192 os.write = eagainWrite 162 193 try: 163 self.assertEqual(self.write( "s"), 0)194 self.assertEqual(self.write(b"s"), 0) 164 195 finally: 165 196 os.write = oldOsWrite 166 197 … … 170 201 raise err 171 202 os.write = eintrWrite 172 203 try: 173 self.assertEqual(self.write( "s"), 0)204 self.assertEqual(self.write(b"s"), 0) 174 205 finally: 175 206 os.write = oldOsWrite 176 207 177 208 178 209 179 class CloseOnExecTests(unittest. TestCase):210 class CloseOnExecTests(unittest.SynchronousTestCase): 180 211 """ 181 212 Tests for L{fdesc._setCloseOnExec} and L{fdesc._unsetCloseOnExec}. 182 213 """ 183 214 program = ''' 184 215 import os, errno 185 216 try: 186 os.write(%d, 'lul')187 except OSError ,e:217 os.write(%d, b'lul') 218 except OSError as e: 188 219 if e.errno == errno.EBADF: 189 220 os._exit(0) 190 221 os._exit(5) … … 215 246 by a new process image created with one of the exec family of 216 247 functions. 217 248 """ 218 fObj = file(self.mktemp(), 'w')249 fObj = open(self.mktemp(), 'wb') 219 250 fdesc._setCloseOnExec(fObj.fileno()) 220 251 status = self._execWithFileDescriptor(fObj) 221 252 self.assertTrue(os.WIFEXITED(status)) … … 227 258 A file descriptor passed to L{fdesc._unsetCloseOnExec} is inherited by 228 259 a new process image created with one of the exec family of functions. 229 260 """ 230 fObj = file(self.mktemp(), 'w')261 fObj = open(self.mktemp(), 'wb') 231 262 fdesc._setCloseOnExec(fObj.fileno()) 232 263 fdesc._unsetCloseOnExec(fObj.fileno()) 233 264 status = self._execWithFileDescriptor(fObj) -
admin/_twistedpython3.py
22 22 "twisted.internet", 23 23 "twisted.internet.defer", 24 24 "twisted.internet.interfaces", 25 "twisted.internet.fdesc", 25 26 "twisted.internet.main", 26 27 "twisted.internet.test", 27 28 "twisted.internet.test.reactormixins", … … 67 68 "twisted.test.test_context", 68 69 "twisted.test.test_defer", 69 70 "twisted.test.test_failure", 71 "twisted.test.test_fdesc", 70 72 "twisted.test.test_log", 71 73 "twisted.test.test_monkey", 72 74 "twisted.test.test_paths",
