]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-46315: Add ifdef HAVE_ feature checks for WASI compatibility (GH-30507)
authorChristian Heimes <christian@python.org>
Thu, 13 Jan 2022 08:46:04 +0000 (10:46 +0200)
committerGitHub <noreply@github.com>
Thu, 13 Jan 2022 08:46:04 +0000 (09:46 +0100)
14 files changed:
Include/internal/pycore_condvar.h
Include/pythread.h
Misc/NEWS.d/next/Build/2022-01-09-15-48-49.bpo-46315.NdCRLu.rst [new file with mode: 0644]
Modules/_randommodule.c
Modules/clinic/posixmodule.c.h
Modules/faulthandler.c
Modules/posixmodule.c
Modules/xxsubtype.c
PC/pyconfig.h
Python/pyfpe.c
Tools/wasm/config.site-wasm32-wasi [new file with mode: 0644]
configure
configure.ac
pyconfig.h.in

index edb7dc8193cb8af9a6e2f53955710a8714d3c5b1..981c962bf7dfdfa31fd774550394efc5702d74dc 100644 (file)
@@ -20,7 +20,9 @@
  */
 #define Py_HAVE_CONDVAR
 
-#include <pthread.h>
+#ifdef HAVE_PTHREAD_H
+#  include <pthread.h>
+#endif
 
 #define PyMUTEX_T pthread_mutex_t
 #define PyCOND_T pthread_cond_t
index 1a6092c4ad0be29980e876e172f924bc68cfded7..034e660551531f3545c4c7ec79e2fb9d6cb1792b 100644 (file)
@@ -125,7 +125,7 @@ Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_ReInitTLS(void);
 typedef struct _Py_tss_t Py_tss_t;  /* opaque */
 
 #ifndef Py_LIMITED_API
-#if defined(_POSIX_THREADS)
+#ifdef HAVE_PTHREAD_H
     /* Darwin needs pthread.h to know type name the pthread_key_t. */
 #   include <pthread.h>
 #   define NATIVE_TSS_KEY_T     pthread_key_t
diff --git a/Misc/NEWS.d/next/Build/2022-01-09-15-48-49.bpo-46315.NdCRLu.rst b/Misc/NEWS.d/next/Build/2022-01-09-15-48-49.bpo-46315.NdCRLu.rst
new file mode 100644 (file)
index 0000000..9360f91
--- /dev/null
@@ -0,0 +1,2 @@
+Added and fixed ``#ifdef HAVE_FEATURE`` checks for functionality that is not
+available on WASI platform.
index 5243d5a05e290020b5b275696fa50f383a8b176e..45860e342eb43ace4cbcc007bce2a42ae34cb9bb 100644 (file)
@@ -258,7 +258,11 @@ random_seed_time_pid(RandomObject *self)
     key[0] = (uint32_t)(now & 0xffffffffU);
     key[1] = (uint32_t)(now >> 32);
 
+#ifdef HAVE_GETPID
     key[2] = (uint32_t)getpid();
+#else
+    key[2] = 0;
+#endif
 
     now = _PyTime_GetMonotonicClock();
     key[3] = (uint32_t)(now & 0xffffffffU);
index 86da08711fd449b4bad8b9054aa568400595300d..282a5410f702066c8a87a852ac5f9ce1e0f7ec7a 100644 (file)
@@ -1831,6 +1831,8 @@ exit:
 
 #endif /* defined(HAVE_SYSTEM) && !defined(MS_WINDOWS) */
 
+#if defined(HAVE_UMASK)
+
 PyDoc_STRVAR(os_umask__doc__,
 "umask($module, mask, /)\n"
 "--\n"
@@ -1859,6 +1861,8 @@ exit:
     return return_value;
 }
 
