Skip to content

Commit 395d4f8

Browse files
gh-11: Add type annotations, enforce static typing in standard extensions.
Remove wasapi extension.
1 parent da858cc commit 395d4f8

File tree

7 files changed

+480
-929
lines changed

7 files changed

+480
-929
lines changed

ext/image.py

Lines changed: 278 additions & 255 deletions
Large diffs are not rendered by default.

ext/networking.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ def _expect_str(v: Any, rule: str, location: Any) -> str:
5959
return str(v.value)
6060

6161

62-
def _make_int(n: int):
62+
def _make_int(n: int) -> Any:
6363
from interpreter import TYPE_INT, Value
6464

6565
return Value(TYPE_INT, int(n))
6666

6767

68-
def _make_str(s: str):
68+
def _make_str(s: str) -> Any:
6969
from interpreter import TYPE_STR, Value
7070

7171
return Value(TYPE_STR, str(s))
7272

7373

74-
def _bytes_to_tns(data: bytes):
74+
def _bytes_to_tns(data: bytes) -> Any:
7575
from interpreter import TYPE_INT, TYPE_TNS, Tensor, Value
7676

7777
# Keep shape at least 1 to match existing BYTES behavior.
@@ -164,8 +164,7 @@ def _ssl_context(*, verify: bool) -> ssl.SSLContext:
164164

165165
# ---- TCP ----
166166

