Skip to content

Commit

Permalink
First unified Windows and non-Windows version.
Browse files Browse the repository at this point in the history
This winloop version also compiles under WSL, using make/setup files from uvloop. The winloop\tests pass on Windows and on WSL.
  • Loading branch information
lschoe committed Jul 2, 2024
1 parent 8e1753c commit 15f1b54
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 61 deletions.
7 changes: 4 additions & 3 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,11 +995,12 @@ def test_process_delayed_stdio__paused__stdin_pipe(self):
('CM', transport),
'PROC_EXIT',
('STDOUT', b'1' + NL),
('STDOUT', 'LOST'),
('STDOUT', 'LOST')
}.union(
# Winloop comment: connection lost is not called because of
# issues with stdin pipe. See process.__socketpair().
# ('CL', 0, None)
})
{('CL', 0, None)} if sys.platform != 'win32' else {}
))

def test_process_delayed_stdio__paused__no_stdin(self):
transport, proto = self.loop.run_until_complete(
Expand Down
4 changes: 2 additions & 2 deletions tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ async def runner():
PROG = R"""\
import asyncio
import signal
import uvloop
import winloop
srv = None
Expand Down Expand Up @@ -269,7 +269,7 @@ async def runner():
PROG = R"""\
import asyncio
import signal
import uvloop
import winloop
srv = None
Expand Down
2 changes: 1 addition & 1 deletion winloop/dns.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ cdef __convert_pyaddr_to_sockaddr(int family, object addr,
(<system.sockaddr_in6*>&ret.addr).sin6_flowinfo = flowinfo
(<system.sockaddr_in6*>&ret.addr).sin6_scope_id = scope_id

elif not system.PLATFORM_IS_WINDOWS and addr.sa_family == uv.AF_UNIX:
elif not system.PLATFORM_IS_WINDOWS and family == uv.AF_UNIX:
if isinstance(addr, str):
addr = addr.encode(sys_getfilesystemencoding())
elif not isinstance(addr, bytes):
Expand Down
7 changes: 7 additions & 0 deletions winloop/includes/compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
#ifndef _WIN32
#include <sys/socket.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <unistd.h>
#else
#include <io.h>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <winsock2.h>
#endif
#include "Python.h"
Expand Down Expand Up @@ -48,6 +54,7 @@ int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) {

#ifdef _WIN32
int SIGCHLD = 0;
int SO_REUSEPORT = 0;

struct sockaddr_un {unsigned short sun_family; char* sun_path;};

Expand Down
9 changes: 4 additions & 5 deletions winloop/includes/stdlib.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,13 @@ cdef subprocess_SubprocessError = subprocess.SubprocessError

cdef int signal_NSIG = signal.NSIG
cdef signal_signal = signal.signal
# FIXME This will need to be handled differently on enclosure...
# cdef signal_siginterrupt = signal.siginterrupt
# "In it's replacement, I'll use SIGABRT Unless some other developer finds problems with this" - Vizonex
#cdef int signal_SIGABRT = signal.SIGABRT
cdef signal_siginterrupt = getattr(signal, 'siginterrupt', None)
# "I'll use SIGABRT Unless some other developer finds problems with this" - Vizonex
cdef signal_SIGABRT = signal.SIGABRT
cdef signal_SIGINT = signal.SIGINT
cdef signal_set_wakeup_fd = signal.set_wakeup_fd
cdef signal_default_int_handler = signal.default_int_handler
cdef signal_SIG_DFL = signal.SIG_DFL
cdef signal_SIGINT = signal.SIGINT

cdef time_sleep = time.sleep
cdef time_monotonic = time.monotonic
Expand Down
40 changes: 12 additions & 28 deletions winloop/includes/system.pxd
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
from libc.stdint cimport int8_t, uint64_t

cdef extern from "includes/compat.h" nogil:

cdef extern from "winsock2.h" nogil:
"""
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
"""

unsigned long ntohl(unsigned long)
unsigned long htonl(unsigned long)
unsigned long ntohs(unsigned long)

ctypedef unsigned long long UINT_PTR
ctypedef UINT_PTR SOCKET
int ntohl(int)
int htonl(int)
int ntohs(int)

struct sockaddr:
unsigned short sa_family
Expand Down Expand Up @@ -47,17 +38,19 @@ cdef extern from "winsock2.h" nogil:

const char *gai_strerror(int errcode)

int setsockopt(SOCKET socket, int level, int option_name,
int socketpair(int domain, int type, int protocol, int socket_vector[2])

int setsockopt(int socket, int level, int option_name,
const void *option_value, int option_len)

struct sockaddr_un:
unsigned short sun_family
char* sun_path
# ...

cdef extern from "io.h" nogil:
int _write(int _FileHandle, const void *_Buf, unsigned int _MaxCharCount)
ssize_t write(int fd, const void *buf, size_t count)
void _exit(int status)


cdef extern from "includes/compat.h" nogil:

cdef int EWOULDBLOCK

cdef int PLATFORM_IS_APPLE
Expand All @@ -70,17 +63,8 @@ cdef extern from "includes/compat.h" nogil:

int EPOLL_CTL_DEL
int epoll_ctl(int epfd, int op, int fd, epoll_event *event)

struct sockaddr_un:
unsigned short sun_family
char* sun_path
# ...

object MakeUnixSockPyAddr(sockaddr_un *addr)

int socketpair(int domain, int type, int protocol, int socket_vector[2])

int write(int fd, const void *buf, unsigned int count)

cdef extern from "includes/fork_handler.h":

Expand Down
16 changes: 4 additions & 12 deletions winloop/includes/uv.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ from libc.stdint cimport uint16_t, uint32_t, uint64_t, int64_t
cdef extern from "includes/compat.h" nogil:
int getuid()
int SIGCHLD
int SO_REUSEPORT

from . cimport system

Expand Down Expand Up @@ -57,8 +58,8 @@ cdef extern from "uv.h" nogil:

cdef int SOL_SOCKET
cdef int SO_ERROR


cdef int SO_REUSEADDR
cdef int SO_BROADCAST
cdef int AF_INET
cdef int AF_INET6
cdef int AF_UNIX
Expand Down Expand Up @@ -226,6 +227,7 @@ cdef extern from "uv.h" nogil:

const char* uv_strerror(int err)
const char* uv_err_name(int err)
int uv_translate_sys_error(int sys_errno)

ctypedef void (*uv_walk_cb)(uv_handle_t* handle, void* arg) with gil

Expand Down Expand Up @@ -375,11 +377,6 @@ cdef extern from "uv.h" nogil:
int uv_tcp_connect(uv_connect_t* req, uv_tcp_t* handle,
const system.sockaddr* addr, uv_connect_cb cb)


# system errors
int uv_translate_sys_error(int sys_errno)


# Pipes

int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc)
Expand Down Expand Up @@ -517,8 +514,3 @@ cdef extern from "uv.h" nogil:
unsigned int uv_version()

int uv_pipe(uv_file fds[2], int read_flags, int write_flags)


cdef extern from "winsock2.h" nogil:
cdef int SO_REUSEADDR
cdef int SO_BROADCAST
24 changes: 14 additions & 10 deletions winloop/loop.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1778,8 +1778,11 @@ cdef class Loop:
if reuse_address:
sock.setsockopt(uv.SOL_SOCKET, uv.SO_REUSEADDR, 1)
if reuse_port:
# replaced uv.SO_REUSEPORT with uv.SO_BROADCAST because it's the equivalent on windows systems...
sock.setsockopt(uv.SOL_SOCKET, uv.SO_BROADCAST, 1)
if system.PLATFORM_IS_WINDOWS:
# replaced uv.SO_REUSEPORT with uv.SO_BROADCAST because it's the equivalent on windows systems...
sock.setsockopt(uv.SOL_SOCKET, uv.SO_BROADCAST, 1)
else:
sock.setsockopt(uv.SOL_SOCKET, uv.SO_REUSEPORT, 1)
# Disable IPv4/IPv6 dual stack support (enabled by
# default on Linux) which makes a single socket
# listen on both address families.
Expand Down Expand Up @@ -2932,15 +2935,16 @@ cdef class Loop:
self._signal_handlers[sig] = h

try:
# Register a dummy signal handler to ask Python to write the signal
# number in the wakeup file descriptor.
# signal_signal(sig, self.__sighandler)
if not system.PLATFORM_IS_WINDOWS:
# Register a dummy signal handler to ask Python to write the signal
# number in the wakeup file descriptor.
signal_signal(sig, self.__sighandler)

# Set SA_RESTART to limit EINTR occurrences.
# signal_siginterrupt(sig, False)

# XXX "WINDOWS DOESN'T Have a signal_siginterrupt so I'll do this until someone smarter than me wants a tackle at it" - Vizonex
signal_signal(signal_SIGINT, self.__sighandler)
# Set SA_RESTART to limit EINTR occurrences.
signal_siginterrupt(sig, False)
else:
# XXX "WINDOWS DOESN'T Have a signal_siginterrupt so I'll do this until someone smarter than me wants a tackle at it" - Vizonex
signal_signal(signal_SIGINT, self.__sighandler)

except OSError as exc:
del self._signal_handlers[sig]
Expand Down

0 comments on commit 15f1b54

Please sign in to comment.