Skip to content

Commit c7a4761

Browse files
committed
gh-100227: Port dup3_works to the module state variable.
1 parent bdd8674 commit c7a4761

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Port dup3_works of :mod:`posix` module to the module state variable. Patch
2+
by Dong-hee Na.

Modules/posixmodule.c

+16-9
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,7 @@ typedef struct {
989989
PyObject *struct_rusage;
990990
#endif
991991
PyObject *st_mode;
992+
int dup3_works;
992993
} _posixstate;
993994

994995

@@ -9407,11 +9408,6 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
94079408
/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
94089409
{
94099410
int res = 0;
9410-
#if defined(HAVE_DUP3) && \
9411-
!(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
9412-
/* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
9413-
static int dup3_works = -1;
9414-
#endif
94159411

94169412
if (fd < 0 || fd2 < 0) {
94179413
posix_error();
@@ -9455,21 +9451,22 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
94559451
#else
94569452

94579453
#ifdef HAVE_DUP3
9458-
if (!inheritable && dup3_works != 0) {
9454+
_posixstate *state = get_posix_state(module);
9455+
if (!inheritable && state->dup3_works != 0) {
94599456
Py_BEGIN_ALLOW_THREADS
94609457
res = dup3(fd, fd2, O_CLOEXEC);
94619458
Py_END_ALLOW_THREADS
94629459
if (res < 0) {
9463-
if (dup3_works == -1)
9460+
if (state->dup3_works == -1)
94649461
dup3_works = (errno != ENOSYS);
9465-
if (dup3_works) {
9462+
if (state->dup3_works) {
94669463
posix_error();
94679464
return -1;
94689465
}
94699466
}
94709467
}
94719468

9472-
if (inheritable || dup3_works == 0)
9469+
if (inheritable || state->dup3_works == 0)
94739470
{
94749471
#endif
94759472
Py_BEGIN_ALLOW_THREADS
@@ -15872,6 +15869,16 @@ posixmodule_exec(PyObject *m)
1587215869
{
1587315870
_posixstate *state = get_posix_state(m);
1587415871

15872+
#if defined(HAVE_DUP3) && \
15873+
!(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
15874+
/* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
15875+
state->dup3_works = -1;
15876+
#elif !defined(HAVE_DUP3)
15877+
state->dup3_works = -1;
15878+
#else
15879+
state->dup3_works = 0;
15880+
#endif
15881+
1587515882
#if defined(HAVE_PWRITEV)
1587615883
if (HAVE_PWRITEV_RUNTIME) {} else {
1587715884
PyObject* dct = PyModule_GetDict(m);

Tools/c-analyzer/cpython/ignored.tsv

-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ Python/bootstrap_hash.c py_getrandom getrandom_works -
1919
Python/fileutils.c - _Py_open_cloexec_works -
2020
Python/fileutils.c set_inheritable ioctl_works -
2121
# (set lazily, *after* first init)
22-
# XXX Is this thread-safe?
23-
Modules/posixmodule.c os_dup2_impl dup3_works -
2422

2523
## guards around resource init
2624
Python/thread_pthread.h PyThread__init_thread lib_initialized -

0 commit comments

Comments
 (0)