Skip to content

Commit

Permalink
Run pre-commit on all files and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Aug 13, 2023
1 parent 0eaaaa3 commit d8d6a2d
Show file tree
Hide file tree
Showing 39 changed files with 184 additions and 88 deletions.
2 changes: 1 addition & 1 deletion docs/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ help:
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
12 changes: 6 additions & 6 deletions docs/source/local_customization.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from docutils.parsers.rst import directives
from docutils.parsers.rst import directives as directives # noqa: F401
from sphinx import addnodes
from sphinx.domains.python import PyClasslike
from sphinx.ext.autodoc import (
FunctionDocumenter,
MethodDocumenter,
ClassLevelDocumenter,
Options,
from sphinx.ext.autodoc import ( # noqa: F401
ClassLevelDocumenter as ClassLevelDocumenter,
FunctionDocumenter as FunctionDocumenter,
MethodDocumenter as MethodDocumenter,
Options as Options,
)

"""
Expand Down
3 changes: 2 additions & 1 deletion docs/source/reference-core/channels-backpressure.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Simulate a producer that generates values 10x faster than the
# consumer can handle them.

import trio
import math

import trio


async def producer(send_channel):
count = 0
Expand Down
3 changes: 2 additions & 1 deletion docs/source/reference-core/channels-mpmc-broken.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# This example usually crashes!

import trio
import random

import trio


async def main():
async with trio.open_nursery() as nursery:
Expand Down
3 changes: 2 additions & 1 deletion docs/source/reference-core/channels-mpmc-fixed.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import trio
import random

import trio


async def main():
async with trio.open_nursery() as nursery:
Expand Down
3 changes: 2 additions & 1 deletion docs/source/reference-core/contextvar-example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import contextvars
import random

import trio
import contextvars

request_info = contextvars.ContextVar("request_info")

Expand Down
1 change: 1 addition & 0 deletions docs/source/reference-core/thread-contextvars-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

request_state = contextvars.ContextVar("request_state")


# Blocking function that should be run on a thread
# It could be reading or writing files, communicating with a database
# with a driver not compatible with async / await, etc.
Expand Down
1 change: 1 addition & 0 deletions docs/source/reference-testing/across-realtime.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# across-realtime.py

import time

import trio
import trio.testing

Expand Down
1 change: 1 addition & 0 deletions docs/source/tutorial/echo-client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# echo-client.py

import sys

import trio

# arbitrary, but:
Expand Down
3 changes: 2 additions & 1 deletion docs/source/tutorial/echo-server.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# echo-server.py

import trio
from itertools import count

import trio

# Port is arbitrary, but:
# - must be in between 1024 and 65535
# - can't be in use by some other program on your computer
Expand Down
18 changes: 12 additions & 6 deletions notes-to-self/afd-lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,27 @@
# matter, energy, and life which lie close at hand yet can never be detected
# with the senses we have."

import sys
import os.path
import sys

sys.path.insert(0, os.path.abspath(os.path.dirname(__file__) + r"\.."))

import trio

print(trio.__file__)
import trio.testing
import socket

import trio.testing
from trio._core._io_windows import _afd_helper_handle, _check, _get_base_socket
from trio._core._windows_cffi import (
ffi, kernel32, AFDPollFlags, IoControlCodes, ErrorCodes
)
from trio._core._io_windows import (
_get_base_socket, _afd_helper_handle, _check
AFDPollFlags,
ErrorCodes,
IoControlCodes,
ffi,
kernel32,
)


class AFDLab:
def __init__(self):
self._afd = _afd_helper_handle()
Expand Down Expand Up @@ -173,4 +178,5 @@ async def main():
await trio.sleep(2)
nursery.cancel_scope.cancel()


trio.run(main)
4 changes: 4 additions & 0 deletions notes-to-self/aio-guest-test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import asyncio

import trio


async def aio_main():
loop = asyncio.get_running_loop()

trio_done_fut = loop.create_future()

def trio_done_callback(main_outcome):
print(f"trio_main finished: {main_outcome!r}")
trio_done_fut.set_result(main_outcome)
Expand Down Expand Up @@ -35,6 +38,7 @@ async def trio_main():
if n >= 10:
return


async def aio_pingpong(from_trio, to_trio):
print("aio_pingpong!")

Expand Down
2 changes: 2 additions & 0 deletions notes-to-self/atomic-local.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
# Has to be a string :-(
sentinel = "_unique_name"


def f():
print(locals())


# code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,
# constants, names, varnames, filename, name, firstlineno,
# lnotab[, freevars[, cellvars]])
Expand Down
8 changes: 6 additions & 2 deletions notes-to-self/blocking-read-hack.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import trio
import errno
import os
import socket
import errno

import trio

bad_socket = socket.socket()


class BlockingReadTimeoutError(Exception):
pass


async def blocking_read_with_timeout(fd, count, timeout):
print("reading from fd", fd)
cancel_requested = False
Expand Down Expand Up @@ -42,4 +45,5 @@ async def kill_it_after_timeout(new_fd):
finally:
os.close(new_fd)


trio.run(blocking_read_with_timeout, 0, 10, 2)
7 changes: 5 additions & 2 deletions notes-to-self/estimate-task-size.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# Little script to get a rough estimate of how much memory each task takes

import resource

import trio
import trio.testing

LOW = 1000
HIGH = 10000


async def tinytask():
await trio.sleep_forever()


async def measure(count):
async with trio.open_nursery() as nursery:
for _ in range(count):
Expand All @@ -23,8 +26,8 @@ async def main():
low_usage = await measure(LOW)
high_usage = await measure(HIGH + LOW)

print("Memory usage per task:",
(high_usage.ru_maxrss - low_usage.ru_maxrss) / HIGH)
print("Memory usage per task:", (high_usage.ru_maxrss - low_usage.ru_maxrss) / HIGH)
print("(kilobytes on Linux, bytes on macOS)")


trio.run(main)
3 changes: 1 addition & 2 deletions notes-to-self/fbsd-pipe-close-notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
#
# Upstream bug: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=246350

import select
import os
import threading
import select

r, w = os.pipe()

Expand Down
9 changes: 6 additions & 3 deletions notes-to-self/file-read-latency.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
# ns per call, instead of ~500 ns/call for the syscall and related overhead.
# That's probably more fair -- the BufferedIOBase code can't service random
# accesses, even if your working set fits entirely in RAM.
f = open("/etc/passwd", "rb")#, buffering=0)
f = open("/etc/passwd", "rb") # , buffering=0)

while True:
start = time.perf_counter()
Expand All @@ -23,5 +23,8 @@
both = (between - start) / COUNT * 1e9
seek = (end - between) / COUNT * 1e9
read = both - seek
print("{:.2f} ns/(seek+read), {:.2f} ns/seek, estimate ~{:.2f} ns/read"
.format(both, seek, read))
print(
"{:.2f} ns/(seek+read), {:.2f} ns/seek, estimate ~{:.2f} ns/read".format(
both, seek, read
)
)
4 changes: 4 additions & 0 deletions notes-to-self/graceful-shutdown-idea.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import trio


class GracefulShutdownManager:
def __init__(self):
self._shutting_down = False
Expand All @@ -21,6 +22,7 @@ def cancel_on_graceful_shutdown(self):
def shutting_down(self):
return self._shutting_down


# Code can check gsm.shutting_down occasionally at appropriate points to see
# if it should exit.
#
Expand All @@ -31,9 +33,11 @@ async def stream_handler(stream):
while True:
with gsm.cancel_on_graceful_shutdown():
data = await stream.receive_some()
print(f"{data = }")
if gsm.shutting_down:
break


# To trigger the shutdown:
async def listen_for_shutdown_signals():
with trio.open_signal_receiver(signal.SIGINT, signal.SIGTERM) as signal_aiter:
Expand Down
2 changes: 1 addition & 1 deletion notes-to-self/how-does-windows-so-reuseaddr-work.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#
# See https://github.com/python-trio/trio/issues/928 for details and context

import socket
import errno
import socket

modes = ["default", "SO_REUSEADDR", "SO_EXCLUSIVEADDRUSE"]
bind_types = ["wildcard", "specific"]
Expand Down
6 changes: 5 additions & 1 deletion notes-to-self/loopy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import trio
import time

import trio


async def loopy():
try:
while True:
Expand All @@ -9,10 +11,12 @@ async def loopy():
except KeyboardInterrupt:
print("KI!")


async def main():
async with trio.open_nursery() as nursery:
nursery.start_soon(loopy)
nursery.start_soon(loopy)
nursery.start_soon(loopy)


trio.run(main)
3 changes: 3 additions & 0 deletions notes-to-self/lots-of-tasks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import sys

import trio

(COUNT_STR,) = sys.argv[1:]
COUNT = int(COUNT_STR)


async def main():
async with trio.open_nursery() as nursery:
for _ in range(COUNT):
nursery.start_soon(trio.sleep, 1)


trio.run(main)
12 changes: 8 additions & 4 deletions notes-to-self/manual-signal-handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@

if os.name == "nt":
import cffi

ffi = cffi.FFI()
ffi.cdef("""
ffi.cdef(
"""
void* WINAPI GetProcAddress(void* hModule, char* lpProcName);
typedef void (*PyOS_sighandler_t)(int);
""")
"""
)
kernel32 = ffi.dlopen("kernel32.dll")
PyOS_getsig_ptr = kernel32.GetProcAddress(
ffi.cast("void*", sys.dllhandle), b"PyOS_getsig")
ffi.cast("void*", sys.dllhandle), b"PyOS_getsig"
)
PyOS_getsig = ffi.cast("PyOS_sighandler_t (*)(int)", PyOS_getsig_ptr)


import signal

PyOS_getsig(signal.SIGINT)(signal.SIGINT)
2 changes: 2 additions & 0 deletions notes-to-self/measure-listen-backlog.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import trio


async def run_test(nominal_backlog):
print("--\nnominal:", nominal_backlog)

Expand All @@ -22,5 +23,6 @@ async def run_test(nominal_backlog):
for client_sock in client_socks:
client_sock.close()


for nominal_backlog in [10, trio.socket.SOMAXCONN, 65535]:
trio.run(run_test, nominal_backlog)
Loading

0 comments on commit d8d6a2d

Please sign in to comment.