FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python/src/eventloop.cpp at develop · Sera91/python · GitHub
Sera91
/
python
Public
forked from
BoostGSoC21/python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python
/
src
/
eventloop.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
475 lines (442 loc) · 15.1 KB
Breadcrumbs
python
/
src
/
eventloop.cpp
Copy path
File metadata and controls
475 lines (442 loc) · 15.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//
Copyright Pan Yue 2021.
//
Distributed under the Boost Software License, Version 1.0. (See
//
accompanying file LICENSE_1_0.txt or copy at
//
http://www.boost.org/LICENSE_1_0.txt)
//
TODO:
//
1. posix::stream_descriptor need windows version
//
2. call_* need return async.Handle
//
3. _ensure_fd_no_transport
//
4. _ensure_resolve
#
include
<
errno.h
>
#
include
<
boost/asio.hpp
>
#
include
<
boost/bind.hpp
>
#
include
<
boost/python.hpp
>
#
include
<
boost/python/eventloop.hpp
>
#
include
<
boost/mpl/vector.hpp
>
#
include
<
Python.h
>
namespace
boost
{
namespace
python
{
namespace
asio
{
namespace
{
bool
_hasattr
(object o,
const
char
* name)
{
return
PyObject_HasAttrString
(o.
ptr
(), name);
}
void
raise_dup_error
()
{
PyErr_SetString
(PyExc_OSError,
std::system_category
().
message
(errno).
c_str
());
throw_error_already_set
();
}
}
//
namespace
void
event_loop::_sock_connect_cb
(object pymod_socket, object fut, object sock, object addr)
{
try
{
object err = sock.
attr
(
"
getsockopt
"
)(
pymod_socket.
attr
(
"
SOL_SOCKET
"
), pymod_socket.
attr
(
"
SO_ERROR
"
));
if
(err !=
object
(
0
)) {
//
TODO: print the address
PyErr_SetString
(PyExc_OSError,
"
Connect call failed {address}
"
);
throw_error_already_set
();
}
fut.
attr
(
"
set_result
"
)(
object
());
}
catch
(
const
error_already_set& e)
{
if
(
PyErr_ExceptionMatches
(PyExc_BlockingIOError)
||
PyErr_ExceptionMatches
(PyExc_InterruptedError))
{
PyErr_Clear
();
//
pass
}
else
if
(
PyErr_ExceptionMatches
(PyExc_SystemExit)
||
PyErr_ExceptionMatches
(PyExc_KeyboardInterrupt))
{
//
raise
}
else
{
PyErr_Clear
();
fut.
attr
(
"
set_exception
"
)(
std::current_exception
());
}
}
}
void
event_loop::_sock_accept
(event_loop& loop, object fut, object sock)
{
int
fd = extract<
int
>(sock.
attr
(
"
fileno
"
)());
object conn, address;
try
{
object ret = sock.
attr
(
"
accept
"
)();
conn = ret[
0
];
address = ret[
1
];
conn.
attr
(
"
setblocking
"
)(
object
(
false
));
fut.
attr
(
"
set_result
"
)(
make_tuple
(conn, address));
}
catch
(
const
error_already_set& e)
{
if
(
PyErr_ExceptionMatches
(PyExc_BlockingIOError)
||
PyErr_ExceptionMatches
(PyExc_InterruptedError))
{
PyErr_Clear
();
loop.
_async_wait_fd
(fd,
bind
(_sock_accept,
boost::ref
(loop), fut, sock), loop.
_write_key
(fd));
}
else
if
(
PyErr_ExceptionMatches
(PyExc_SystemExit)
||
PyErr_ExceptionMatches
(PyExc_KeyboardInterrupt))
{
//
raise
}
else
{
PyErr_Clear
();
fut.
attr
(
"
set_exception
"
)(
std::current_exception
());
}
}
}
void
event_loop::call_later
(
double
delay, object f)
{
auto
p_timer = std::make_shared<boost::asio::steady_timer>(
_strand.
context
(),
std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<
double
>(delay)));
p_timer->
async_wait
(
boost::asio::bind_executor
(_strand,
[f, p_timer] (
const
boost::system::error_code& ec) {
PyEval_AcquireLock
();
f
();
PyEval_ReleaseLock
();
}));
}
void
event_loop::call_at
(
double
when, object f)
{
using
sc = std::chrono::steady_clock;
auto
p_timer = std::make_shared<boost::asio::steady_timer>(
_strand.
context
(),
sc::duration
(
static_cast
<sc::time_point::rep>(when)));
p_timer->
async_wait
(
boost::asio::bind_executor
(_strand,
[f, p_timer] (
const
boost::system::error_code& ec) {
f
();}));
}
//
TODO: support windows socket
object
event_loop::sock_recv
(object sock,
size_t
nbytes)
{
int
fd = extract<
int
>(sock.
attr
(
"
fileno
"
)());
int
fd_dup =
dup
(fd);
if
(fd_dup == -
1
)
raise_dup_error
();
object py_fut =
create_future
();
_async_wait_fd
(fd_dup,
[py_fut, nbytes, fd=fd_dup] {
PyEval_AcquireLock
();
std::vector<
char
>
buffer
(nbytes);
read
(fd, buffer.
data
(), nbytes);
py_fut.
attr
(
"
set_result
"
)(
object
(handle<>(
PyBytes_FromStringAndSize
(buffer.
data
(), nbytes))));
PyEval_ReleaseLock
();
},
_read_key
(fd));
return
py_fut;
}
//
TODO: support windows socket
object
event_loop::sock_recv_into
(object sock, object buffer)
{
int
fd = extract<
int
>(sock.
attr
(
"
fileno
"
)());
int
fd_dup =
dup
(fd);
if
(fd_dup == -
1
)
raise_dup_error
();
ssize_t
nbytes =
len
(buffer);
object py_fut =
create_future
();
_async_wait_fd
(fd_dup,
[py_fut, nbytes, fd=fd_dup] {
PyEval_AcquireLock
();
std::vector<
char
>
buffer
(nbytes);
ssize_t
nbytes_read =
read
(fd, buffer.
data
(), nbytes);
py_fut.
attr
(
"
set_result
"
)(nbytes_read);
PyEval_ReleaseLock
();
},
_read_key
(fd));
return
py_fut;
}
//
TODO: support windows socket
object
event_loop::sock_sendall
(object sock, object data)
{
int
fd = extract<
int
>(sock.
attr
(
"
fileno
"
)());
int
fd_dup =
dup
(fd);
if
(fd_dup == -
1
)
raise_dup_error
();
char
const
* py_str = extract<
char
const
*>(data.
attr
(
"
decode
"
)());
ssize_t
py_str_len =
len
(data);
object py_fut =
create_future
();
_async_wait_fd
(fd_dup,
[py_fut, fd, py_str, py_str_len] {
PyEval_AcquireLock
();
write
(fd, py_str, py_str_len);
py_fut.
attr
(
"
set_result
"
)(
object
());
PyEval_ReleaseLock
();
},
_write_key
(fd));
return
py_fut;
}
//
TODO: support windows socket
object
event_loop::sock_connect
(object sock, object address)
{
if
(!
_hasattr
(_pymod_socket,
"
AF_UNIX
"
) || sock.
attr
(
"
family
"
) != _pymod_socket.
attr
(
"
AF_UNIX
"
))
{
//
TODO: _ensure_resolve
}
object py_fut =
create_future
();
int
fd = extract<
int
>(sock.
attr
(
"
fileno
"
)());
try
{
sock.
attr
(
"
connect
"
)(address);
py_fut.
attr
(
"
set_result
"
)(
object
());
}
catch
(
const
error_already_set& e)
{
if
(
PyErr_ExceptionMatches
(PyExc_BlockingIOError)
||
PyErr_ExceptionMatches
(PyExc_InterruptedError))
{
PyErr_Clear
();
int
fd_dup =
dup
(fd);
if
(fd_dup == -
1
)
raise_dup_error
();
_async_wait_fd
(fd_dup,
bind
(_sock_connect_cb, _pymod_socket, py_fut, sock, address),
_write_key
(fd));
}
else
if
(
PyErr_ExceptionMatches
(PyExc_SystemExit)
||
PyErr_ExceptionMatches
(PyExc_KeyboardInterrupt))
{
//
raise
}
else
{
PyErr_Clear
();
py_fut.
attr
(
"
set_exception
"
)(
std::current_exception
());
}
}
return
py_fut;
}
object
event_loop::sock_accept
(object sock)
{
object py_fut =
create_future
();
_sock_accept
(*
this
, py_fut, sock);
return
py_fut;
}
//
TODO: implement this
object
event_loop::sock_sendfile
(object sock, object file,
int
offset,
int
count,
bool
fallback)
{
PyErr_SetString
(PyExc_NotImplementedError,
"
Not implemented!
"
);
throw_error_already_set
();
return
object
();
}
//
TODO: implement this
object
event_loop::start_tls
(object transport, object protocol, object sslcontext,
bool
server_side, object server_hostname, object ssl_handshake_timeout)
{
PyErr_SetString
(PyExc_NotImplementedError,
"
Not implemented!
"
);
throw_error_already_set
();
return
object
();
}
object
event_loop::getaddrinfo
(object host,
int
port,
int
family,
int
type,
int
proto,
int
flags)
{
object py_fut =
create_future
();
_strand.
post
(
[
this
, py_fut, host, port, family, type, proto, flags] {
PyEval_AcquireLock
();
object res = _pymod_socket.
attr
(
"
getaddrinfo
"
)(host, port, family, type, proto, flags);
py_fut.
attr
(
"
set_result
"
)(res);
PyEval_ReleaseLock
();
});
return
py_fut;
}
object
event_loop::getnameinfo
(object sockaddr,
int
flags)
{
object py_fut =
create_future
();
_strand.
post
(
[
this
, py_fut, sockaddr, flags] {
PyEval_AcquireLock
();
object res = _pymod_socket.
attr
(
"
getnameinfo
"
)(sockaddr, flags);
py_fut.
attr
(
"
set_result
"
)(res);
PyEval_ReleaseLock
();
});
return
py_fut;
}
void
event_loop::default_exception_handler
(object context)
{
object message = context.
attr
(
"
get
"
)(
str
(
"
message
"
));
if
(message ==
object
())
{
message =
str
(
"
Unhandled exception in event loop
"
);
}
object exception = context.
attr
(
"
get
"
)(
str
(
"
exception
"
));
object exc_info;
if
(exception !=
object
())
{
exc_info =
make_tuple
(exception.
attr
(
"
__class__
"
), exception, exception.
attr
(
"
__traceback__
"
));
}
else
{
exc_info =
object
(
false
);
}
if
(!
PyObject_IsTrue
(context.
attr
(
"
__contains__
"
)(
str
(
"
source_traceback
"
)).
ptr
()) &&
_exception_handler !=
object
() &&
_exception_handler.
attr
(
"
_source_traceback
"
) !=
object
())
{
context[
"
handle_traceback
"
] = _exception_handler.
attr
(
"
_source_traceback
"
);
}
list log_lines;
log_lines.
append
(message);
list
context_keys
(context.
attr
(
"
keys
"
));
context_keys.
sort
();
for
(
int
i =
0
; i <
len
(context_keys); i++)
{
std::string key = extract<std::string>(context_keys[i]);
if
(key ==
"
message
"
|| key ==
"
exception
"
)
continue
;
str
value
(context[key]);
if
(key ==
"
source_traceback
"
)
{
str tb =
str
(
"
"
).
join
(_pymod_traceback.
attr
(
"
format_list
"
)(value));
value =
str
(
"
Object created at (most recent call last):
\n
"
);
value += tb.
rstrip
();
}
else
if
(key ==
"
handle_traceback
"
)
{
str tb =
str
(
"
"
).
join
(_pymod_traceback.
attr
(
"
format_list
"
)(value));
value =
str
(
"
Handle created at (most recent call last):
\n
"
);
value += tb.
rstrip
();
}
else
{
value =
str
(value.
attr
(
"
__str__
"
)());
}
std::ostringstream stringStream;
stringStream << key <<
"
:
"
<< value;
log_lines.
append
(
str
(stringStream.
str
()));
}
list args;
dict kwargs;
args.
append
(
str
(
"
\n
"
).
join
(log_lines));
kwargs[
"
exc_info
"
] = exc_info;
_py_logger.
attr
(
"
error
"
)(
tuple
(args), **kwargs);
}
void
event_loop::call_exception_handler
(object context)
{
if
(_exception_handler ==
object
())
{
try
{
default_exception_handler
(context);
}
catch
(
const
error_already_set& e)
{
if
(
PyErr_ExceptionMatches
(PyExc_SystemExit)
||
PyErr_ExceptionMatches
(PyExc_KeyboardInterrupt))
{
//
raise
}
else
{
PyErr_Clear
();
list args;
dict kwargs;
args.
append
(
str
(
"
Exception in default exception handler
"
));
kwargs[
"
exc_info
"
] =
true
;
_py_logger.
attr
(
"
error
"
)(
tuple
(args), **kwargs);
}
}
}
else
{
try
{
_exception_handler
(context);
}
catch
(
const
error_already_set& e)
{
if
(
PyErr_ExceptionMatches
(PyExc_SystemExit)
||
PyErr_ExceptionMatches
(PyExc_KeyboardInterrupt))
{
//
raise
}
else
{
PyObject *ptype, *pvalue, *ptraceback;
PyErr_Fetch
(&ptype, &pvalue, &ptraceback);
PyErr_NormalizeException
(&ptype, &pvalue, &ptraceback);
object type{handle<>(ptype)};
object value{handle<>(pvalue)};
object traceback{handle<>(ptraceback)};
try
{
dict tmp_dict;
tmp_dict[
"
message
"
] =
str
(
"
Unhandled error in exception handler
"
);
tmp_dict[
"
exception
"
] = value;
tmp_dict[
"
context
"
] = context;
default_exception_handler
(tmp_dict);
}
catch
(
const
error_already_set& e)
{
if
(
PyErr_ExceptionMatches
(PyExc_SystemExit)
||
PyErr_ExceptionMatches
(PyExc_KeyboardInterrupt))
{
//
raise
}
else
{
boost::python::list args;
boost::python::dict kwargs;
args.
append
(
str
(
"
Exception in default exception handler
"
));
kwargs[
"
exc_info
"
] =
true
;
_py_logger.
attr
(
"
error
"
)(
tuple
(args), **kwargs);
}
}
}
}
}
}
object
event_loop::create_future
()
{
boost::python::dict kwargs;
kwargs[
"
loop
"
] =
boost::ref
(*
this
);
return
_pymod_asyncio_futures.
attr
(
"
Future
"
)(*
boost::python::tuple
(), **kwargs);
}
void
set_default_event_loop
(
const
boost::asio::io_context::strand& strand)
{
class_<event_loop, boost::noncopyable>(
"
BoostAsioEventLoop
"
, init<boost::asio::io_context::strand&>())
.
def
(
"
call_soon
"
, &event_loop::call_soon)
.
def
(
"
call_soon_thread_safe
"
, &event_loop::call_soon_thread_safe)
.
def
(
"
call_later
"
, &event_loop::call_later)
.
def
(
"
call_at
"
, &event_loop::call_at)
.
def
(
"
time
"
, &event_loop::time)
.
def
(
"
add_reader
"
, &event_loop::add_reader)
.
def
(
"
remove_reader
"
, &event_loop::remove_reader)
.
def
(
"
add_writer
"
, &event_loop::add_writer)
.
def
(
"
remove_writer
"
, &event_loop::remove_writer)
.
def
(
"
sock_recv
"
, &event_loop::sock_recv)
.
def
(
"
sock_recv_into
"
, &event_loop::sock_recv_into)
.
def
(
"
sock_sendall
"
, &event_loop::sock_sendall)
.
def
(
"
sock_connect
"
, &event_loop::sock_connect)
.
def
(
"
sock_accept
"
, &event_loop::sock_accept)
.
def
(
"
sock_sendfile
"
, &event_loop::sock_sendfile)
.
def
(
"
start_tls
"
, &event_loop::start_tls)
.
def
(
"
getaddrinfo
"
, &event_loop::getaddrinfo)
.
def
(
"
getnameinfo
"
, &event_loop::getnameinfo)
.
def
(
"
set_exception_handler
"
, &event_loop::set_exception_handler)
.
def
(
"
get_exception_handler
"
, &event_loop::get_exception_handler)
.
def
(
"
default_exception_handler
"
, &event_loop::default_exception_handler)
.
def
(
"
call_exception_handler
"
, &event_loop::call_exception_handler)
.
def
(
"
create_future
"
, &event_loop::create_future)
.
def
(
"
get_debug
"
, &event_loop::get_debug);
object asyncio =
import
(
"
asyncio
"
);
object abstract_policy = asyncio.
attr
(
"
AbstractEventLoopPolicy
"
);
dict method_dict;
std::shared_ptr<event_loop> p_loop = std::make_shared<event_loop>(strand);
method_dict[
"
get_event_loop
"
] =
make_function
(
[p_loop] (object e) {
return
object
(
boost::ref
(*p_loop));},
default_call_policies
(),
boost::mpl::vector<object, object>()
);
object class_boost_policy = call<object>(
(PyObject*)&PyType_Type,
str
(
"
BoostEventLoopPolicy
"
),
boost::python::make_tuple
(abstract_policy),
method_dict);
object boost_policy_instance = class_boost_policy.
attr
(
"
__call__
"
)();
asyncio.
attr
(
"
set_event_loop_policy
"
)(boost_policy_instance);
}
}}}
//
namespace boost::python::asio
Back
|
FazBrowse Home
|
New Git URL