Skip to content

Commit 3fba9fa

Browse files
alan-brooksfantix
andauthored
Updates for Cython3 (#587)
* Remove SSL depreciation warnings * Use Cython 3.0 and fix deprecated test --------- Co-authored-by: Fantix King <fantix.king@gmail.com>
1 parent 3c3bbef commit 3fba9fa

15 files changed

+76
-50
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ _default: compile
99

1010

1111
clean:
12-
rm -fr dist/ doc/_build/ *.egg-info uvloop/loop.*.pyd
12+
rm -fr dist/ doc/_build/ *.egg-info uvloop/loop.*.pyd uvloop/loop_d.*.pyd
1313
rm -fr uvloop/*.c uvloop/*.html uvloop/*.so
1414
rm -fr uvloop/handles/*.html uvloop/includes/*.html
1515
find . -name '__pycache__' | xargs rm -rf

pyproject.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,16 @@ test = [
3636
# pycodestyle is a dependency of flake8, but it must be frozen because
3737
# their combination breaks too often
3838
# (example breakage: https://gitlab.com/pycqa/flake8/issues/427)
39-
'aiohttp>=3.8.1; python_version < "3.12"',
40-
'aiohttp==3.9.0b0; python_version >= "3.12"',
39+
'aiohttp>=3.10.5',
4140
'flake8~=5.0',
4241
'psutil',
4342
'pycodestyle~=2.9.0',
4443
'pyOpenSSL~=23.0.0',
4544
'mypy>=0.800',
46-
'Cython(>=0.29.36,<0.30.0)',
45+
]
46+
dev = [
47+
'setuptools>=60',
48+
'Cython~=3.0',
4749
]
4850
docs = [
4951
'Sphinx~=4.1.2',
@@ -55,7 +57,7 @@ docs = [
5557
requires = [
5658
"setuptools>=60",
5759
"wheel",
58-
"Cython(>=0.29.36,<0.30.0)",
60+
"Cython~=3.0",
5961
]
6062
build-backend = "setuptools.build_meta"
6163

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from setuptools.command.sdist import sdist
2222

2323

24-
CYTHON_DEPENDENCY = 'Cython(>=0.29.36,<0.30.0)'
24+
CYTHON_DEPENDENCY = 'Cython~=3.0'
2525
MACHINE = platform.machine()
2626
MODULES_CFLAGS = [os.getenv('UVLOOP_OPT_CFLAGS', '-O2')]
2727
_ROOT = pathlib.Path(__file__).parent
@@ -144,7 +144,9 @@ def finalize_options(self):
144144
self.distribution.ext_modules[:] = cythonize(
145145
self.distribution.ext_modules,
146146
compiler_directives=directives,
147-
annotate=self.cython_annotate)
147+
annotate=self.cython_annotate,
148+
compile_time_env=dict(DEFAULT_FREELIST_SIZE=250),
149+
emit_linenums=self.debug)
148150

149151
super().finalize_options()
150152

tests/test_tcp.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,17 +1631,22 @@ async def client(addr):
16311631
self.fail("unexpected call to connection_made()")
16321632

16331633
def test_ssl_connect_accepted_socket(self):
1634-
if hasattr(ssl, 'PROTOCOL_TLS'):
1635-
proto = ssl.PROTOCOL_TLS
1634+
if hasattr(ssl, 'PROTOCOL_TLS_SERVER'):
1635+
server_proto = ssl.PROTOCOL_TLS_SERVER
1636+
client_proto = ssl.PROTOCOL_TLS_CLIENT
16361637
else:
1637-
proto = ssl.PROTOCOL_SSLv23
1638-
server_context = ssl.SSLContext(proto)
1638+
if hasattr(ssl, 'PROTOCOL_TLS'):
1639+
client_proto = server_proto = ssl.PROTOCOL_TLS
1640+
else:
1641+
client_proto = server_proto = ssl.PROTOCOL_SSLv23
1642+
1643+
server_context = ssl.SSLContext(server_proto)
16391644
server_context.load_cert_chain(self.ONLYCERT, self.ONLYKEY)
16401645
if hasattr(server_context, 'check_hostname'):
16411646
server_context.check_hostname = False
16421647
server_context.verify_mode = ssl.CERT_NONE
16431648

1644-
client_context = ssl.SSLContext(proto)
1649+
client_context = ssl.SSLContext(client_proto)
16451650
if hasattr(server_context, 'check_hostname'):
16461651
client_context.check_hostname = False
16471652
client_context.verify_mode = ssl.CERT_NONE
@@ -2234,8 +2239,7 @@ def test_renegotiation(self):
22342239
sslctx.use_privatekey_file(self.ONLYKEY)
22352240
sslctx.use_certificate_chain_file(self.ONLYCERT)
22362241
client_sslctx = self._create_client_ssl_context()
2237-
if hasattr(ssl, 'OP_NO_TLSv1_3'):
2238-
client_sslctx.options |= ssl.OP_NO_TLSv1_3
2242+
client_sslctx.maximum_version = ssl.TLSVersion.TLSv1_2
22392243

22402244
def server(sock):
22412245
conn = openssl_ssl.Connection(sslctx, sock)
@@ -2593,8 +2597,7 @@ def test_flush_before_shutdown(self):
25932597
sslctx_openssl.use_privatekey_file(self.ONLYKEY)
25942598
sslctx_openssl.use_certificate_chain_file(self.ONLYCERT)
25952599
client_sslctx = self._create_client_ssl_context()
2596-
if hasattr(ssl, 'OP_NO_TLSv1_3'):
2597-
client_sslctx.options |= ssl.OP_NO_TLSv1_3
2600+
client_sslctx.maximum_version = ssl.TLSVersion.TLSv1_2
25982601

25992602
future = None
26002603

uvloop/_testbase.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,9 @@ def find_free_port(start_from=50000):
269269
class SSLTestCase:
270270

271271
def _create_server_ssl_context(self, certfile, keyfile=None):
272-
if hasattr(ssl, 'PROTOCOL_TLS'):
272+
if hasattr(ssl, 'PROTOCOL_TLS_SERVER'):
273+
sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
274+
elif hasattr(ssl, 'PROTOCOL_TLS'):
273275
sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLS)
274276
else:
275277
sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)

uvloop/dns.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ cdef class AddrInfo:
298298
uv.uv_freeaddrinfo(self.data) # returns void
299299
self.data = NULL
300300

301-
cdef void set_data(self, system.addrinfo *data):
301+
cdef void set_data(self, system.addrinfo *data) noexcept:
302302
self.data = data
303303

304304
cdef unpack(self):

uvloop/handles/handle.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ cdef void __uv_close_handle_cb(uv.uv_handle_t* handle) noexcept with gil:
363363
Py_DECREF(h) # Was INCREFed in UVHandle._close
364364

365365

366-
cdef void __close_all_handles(Loop loop):
366+
cdef void __close_all_handles(Loop loop) noexcept:
367367
uv.uv_walk(loop.uvloop,
368368
__uv_walk_close_all_handles_cb,
369369
<void*>loop) # void

uvloop/handles/pipe.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ cdef __pipe_init_uv_handle(UVStream handle, Loop loop):
2525
cdef __pipe_open(UVStream handle, int fd):
2626
cdef int err
2727
err = uv.uv_pipe_open(<uv.uv_pipe_t *>handle._handle,
28-
<uv.uv_file>fd)
28+
<uv.uv_os_fd_t>fd)
2929
if err < 0:
3030
exc = convert_error(err)
3131
raise exc

uvloop/handles/poll.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ cdef class UVPoll(UVHandle):
1010
cdef inline _poll_start(self, int flags)
1111
cdef inline _poll_stop(self)
1212

13-
cdef int is_active(self)
13+
cdef int is_active(self) noexcept
1414

1515
cdef is_reading(self)
1616
cdef is_writing(self)

uvloop/handles/poll.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ cdef class UVPoll(UVHandle):
2929
handle._init(loop, fd)
3030
return handle
3131

32-
cdef int is_active(self):
32+
cdef int is_active(self) noexcept:
3333
return (self.reading_handle is not None or
3434
self.writing_handle is not None)
3535

uvloop/handles/stream.pyx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
DEF __PREALLOCED_BUFS = 4
1+
cdef extern from *:
2+
'''
3+
enum {__PREALLOCED_BUFS = 4};
4+
'''
5+
const bint __PREALLOCED_BUFS
26

37

48
@cython.no_gc_clear

uvloop/includes/consts.pxi

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
1-
DEF UV_STREAM_RECV_BUF_SIZE = 256000 # 250kb
1+
cdef enum:
2+
UV_STREAM_RECV_BUF_SIZE = 256000 # 250kb
23

3-
DEF FLOW_CONTROL_HIGH_WATER = 64 # KiB
4-
DEF FLOW_CONTROL_HIGH_WATER_SSL_READ = 256 # KiB
5-
DEF FLOW_CONTROL_HIGH_WATER_SSL_WRITE = 512 # KiB
4+
FLOW_CONTROL_HIGH_WATER = 64 # KiB
5+
FLOW_CONTROL_HIGH_WATER_SSL_READ = 256 # KiB
6+
FLOW_CONTROL_HIGH_WATER_SSL_WRITE = 512 # KiB
67

7-
DEF DEFAULT_FREELIST_SIZE = 250
8-
DEF DNS_PYADDR_TO_SOCKADDR_CACHE_SIZE = 2048
8+
DEFAULT_FREELIST_SIZE = 250
9+
DNS_PYADDR_TO_SOCKADDR_CACHE_SIZE = 2048
910

10-
DEF DEBUG_STACK_DEPTH = 10
11+
DEBUG_STACK_DEPTH = 10
1112

1213

13-
DEF __PROCESS_DEBUG_SLEEP_AFTER_FORK = 1
14+
__PROCESS_DEBUG_SLEEP_AFTER_FORK = 1
1415

1516

16-
DEF LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5
17+
LOG_THRESHOLD_FOR_CONNLOST_WRITES = 5
18+
SSL_READ_MAX_SIZE = 256 * 1024
1719

1820

19-
# Number of seconds to wait for SSL handshake to complete
20-
# The default timeout matches that of Nginx.
21-
DEF SSL_HANDSHAKE_TIMEOUT = 60.0
22-
# Number of seconds to wait for SSL shutdown to complete
23-
# The default timeout mimics lingering_time
24-
DEF SSL_SHUTDOWN_TIMEOUT = 30.0
25-
DEF SSL_READ_MAX_SIZE = 256 * 1024
21+
cdef extern from *:
22+
'''
23+
// Number of seconds to wait for SSL handshake to complete
24+
// The default timeout matches that of Nginx.
25+
#define SSL_HANDSHAKE_TIMEOUT 60.0
26+
27+
// Number of seconds to wait for SSL shutdown to complete
28+
// The default timeout mimics lingering_time
29+
#define SSL_SHUTDOWN_TIMEOUT 30.0
30+
'''
31+
32+
const float SSL_HANDSHAKE_TIMEOUT
33+
const float SSL_SHUTDOWN_TIMEOUT

uvloop/includes/fork_handler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
#ifndef UVLOOP_FORK_HANDLER_H_
2+
#define UVLOOP_FORK_HANDLER_H_
3+
14
volatile uint64_t MAIN_THREAD_ID = 0;
25
volatile int8_t MAIN_THREAD_ID_SET = 0;
36

4-
typedef void (*OnForkHandler)();
7+
typedef void (*OnForkHandler)(void);
58

69
OnForkHandler __forkHandler = NULL;
710

@@ -36,3 +39,4 @@ void setMainThreadID(uint64_t id) {
3639
MAIN_THREAD_ID = id;
3740
MAIN_THREAD_ID_SET = 1;
3841
}
42+
#endif

uvloop/includes/uv.pxd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ cdef extern from "uv.h" nogil:
220220
UV_LEAVE_GROUP = 0,
221221
UV_JOIN_GROUP
222222

223-
cpdef enum uv_fs_event:
223+
cdef enum uv_fs_event:
224224
UV_RENAME = 1,
225225
UV_CHANGE = 2
226226

@@ -282,7 +282,7 @@ cdef extern from "uv.h" nogil:
282282
int uv_loop_close(uv_loop_t* loop)
283283
int uv_loop_alive(uv_loop_t* loop)
284284
int uv_loop_fork(uv_loop_t* loop)
285-
int uv_backend_fd(uv_loop_t* loop)
285+
uv_os_fd_t uv_backend_fd(uv_loop_t* loop)
286286

287287
void uv_update_time(uv_loop_t* loop)
288288
uint64_t uv_now(const uv_loop_t*)
@@ -378,7 +378,7 @@ cdef extern from "uv.h" nogil:
378378
# Pipes
379379

380380
int uv_pipe_init(uv_loop_t* loop, uv_pipe_t* handle, int ipc)
381-
int uv_pipe_open(uv_pipe_t* handle, uv_file file)
381+
int uv_pipe_open(uv_pipe_t* handle, uv_os_fd_t file)
382382
int uv_pipe_bind(uv_pipe_t* handle, const char* name)
383383

384384
void uv_pipe_connect(uv_connect_t* req, uv_pipe_t* handle,

uvloop/loop.pyx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ from cpython.pycapsule cimport PyCapsule_New, PyCapsule_GetPointer
4343
from . import _noop
4444

4545

46-
include "includes/consts.pxi"
4746
include "includes/stdlib.pxi"
4847

4948
include "errors.pyx"
@@ -1118,7 +1117,7 @@ cdef class Loop:
11181117

11191118
cdef _sock_set_reuseport(self, int fd):
11201119
cdef:
1121-
int err
1120+
int err = 0
11221121
int reuseport_flag = 1
11231122

11241123
err = system.setsockopt(
@@ -1396,8 +1395,7 @@ cdef class Loop:
13961395
def set_debug(self, enabled):
13971396
self._debug = bool(enabled)
13981397
if self.is_running():
1399-
self.call_soon_threadsafe(
1400-
self._set_coroutine_debug, self, self._debug)
1398+
self.call_soon_threadsafe(self._set_coroutine_debug, self._debug)
14011399

14021400
def is_running(self):
14031401
"""Return whether the event loop is currently running."""
@@ -2749,8 +2747,7 @@ cdef class Loop:
27492747
start_new_session=False,
27502748
executable=None,
27512749
pass_fds=(),
2752-
# For tests only! Do not use in your code. Ever.
2753-
__uvloop_sleep_after_fork=False):
2750+
**kwargs):
27542751

27552752
# TODO: Implement close_fds (might not be very important in
27562753
# Python 3.5, since all FDs aren't inheritable by default.)
@@ -2770,8 +2767,12 @@ cdef class Loop:
27702767
if executable is not None:
27712768
args[0] = executable
27722769

2773-
if __uvloop_sleep_after_fork:
2770+
# For tests only! Do not use in your code. Ever.
2771+
if kwargs.pop("__uvloop_sleep_after_fork", False):
27742772
debug_flags |= __PROCESS_DEBUG_SLEEP_AFTER_FORK
2773+
if kwargs:
2774+
raise ValueError(
2775+
'unexpected kwargs: {}'.format(', '.join(kwargs.keys())))
27752776

27762777
waiter = self._new_future()
27772778
protocol = protocol_factory()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy