Skip to content

Commit

Permalink
Add Path support on web app run (#6843)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamirPS committed Jul 24, 2022
1 parent 934db9a commit f90cb19
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES/6839.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``web.run_app()`` now accept a ``Path`` object for the path parameter.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ Robert Lu
Robert Nikolich
Roman Markeloff
Roman Podoliaka
Samir Akarioh
Samuel Colvin
Sean Hunt
Sebastian Acuna
Expand Down
7 changes: 4 additions & 3 deletions aiohttp/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from argparse import ArgumentParser
from collections.abc import Iterable
from importlib import import_module
from pathlib import Path
from typing import (
Any,
Awaitable,
Expand Down Expand Up @@ -290,7 +291,7 @@ async def _run_app(
*,
host: Optional[Union[str, HostSequence]] = None,
port: Optional[int] = None,
path: Optional[str] = None,
path: Optional[Union[str, Path]] = None,
sock: Optional[Union[socket.socket, TypingIterable[socket.socket]]] = None,
shutdown_timeout: float = 60.0,
keepalive_timeout: float = 75.0,
Expand Down Expand Up @@ -366,7 +367,7 @@ async def _run_app(
)

if path is not None:
if isinstance(path, (str, bytes, bytearray, memoryview)):
if isinstance(path, (str, bytes, bytearray, memoryview, Path)):
sites.append(
UnixSite(
runner,
Expand Down Expand Up @@ -464,7 +465,7 @@ def run_app(
debug: bool = False,
host: Optional[Union[str, HostSequence]] = None,
port: Optional[int] = None,
path: Optional[str] = None,
path: Optional[Union[str, Path]] = None,
sock: Optional[Union[socket.socket, TypingIterable[socket.socket]]] = None,
shutdown_timeout: float = 60.0,
keepalive_timeout: float = 75.0,
Expand Down
10 changes: 7 additions & 3 deletions aiohttp/web_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import signal
import socket
from abc import ABC, abstractmethod
from typing import Any, List, Optional, Set, Type
from pathlib import Path
from typing import Any, List, Optional, Set, Type, Union

from yarl import URL

Expand Down Expand Up @@ -141,7 +142,7 @@ class UnixSite(BaseSite):
def __init__(
self,
runner: "BaseRunner",
path: str,
path: Union[str, Path],
*,
shutdown_timeout: float = 60.0,
ssl_context: Optional[SSLContext] = None,
Expand All @@ -166,7 +167,10 @@ async def start(self) -> None:
server = self._runner.server
assert server is not None
self._server = await loop.create_unix_server(
server, self._path, ssl=self._ssl_context, backlog=self._backlog
server,
self._path, # type: ignore[arg-type]
ssl=self._ssl_context,
backlog=self._backlog,
)


Expand Down
4 changes: 2 additions & 2 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2843,10 +2843,10 @@ Utilities
text HTTP and ``8443`` for HTTP via SSL (when
*ssl_context* parameter is specified).

:param str path: file system path for HTTP server Unix domain socket.
:param path: file system path for HTTP server Unix domain socket.
A sequence of file system paths can be used to bind
multiple domain sockets. Listening on Unix domain
sockets is not supported by all operating systems.
sockets is not supported by all operating systems, :class:`str` or :class:`pathlib.Path` .

:param socket.socket sock: a preexisting socket object to accept connections on.
A sequence of socket objects can be passed.
Expand Down
4 changes: 2 additions & 2 deletions tests/test_run_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def test_run_app_custom_backlog_unix(patched_loop: Any) -> None:
def test_run_app_http_unix_socket(patched_loop: Any, tmp_path: Any) -> None:
app = web.Application()

sock_path = str(tmp_path / "socket.sock")
sock_path = tmp_path / "socket.sock"
printer = mock.Mock(wraps=stopper(patched_loop))
web.run_app(app, path=sock_path, print=printer, loop=patched_loop)

Expand All @@ -532,7 +532,7 @@ def test_run_app_http_unix_socket(patched_loop: Any, tmp_path: Any) -> None:
def test_run_app_https_unix_socket(patched_loop: Any, tmp_path: Any) -> None:
app = web.Application()

sock_path = str(tmp_path / "socket.sock")
sock_path = tmp_path / "socket.sock"
ssl_context = ssl.create_default_context()
printer = mock.Mock(wraps=stopper(patched_loop))
web.run_app(
Expand Down

0 comments on commit f90cb19

Please sign in to comment.