Skip to content

Commit

Permalink
[sapphire] Add more port value checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tysmith committed Oct 3, 2023
1 parent 2fb29ef commit 108a45b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions sapphire/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def parse_args(argv=None):
help="Configure console logging (default: %(default)s)",
)
parser.add_argument(
"--port", type=int, help="Specify a port to bind to (default: random)"
"--port", default=0, type=int, help="Specify port (default: automatic)"
)
parser.add_argument(
"--remote",
Expand All @@ -46,7 +46,9 @@ def parse_args(argv=None):
args = parser.parse_args(argv)
# sanity check
if not args.path.is_dir():
parser.error(f"Path does not exist '{args.path}'")
parser.error(f"Directory does not exist '{args.path}'")
if args.port < 0 or args.port > 65535:
parser.error("--port must be >= 0 and <= 65535")
if args.timeout < 0:
parser.error("--timeout must be >= 0")
args.log_level = level_map[args.log_level]
Expand Down
4 changes: 2 additions & 2 deletions sapphire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

def create_listening_socket(attempts=10, port=0, remote=False, timeout=None):
"""Create listening socket. Search for an open socket if needed and
and configure the socket. If a specific port is unavailable or no
configure the socket. If a specific port is unavailable or no
available ports can be found socket.error will be raised.
Args:
Expand All @@ -62,7 +62,7 @@ def create_listening_socket(attempts=10, port=0, remote=False, timeout=None):
socket: A listening socket.
"""
assert attempts > 0
assert port >= 0
assert 0 <= port <= 65535
assert timeout is None or timeout > 0

for remaining in reversed(range(attempts)):
Expand Down

0 comments on commit 108a45b

Please sign in to comment.