From 61e77a4e88c4b6971fd997191de01e5ab08e46c5 Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Sat, 30 Jul 2016 20:05:56 +0300
Subject: [PATCH] py/mpconfig.h: Add MICROPY_STREAMS_POSIX_API setting.

To filter out even prototypes of mp_stream_posix_*() functions, which
require POSIX types like ssize_t & off_t, which may be not available in
some ports.
---
 esp8266/mpconfigport.h | 1 +
 py/mpconfig.h          | 6 ++++++
 py/stream.c            | 4 +++-
 py/stream.h            | 2 ++
 unix/mpconfigport.h    | 1 +
 5 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/esp8266/mpconfigport.h b/esp8266/mpconfigport.h
index 299dc8eae..0cd7e8c31 100644
--- a/esp8266/mpconfigport.h
+++ b/esp8266/mpconfigport.h
@@ -75,6 +75,7 @@
 #define MICROPY_WARNINGS            (1)
 #define MICROPY_PY_STR_BYTES_CMP_WARN (1)
 #define MICROPY_STREAMS_NON_BLOCK   (1)
+#define MICROPY_STREAMS_POSIX_API   (1)
 #define MICROPY_MODULE_FROZEN_STR   (1)
 #define MICROPY_MODULE_FROZEN_MPY   (1)
 #define MICROPY_MODULE_FROZEN_LEXER mp_lexer_new_from_str32
diff --git a/py/mpconfig.h b/py/mpconfig.h
index edf6e71d0..890e072ab 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -522,6 +522,12 @@ typedef double mp_float_t;
 #define MICROPY_STREAMS_NON_BLOCK (0)
 #endif
 
+// Whether to provide stream functions with POSIX-like signatures
+// (useful for porting existing libraries to MicroPython).
+#ifndef MICROPY_STREAMS_POSIX_API
+#define MICROPY_STREAMS_POSIX_API (0)
+#endif
+
 // Whether to call __init__ when importing builtin modules for the first time
 #ifndef MICROPY_MODULE_BUILTIN_INIT
 #define MICROPY_MODULE_BUILTIN_INIT (0)
diff --git a/py/stream.c b/py/stream.c
index d426973e8..473eb9690 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -511,6 +511,7 @@ STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
 }
 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
 
+#if MICROPY_STREAMS_POSIX_API
 /*
  * POSIX-like functions
  *
@@ -519,7 +520,6 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream_ioctl_obj, 2, 3, stream_ioctl);
  * POSIX-compatible software to work with MicroPython streams.
  */
 
-
 // errno-like variable. If any of the functions below returned with error
 // status, this variable will contain error no.
 int mp_stream_errno;
@@ -568,3 +568,5 @@ int mp_stream_posix_fsync(mp_obj_t stream) {
     }
     return res;
 }
+
+#endif
diff --git a/py/stream.h b/py/stream.h
index 8dd689887..33d85e823 100644
--- a/py/stream.h
+++ b/py/stream.h
@@ -84,11 +84,13 @@ mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf, mp_uint_t size, int *errcode,
 
 void mp_stream_write_adaptor(void *self, const char *buf, size_t len);
 
+#if MICROPY_STREAMS_POSIX_API
 // Functions with POSIX-compatible signatures
 ssize_t mp_stream_posix_write(mp_obj_t stream, const void *buf, size_t len);
 ssize_t mp_stream_posix_read(mp_obj_t stream, void *buf, size_t len);
 off_t mp_stream_posix_lseek(mp_obj_t stream, off_t offset, int whence);
 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)
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index 2f4ec1121..58061b28a 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -63,6 +63,7 @@
 #define MICROPY_FLOAT_IMPL          (MICROPY_FLOAT_IMPL_DOUBLE)
 #define MICROPY_LONGINT_IMPL        (MICROPY_LONGINT_IMPL_MPZ)
 #define MICROPY_STREAMS_NON_BLOCK   (1)
+#define MICROPY_STREAMS_POSIX_API   (1)
 #define MICROPY_OPT_COMPUTED_GOTO   (1)
 #ifndef MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE
 #define MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE (1)
-- 
GitLab