Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 94bbdba

Browse files
committed
also run it on the notes-to-self directory
1 parent 9083fa8 commit 94bbdba

10 files changed

+68
-32
lines changed

notes-to-self/how-does-windows-so-reuseaddr-work.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
modes = ["default", "SO_REUSEADDR", "SO_EXCLUSIVEADDRUSE"]
1111
bind_types = ["wildcard", "specific"]
1212

13+
1314
def sock(mode):
1415
s = socket.socket(family=socket.AF_INET)
1516
if mode == "SO_REUSEADDR":
@@ -18,6 +19,7 @@ def sock(mode):
1819
s.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
1920
return s
2021

22+
2123
def bind(sock, bind_type):
2224
if bind_type == "wildcard":
2325
sock.bind(("0.0.0.0", 12345))
@@ -26,6 +28,7 @@ def bind(sock, bind_type):
2628
else:
2729
assert False
2830

31+
2932
def table_entry(mode1, bind_type1, mode2, bind_type2):
3033
with sock(mode1) as sock1:
3134
bind(sock1, bind_type1)
@@ -41,19 +44,22 @@ def table_entry(mode1, bind_type1, mode2, bind_type2):
4144
else:
4245
return "Success"
4346

44-
print("""
47+
48+
print(
49+
"""
4550
second bind
4651
| """
47-
+ " | ".join(["%-19s" % mode for mode in modes])
52+
+ " | ".join(["%-19s" % mode for mode in modes])
4853
)
4954

50-
print(""" """, end='')
55+
print(""" """, end="")
5156
for mode in modes:
52-
print(" | " + " | ".join(["%8s" % bind_type for bind_type in bind_types]), end='')
57+
print(" | " + " | ".join(["%8s" % bind_type for bind_type in bind_types]), end="")
5358

54-
print("""
59+
print(
60+
"""
5561
first bind -----------------------------------------------------------------"""
56-
# default | wildcard | INUSE | Success | ACCESS | Success | INUSE | Success
62+
# default | wildcard | INUSE | Success | ACCESS | Success | INUSE | Success
5763
)
5864

5965
for i, mode1 in enumerate(modes):
@@ -63,6 +69,8 @@ def table_entry(mode1, bind_type1, mode2, bind_type2):
6369
for l, bind_type2 in enumerate(bind_types):
6470
entry = table_entry(mode1, bind_type1, mode2, bind_type2)
6571
row.append(entry)
66-
#print(mode1, bind_type1, mode2, bind_type2, entry)
67-
print("{:>19} | {:>8} | ".format(mode1, bind_type1)
68-
+ " | ".join(["%8s" % entry for entry in row]))
72+
# print(mode1, bind_type1, mode2, bind_type2, entry)
73+
print(
74+
f"{mode1:>19} | {bind_type1:>8} | "
75+
+ " | ".join(["%8s" % entry for entry in row])
76+
)

notes-to-self/reopen-pipe.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import time
44
import tempfile
55

6+
67
def check_reopen(r1, w):
78
try:
89
print("Reopening read end")
9-
r2 = os.open("/proc/self/fd/{}".format(r1), os.O_RDONLY)
10+
r2 = os.open(f"/proc/self/fd/{r1}", os.O_RDONLY)
1011

11-
print("r1 is {}, r2 is {}".format(r1, r2))
12+
print(f"r1 is {r1}, r2 is {r2}")
1213

1314
print("checking they both can receive from w...")
1415

@@ -36,11 +37,12 @@ def check_reopen(r1, w):
3637
def sleep_then_write():
3738
time.sleep(1)
3839
os.write(w, b"c")
40+
3941
threading.Thread(target=sleep_then_write, daemon=True).start()
4042
assert os.read(r1, 1) == b"c"
4143
print("r1 definitely seems to be in blocking mode")
4244
except Exception as exc:
43-
print("ERROR: {!r}".format(exc))
45+
print(f"ERROR: {exc!r}")
4446

4547

4648
print("-- testing anonymous pipe --")
@@ -63,6 +65,6 @@ def sleep_then_write():
6365

6466
print("-- testing socketpair --")
6567
import socket
68+
6669
rs, ws = socket.socketpair()
6770
check_reopen(rs.fileno(), ws.fileno())
68-

notes-to-self/schedule-timing.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
LOOPS = 0
55
RUNNING = True
66

7+
78
async def reschedule_loop(depth):
89
if depth == 0:
910
global LOOPS
1011
while RUNNING:
1112
LOOPS += 1
1213
await trio.sleep(0)
13-
#await trio.lowlevel.cancel_shielded_checkpoint()
14+
# await trio.lowlevel.cancel_shielded_checkpoint()
1415
else:
1516
await reschedule_loop(depth - 1)
1617

18+
1719
async def report_loop():
1820
global RUNNING
1921
try:
@@ -25,13 +27,15 @@ async def report_loop():
2527
end_count = LOOPS
2628
loops = end_count - start_count
2729
duration = end_time - start_time
28-
print("{} loops/sec".format(loops / duration))
30+
print(f"{loops / duration} loops/sec")
2931
finally:
3032
RUNNING = False
3133

34+
3235
async def main():
3336
async with trio.open_nursery() as nursery:
3437
nursery.start_soon(reschedule_loop, 10)
3538
nursery.start_soon(report_loop)
3639

40+
3741
trio.run(main)

notes-to-self/socketpair-buffering.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@
3232
except BlockingIOError:
3333
pass
3434

35-
print("setsockopt bufsize {}: {}".format(bufsize, i))
35+
print(f"setsockopt bufsize {bufsize}: {i}")
3636
a.close()
3737
b.close()

notes-to-self/ssl-handshake/ssl-handshake.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
server_ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
99
server_ctx.load_cert_chain("trio-test-1.pem")
1010

11+
1112
def _ssl_echo_serve_sync(sock):
1213
try:
1314
wrapped = server_ctx.wrap_socket(sock, server_side=True)
@@ -20,16 +21,19 @@ def _ssl_echo_serve_sync(sock):
2021
except BrokenPipeError:
2122
pass
2223

24+
2325
@contextmanager
2426
def echo_server_connection():
2527
client_sock, server_sock = socket.socketpair()
2628
with client_sock, server_sock:
2729
t = threading.Thread(
28-
target=_ssl_echo_serve_sync, args=(server_sock,), daemon=True)
30+
target=_ssl_echo_serve_sync, args=(server_sock,), daemon=True
31+
)
2932
t.start()
3033

