Skip to content

Commit db13c0c

Browse files
authored
[3.11] gh-95174: Handle missing dup() and constants in WASI (GH-95229) (GH-95272)
Co-authored-by: Christian Heimes <[email protected]>
1 parent c7ac8b6 commit db13c0c

12 files changed

+47
-5
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Python now detects missing ``dup`` function in WASI and works around some
2+
missing :mod:`errno`, :mod:`select`, and :mod:`socket` constants.

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/errnomodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ errno_exec(PyObject *module)
280280
#ifdef ENOANO
281281
add_errcode("ENOANO", ENOANO, "No anode");
282282
#endif
283+
#if defined(__wasi__) && !defined(ESHUTDOWN)
284+
// WASI SDK 16 does not have ESHUTDOWN, shutdown results in EPIPE.
285+
#define ESHUTDOWN EPIPE
286+
#endif
283287
#ifdef ESHUTDOWN
284288
add_errcode("ESHUTDOWN", ESHUTDOWN, "Cannot send after transport endpoint shutdown");
285289
#else

Modules/posixmodule.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9346,7 +9346,9 @@ os_dup_impl(PyObject *module, int fd)
93469346
return _Py_dup(fd);
93479347
}
93489348

9349-
9349+
// dup2() is either provided by libc or dup2.c with AC_REPLACE_FUNCS().
9350+
// dup2.c provides working dup2() if and only if F_DUPFD is available.
9351+
#if (defined(HAVE_DUP3) || defined(F_DUPFD) || defined(MS_WINDOWS))
93509352
/*[clinic input]
93519353
os.dup2 -> int
93529354
fd: int
@@ -9446,6 +9448,7 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
94469448

94479449
return res;
94489450
}
9451+
#endif
94499452

94509453

94519454
#ifdef HAVE_LOCKF

Modules/selectmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ extern void bzero(void *, int);
6363
# define SOCKET int
6464
#endif
6565

66+
// WASI SDK 16 does not have POLLPRIO, define as no-op
67+
#if defined(__wasi__) && !defined(POLLPRI)
68+
# define POLLPRI 0
69+
#endif
70+
6671
typedef struct {
6772
PyObject *close;
6873
PyTypeObject *poll_Type;

Modules/socketmodule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7650,6 +7650,10 @@ PyInit__socket(void)
76507650
PyModule_AddIntMacro(m, MSG_EOR);
76517651
#endif
76527652
#ifdef MSG_TRUNC
7653+
// workaround for https://github.com/WebAssembly/wasi-libc/issues/305
7654+
#if defined(__wasi__) && !defined(__WASI_RIFLAGS_RECV_DATA_TRUNCATED)
7655+
# define __WASI_RIFLAGS_RECV_DATA_TRUNCATED 2
7656+
#endif
76537657
PyModule_AddIntMacro(m, MSG_TRUNC);
76547658
#endif
76557659
#ifdef MSG_CTRUNC

PC/pyconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,9 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */
681681
/* Define if you have the 'inet_pton' function. */
682682
#define HAVE_INET_PTON 1
683683

684+
/* Define to 1 if you have the `dup' function. */
685+
#define HAVE_DUP 1
686+
684687
/* framework name */
685688
#define _PYTHONFRAMEWORK ""
686689

Python/dup2.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* Return fd2 if all went well; return BADEXIT otherwise.
1212
*/
1313

14+
#include <errno.h>
1415
#include <fcntl.h>
1516
#include <unistd.h>
1617

@@ -20,12 +21,17 @@ int
2021
dup2(int fd1, int fd2)
2122
{
2223
if (fd1 != fd2) {
24+
#ifdef F_DUPFD
2325
if (fcntl(fd1, F_GETFL) < 0)
2426
return BADEXIT;
2527
if (fcntl(fd2, F_GETFL) >= 0)
2628
close(fd2);
2729
if (fcntl(fd1, F_DUPFD, fd2) < 0)
2830
return BADEXIT;
31+
#else
32+
errno = ENOTSUP;
33+
return BADEXIT;
34+
#endif
2935
}
3036
return fd2;
3137
}

Python/fileutils.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2379,7 +2379,7 @@ _Py_dup(int fd)
23792379
return -1;
23802380
}
23812381

2382-
#else
2382+
#elif HAVE_DUP
23832383
Py_BEGIN_ALLOW_THREADS
23842384
_Py_BEGIN_SUPPRESS_IPH
23852385
fd = dup(fd);
@@ -2396,6 +2396,10 @@ _Py_dup(int fd)
23962396
_Py_END_SUPPRESS_IPH
23972397
return -1;
23982398
}
2399+
#else
2400+
errno = ENOTSUP;
2401+
PyErr_SetFromErrno(PyExc_OSError);
2402+
return -1;
23992403
#endif
24002404
return fd;
24012405
}

configure

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

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4537,7 +4537,7 @@ fi
45374537
# checks for library functions
45384538
AC_CHECK_FUNCS([ \
45394539
accept4 alarm bind_textdomain_codeset chmod chown clock close_range confstr \
4540-
copy_file_range ctermid dup3 execv explicit_bzero explicit_memset \
4540+
copy_file_range ctermid dup dup3 execv explicit_bzero explicit_memset \
45414541
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
45424542
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
45434543
gai_strerror getegid getentropy geteuid getgid getgrgid getgrgid_r \

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@
278278
/* Define to 1 if you have the `dlopen' function. */
279279
#undef HAVE_DLOPEN
280280

281+
/* Define to 1 if you have the `dup' function. */
282+
#undef HAVE_DUP
283+
281284
/* Define to 1 if you have the `dup2' function. */
282285
#undef HAVE_DUP2
283286

0 commit comments

Comments
 (0)