Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-100227: Port dup3_works to the module state variable. #100262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
28 changes: 18 additions & 10 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,23 @@ 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)
dup3_works = (errno != ENOSYS);
if (dup3_works) {
if (state->dup3_works == -1) {
state->dup3_works = (errno != ENOSYS);
}
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 +15870,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
3 changes: 0 additions & 3 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ filename funcname name reason
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