Skip to content

Commit

Permalink
Merge pull request #1618 from actonlang/default-args
Browse files Browse the repository at this point in the history
Add default argument values in various places
  • Loading branch information
plajjan authored Jan 8, 2024
2 parents d11e8c4 + b25a198 commit 03e02a0
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 20 deletions.
8 changes: 2 additions & 6 deletions base/src/http.act
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import acton.rts
import json
import logging
import net
from net import TCPListenConnection
import time

responses = {
Expand Down Expand Up @@ -444,10 +443,7 @@ actor Listener(cap: net.TCPListenCap, address: str, port: int, on_listen_error:


# TODO: change port to u16, when u16 has a sub-type relationship to int
# TODO: default schema="https"
# TODO: default port=None
# TODO: default tls_verify=True
actor Client(cap: net.TCPConnectCap, scheme: str, address: str, port: ?int, tls_verify: bool, on_connect: action(Client) -> None, on_error: action(Client, str) -> None, log_handler: ?logging.Handler):
actor Client(cap: net.TCPConnectCap, address: str, on_connect: action(Client) -> None, on_error: action(Client, str) -> None, scheme: str="https", port: ?int=None, tls_verify: bool=True, log_handler: ?logging.Handler):
"""HTTP(S) Client

scheme is either 'http' or 'https', the default is 'https'
Expand All @@ -465,7 +461,7 @@ actor Client(cap: net.TCPConnectCap, scheme: str, address: str, port: ?int, tls_
if scheme == "http":
_log.verbose("Using http scheme and port 80", None)
tcp_port = port if port is not None else 80
tcp_conn = net.TCPConnection(cap, address, tcp_port, _on_tcp_connect, _on_tcp_receive, _on_tcp_error)
tcp_conn = net.TCPConnection(cap, address, tcp_port, _on_tcp_connect, _on_tcp_receive, _on_tcp_error, connect_timeout=10.0)
elif scheme == "https":
_log.verbose("Using https scheme and port 443", None)
tls_port = port if port is not None else 443
Expand Down
8 changes: 2 additions & 6 deletions base/src/net.act
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def lookup_aaaa(cap: DNSCap, name: str, on_resolve: action(list[str]) -> None, o
_lookup_aaaa(name, on_resolve, on_error)


actor TCPConnection(cap: TCPConnectCap, address: str, port: int, on_connect: action(TCPConnection) -> None, on_receive: action(TCPConnection, bytes) -> None, on_error: action(TCPConnection, str) -> None):
actor TCPConnection(cap: TCPConnectCap, address: str, port: int, on_connect: action(TCPConnection) -> None, on_receive: action(TCPConnection, bytes) -> None, on_error: action(TCPConnection, str) -> None, connect_timeout: float=10.0):
"""TCP IP Connection"""
var _a_res: list[str] = []
var _aaaa_res: list[str] = []
Expand All @@ -77,9 +77,6 @@ actor TCPConnection(cap: TCPConnectCap, address: str, port: int, on_connect: act
STATE_CLOSED = 4 # TCP Connection disconnected / closed (it might never have been connected)
var _state = STATE_NONE

# TODO: turn this into an argument when we support default argument values
var connect_timeout = 10.0

var _connections = 0
var _consecutive_connect_errors = 0
var _bytes_in: u64 = 0
Expand Down Expand Up @@ -293,8 +290,7 @@ actor TCPListener(cap: TCPListenCap, address: str, port: int, on_error: action(T
NotImplemented
_init()

# TODO default: verify_tls=True
actor TLSConnection(cap: TCPConnectCap, address: str, port: int, on_connect: action(TLSConnection) -> None, on_receive: action(TLSConnection, bytes) -> None, on_error: action(TLSConnection, str) -> None, verify_tls: bool):
actor TLSConnection(cap: TCPConnectCap, address: str, port: int, on_connect: action(TLSConnection) -> None, on_receive: action(TLSConnection, bytes) -> None, on_error: action(TLSConnection, str) -> None, verify_tls: bool=True):
# TODO: support hostname/CN mismatch, good when connecting with IP address?
# TODO: do DNS lookup in Acton
# TODO: support ALPN
Expand Down
5 changes: 2 additions & 3 deletions base/src/time.act
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,8 @@ class Clock:
class RealtimeClock(Clock):
"""Realtime clock
"""
# TODO: add standard argument with default = UTC
def __init__(self, sync: int, offset: int, error: int):
self.standard = "UTC"
def __init__(self, sync: int, offset: int, error: int, standard="UTC"):
self.standard = standard
self.incarnation = _get_incarnation()
self.sync = sync
self.offset = offset
Expand Down
3 changes: 1 addition & 2 deletions docs/acton-by-example/src/primitives/sets.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ actor main(env):
print("Set after discarding 'foo':", s)

# Use set() to create an empty set. {} means an empty dict!
# TODO: remove None here once default args are in
empty_set = set(None)
empty_set = set()

env.exit(0)
```
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib/test_http.act
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ actor main(env):
print(errmsg)

print("asdf")
hc = http.Client(tcpccap, "https", "dummyjson.com", None, True, _on_http_connect, _on_http_error, log_handler)
hc2 = http.Client(tcpccap, "http", "neverssl.com", None, True, _on_http_connect, _on_http_error, log_handler)
hc = http.Client(tcpccap, "dummyjson.com", _on_http_connect, _on_http_error, log_handler)
#hc2 = http.Client(tcpccap, "neverssl.com", _on_http_connect, _on_http_error, scheme="http", log_handler)
2 changes: 1 addition & 1 deletion test/stdlib_auto/test_random.act
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def _test_choice_single_element():

def _test_choice_repeatability():
lst = [1, 2, 3, 4, 5]
results = set(None)
results = set()
for _ in range(1000):
results.add(random.choice(lst))
testing.assertTrue(bool(len(results) > 1), "Expected multiple unique results for repeated calls")
Expand Down

0 comments on commit 03e02a0

Please sign in to comment.