(self, port, sock_type)
| 148 | return res |
| 149 | |
| 150 | def _get_port(self, port, sock_type): |
| 151 | if port and self._no_random_ports(): |
| 152 | return port |
| 153 | |
| 154 | if len(self._filelocks) >= self._valid_port_count: |
| 155 | raise PortManagerException("All valid ports are taken ({}): {}".format(self._valid_range, self._filelocks)) |
| 156 | |
| 157 | salt = random.randint(0, UI16MAXVAL) |
| 158 | for attempt in six.moves.range(self._valid_port_count): |
| 159 | probe_port = (salt + attempt) % self._valid_port_count |
| 160 | |
| 161 | for left, right in self._valid_range: |
| 162 | if probe_port >= (right - left): |
| 163 | probe_port -= right - left |
| 164 | else: |
| 165 | probe_port += left |
| 166 | break |
| 167 | if not self._capture_port(probe_port, sock_type): |
| 168 | continue |
| 169 | return probe_port |
| 170 | |
| 171 | raise PortManagerException( |
| 172 | "Failed to find valid port (range: {} used: {})".format(self._valid_range, self._filelocks) |
| 173 | ) |
| 174 | |
| 175 | def _capture_port(self, port, sock_type): |
| 176 | with self._lock: |
no test coverage detected