3134
yield client_sock
3235

36+
3337
class ManuallyWrappedSocket:
3438
def __init__(self, ctx, sock, **kwargs):
3539
self.incoming = ssl.MemoryBIO()
@@ -82,21 +86,23 @@ def unwrap(self):
8286
def wrap_socket_via_wrap_socket(ctx, sock, **kwargs):
8387
return ctx.wrap_socket(sock, do_handshake_on_connect=False, **kwargs)
8488

89+
8590
def wrap_socket_via_wrap_bio(ctx, sock, **kwargs):
8691
return ManuallyWrappedSocket(ctx, sock, **kwargs)
8792

8893

8994
for wrap_socket in [
90-
wrap_socket_via_wrap_socket,
91-
wrap_socket_via_wrap_bio,
95+
wrap_socket_via_wrap_socket,
96+
wrap_socket_via_wrap_bio,
9297
]:
93-
print("\n--- checking {} ---\n".format(wrap_socket.__name__))
98+
print(f"\n--- checking {wrap_socket.__name__} ---\n")
9499

95100
print("checking with do_handshake + correct hostname...")
96101
with echo_server_connection() as client_sock:
97102
client_ctx = ssl.create_default_context(cafile="trio-test-CA.pem")
98103
wrapped = wrap_socket(
99-
client_ctx, client_sock, server_hostname="trio-test-1.example.org")
104+
client_ctx, client_sock, server_hostname="trio-test-1.example.org"
105+
)
100106
wrapped.do_handshake()
101107
wrapped.sendall(b"x")
102108
assert wrapped.recv(1) == b"x"
@@ -107,7 +113,8 @@ def wrap_socket_via_wrap_bio(ctx, sock, **kwargs):
107113
with echo_server_connection() as client_sock:
108114
client_ctx = ssl.create_default_context(cafile="trio-test-CA.pem")
109115
wrapped = wrap_socket(
110-
client_ctx, client_sock, server_hostname="trio-test-2.example.org")
116+
client_ctx, client_sock, server_hostname="trio-test-2.example.org"
117+
)
111118
try:
112119
wrapped.do_handshake()
113120
except Exception:
@@ -119,7 +126,8 @@ def wrap_socket_via_wrap_bio(ctx, sock, **kwargs):
119126
with echo_server_connection() as client_sock:
120127
client_ctx = ssl.create_default_context(cafile="trio-test-CA.pem")
121128
wrapped = wrap_socket(
122-
client_ctx, client_sock, server_hostname="trio-test-2.example.org")
129+
client_ctx, client_sock, server_hostname="trio-test-2.example.org"
130+
)
123131
# We forgot to call do_handshake
124132
# But the hostname is wrong so something had better error out...
125133
sent = b"x"

