Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit #2744

Merged
merged 15 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-merge-conflict
- id: mixed-line-ending
- id: check-case-conflict
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies:
- "flake8-pyproject==1.2.3"
CoolCat467 marked this conversation as resolved.
Show resolved Hide resolved
types: [file]
types_or: [python, pyi]
- repo: https://github.com/codespell-project/codespell
rev: v2.2.5
hooks:
- id: codespell

ci:
autofix_commit_msg: "[pre-commit.ci] auto fixes from pre-commit.com hooks"
autofix_prs: true
autoupdate_commit_msg: "[pre-commit.ci] pre-commit autoupdate"
autoupdate_schedule: weekly
submodules: false
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
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 = }")
jakkdl marked this conversation as resolved.
Show resolved Hide resolved
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)
17 changes: 11 additions & 6 deletions notes-to-self/ntp-example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
# - use the hostname "2.pool.ntp.org"
# (see: https://news.ntppool.org/2011/06/continuing-ipv6-deployment/)

import trio
import struct
import datetime
import struct

import trio


def make_query_packet():
"""Construct a UDP packet suitable for querying an NTP server to ask for
Expand All @@ -27,6 +29,7 @@ def make_query_packet():

return packet


def extract_transmit_timestamp(ntp_packet):
"""Given an NTP packet, extract the "transmit timestamp" field, as a
Python datetime."""
Expand All @@ -49,15 +52,16 @@ def extract_transmit_timestamp(ntp_packet):
offset = datetime.timedelta(seconds=seconds + fraction / 2**32)
return base_time + offset


async def main():
print("Our clock currently reads (in UTC):", datetime.datetime.utcnow())

# Look up some random NTP servers.
# (See www.pool.ntp.org for information about the NTP pool.)
servers = await trio.socket.getaddrinfo(
"pool.ntp.org", # host
"ntp", # port
family=trio.socket.AF_INET, # IPv4
"pool.ntp.org", # host
"ntp", # port
family=trio.socket.AF_INET, # IPv4
type=trio.socket.SOCK_DGRAM, # UDP
)

Expand All @@ -66,7 +70,7 @@ async def main():

# Create a UDP socket
udp_sock = trio.socket.socket(
family=trio.socket.AF_INET, # IPv4
family=trio.socket.AF_INET, # IPv4
type=trio.socket.SOCK_DGRAM, # UDP
)

Expand All @@ -88,4 +92,5 @@ async def main():
transmit_timestamp = extract_transmit_timestamp(data)
print("Their clock read (in UTC):", transmit_timestamp)


trio.run(main)
Loading