Skip to content

Commit

Permalink
pythongh-100227: Port dup3_works to the module state variable.
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Dec 15, 2022
1 parent bdd8674 commit c7a4761
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Port dup3_works of :mod:`posix` module to the module state variable. Patch
by Dong-hee Na.
25 changes: 16 additions & 9 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,7 @@ typedef struct {
PyObject *struct_rusage;
#endif
PyObject *st_mode;
int dup3_works;
} _posixstate;


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

if (fd < 0 || fd2 < 0) {
posix_error();
Expand Down Expand Up @@ -9455,21 +9451,22 @@ os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
#else

#ifdef HAVE_DUP3
if (!inheritable && dup3_works != 0) {
_posixstate *state = get_posix_state(module);
if (!inheritable && state->dup3_works != 0) {
Py_BEGIN_ALLOW_THREADS
res = dup3(fd, fd2, O_CLOEXEC);
Py_END_ALLOW_THREADS
if (res < 0) {
if (dup3_works == -1)
if (state->dup3_works == -1)
dup3_works = (errno != ENOSYS);
if (dup3_works) {
if (state->dup3_works) {
posix_error();
return -1;
}
}
}

if (inheritable || dup3_works == 0)
if (inheritable || state->dup3_works == 0)
{
#endif
Py_BEGIN_ALLOW_THREADS
Expand Down Expand Up @@ -15872,6 +15869,16 @@ posixmodule_exec(PyObject *m)
{
_posixstate *state = get_posix_state(m);

#if defined(HAVE_DUP3) && \
!(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
/* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
state->dup3_works = -1;
#elif !defined(HAVE_DUP3)
state->dup3_works = -1;
#else
state->dup3_works = 0;
#endif

#if defined(HAVE_PWRITEV)
if (HAVE_PWRITEV_RUNTIME) {} else {
PyObject* dct = PyModule_GetDict(m);
Expand Down
2 changes: 0 additions & 2 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ Python/bootstrap_hash.c py_getrandom getrandom_works -
Python/fileutils.c - _Py_open_cloexec_works -
Python/fileutils.c set_inheritable ioctl_works -
# (set lazily, *after* first init)
# XXX Is this thread-safe?
Modules/posixmodule.c os_dup2_impl dup3_works -

## guards around resource init
Python/thread_pthread.h PyThread__init_thread lib_initialized -
Expand Down

0 comments on commit c7a4761

Please sign in to comment.