notes-to-self/sslobject.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
soutb = ssl.MemoryBIO()
1616
sso = server_ctx.wrap_bio(sinb, soutb, server_side=True)
1717

18+
1819
@contextmanager
1920
def expect(etype):
2021
try:
2122
yield
2223
except etype:
2324
pass
2425
else:
25-
raise AssertionError("expected {}".format(etype))
26+
raise AssertionError(f"expected {etype}")
27+
2628

2729
with expect(ssl.SSLWantReadError):
2830
cso.do_handshake()

notes-to-self/thread-closure-bug-demo.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,21 @@
88

99
COUNT = 100
1010

11+
1112
def slow_tracefunc(frame, event, arg):
1213
# A no-op trace function that sleeps briefly to make us more likely to hit
1314
# the race condition.
1415
time.sleep(0.01)
1516
return slow_tracefunc
1617

18+
1719
def run_with_slow_tracefunc(fn):
1820
# settrace() only takes effect when you enter a new frame, so we need this
1921
# little dance:
2022
sys.settrace(slow_tracefunc)
2123
return fn()
2224

25+
2326
def outer():
2427
x = 0
2528
# We hide the done variable inside a list, because we want to use it to
@@ -46,13 +49,14 @@ def traced_looper():
4649
t.start()
4750

4851
for i in range(COUNT):
49-
print("after {} increments, x is {}".format(i, x))
52+
print(f"after {i} increments, x is {x}")
5053
x += 1
5154
time.sleep(0.01)
5255

5356
done[0] = True
5457
t.join()
5558

56-
print("Final discrepancy: {} (should be 0)".format(COUNT - x))
59+
print(f"Final discrepancy: {COUNT - x} (should be 0)")
60+
5761

5862
outer()

notes-to-self/thread-dispatch-bench.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
COUNT = 10000
1212

13+
1314
def worker(in_q, out_q):
1415
while True:
1516
job = in_q.get()
1617
out_q.put(job())
1718

19+
1820
def main():
1921
in_q = Queue()
2022
out_q = Queue()
@@ -28,6 +30,7 @@ def main():
2830
in_q.put(lambda: None)
2931
out_q.get()
3032
end = time.monotonic()
31-
print("{:.2f} µs/job".format((end - start) / COUNT * 1e6))
33+
print(f"{(end - start) / COUNT * 1e6:.2f} µs/job")
34+
3235

3336
main()

notes-to-self/time-wait-windows-exclusiveaddruse.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
import socket
99
from contextlib import contextmanager
1010

11+
1112
@contextmanager
1213
def report_outcome(tagline):
1314
try:
1415
yield
1516
except OSError as exc:
16-
print("{}: failed".format(tagline))
17-
print(" details: {!r}".format(exc))
17+
print(f"{tagline}: failed")
18+
print(f" details: {exc!r}")
1819
else:
19-
print("{}: succeeded".format(tagline))
20+
print(f"{tagline}: succeeded")
21+
2022

2123
# Set up initial listening socket
2224
lsock = socket.socket()

notes-to-self/time-wait.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import attr
3333

34+
3435
@attr.s(repr=False)
3536
class Options:
3637
listen1_early = attr.ib(default=None)
@@ -49,9 +50,10 @@ def describe(self):
4950
for f in attr.fields(self.__class__):
5051
value = getattr(self, f.name)
5152
if value is not None:
52-
info.append("{}={}".format(f.name, value))
53+
info.append(f"{f.name}={value}")
5354
return "Set/unset: {}".format(", ".join(info))
5455

56+
5557
def time_wait(options):
5658
print(options.describe())
5759

@@ -60,7 +62,7 @@ def time_wait(options):
6062
listen0 = socket.socket()
6163
listen0.bind(("127.0.0.1", 0))
6264
sockaddr = listen0.getsockname()
63-
#print(" ", sockaddr)
65+
# print(" ", sockaddr)
6466
listen0.close()
6567

6668
listen1 = socket.socket()
@@ -98,6 +100,7 @@ def time_wait(options):
98100
else:
99101
print(" -> ok")
100102

103+
101104
time_wait(Options())
102105
time_wait(Options(listen1_early=True, server=True, listen2=True))
103106
time_wait(Options(listen1_early=True))

0 commit comments

Comments
 (0)