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

Updates for Cython3 #587

Merged
merged 17 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _default: compile


clean:
rm -fr dist/ doc/_build/ *.egg-info uvloop/loop.*.pyd
rm -fr dist/ doc/_build/ *.egg-info uvloop/loop.*.pyd uvloop/loop_d.*.pyd
rm -fr uvloop/*.c uvloop/*.html uvloop/*.so
rm -fr uvloop/handles/*.html uvloop/includes/*.html
find . -name '__pycache__' | xargs rm -rf
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from setuptools.command.sdist import sdist


CYTHON_DEPENDENCY = 'Cython(>=0.29.36,<0.30.0)'
CYTHON_DEPENDENCY = 'Cython(>=0.29.36)'
MACHINE = platform.machine()
MODULES_CFLAGS = [os.getenv('UVLOOP_OPT_CFLAGS', '-O2')]
_ROOT = pathlib.Path(__file__).parent
Expand Down Expand Up @@ -144,7 +144,9 @@ def finalize_options(self):
self.distribution.ext_modules[:] = cythonize(
self.distribution.ext_modules,
compiler_directives=directives,
annotate=self.cython_annotate)
annotate=self.cython_annotate,
compile_time_env=dict(DEFAULT_FREELIST_SIZE=250, SSL_READ_MAX_SIZE=256 * 1024),
emit_linenums=True)

super().finalize_options()

Expand Down
4 changes: 2 additions & 2 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ def test_process_delayed_stdio__paused__stdin_pipe(self):
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
__uvloop_sleep_after_fork=True))
fantix marked this conversation as resolved.
Show resolved Hide resolved
uvloop_sleep_after_fork=True))
self.assertIsNot(transport, None)
self.assertEqual(transport.get_returncode(), 0)
self.assertEqual(
Expand All @@ -931,7 +931,7 @@ def test_process_delayed_stdio__paused__no_stdin(self):
stdin=None,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
__uvloop_sleep_after_fork=True))
uvloop_sleep_after_fork=True))
self.assertIsNot(transport, None)
self.assertEqual(transport.get_returncode(), 0)
self.assertEqual(
Expand Down
19 changes: 12 additions & 7 deletions tests/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1630,17 +1630,22 @@ async def client(addr):
self.fail("unexpected call to connection_made()")

def test_ssl_connect_accepted_socket(self):
if hasattr(ssl, 'PROTOCOL_TLS'):
proto = ssl.PROTOCOL_TLS
if hasattr(ssl, 'PROTOCOL_TLS_SERVER'):
server_proto = ssl.PROTOCOL_TLS_SERVER
client_proto = ssl.PROTOCOL_TLS_CLIENT
else:
proto = ssl.PROTOCOL_SSLv23
server_context = ssl.SSLContext(proto)
if hasattr(ssl, 'PROTOCOL_TLS'):
client_proto = server_proto = ssl.PROTOCOL_TLS
else:
client_proto = server_proto = ssl.PROTOCOL_SSLv23

server_context = ssl.SSLContext(server_proto)
server_context.load_cert_chain(self.ONLYCERT, self.ONLYKEY)
if hasattr(server_context, 'check_hostname'):
server_context.check_hostname = False
server_context.verify_mode = ssl.CERT_NONE

client_context = ssl.SSLContext(proto)
client_context = ssl.SSLContext(client_proto)
if hasattr(server_context, 'check_hostname'):
client_context.check_hostname = False
client_context.verify_mode = ssl.CERT_NONE
Expand Down Expand Up @@ -2233,7 +2238,7 @@ def test_renegotiation(self):
sslctx.use_privatekey_file(self.ONLYKEY)
sslctx.use_certificate_chain_file(self.ONLYCERT)
client_sslctx = self._create_client_ssl_context()
if hasattr(ssl, 'OP_NO_TLSv1_3'):
if sys.version_info < (3, 8) and hasattr(ssl, 'OP_NO_TLSv1_3'):
client_sslctx.options |= ssl.OP_NO_TLSv1_3

def server(sock):
Expand Down Expand Up @@ -2592,7 +2597,7 @@ def test_flush_before_shutdown(self):
sslctx_openssl.use_privatekey_file(self.ONLYKEY)
sslctx_openssl.use_certificate_chain_file(self.ONLYCERT)
client_sslctx = self._create_client_ssl_context()
if hasattr(ssl, 'OP_NO_TLSv1_3'):
if sys.version_info < (3, 8) and hasattr(ssl, 'OP_NO_TLSv1_3'):
client_sslctx.options |= ssl.OP_NO_TLSv1_3

future = None
Expand Down
4 changes: 3 additions & 1 deletion uvloop/_testbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ def find_free_port(start_from=50000):
class SSLTestCase:

def _create_server_ssl_context(self, certfile, keyfile=None):
if hasattr(ssl, 'PROTOCOL_TLS'):
if hasattr(ssl, 'PROTOCOL_TLS_SERVER'):
sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
elif hasattr(ssl, 'PROTOCOL_TLS'):
sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS)
else:
sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
Expand Down
4 changes: 2 additions & 2 deletions uvloop/cbhandles.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ cdef class Handle:
self.arg1, self.arg2, self.arg3, self.arg4)

else:
raise RuntimeError('invalid Handle.cb_type: {}'.format(
cb_type))
raise RuntimeError('invalid Handle.cb_type: {} {!r}/{!r}'.format(
cb_type, self.args1, self.args2))
fantix marked this conversation as resolved.
Show resolved Hide resolved

except (KeyboardInterrupt, SystemExit):
raise
Expand Down
4 changes: 2 additions & 2 deletions uvloop/dns.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ cdef class AddrInfo:
uv.uv_freeaddrinfo(self.data) # returns void
self.data = NULL

cdef void set_data(self, system.addrinfo *data):
cdef void set_data(self, system.addrinfo *data) noexcept:
self.data = data

cdef unpack(self):
Expand Down Expand Up @@ -326,7 +326,7 @@ cdef class AddrInfo:
return result

@staticmethod
cdef int isinstance(object other):
cdef int isinstance(object other) noexcept:
return type(other) is AddrInfo


Expand Down
4 changes: 2 additions & 2 deletions uvloop/handles/basetransport.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ cdef class UVBaseTransport(UVSocketHandle):
# === overloads ===

cdef _new_socket(self)
cdef size_t _get_write_buffer_size(self)
cdef size_t _get_write_buffer_size(self) noexcept

cdef bint _is_reading(self)
cdef bint _is_reading(self) noexcept
cdef _start_reading(self)
cdef _stop_reading(self)
4 changes: 2 additions & 2 deletions uvloop/handles/basetransport.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cdef class UVBaseTransport(UVSocketHandle):

self._closing = 0

cdef size_t _get_write_buffer_size(self):
cdef size_t _get_write_buffer_size(self) noexcept:
return 0

cdef inline _schedule_call_connection_made(self):
Expand Down Expand Up @@ -211,7 +211,7 @@ cdef class UVBaseTransport(UVSocketHandle):
self._extra_info = {}
self._extra_info[name] = obj

cdef bint _is_reading(self):
cdef bint _is_reading(self) noexcept:
raise NotImplementedError

cdef _start_reading(self):
Expand Down
2 changes: 1 addition & 1 deletion uvloop/handles/handle.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ cdef void __uv_close_handle_cb(uv.uv_handle_t* handle) noexcept with gil:
Py_DECREF(h) # Was INCREFed in UVHandle._close


cdef void __close_all_handles(Loop loop):
cdef void __close_all_handles(Loop loop) noexcept:
uv.uv_walk(loop.uvloop,
__uv_walk_close_all_handles_cb,
<void*>loop) # void
Expand Down
2 changes: 1 addition & 1 deletion uvloop/handles/pipe.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cdef __pipe_init_uv_handle(UVStream handle, Loop loop):
cdef __pipe_open(UVStream handle, int fd):
cdef int err
err = uv.uv_pipe_open(<uv.uv_pipe_t *>handle._handle,
<uv.uv_file>fd)
<uv.uv_os_fd_t>fd)
if err < 0:
exc = convert_error(err)
raise exc
Expand Down
2 changes: 1 addition & 1 deletion uvloop/handles/poll.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cdef class UVPoll(UVHandle):
cdef inline _poll_start(self, int flags)
cdef inline _poll_stop(self)

cdef int is_active(self)
cdef int is_active(self) noexcept

cdef is_reading(self)
cdef is_writing(self)
Expand Down
2 changes: 1 addition & 1 deletion uvloop/handles/poll.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cdef class UVPoll(UVHandle):
handle._init(loop, fd)
return handle

cdef int is_active(self):
cdef int is_active(self) noexcept:
return (self.reading_handle is not None or
self.writing_handle is not None)

Expand Down
16 changes: 10 additions & 6 deletions uvloop/handles/stream.pyx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
DEF __PREALLOCED_BUFS = 4
cdef extern from *:
'''
enum {__PREALLOCED_BUFS = 4};
'''
const bint __PREALLOCED_BUFS
Comment on lines -1 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Am curious whether it would make sense to make this a Cython enum (or if there was some issue seen doing that)

Suggested change
DEF __PREALLOCED_BUFS = 4
cdef extern from *:
'''
enum {__PREALLOCED_BUFS = 4};
'''
const bint __PREALLOCED_BUFS
cdef enum:
__PREALLOCED_BUFS = 4

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this is a good suggestion - it pretty much generates the same C code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Fantix! 🙏

Submitted a PR: #634



@cython.no_gc_clear
Expand Down Expand Up @@ -279,7 +283,7 @@ cdef class UVStream(UVBaseTransport):
cdef inline _close_on_read_error(self):
self.__read_error_close = 1

cdef bint _is_reading(self):
cdef bint _is_reading(self) noexcept:
return self.__reading

cdef _start_reading(self):
Expand Down Expand Up @@ -578,7 +582,7 @@ cdef class UVStream(UVBaseTransport):

self._maybe_resume_protocol()

cdef size_t _get_write_buffer_size(self):
cdef size_t _get_write_buffer_size(self) noexcept:
if self._handle is NULL:
return 0
return ((<uv.uv_stream_t*>self._handle).write_queue_size +
Expand Down Expand Up @@ -755,7 +759,7 @@ cdef inline bint __uv_stream_on_read_common(
UVStream sc,
Loop loop,
ssize_t nread,
):
) noexcept:
if sc._closed:
# The stream was closed, there is no reason to
# do any work now.
Expand Down Expand Up @@ -818,7 +822,7 @@ cdef inline void __uv_stream_on_read_impl(
uv.uv_stream_t* stream,
ssize_t nread,
const uv.uv_buf_t* buf,
):
) noexcept:
cdef:
UVStream sc = <UVStream>stream.data
Loop loop = sc._loop
Expand Down Expand Up @@ -849,7 +853,7 @@ cdef inline void __uv_stream_on_read_impl(
cdef inline void __uv_stream_on_write_impl(
uv.uv_write_t* req,
int status,
):
) noexcept:
cdef:
_StreamWriteContext ctx = <_StreamWriteContext> req.data
UVStream stream = <UVStream>ctx.stream
Expand Down
4 changes: 2 additions & 2 deletions uvloop/handles/udp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ cdef class UDPTransport(UVBaseTransport):
exc = convert_error(err)
raise exc

cdef size_t _get_write_buffer_size(self):
cdef size_t _get_write_buffer_size(self) noexcept:
if self._handle is NULL:
return 0
return (<uv.uv_udp_t*>self._handle).send_queue_size

cdef bint _is_reading(self):
cdef bint _is_reading(self) noexcept:
return self.__receiving

cdef _start_reading(self):
Expand Down
25 changes: 0 additions & 25 deletions uvloop/includes/consts.pxi

This file was deleted.

6 changes: 5 additions & 1 deletion uvloop/includes/fork_handler.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#ifndef UVLOOP_FORK_HANDLER_H_
#define UVLOOP_FORK_HANDLER_H_

volatile uint64_t MAIN_THREAD_ID = 0;
volatile int8_t MAIN_THREAD_ID_SET = 0;

typedef void (*OnForkHandler)();
typedef void (*OnForkHandler)(void);

OnForkHandler __forkHandler = NULL;

Expand Down Expand Up @@ -36,3 +39,4 @@ void setMainThreadID(uint64_t id) {
MAIN_THREAD_ID = id;
MAIN_THREAD_ID_SET = 1;
}
#endif
16 changes: 16 additions & 0 deletions uvloop/includes/system.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef UVLOOP_SYSTEM_H_
#define UVLOOP_SYSTEM_H_
#if defined(_WIN32) || defined(MS_WINDOWS) || defined(_MSC_VER)
#include "Winsock2.h"
#include "ws2def.h"
#include "includes/fork_handler.h"
#else
fantix marked this conversation as resolved.
Show resolved Hide resolved
#include "arpa/inet.h"
#include "sys/socket.h"
#include "sys/un.h"
#include "unistd.h"
#include "pthread.h"
#endif
#endif


12 changes: 4 additions & 8 deletions uvloop/includes/system.pxd
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
from libc.stdint cimport int8_t, uint64_t

cdef extern from "arpa/inet.h" nogil:

int ntohl(int)
int htonl(int)
int ntohs(int)


cdef extern from "sys/socket.h" nogil:
cdef extern from "includes/system.h":
int ntohl(int) nogil
int htonl(int) nogil
int ntohs(int) nogil

struct sockaddr:
unsigned short sa_family
Expand Down
6 changes: 3 additions & 3 deletions uvloop/includes/uv.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ cdef extern from "uv.h" nogil:
UV_LEAVE_GROUP = 0,
UV_JOIN_GROUP

cpdef enum uv_fs_event:
cdef enum uv_fs_event:
UV_RENAME = 1,
UV_CHANGE = 2

Expand Down Expand Up @@ -282,7 +282,7 @@ cdef extern from "uv.h" nogil:
int uv_loop_close(uv_loop_t* loop)
int uv_loop_alive(uv_loop_t* loop)
int uv_loop_fork(uv_loop_t* loop)
int uv_backend_fd(uv_loop_t* loop)
uv_os_fd_t uv_backend_fd(uv_loop_t* loop)

void uv_update_time(uv_loop_t* loop)
uint64_t uv_now(const uv_loop_t*)
Expand Down Expand Up @@ -378,7 +378,7 @@ cdef extern from "uv.h" nogil:
# Pipes

int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc)
int uv_pipe_open(uv_pipe_t* handle, uv_file file)
int uv_pipe_open(uv_pipe_t* handle, uv_os_fd_t file)
int uv_pipe_bind(uv_pipe_t* handle, const char* name)

void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle,
Expand Down
Loading