Skip to content

Commit 6606b2e

Browse files
committed
Avoid working harder than necessary and remove the annotations-as-documentation strings.
1 parent 39ca198 commit 6606b2e

1 file changed

Lines changed: 17 additions & 33 deletions

File tree

postgresql/python/socket.py

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import os
66
import random
77
import socket
8-
import math
98
import errno
109
import ssl
1110

@@ -51,7 +50,9 @@ def fatal_exception_message(typ, err) -> (str, None):
5150
return getattr(err, 'strerror', '<strerror not present>')
5251

5352
def secure(self, socket : socket.socket) -> ssl.SSLSocket:
54-
"secure a socket with SSL"
53+
"""
54+
Secure a socket with SSL.
55+
"""
5556
if self.socket_secure is not None:
5657
return ssl.wrap_socket(socket, **self.socket_secure)
5758
else:
@@ -69,9 +70,9 @@ def __call__(self, timeout = None):
6970
return s
7071

7172
def __init__(self,
72-
socket_create : "positional parameters given to socket.socket()",
73-
socket_connect : "parameter given to socket.connect()",
74-
socket_secure : "keywords given to ssl.wrap_socket" = None,
73+
socket_create,
74+
socket_connect,
75+
socket_secure = None,
7576
):
7677
self.socket_create = socket_create
7778
self.socket_connect = socket_connect
@@ -81,36 +82,19 @@ def __str__(self):
8182
return 'socket' + repr(self.socket_connect)
8283

8384
def find_available_port(
84-
interface : "attempt to bind to interface" = 'localhost',
85-
address_family : "address family to use (default: AF_INET)" = socket.AF_INET,
86-
limit : "Number tries to make before giving up" = 1024,
87-
port_range = (6600, 56600)
88-
) -> (int, None):
85+
interface = 'localhost',
86+
address_family = socket.AF_INET,
87+
):
8988
"""
9089
Find an available port on the given interface for the given address family.
91-
92-
Returns a port number that was successfully bound to or `None` if the
93-
attempt limit was reached.
9490
"""
95-
i = 0
96-
while i < limit:
97-
i += 1
98-
port = (
99-
math.floor(
100-
random.random() * (port_range[1] - port_range[0])
101-
) + port_range[0]
102-
)
103-
s = socket.socket(address_family, socket.SOCK_STREAM,)
104-
try:
105-
s.bind(('localhost', port))
106-
s.close()
107-
except socket.error as e:
108-
s.close()
109-
if e.errno in (errno.EACCES, errno.EADDRINUSE, errno.EINTR):
110-
# try again
111-
continue
112-
break
113-
else:
114-
port = None
91+
92+
port = None
93+
s = socket.socket(address_family, socket.SOCK_STREAM,)
94+
try:
95+
s.bind(('localhost', 0))
96+
port = s.getsockname()[1]
97+
finally:
98+
s.close()
11599

116100
return port

0 commit comments

Comments
 (0)