+#endif /* defined(HAVE_UMASK) */
+
 PyDoc_STRVAR(os_unlink__doc__,
 "unlink($module, /, path, *, dir_fd=None)\n"
 "--\n"
@@ -8812,6 +8816,10 @@ exit:
     #define OS_SYSTEM_METHODDEF
 #endif /* !defined(OS_SYSTEM_METHODDEF) */
 
+#ifndef OS_UMASK_METHODDEF
+    #define OS_UMASK_METHODDEF
+#endif /* !defined(OS_UMASK_METHODDEF) */
+
 #ifndef OS_UNAME_METHODDEF
     #define OS_UNAME_METHODDEF
 #endif /* !defined(OS_UNAME_METHODDEF) */
@@ -9295,4 +9303,4 @@ exit:
 #ifndef OS_WAITSTATUS_TO_EXITCODE_METHODDEF
     #define OS_WAITSTATUS_TO_EXITCODE_METHODDEF
 #endif /* !defined(OS_WAITSTATUS_TO_EXITCODE_METHODDEF) */
-/*[clinic end generated code: output=05505f171cdcff72 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=d95ba7b0b9c52685 input=a9049054013a1b77]*/
index cb2e2588e19b24cfed65cedbcd425714bd8ca8d7..1888337cf9f39bdda34b4c8248ba6a779d01dee1 100644 (file)
@@ -10,7 +10,7 @@
 #include <signal.h>
 #include <signal.h>
 #include <stdlib.h>               // abort()
-#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
+#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
 #  include <pthread.h>
 #endif
 #ifdef MS_WINDOWS
index 21adf806a4e85b957129a04ad55257a817746fd6..904f8bfa55807e6c8e3e985b80ae709898ba8e8f 100644 (file)
@@ -3292,7 +3292,14 @@ os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
     }
     else
 #endif /* HAVE_FHCMODAT */
+    {
+#ifdef HAVE_CHMOD
         result = chmod(path->narrow, mode);
+#else
+        result = -1;
+        errno = ENOSYS;
+#endif
+    }
     Py_END_ALLOW_THREADS
 
     if (result) {
@@ -4885,6 +4892,7 @@ os_system_impl(PyObject *module, PyObject *command)
 #endif /* HAVE_SYSTEM */
 
 
+#ifdef HAVE_UMASK
 /*[clinic input]
 os.umask
 
@@ -4903,6 +4911,7 @@ os_umask_impl(PyObject *module, int mask)
         return posix_error();
     return PyLong_FromLong((long)i);
 }
+#endif
 
 #ifdef MS_WINDOWS
 
index 7200337724e080b40ee77aae14de51e2e45eb558..768dac9d1b19b0501bd41a69f07afa1c45564c38 100644 (file)
@@ -237,10 +237,11 @@ spam_bench(PyObject *self, PyObject *args)
 {
     PyObject *obj, *name, *res;
     int n = 1000;
-    time_t t0, t1;
+    time_t t0 = 0, t1 = 0;
 
     if (!PyArg_ParseTuple(args, "OU|i", &obj, &name, &n))
         return NULL;
+#ifdef HAVE_CLOCK
     t0 = clock();
     while (--n >= 0) {
         res = PyObject_GetAttr(obj, name);
@@ -249,6 +250,7 @@ spam_bench(PyObject *self, PyObject *args)
         Py_DECREF(res);
     }
     t1 = clock();
+#endif
     return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
 }
 
index e0d875adf2e4a58cdb6c39e65cd343d9227cb4ee..e8649be5684208ede74793c584311c1dfef4fb8e 100644 (file)
@@ -529,6 +529,9 @@ Py_NO_ENABLE_SHARED to find out.  Also support MS_NO_COREDLL for b/w compat */
 /* Define if you have times.  */
 /* #undef HAVE_TIMES */
 
+/* Define to 1 if you have the `umask' function. */
+#define HAVE_UMASK 1
+
 /* Define if you have uname.  */
 /* #undef HAVE_UNAME */
 
index 31ef5d73b70ac70a9f6e449a437e68b698e09590..9b1260f687a77aab89d4f907090b7393cb9400aa 100644 (file)
@@ -3,9 +3,12 @@
  * though, because they may be referenced by extensions using the stable ABI.
  */
 
-#include "setjmp.h"
+#ifdef HAVE_SETJMP_H
+#include <setjmp.h>
 
 jmp_buf PyFPE_jbuf;
+#endif
+
 int PyFPE_counter;
 
 double
diff --git a/Tools/wasm/config.site-wasm32-wasi b/Tools/wasm/config.site-wasm32-wasi
new file mode 100644 (file)
index 0000000..be26c46
--- /dev/null
@@ -0,0 +1,17 @@
+# config.site override for cross compiling to wasm32-wasi platform
+#
+# Written by Christian Heimes <christian@python.org>
+# Partly based on pyodide's pyconfig.undefs.h file.
+
+
+# cannot be detected in cross builds
+ac_cv_buggy_getaddrinfo=no
+
+# WASI has no /dev/pt*
+ac_cv_file__dev_ptmx=no
+ac_cv_file__dev_ptc=no
+
+# dummy readelf, WASI build does not need readelf.
+ac_cv_prog_ac_ct_READELF=true
+
+ac_cv_func_eventfd=no
index 6c9aacc68a95614aa593a216e9480f74696e457f..127b350b4bb04503bf2ef11375483737eab9e97e 100755 (executable)
--- a/configure
+++ b/configure
@@ -6268,7 +6268,7 @@ else
     EXEEXT=.html ;; #(
   Emscripten/node) :
     EXEEXT=.js ;; #(
