Skip to content

Commit a6ca8ee

Browse files
authored
bpo-46315: Add ifdef HAVE_ feature checks for WASI compatibility (GH-30507)
1 parent 1de6015 commit a6ca8ee

14 files changed

+90
-14
lines changed

Include/internal/pycore_condvar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
*/
2121
#define Py_HAVE_CONDVAR
2222

23-
#include <pthread.h>
23+
#ifdef HAVE_PTHREAD_H
24+
# include <pthread.h>
25+
#endif
2426

2527
#define PyMUTEX_T pthread_mutex_t
2628
#define PyCOND_T pthread_cond_t

Include/pythread.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Py_DEPRECATED(3.7) PyAPI_FUNC(void) PyThread_ReInitTLS(void);
125125
typedef struct _Py_tss_t Py_tss_t; /* opaque */
126126

127127
#ifndef Py_LIMITED_API
128-
#if defined(_POSIX_THREADS)
128+
#ifdef HAVE_PTHREAD_H
129129
/* Darwin needs pthread.h to know type name the pthread_key_t. */
130130
# include <pthread.h>
131131
# define NATIVE_TSS_KEY_T pthread_key_t
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Added and fixed ``#ifdef HAVE_FEATURE`` checks for functionality that is not
2+
available on WASI platform.

Modules/_randommodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,11 @@ random_seed_time_pid(RandomObject *self)
258258
key[0] = (uint32_t)(now & 0xffffffffU);
259259
key[1] = (uint32_t)(now >> 32);
260260

261+
#ifdef HAVE_GETPID
261262
key[2] = (uint32_t)getpid();
263+
#else
264+
key[2] = 0;
265+
#endif
262266

263267
now = _PyTime_GetMonotonicClock();
264268
key[3] = (uint32_t)(now & 0xffffffffU);

Modules/clinic/posixmodule.c.h

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/faulthandler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <signal.h>
1111
#include <signal.h>
1212
#include <stdlib.h> // abort()
13-
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK)
13+
#if defined(HAVE_PTHREAD_SIGMASK) && !defined(HAVE_BROKEN_PTHREAD_SIGMASK) && defined(HAVE_PTHREAD_H)
1414
# include <pthread.h>
1515
#endif
1616
#ifdef MS_WINDOWS

Modules/posixmodule.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,7 +3292,14 @@ os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
32923292
}
32933293
else
32943294
#endif /* HAVE_FHCMODAT */
3295+
{
3296+
#ifdef HAVE_CHMOD
32953297
result = chmod(path->narrow, mode);
3298+
#else
3299+
result = -1;
3300+
errno = ENOSYS;
3301+
#endif
3302+
}
32963303
Py_END_ALLOW_THREADS
32973304

