| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent dd9e3b8 commit 39eb8fa
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,6 +26,15 @@ PyAPI_FUNC(size_t) PyLong_AsSize_t(PyObject *); | |||
| 26 | 26 | PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLong(PyObject *); | |
| 27 | 27 | PyAPI_FUNC(unsigned long) PyLong_AsUnsignedLongMask(PyObject *); | |
| 28 | 28 | ||
| 29 | + /* Used by socketmodule.c */ | ||
| 30 | + #if SIZEOF_SOCKET_T <= SIZEOF_LONG | ||
| 31 | + #define PyLong_FromSocket_t(fd) PyLong_FromLong((SOCKET_T)(fd)) | ||
| 32 | + #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLong(fd) | ||
| 33 | + #else | ||
| 34 | + #define PyLong_FromSocket_t(fd) PyLong_FromLongLong(((SOCKET_T)(fd)); | ||
| 35 | + #define PyLong_AsSocket_t(fd) (SOCKET_T)PyLong_AsLongLong(fd) | ||
| 36 | + #endif | ||
| 37 | + | ||
| 29 | 38 | /* For use by intobject.c only */ | |
| 30 | 39 | PyAPI_DATA(int) _PyLong_DigitValue[256]; | |
| 31 | 40 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -79,28 +79,14 @@ | |||
| 79 | 79 | __all__.append("errorTab") | |
| 80 | 80 | ||
| 81 | 81 | ||
| 82 | - # True if os.dup() can duplicate socket descriptors. | ||
| 83 | - # (On Windows at least, os.dup only works on files) | ||
| 84 | - _can_dup_socket = hasattr(_socket.socket, "dup") | ||
| 85 | - | ||
| 86 | - if _can_dup_socket: | ||
| 87 | - def fromfd(fd, family=AF_INET, type=SOCK_STREAM, proto=0): | ||
| 88 | - nfd = os.dup(fd) | ||
| 89 | - return socket(family, type, proto, fileno=nfd) | ||
| 90 | - | ||
| 91 | 82 | class socket(_socket.socket): | |
| 92 | 83 | ||
| 93 | 84 | """A subclass of _socket.socket adding the makefile() method.""" | |
| 94 | 85 | ||
| 95 | 86 | __slots__ = ["__weakref__", "_io_refs", "_closed"] | |
| 96 | - if not _can_dup_socket: | ||
| 97 | - __slots__.append("_base") | ||
| 98 | 87 | ||
| 99 | 88 | def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None): | |
| 100 | - if fileno is None: | ||
| 101 | - _socket.socket.__init__(self, family, type, proto) | ||
| 102 | - else: | ||
| 103 | - _socket.socket.__init__(self, family, type, proto, fileno) | ||
| 89 | + _socket.socket.__init__(self, family, type, proto, fileno) | ||
| 104 | 90 | self._io_refs = 0 | |
| 105 | 91 | self._closed = False | |
| 106 | 92 | ||
@@ -114,23 +100,29 @@ def __repr__(self): | |||
| 114 | 100 | s[7:]) | |
| 115 | 101 | return s | |
| 116 | 102 | ||
| 103 | + def dup(self): | ||
| 104 | + """dup() -> socket object | ||
| 105 | + | ||
| 106 | + Return a new socket object connected to the same system resource. | ||
| 107 | + """ | ||
| 108 | + fd = dup(self.fileno()) | ||
| 109 | + sock = self.__class__(self.family, self.type, self.proto, fileno=fd) | ||
| 110 | + sock.settimeout(self.gettimeout()) | ||
| 111 | + return sock | ||
| 112 | + | ||
| 117 | 113 | def accept(self): | |
| 118 | - """Wrap accept() to give the connection the right type.""" | ||
| 119 | - conn, addr = _socket.socket.accept(self) | ||
| 120 | - fd = conn.fileno() | ||
| 121 | - nfd = fd | ||
| 122 | - if _can_dup_socket: | ||
| 123 | - nfd = os.dup(fd) | ||
| 124 | - wrapper = socket(self.family, self.type, self.proto, fileno=nfd) | ||
| 125 | - if fd == nfd: | ||
| 126 | - wrapper._base = conn # Keep the base alive | ||
| 127 | - else: | ||
| 128 | - conn.close() | ||
| 129 | - return wrapper, addr | ||
| 114 | + """accept() -> (socket object, address info) | ||
| 115 | + | ||
| 116 | + Wait for an incoming connection. Return a new socket | ||
| 117 | + representing the connection, and the address of the client. | ||
| 118 | + For IP sockets, the address info is a pair (hostaddr, port). | ||
| 119 | + """ | ||
| 120 | + fd, addr = self._accept() | ||
| 121 | + return socket(self.family, self.type, self.proto, fileno=fd), addr | ||
| 130 | 122 | ||
| 131 | 123 | def makefile(self, mode="r", buffering=None, *, | |
| 132 | 124 | encoding=None, newline=None): | |
| 133 | - """Return an I/O stream connected to the socket. | ||
| 125 | + """makefile(...) -> an I/O stream connected to the socket | ||
| 134 | 126 | ||
| 135 | 127 | The arguments are as for io.open() after the filename, | |
| 136 | 128 | except the only mode characters supported are 'r', 'w' and 'b'. | |
@@ -184,21 +176,18 @@ def _decref_socketios(self): | |||
| 184 | 176 | ||
| 185 | 177 | def close(self): | |
| 186 | 178 | self._closed = True | |
| 187 | - if self._io_refs < 1: | ||
| 188 | - self._real_close() | ||
| 179 | + if self._io_refs <= 0: | ||
| 180 | + _socket.socket.close(self) | ||
| 189 | 181 | ||
| 190 | - # _real_close calls close on the _socket.socket base class. | ||
| 191 | 182 | ||
| 192 | - if not _can_dup_socket: | ||
| 193 | - def _real_close(self): | ||
| 194 | - _socket.socket.close(self) | ||
| 195 | - base = getattr(self, "_base", None) | ||
| 196 | - if base is not None: | ||
| 197 | - self._base = None | ||
| 198 | - base.close() | ||
| 199 | - else: | ||
| 200 | - def _real_close(self): | ||
| 201 | - _socket.socket.close(self) | ||
| 183 | + def fromfd(fd, family, type, proto=0): | ||
| 184 | + """ fromfd(fd, family, type[, proto]) -> socket object | ||
| 185 | + | ||
| 186 | + Create a socket object from a duplicate of the given file | ||
| 187 | + descriptor. The remaining arguments are the same as for socket(). | ||
| 188 | + """ | ||
| 189 | + nfd = dup(fd) | ||
| 190 | + return socket(family, type, proto, nfd) | ||
| 202 | 191 | ||
| 203 | 192 | ||
| 204 | 193 | class SocketIO(io.RawIOBase): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -78,8 +78,8 @@ | |||
| 78 | 78 | from socket import socket, AF_INET, SOCK_STREAM, error | |
| 79 | 79 | from socket import getnameinfo as _getnameinfo | |
| 80 | 80 | from socket import error as socket_error | |
| 81 | + from socket import dup as _dup | ||
| 81 | 82 | import base64 # for DER-to-PEM translation | |
| 82 | - _can_dup_socket = hasattr(socket, "dup") | ||
| 83 | 83 | ||
| 84 | 84 | class SSLSocket(socket): | |
| 85 | 85 | ||
@@ -99,20 +99,11 @@ def __init__(self, sock=None, keyfile=None, certfile=None, | |||
| 99 | 99 | if sock is not None: | |
| 100 | 100 | # copied this code from socket.accept() | |
| 101 | 101 | fd = sock.fileno() | |
| 102 | - nfd = fd | ||
| 103 | - if _can_dup_socket: | ||
| 104 | - nfd = os.dup(fd) | ||
| 105 | - try: | ||
| 106 | - socket.__init__(self, family=sock.family, type=sock.type, | ||
| 107 | - proto=sock.proto, fileno=nfd) | ||
| 108 | - except: | ||
| 109 | - if nfd != fd: | ||
| 110 | - os.close(nfd) | ||
| 111 | - else: | ||
| 112 | - if fd != nfd: | ||
| 113 | - sock.close() | ||
| 114 | - sock = None | ||
| 115 | - | ||
| 102 | + nfd = _dup(fd) | ||
| 103 | + socket.__init__(self, family=sock.family, type=sock.type, | ||
| 104 | + proto=sock.proto, fileno=nfd) | ||
| 105 | + sock.close() | ||
| 106 | + sock = None | ||
| 116 | 107 | elif fileno is not None: | |
| 117 | 108 | socket.__init__(self, fileno=fileno) | |
| 118 | 109 | else: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -575,6 +575,15 @@ def testFromFd(self): | |||
| 575 | 575 | def _testFromFd(self): | |
| 576 | 576 | self.serv_conn.send(MSG) | |
| 577 | 577 | ||
| 578 | + def testDup(self): | ||
| 579 | + # Testing dup() | ||
| 580 | + sock = self.cli_conn.dup() | ||
| 581 | + msg = sock.recv(1024) | ||
| 582 | + self.assertEqual(msg, MSG) | ||
| 583 | + | ||
| 584 | + def _testDup(self): | ||
| 585 | + self.serv_conn.send(MSG) | ||
| 586 | + | ||
| 578 | 587 | def testShutdown(self): | |
| 579 | 588 | # Testing shutdown() | |
| 580 | 589 | msg = self.cli_conn.recv(1024) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments