| 109 | |
| 110 | |
| 111 | class SockWrapper: |
| 112 | |
| 113 | def __init__(self, rsock, wsock, connect_to=None, peername=None): |
| 114 | global _swcount |
| 115 | _swcount += 1 |
| 116 | debug3('creating new SockWrapper (%d now exist)' % _swcount) |
| 117 | self.exc = None |
| 118 | self.rsock = rsock |
| 119 | self.wsock = wsock |
| 120 | self.shut_read = self.shut_write = False |
| 121 | self.buf = [] |
| 122 | self.connect_to = connect_to |
| 123 | self.peername = peername or _try_peername(self.rsock) |
| 124 | self.try_connect() |
| 125 | |
| 126 | def __del__(self): |
| 127 | global _swcount |
| 128 | _swcount -= 1 |
| 129 | debug1('%r: deleting (%d remain)' % (self, _swcount)) |
| 130 | if self.exc: |
| 131 | debug1('%r: error was: %s' % (self, self.exc)) |
| 132 | |
| 133 | def __repr__(self): |
| 134 | if self.rsock == self.wsock: |
| 135 | fds = '#%d' % self.rsock.fileno() |
| 136 | else: |
| 137 | fds = '#%d,%d' % (self.rsock.fileno(), self.wsock.fileno()) |
| 138 | return 'SW%s:%s' % (fds, self.peername) |
| 139 | |
| 140 | def seterr(self, e): |
| 141 | if not self.exc: |
| 142 | self.exc = e |
| 143 | self.nowrite() |
| 144 | self.noread() |
| 145 | |
| 146 | def try_connect(self): |
| 147 | if self.connect_to and self.shut_write: |
| 148 | self.noread() |
| 149 | self.connect_to = None |
| 150 | if not self.connect_to: |
| 151 | return # already connected |
| 152 | self.rsock.setblocking(False) |
| 153 | debug3('%r: trying connect to %r' % (self, self.connect_to)) |
| 154 | try: |
| 155 | self.rsock.connect(self.connect_to) |
| 156 | # connected successfully (Linux) |
| 157 | self.connect_to = None |
| 158 | except socket.error: |
| 159 | _, e = sys.exc_info()[:2] |
| 160 | debug3('%r: connect result: %s' % (self, e)) |
| 161 | if e.args[0] == errno.EINVAL: |
| 162 | # this is what happens when you call connect() on a socket |
| 163 | # that is now connected but returned EINPROGRESS last time, |
| 164 | # on BSD, on python pre-2.5.1. We need to use getsockopt() |
| 165 | # to get the "real" error. Later pythons do this |
| 166 | # automatically, so this code won't run. |
| 167 | realerr = self.rsock.getsockopt(socket.SOL_SOCKET, |
| 168 | socket.SO_ERROR) |
no outgoing calls
no test coverage detected