Skip to content

Commit 79424a4

Browse files
committed
Implement FIONBIO using fcntl
1 parent 60e65f5 commit 79424a4

2 files changed

Lines changed: 21 additions & 5 deletions

File tree

Lib/test/test_signal.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ def test_invalid_socket(self):
254254
signal.set_wakeup_fd, fd)
255255

256256
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
257-
@unittest.skipIf(support.is_emscripten, "Doesn't work")
258257
def test_set_wakeup_fd_result(self):
259258
r1, w1 = os.pipe()
260259
self.addCleanup(os.close, r1)
@@ -293,7 +292,6 @@ def test_set_wakeup_fd_socket_result(self):
293292
# function to test if a socket is in non-blocking mode.
294293
@unittest.skipIf(sys.platform == "win32", "tests specific to POSIX")
295294
@unittest.skipUnless(hasattr(os, "pipe"), "requires os.pipe()")
296-
@unittest.skipIf(support.is_emscripten, "Doesn't work")
297295
def test_set_wakeup_fd_blocking(self):
298296
rfd, wfd = os.pipe()
299297
self.addCleanup(os.close, rfd)

Python/emscripten_syscalls.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ int __syscall_umask(int mask) {
4242
#include <wasi/api.h>
4343
#include <errno.h>
4444
#include <fcntl.h>
45-
#undef errno
4645

4746
// Variant of EM_JS that does C preprocessor substitution on the body
4847
#define EM_JS_MACROS(ret, func_name, args, body...) \
@@ -187,7 +186,7 @@ EM_JS_MACROS(__externref_t, __maybe_fd_read_async, (
187186
if (e.name !== 'ErrnoError') {
188187
throw e;
189188
}
190-
return e.errno;
189+
return e["errno"];
191190
}
192191
})();
193192
};
@@ -269,7 +268,7 @@ EM_JS_MACROS(__externref_t, __maybe_poll_async, (intptr_t fds, int nfds, int tim
269268
return nonzero;
270269
} catch(e) {
271270
if (e?.name !== "ErrnoError") throw e;
272-
return -e.errno;
271+
return -e["errno"];
273272
}
274273
})();
275274
});
@@ -297,5 +296,24 @@ int __syscall_ioctl(int fd, int request, void* varargs) {
297296
if (request == FIOCLEX || request == FIONCLEX) {
298297
return 0;
299298
}
299+
if (request == FIONBIO) {
300+
// Implement FIONBIO via fcntl.
301+
// TODO: Upstream this.
302+
int flags = fcntl(fd, F_GETFL, 0);
303+
int nonblock = **((int**)varargs);
304+
if (flags < 0) {
305+
return errno;
306+
}
307+
if (nonblock) {
308+
flags |= O_NONBLOCK;
309+
} else {
310+
flags &= (~O_NONBLOCK);
311+
}
312+
int res = fcntl(fd, F_SETFL, flags);
313+
if (res < 0) {
314+
return errno;
315+
}
316+
return res;
317+
}
300318
return syscall_ioctl_orig(fd, request, varargs);
301319
}

0 commit comments

Comments
 (0)