Browse Source

shared/runtime/sys_stdio_mphal: Fix printed type for stdio streams.

The printed type for stdio streams indicates "FileIO", which is a binary IO
stream.  Stdio is not binary by design, and its printed type should
indicate a text stream.  "TextIOWrapper" suits that purpose, and is used
by VfsPosix files.

Signed-off-by: timdechant <timdechant.git@gmail.com>
pull/15724/head
timdechant 2 months ago
committed by Damien George
parent
commit
455415b1e1
  1. 4
      shared/runtime/sys_stdio_mphal.c
  2. 21
      tests/basics/sys_stdio.py
  3. 21
      tests/basics/sys_stdio_buffer.py
  4. 6
      tests/basics/sys_stdio_buffer.py.exp

4
shared/runtime/sys_stdio_mphal.c

@ -54,7 +54,7 @@ static const sys_stdio_obj_t stdio_buffer_obj;
static void stdio_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { static void stdio_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in); sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "<io.FileIO %d>", self->fd); mp_printf(print, "<io.%s %d>", mp_obj_get_type_str(self_in), self->fd);
} }
static mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) { static mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
@ -122,7 +122,7 @@ static const mp_stream_p_t stdio_obj_stream_p = {
MP_DEFINE_CONST_OBJ_TYPE( MP_DEFINE_CONST_OBJ_TYPE(
stdio_obj_type, stdio_obj_type,
MP_QSTR_FileIO, MP_QSTR_TextIOWrapper,
MP_TYPE_FLAG_ITER_IS_STREAM, MP_TYPE_FLAG_ITER_IS_STREAM,
print, stdio_obj_print, print, stdio_obj_print,
protocol, &stdio_obj_stream_p, protocol, &stdio_obj_stream_p,

21
tests/basics/sys_stdio.py

@ -0,0 +1,21 @@
# Test sys.std* objects.
import sys
try:
sys.stdout
sys.stdin
sys.stderr
except AttributeError:
print("SKIP")
raise SystemExit
# CPython is more verbose; no need to match exactly
print('TextIOWrapper' in str(sys.stdout))
print('TextIOWrapper' in str(sys.stderr))
print('TextIOWrapper' in str(sys.stdin))
print('TextIOWrapper' in str(type(sys.stdout)))
print('TextIOWrapper' in str(type(sys.stderr)))
print('TextIOWrapper' in str(type(sys.stdin)))

21
tests/basics/sys_stdio_buffer.py

@ -0,0 +1,21 @@
# Test sys.std*.buffer objects.
import sys
try:
sys.stdout.buffer
sys.stdin.buffer
sys.stderr.buffer
except AttributeError:
print("SKIP")
raise SystemExit
# CPython is more verbose; no need to match exactly
print('FileIO' in str(sys.stdout.buffer))
print('FileIO' in str(sys.stderr.buffer))
print('FileIO' in str(sys.stdin.buffer))
print('FileIO' in str(type(sys.stdout.buffer)))
print('FileIO' in str(type(sys.stderr.buffer)))
print('FileIO' in str(type(sys.stdin.buffer)))

6
tests/basics/sys_stdio_buffer.py.exp

@ -0,0 +1,6 @@
True
True
True
True
True
True
Loading…
Cancel
Save