32983305
if (result) {
@@ -4885,6 +4892,7 @@ os_system_impl(PyObject *module, PyObject *command)
48854892
#endif /* HAVE_SYSTEM */
48864893

48874894

4895+
#ifdef HAVE_UMASK
48884896
/*[clinic input]
48894897
os.umask
48904898
@@ -4903,6 +4911,7 @@ os_umask_impl(PyObject *module, int mask)
49034911
return posix_error();
49044912
return PyLong_FromLong((long)i);
49054913
}
4914+
#endif
49064915

49074916
#ifdef MS_WINDOWS
49084917

Modules/xxsubtype.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,11 @@ spam_bench(PyObject *self, PyObject *args)
237237
{
238238
PyObject *obj, *name, *res;
239239
int n = 1000;
240-
time_t t0, t1;
240+
time_t t0 = 0, t1 = 0;
241241

242242
if (!PyArg_ParseTuple(args, "OU|i", &obj, &name, &n))
243243
return NULL;
244+
#ifdef HAVE_CLOCK
244245
t0 = clock();
245246
while (--n >= 0) {
246247
res = PyObject_GetAttr(obj, name);
@@ -249,6 +250,7 @@ spam_bench(PyObject *self, PyObject *args)
249250
Py_DECREF(res);
250251
}
251252
t1 = clock();
253+
#endif
252254
return PyFloat_FromDouble((double)(t1-t0) / CLOCKS_PER_SEC);
253255
}
254256

PC/pyconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
529529
/* Define if you have times. */
530530
/* #undef HAVE_TIMES */
531531

532+
/* Define to 1 if you have the `umask' function. */
533+
#define HAVE_UMASK 1
534+
532535
/* Define if you have uname. */
533536
/* #undef HAVE_UNAME */
534537

Python/pyfpe.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
* though, because they may be referenced by extensions using the stable ABI.
44
*/
55

6-
#include "setjmp.h"
6+
#ifdef HAVE_SETJMP_H
7+
#include <setjmp.h>
78

89
jmp_buf PyFPE_jbuf;
10+
#endif
11+
912
int PyFPE_counter;
1013

1114
double

Tools/wasm/config.site-wasm32-wasi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# config.site override for cross compiling to wasm32-wasi platform
2+
#
3+
# Written by Christian Heimes <[email protected]>
4+
# Partly based on pyodide's pyconfig.undefs.h file.
5+
6+
7+
# cannot be detected in cross builds
8+
ac_cv_buggy_getaddrinfo=no
9+
10+
# WASI has no /dev/pt*
11+
ac_cv_file__dev_ptmx=no
12+
ac_cv_file__dev_ptc=no
13+
14+
# dummy readelf, WASI build does not need readelf.
15+
ac_cv_prog_ac_ct_READELF=true
16+
17+
ac_cv_func_eventfd=no

configure

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6268,7 +6268,7 @@ else
62686268
EXEEXT=.html ;; #(
62696269
Emscripten/node) :
62706270
EXEEXT=.js ;; #(
6271-
wasi/*) :
6271+
WASI/*) :
62726272
EXEEXT=.wasm ;; #(
62736273
*) :
62746274
EXEEXT=
@@ -7627,6 +7627,15 @@ case $ac_sys_system/$ac_sys_emscripten_target in #(
76277627
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"
76287628
CFLAGS_NODIST="$(CFLAGS_NODIST) -pthread"
76297629
;; #(
7630+
WASI) :
7631+
7632+
7633+
$as_echo "#define _WASI_EMULATED_SIGNAL 1" >>confdefs.h
7634+
7635+
LIBS="$LIBS -lwasi-emulated-signal"
7636+
echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h
7637+
7638+
;; #(
76307639
*) :
76317640
;;
76327641
esac
@@ -8543,7 +8552,7 @@ for ac_header in \
85438552
alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
85448553
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/memfd.h linux/random.h linux/soundcard.h \
85458554
linux/tipc.h linux/wait.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
8546-
sched.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
8555+
sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
85478556
sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \
85488557
sys/loadavg.h sys/lock.h sys/memfd.h sys/mkdev.h sys/mman.h sys/modem.h sys/param.h sys/poll.h \
85498558
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
1363013639

1363113640
# checks for library functions
1363213641
for ac_func in \
13633-
accept4 alarm bind_textdomain_codeset chown clock close_range confstr \
13642+
accept4 alarm bind_textdomain_codeset chmod chown clock close_range confstr \
1363413643
copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \
1363513644
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
1363613645
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
@@ -13652,7 +13661,7 @@ for ac_func in \
1365213661
sigfillset siginterrupt sigpending sigrelse sigtimedwait sigwait \
1365313662
sigwaitinfo snprintf splice strftime strlcpy strsignal symlinkat sync \
1365413663
sysconf system tcgetpgrp tcsetpgrp tempnam timegm times tmpfile \
13655-
tmpnam tmpnam_r truncate ttyname uname unlinkat utimensat utimes vfork \
13664+
tmpnam tmpnam_r truncate ttyname umask uname unlinkat utimensat utimes vfork \
1365613665
wait wait3 wait4 waitid waitpid wcscoll wcsftime wcsxfrm wmemcmp writev \
1365713666

1365813667
do :

configure.ac

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ AC_ARG_WITH([suffix],
10931093
AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
10941094
[Emscripten/browser], [EXEEXT=.html],
10951095
[Emscripten/node], [EXEEXT=.js],
1096-
[wasi/*], [EXEEXT=.wasm],
1096+
[WASI/*], [EXEEXT=.wasm],
10971097
[EXEEXT=]
10981098
)
10991099
])
@@ -1805,6 +1805,11 @@ AS_CASE([$ac_sys_system/$ac_sys_emscripten_target],
18051805
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"
18061806
CFLAGS_NODIST="$(CFLAGS_NODIST) -pthread"
18071807
],
1808+
[WASI], [
1809+
AC_DEFINE([_WASI_EMULATED_SIGNAL], [1], [Define to 1 if you want to emulate signals on WASI])
1810+
LIBS="$LIBS -lwasi-emulated-signal"
1811+
echo "#define _WASI_EMULATED_SIGNAL 1" >> confdefs.h
1812+
]
18081813
)
18091814

18101815
AC_SUBST(BASECFLAGS)
@@ -2306,7 +2311,7 @@ AC_CHECK_HEADERS([ \
23062311
alloca.h asm/types.h bluetooth.h conio.h crypt.h direct.h dlfcn.h endian.h errno.h fcntl.h grp.h \
23072312
ieeefp.h io.h langinfo.h libintl.h libutil.h linux/memfd.h linux/random.h linux/soundcard.h \
23082313
linux/tipc.h linux/wait.h netinet/in.h netpacket/packet.h poll.h process.h pthread.h pty.h \
2309-
sched.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
2314+
sched.h setjmp.h shadow.h signal.h spawn.h stropts.h sys/audioio.h sys/bsdtty.h sys/devpoll.h \
23102315
sys/endian.h sys/epoll.h sys/event.h sys/eventfd.h sys/file.h sys/ioctl.h sys/kern_control.h \
23112316
sys/loadavg.h sys/lock.h sys/memfd.h sys/mkdev.h sys/mman.h sys/modem.h sys/param.h sys/poll.h \
23122317
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
40624067

40634068
# checks for library functions
40644069
AC_CHECK_FUNCS([ \
4065-
accept4 alarm bind_textdomain_codeset chown clock close_range confstr \
4070+
accept4 alarm bind_textdomain_codeset chmod chown clock close_range confstr \
40664071
copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \
40674072
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
40684073
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
@@ -4084,7 +4089,7 @@ AC_CHECK_FUNCS([ \
40844089
sigfillset siginterrupt sigpending sigrelse sigtimedwait sigwait \
40854090
sigwaitinfo snprintf splice strftime strlcpy strsignal symlinkat sync \
40864091
sysconf system tcgetpgrp tcsetpgrp tempnam timegm times tmpfile \
4087-
tmpnam tmpnam_r truncate ttyname uname unlinkat utimensat utimes vfork \
4092+
tmpnam tmpnam_r truncate ttyname umask uname unlinkat utimensat utimes vfork \
40884093
wait wait3 wait4 waitid waitpid wcscoll wcsftime wcsxfrm wmemcmp writev \
40894094
])
40904095

pyconfig.h.in

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@
127127
/* Define to 1 if you have the 'chflags' function. */
128128
#undef HAVE_CHFLAGS
129129