167-
168-
def _tcp_connect(interpreter, args, _arg_nodes, _env, location):
167+
def _tcp_connect(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
169168
from interpreter import ASMRuntimeError
170169

171170
host = _expect_str(args[0], "TCP_CONNECT", location)
@@ -199,7 +198,7 @@ def _tcp_connect(interpreter, args, _arg_nodes, _env, location):
199198
return _make_int(hid)
200199

201200

202-
def _tcp_send(interpreter, args, _arg_nodes, _env, location):
201+
def _tcp_send(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
203202
from interpreter import ASMRuntimeError
204203

205204
hid = _expect_int(args[0], "TCP_SEND", location)
@@ -217,7 +216,7 @@ def _tcp_send(interpreter, args, _arg_nodes, _env, location):
217216
raise ASMRuntimeError(f"TCP_SEND failed: {exc}", location=location, rewrite_rule="TCP_SEND")
218217

219218

220-
def _tcp_recv_text(interpreter, args, _arg_nodes, _env, location):
219+
def _tcp_recv_text(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
221220
from interpreter import ASMRuntimeError
222221

223222
hid = _expect_int(args[0], "TCP_RECV_TEXT", location)
@@ -237,7 +236,7 @@ def _tcp_recv_text(interpreter, args, _arg_nodes, _env, location):
237236
return _make_str(data.decode(enc, errors="replace"))
238237

239238

240-
def _tcp_recv_bytes(interpreter, args, _arg_nodes, _env, location):
239+
def _tcp_recv_bytes(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
241240
from interpreter import ASMRuntimeError
242241

243242
hid = _expect_int(args[0], "TCP_RECV_BYTES", location)
@@ -255,7 +254,7 @@ def _tcp_recv_bytes(interpreter, args, _arg_nodes, _env, location):
255254
return _bytes_to_tns(data)
256255

257256

258-
def _tcp_close(interpreter, args, _arg_nodes, _env, location):
257+
def _tcp_close(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
259258
from interpreter import ASMRuntimeError
260259

261260
hid = _expect_int(args[0], "TCP_CLOSE", location)
@@ -273,7 +272,7 @@ def _tcp_close(interpreter, args, _arg_nodes, _env, location):
273272
# ---- UDP ----
274273

275274

276-
def _udp_bind(interpreter, args, _arg_nodes, _env, location):
275+
def _udp_bind(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
277276
from interpreter import ASMRuntimeError
278277

279278
host = _expect_str(args[0], "UDP_BIND", location)
@@ -299,7 +298,7 @@ def _udp_bind(interpreter, args, _arg_nodes, _env, location):
299298
return _make_int(hid)
300299

301300

302-
def _udp_send(interpreter, args, _arg_nodes, _env, location):
301+
def _udp_send(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
303302
from interpreter import ASMRuntimeError
304303

305304
hid = _expect_int(args[0], "UDP_SEND", location)
@@ -321,7 +320,7 @@ def _udp_send(interpreter, args, _arg_nodes, _env, location):
321320
raise ASMRuntimeError(f"UDP_SEND failed: {exc}", location=location, rewrite_rule="UDP_SEND")
322321

323322

324-
def _udp_recv_text(interpreter, args, _arg_nodes, _env, location):
323+
def _udp_recv_text(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
325324
from interpreter import ASMRuntimeError
326325

327326
hid = _expect_int(args[0], "UDP_RECV_TEXT", location)
@@ -346,7 +345,7 @@ def _udp_recv_text(interpreter, args, _arg_nodes, _env, location):
346345
return _make_str(data.decode(enc, errors="replace"))
347346

348347

349-
def _udp_recv_bytes(interpreter, args, _arg_nodes, _env, location):
348+
def _udp_recv_bytes(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
350349
from interpreter import ASMRuntimeError
351350

352351
hid = _expect_int(args[0], "UDP_RECV_BYTES", location)
@@ -369,7 +368,7 @@ def _udp_recv_bytes(interpreter, args, _arg_nodes, _env, location):
369368
return _bytes_to_tns(data)
370369

371370

372-
def _udp_close(interpreter, args, _arg_nodes, _env, location):
371+
def _udp_close(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
373372
from interpreter import ASMRuntimeError
374373

375374
hid = _expect_int(args[0], "UDP_CLOSE", location)
@@ -401,7 +400,7 @@ def _http_request_bytes(method: str, url: str, *, body: Optional[bytes], content
401400
return int(status), data
402401

403402

404-
def _http_get_text(interpreter, args, _arg_nodes, _env, location):
403+
def _http_get_text(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
405404
from interpreter import ASMRuntimeError
406405

407406
url = _expect_str(args[0], "HTTP_GET_TEXT", location)
@@ -416,7 +415,7 @@ def _http_get_text(interpreter, args, _arg_nodes, _env, location):
416415
return _make_str(data.decode("utf-8", errors="replace"))
417416

418417

419-
def _http_get_bytes(interpreter, args, _arg_nodes, _env, location):
418+
def _http_get_bytes(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
420419
from interpreter import ASMRuntimeError
421420

422421
url = _expect_str(args[0], "HTTP_GET_BYTES", location)
@@ -431,7 +430,7 @@ def _http_get_bytes(interpreter, args, _arg_nodes, _env, location):
431430
return _bytes_to_tns(data)
432431

433432

434-
def _http_get_status(interpreter, args, _arg_nodes, _env, location):
433+
def _http_get_status(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
435434
from interpreter import ASMRuntimeError
436435

437436
url = _expect_str(args[0], "HTTP_GET_STATUS", location)
@@ -446,7 +445,7 @@ def _http_get_status(interpreter, args, _arg_nodes, _env, location):
446445
return _make_int(status)
447446

448447

449-
def _http_post_text(interpreter, args, _arg_nodes, _env, location):
448+
def _http_post_text(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
450449
from interpreter import ASMRuntimeError
451450

452451
url = _expect_str(args[0], "HTTP_POST_TEXT", location)
@@ -473,13 +472,13 @@ def _http_post_text(interpreter, args, _arg_nodes, _env, location):
473472
# ---- FTP / FTPS ----
474473

475474

476-
def _ftp_login(host: str, port: int, user: str, password: str, *, tls: bool, timeout_s: Optional[float], verify: bool):
475+
def _ftp_login(host: str, port: int, user: str, password: str, *, tls: bool, timeout_s: Optional[float], verify: bool) -> Any:
477476
import ftplib
478477

479478
if tls:
480479
ftps = ftplib.FTP_TLS()
481480
ftps.context = ssl.create_default_context() if verify else _ssl_context(verify=False)
482-
ftps.connect(host=host, port=port, timeout=timeout_s)
481+
ftps.connect(host=host, port=port, timeout=(float(timeout_s) if timeout_s is not None else 0.0))
483482
if user or password:
484483
ftps.login(user=user, passwd=password)
485484
else:
@@ -488,15 +487,15 @@ def _ftp_login(host: str, port: int, user: str, password: str, *, tls: bool, tim
488487
return ftps
489488

490489
ftp = ftplib.FTP()
491-
ftp.connect(host=host, port=port, timeout=timeout_s)
490+
ftp.connect(host=host, port=port, timeout=(float(timeout_s) if timeout_s is not None else 0.0))
492491
if user or password:
493492
ftp.login(user=user, passwd=password)
494493
else:
495494
ftp.login()
496495
return ftp
497496

498497

499-
def _ftp_list(interpreter, args, _arg_nodes, _env, location):
498+
def _ftp_list(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
500499
from interpreter import ASMRuntimeError
501500

502501
host = _expect_str(args[0], "FTP_LIST", location)
@@ -528,7 +527,7 @@ def _ftp_list(interpreter, args, _arg_nodes, _env, location):
528527
raise ASMRuntimeError(f"FTP_LIST failed: {exc}", location=location, rewrite_rule="FTP_LIST")
529528

530529

531-
def _ftp_get_bytes(interpreter, args, _arg_nodes, _env, location):
530+
def _ftp_get_bytes(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
532531
from interpreter import ASMRuntimeError
533532

534533
host = _expect_str(args[0], "FTP_GET_BYTES", location)
@@ -560,7 +559,7 @@ def _ftp_get_bytes(interpreter, args, _arg_nodes, _env, location):
560559
raise ASMRuntimeError(f"FTP_GET_BYTES failed: {exc}", location=location, rewrite_rule="FTP_GET_BYTES")
561560

562561

563-
def _ftp_put_bytes(interpreter, args, _arg_nodes, _env, location):
562+
def _ftp_put_bytes(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
564563
from interpreter import ASMRuntimeError
565564

566565
host = _expect_str(args[0], "FTP_PUT_BYTES", location)
@@ -596,7 +595,7 @@ def _ftp_put_bytes(interpreter, args, _arg_nodes, _env, location):
596595
# ---- SMTP / SMTPS ----
597596

598597

599-
def _smtp_send(interpreter, args, _arg_nodes, _env, location):
598+
def _smtp_send(interpreter: Any, args: List[Any], _arg_nodes: List[Any], _env: Any, location: Any) -> Any:
600599
from interpreter import ASMRuntimeError
601600

602601
host = _expect_str(args[0], "SMTP_SEND", location)
@@ -635,11 +634,13 @@ def _smtp_send(interpreter, args, _arg_nodes, _env, location):
635634
try:
636635
import smtplib
637636

637+
timeout_val: float = float(timeout_s) if timeout_s is not None else 0.0
638+
client: Any
638639
if tls != 0:
639640
ctx = _ssl_context(verify=verify != 0)
640-
client = smtplib.SMTP_SSL(host=host, port=port, timeout=timeout_s, context=ctx)
641+
client = smtplib.SMTP_SSL(host=host, port=port, timeout=timeout_val, context=ctx)
641642
else:
642-
client = smtplib.SMTP(host=host, port=port, timeout=timeout_s)
643+
client = smtplib.SMTP(host=host, port=port, timeout=timeout_val)
643644

644645
try:
645646
client.ehlo()

0 commit comments

Comments
 (0)