Skip to content

Commit 0c97e4c

Browse files
committed
py/stream: Add Python-level ioctl() method.
Will call underlying C virtual methods of stream interface. This isn't intended to be added to every stream object (it's not in CPython), but is convenient way to expose extra operation on Python side without adding bunch of Python-level methods.
1 parent a45e280 commit 0c97e4c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

py/stream.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,28 @@ STATIC mp_obj_t stream_tell(mp_obj_t self) {
410410
}
411411
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
412412

413+
STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
414+
const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
415+
416+
mp_buffer_info_t bufinfo;
417+
uintptr_t val;
418+
if (MP_OBJ_IS_INT(args[2])) {
419+
val = mp_obj_get_int(args[2]);
420+
} else {
421+
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
422+
val = (uintptr_t)bufinfo.buf;
423+
}
424+
425+
int error;
426+
mp_int_t res = stream_p->ioctl(args[0], mp_obj_get_int(args[1]), val, &error);
427+
if (res == MP_STREAM_ERROR) {
428+
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(error)));
429+
}
430+
431+
return mp_obj_new_int(res);
432+
}
433+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
434+
413435
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_read_obj, 1, 2, stream_read);
414436
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_readinto_obj, 2, 3, stream_readinto);
415437
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_readall_obj, stream_readall);

py/stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ MP_DECLARE_CONST_FUN_OBJ(mp_stream_unbuffered_readlines_obj);
5555
MP_DECLARE_CONST_FUN_OBJ(mp_stream_write_obj);
5656
MP_DECLARE_CONST_FUN_OBJ(mp_stream_seek_obj);
5757
MP_DECLARE_CONST_FUN_OBJ(mp_stream_tell_obj);
58+
MP_DECLARE_CONST_FUN_OBJ(mp_stream_ioctl_obj);
5859

5960
// these are for mp_get_stream_raise and can be or'd together
6061
#define MP_STREAM_OP_READ (1)

0 commit comments

Comments
 (0)