130+
/* Define to 1 if you have the `chmod' function. */
131+
#undef HAVE_CHMOD
132+
130133
/* Define to 1 if you have the `chown' function. */
131134
#undef HAVE_CHOWN
132135

@@ -977,6 +980,9 @@
977980
/* Define to 1 if you have the `setitimer' function. */
978981
#undef HAVE_SETITIMER
979982

983+
/* Define to 1 if you have the <setjmp.h> header file. */
984+
#undef HAVE_SETJMP_H
985+
980986
/* Define to 1 if you have the `setlocale' function. */
981987
#undef HAVE_SETLOCALE
982988

@@ -1336,6 +1342,9 @@
13361342
/* Define this if you have tcl and TCL_UTF_MAX==6 */
13371343
#undef HAVE_UCS4_TCL
13381344

1345+
/* Define to 1 if you have the `umask' function. */
1346+
#undef HAVE_UMASK
1347+
13391348
/* Define to 1 if you have the `uname' function. */
13401349
#undef HAVE_UNAME
13411350

@@ -1704,6 +1713,9 @@
17041713
/* Define to force use of thread-safe errno, h_errno, and other functions */
17051714
#undef _REENTRANT
17061715

1716+
/* Define to 1 if you want to emulate signals on WASI */
1717+
#undef _WASI_EMULATED_SIGNAL
1718+
17071719
/* Define to the level of X/Open that your system supports */
17081720
#undef _XOPEN_SOURCE
17091721

0 commit comments

Comments
 (0)