-  wasi/*) :
+  WASI/*) :
     EXEEXT=.wasm ;; #(
   *) :
     EXEEXT=
@@ -7627,6 +7627,15 @@ case $ac_sys_system/$ac_sys_emscripten_target in #(
     LDFLAGS_NODIST="$(LDFLAGS_NODIST) -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1 -s NODERAWFS=1 -s EXIT_RUNTIME=1 -s USE_PTHREADS -s PROXY_TO_PTHREAD"
     CFLAGS_NODIST="$(CFLAGS_NODIST) -pthread"
    ;; #(
+  WASI) :
+
+
+$as_echo "#define _WASI_EMULATED_SIGNAL 1" >>confdefs.h
+
+    LIBS="$LIBS -lwasi-emulated-signal"
+    echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h
+
+ ;; #(
   *) :
      ;;
 esac
@@ -8543,7 +8552,7 @@ for ac_header in  \
   alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
   ieeefp.h io.h langinfo.h libintl.h libutil.h linux/memfd.h linux/random.h linux/soundcard.h \
   linux/tipc.h linux/wait.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
-  sched.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
+  sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
   sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \
   sys/loadavg.h sys/lock.h sys/memfd.h sys/mkdev.h sys/mman.h sys/modem.h sys/param.h sys/poll.h \
   sys/random.h sys/resource.h sys/select.h sys/sendfile.h sys/socket.h sys/soundcard.h sys/stat.h \
@@ -13630,7 +13639,7 @@ fi
 
 # checks for library functions
 for ac_func in  \
-  accept4 alarm bind_textdomain_codeset chown clock close_range confstr \
+  accept4 alarm bind_textdomain_codeset chmod chown clock close_range confstr \
   copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \
   faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
   fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
@@ -13652,7 +13661,7 @@ for ac_func in  \
   sigfillset siginterrupt sigpending sigrelse sigtimedwait sigwait \
   sigwaitinfo snprintf splice strftime strlcpy strsignal symlinkat sync \
   sysconf system tcgetpgrp tcsetpgrp tempnam timegm times tmpfile \
-  tmpnam tmpnam_r truncate ttyname uname unlinkat utimensat utimes vfork \
+  tmpnam tmpnam_r truncate ttyname umask uname unlinkat utimensat utimes vfork \
   wait wait3 wait4 waitid waitpid wcscoll wcsftime wcsxfrm wmemcmp writev \
 
 do :
index 4396828bf6fe6df1e69067149661d6a7f403b4cf..e5ebf7bc2e07aa4c029f64a54dcccaaf51893c53 100644 (file)
@@ -1093,7 +1093,7 @@ AC_ARG_WITH([suffix],
   AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
     [Emscripten/browser], [EXEEXT=.html],
     [Emscripten/node], [EXEEXT=.js],
-    [wasi/*], [EXEEXT=.wasm],
+    [WASI/*], [EXEEXT=.wasm],
     [EXEEXT=]
   )
 ])
@@ -1805,6 +1805,11 @@ AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
     LDFLAGS_NODIST="$(LDFLAGS_NODIST) -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1 -s NODERAWFS=1 -s EXIT_RUNTIME=1 -s USE_PTHREADS -s PROXY_TO_PTHREAD"
     CFLAGS_NODIST="$(CFLAGS_NODIST) -pthread"
   ],
+  [WASI], [
+    AC_DEFINE([_WASI_EMULATED_SIGNAL], [1], [Define to 1 if you want to emulate signals on WASI])
+    LIBS="$LIBS -lwasi-emulated-signal"
+    echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h
+  ]
 )
 
 AC_SUBST(BASECFLAGS)
@@ -2306,7 +2311,7 @@ AC_CHECK_HEADERS([ \
   alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
   ieeefp.h io.h langinfo.h libintl.h libutil.h linux/memfd.h linux/random.h linux/soundcard.h \
   linux/tipc.h linux/wait.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
-  sched.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
+  sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
   sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \
   sys/loadavg.h sys/lock.h sys/memfd.h sys/mkdev.h sys/mman.h sys/modem.h sys/param.h sys/poll.h \
   sys/random.h sys/resource.h sys/select.h sys/sendfile.h sys/socket.h sys/soundcard.h sys/stat.h \
@@ -4062,7 +4067,7 @@ fi
 
 # checks for library functions
 AC_CHECK_FUNCS([ \
-  accept4 alarm bind_textdomain_codeset chown clock close_range confstr \
+  accept4 alarm bind_textdomain_codeset chmod chown clock close_range confstr \
   copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \
   faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
   fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
@@ -4084,7 +4089,7 @@ AC_CHECK_FUNCS([ \
   sigfillset siginterrupt sigpending sigrelse sigtimedwait sigwait \
   sigwaitinfo snprintf splice strftime strlcpy strsignal symlinkat sync \
   sysconf system tcgetpgrp tcsetpgrp tempnam timegm times tmpfile \
-  tmpnam tmpnam_r truncate ttyname uname unlinkat utimensat utimes vfork \
+  tmpnam tmpnam_r truncate ttyname umask uname unlinkat utimensat utimes vfork \
   wait wait3 wait4 waitid waitpid wcscoll wcsftime wcsxfrm wmemcmp writev \
 ])
 
index f496b771999d917ba0ab388e6933d083df5a40f2..21822197708d3f3817ef7f961a462ce8aa116251 100644 (file)
 /* Define to 1 if you have the 'chflags' function. */
 #undef HAVE_CHFLAGS
 
+/* Define to 1 if you have the `chmod' function. */
+#undef HAVE_CHMOD
+
 /* Define to 1 if you have the `chown' function. */
 #undef HAVE_CHOWN
 
 /* Define to 1 if you have the `setitimer' function. */
 #undef HAVE_SETITIMER
 
+/* Define to 1 if you have the <setjmp.h> header file. */
+#undef HAVE_SETJMP_H
+
 /* Define to 1 if you have the `setlocale' function. */
 #undef HAVE_SETLOCALE
 
 /* Define this if you have tcl and TCL_UTF_MAX==6 */
 #undef HAVE_UCS4_TCL
 
+/* Define to 1 if you have the `umask' function. */
+#undef HAVE_UMASK
+
 /* Define to 1 if you have the `uname' function. */
 #undef HAVE_UNAME
 
 /* Define to force use of thread-safe errno, h_errno, and other functions */
 #undef _REENTRANT
 
+/* Define to 1 if you want to emulate signals on WASI */
+#undef _WASI_EMULATED_SIGNAL
+
 /* Define to the level of X/Open that your system supports */
 #undef _XOPEN_SOURCE