From 479ca6128341570e5f02d63466eddc786c359015 Mon Sep 17 00:00:00 2001 From: James Yin Date: Fri, 7 Sep 2018 13:15:59 +0800 Subject: [PATCH 1/2] supported new uvloop(>=0.9.0) --- src/japronto/protocol/cprotocol.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/japronto/protocol/cprotocol.c b/src/japronto/protocol/cprotocol.c index 204c808..bc0f09d 100644 --- a/src/japronto/protocol/cprotocol.c +++ b/src/japronto/protocol/cprotocol.c @@ -190,7 +190,9 @@ Protocol_connection_made(Protocol* self, PyObject* transport) #endif PyObject* get_extra_info = NULL; - PySocketSockObject* socket = NULL; + PyObject* socket = NULL; + PyObject* socket_fileno = NULL; + PyObject* fileno = NULL; PyObject* connections = NULL; self->transport = transport; Py_INCREF(self->transport); @@ -198,13 +200,22 @@ Protocol_connection_made(Protocol* self, PyObject* transport) if(!(get_extra_info = PyObject_GetAttrString(transport, "get_extra_info"))) goto error; - if(!(socket = (PySocketSockObject*)PyObject_CallFunctionObjArgs( + // NOTE: this will return a PseudoSocket object(uvloop>=0.9.0), not PySocketSockObject(socket_socket in uvloop<0.9.0) + if(!(socket = PyObject_CallFunctionObjArgs( get_extra_info, socket_str, NULL))) goto error; + if(!(socket_fileno = PyObject_GetAttrString(socket, "fileno"))) + goto error; + + if(!(fileno = PyObject_CallFunctionObjArgs(socket_fileno, NULL))) + goto error; + + int sock_fd = (int)PyLong_AsLong(fileno); const int on = 1; - if(setsockopt(socket->sock_fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) != 0) + // in uvloop(>=0.9.0) TCPTransport.new, the socket have set nodelay now. + if(setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) != 0) goto error; if(!(self->write = PyObject_GetAttrString(transport, "write"))) From 847b62823916022d2e2767aebf395411495b7694 Mon Sep 17 00:00:00 2001 From: James Yin Date: Tue, 11 Sep 2018 06:47:27 +0800 Subject: [PATCH 2/2] update uvloop to 0.9.0+ --- setup.py | 3 +- src/japronto/protocol/cprotocol.c | 46 ------------------------------- 2 files changed, 2 insertions(+), 47 deletions(-) diff --git a/setup.py b/setup.py index 5bc0991..a6681c4 100644 --- a/setup.py +++ b/setup.py @@ -33,7 +33,7 @@ keywords=['web', 'asyncio'], platforms='x86_64 Linux and MacOS X', install_requires=[ - 'uvloop<0.9.0', + 'uvloop>=0.9.0', ], entry_points=""" [console_scripts] @@ -49,6 +49,7 @@ 'Programming Language :: C', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: Implementation :: CPython', 'Topic :: Internet :: WWW/HTTP' ], diff --git a/src/japronto/protocol/cprotocol.c b/src/japronto/protocol/cprotocol.c index bc0f09d..0473cd5 100644 --- a/src/japronto/protocol/cprotocol.c +++ b/src/japronto/protocol/cprotocol.c @@ -14,8 +14,6 @@ static PyObject* Parser; static PyObject* PyRequest; static PyObject* RouteNotFoundException; -static PyObject* socket_str; - static Request_CAPI* request_capi; static Matcher_CAPI* matcher_capi; static Response_CAPI* response_capi; @@ -165,19 +163,6 @@ Protocol_init(Protocol* self, PyObject *args, PyObject *kw) } -// copied from Modules/socketmodule.h -// FIXME on Windows SOCKET_T is a different type -typedef int SOCKET_T; -typedef struct { - PyObject_HEAD - SOCKET_T sock_fd; - // more things here that we dont need -} PySocketSockObject; - - -#include -#include - static PyObject* Protocol_connection_made(Protocol* self, PyObject* transport) { @@ -189,35 +174,10 @@ Protocol_connection_made(Protocol* self, PyObject* transport) self->false_cnt = Py_REFCNT(Py_False); #endif - PyObject* get_extra_info = NULL; - PyObject* socket = NULL; - PyObject* socket_fileno = NULL; - PyObject* fileno = NULL; PyObject* connections = NULL; self->transport = transport; Py_INCREF(self->transport); - if(!(get_extra_info = PyObject_GetAttrString(transport, "get_extra_info"))) - goto error; - - // NOTE: this will return a PseudoSocket object(uvloop>=0.9.0), not PySocketSockObject(socket_socket in uvloop<0.9.0) - if(!(socket = PyObject_CallFunctionObjArgs( - get_extra_info, socket_str, NULL))) - goto error; - - if(!(socket_fileno = PyObject_GetAttrString(socket, "fileno"))) - goto error; - - if(!(fileno = PyObject_CallFunctionObjArgs(socket_fileno, NULL))) - goto error; - - int sock_fd = (int)PyLong_AsLong(fileno); - const int on = 1; - - // in uvloop(>=0.9.0) TCPTransport.new, the socket have set nodelay now. - if(setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof(on)) != 0) - goto error; - if(!(self->write = PyObject_GetAttrString(transport, "write"))) goto error; @@ -245,8 +205,6 @@ Protocol_connection_made(Protocol* self, PyObject* transport) finally: Py_XDECREF(connections); - Py_XDECREF(socket); - Py_XDECREF(get_extra_info); Py_RETURN_NONE; } @@ -924,7 +882,6 @@ PyInit_cprotocol(void) PyObject* api_capsule = NULL; PyObject* crequest = NULL; PyObject* route = NULL; - socket_str = NULL; if (PyType_Ready(&ProtocolType) < 0) goto error; @@ -979,9 +936,6 @@ PyInit_cprotocol(void) if(!response_capi) goto error; - if(!(socket_str = PyUnicode_FromString("socket"))) - goto error; - Py_INCREF(&ProtocolType); PyModule_AddObject(m, "Protocol", (PyObject*)&ProtocolType);