Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Commit

Permalink
Fix compatibility with Python 3.13
Browse files Browse the repository at this point in the history
_UnixSelectorEventLoop in Python 3.13 expect _unix_server_sockets dict,
add one. It's later used (among others) in _stop_serving(), but not in
close() directly, so just adding attribute is enough.

The related CPython change: 74b868f636a "gh-111246: Remove listening Unix socket on close (#111483)"

Add a test for unix sockets too.
  • Loading branch information
marmarek committed Aug 16, 2024
1 parent b2ae2b0 commit 71eca70
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions changes/176.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix unix socket handling on Python 3.13
1 change: 1 addition & 0 deletions src/gbulb/glib_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class GLibBaseEventLoopPlatformExt(unix_events.SelectorEventLoop):

def __init__(self):
self._sighandlers = {}
self._unix_server_sockets = {}

def close(self):
for sig in list(self._sighandlers):
Expand Down
42 changes: 42 additions & 0 deletions tests/test_glib_events.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import os
import sys
import tempfile
from unittest import mock, skipIf

import pytest
Expand Down Expand Up @@ -575,3 +577,43 @@ async def run():
assert server_success

glib_loop.run_until_complete(run())


def test_unix_sockets(glib_loop):
server_done = asyncio.Event()
server_done._loop = glib_loop
server_success = False

async def cb(reader, writer):
nonlocal server_success

writer.write(b"cool data\n")
await writer.drain()

print("reading")
d = await reader.readline()
print("hrm", d)
server_success = d == b"thank you\n"

writer.close()
server_done.set()

async def run():
with tempfile.TemporaryDirectory() as tmpdir:
path = os.path.join(tmpdir, "socket")
s = await asyncio.start_unix_server(cb, path)
reader, writer = await asyncio.open_unix_connection(path)

d = await reader.readline()
assert d == b"cool data\n"

writer.write(b"thank you\n")
await writer.drain()

writer.close()

await server_done.wait()

assert server_success

glib_loop.run_until_complete(run())

0 comments on commit 71eca70

Please sign in to comment.