Sfoglia il codice sorgente

【更新】stream 代码至最新版。

Signed-off-by: armink <armink.ztl@gmail.com>
armink 7 anni fa
parent
commit
9f6d77a1c7
2 ha cambiato i file con 14 aggiunte e 16 eliminazioni
  1. 11 14
      py/stream.c
  2. 3 2
      py/stream.h

+ 11 - 14
py/stream.c

@@ -32,13 +32,6 @@
 #include "py/stream.h"
 #include "py/runtime.h"
 
-#if MICROPY_STREAMS_NON_BLOCK
-#include <errno.h>
-#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
-#define EWOULDBLOCK 140
-#endif
-#endif
-
 // This file defines generic Python stream read/write methods which
 // dispatch to the underlying stream interface of an object.
 
@@ -105,13 +98,6 @@ const mp_stream_p_t *mp_get_stream_raise(mp_obj_t self_in, int flags) {
     return stream_p;
 }
 
-mp_obj_t mp_stream_close(mp_obj_t stream) {
-    // TODO: Still consider using ioctl for close
-    mp_obj_t dest[2];
-    mp_load_method(stream, MP_QSTR_close, dest);
-    return mp_call_method_n_kw(0, 0, dest);
-}
-
 STATIC mp_obj_t stream_read_generic(size_t n_args, const mp_obj_t *args, byte flags) {
     const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_READ);
 
@@ -434,6 +420,17 @@ mp_obj_t mp_stream_unbuffered_iter(mp_obj_t self) {
     return MP_OBJ_STOP_ITERATION;
 }
 
+mp_obj_t mp_stream_close(mp_obj_t stream) {
+    const mp_stream_p_t *stream_p = mp_get_stream_raise(stream, MP_STREAM_OP_IOCTL);
+    int error;
+    mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
+    if (res == MP_STREAM_ERROR) {
+        mp_raise_OSError(error);
+    }
+    return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_close_obj, mp_stream_close);
+
 STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) {
     const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL);
 

+ 3 - 2
py/stream.h

@@ -35,7 +35,7 @@
 #define MP_STREAM_FLUSH (1)
 #define MP_STREAM_SEEK  (2)
 #define MP_STREAM_POLL  (3)
-//#define MP_STREAM_CLOSE       (4)  // Not yet implemented
+#define MP_STREAM_CLOSE         (4)
 #define MP_STREAM_TIMEOUT       (5)  // Get/set timeout (single op)
 #define MP_STREAM_GET_OPTS      (6)  // Get stream options
 #define MP_STREAM_SET_OPTS      (7)  // Set stream options
@@ -69,6 +69,7 @@ MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_unbuffered_readline_obj);
 MP_DECLARE_CONST_FUN_OBJ_1(mp_stream_unbuffered_readlines_obj);
 MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_write_obj);
 MP_DECLARE_CONST_FUN_OBJ_2(mp_stream_write1_obj);
+MP_DECLARE_CONST_FUN_OBJ_1(mp_stream_close_obj);
 MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_seek_obj);
 MP_DECLARE_CONST_FUN_OBJ_1(mp_stream_tell_obj);
 MP_DECLARE_CONST_FUN_OBJ_1(mp_stream_flush_obj);
@@ -106,7 +107,7 @@ int mp_stream_posix_fsync(mp_obj_t stream);
 #endif
 
 #if MICROPY_STREAMS_NON_BLOCK
-#define mp_is_nonblocking_error(errno) ((errno) == EAGAIN || (errno) == EWOULDBLOCK)
+#define mp_is_nonblocking_error(errno) ((errno) == MP_EAGAIN || (errno) == MP_EWOULDBLOCK)
 #else
 #define mp_is_nonblocking_error(errno) (0)
 #endif