X Tutup
The Wayback Machine - https://web.archive.org/web/20260215072913/https://github.com/python/cpython/pull/102148
Skip to content

gh-77043: Make os.dup2(fd, fd, False) always remove inheritability#102148

Open
izbyshev wants to merge 2 commits intopython:mainfrom
izbyshev:gh-77043-dup2-eq-fd
Open

gh-77043: Make os.dup2(fd, fd, False) always remove inheritability#102148
izbyshev wants to merge 2 commits intopython:mainfrom
izbyshev:gh-77043-dup2-eq-fd

Conversation

@izbyshev
Copy link
Contributor

@izbyshev izbyshev commented Feb 22, 2023

Currently, os.dup2(fd, fd, False) behaves differently depending on the
platform:

  • On Windows, Linux before 2.6.27, macOS, and FreeBSD it validates the
    fd and makes it non-inheritable.

  • On Solaris it validates the fd, but doesn't change its inheritability.

  • On modern Linux and Illumos it fails with OSError.

Fix this inconsistency by making it always behave as in the first point.

@izbyshev
Copy link
Contributor Author

In contrast to PR #5713, instead of adding more conditions to already unnecessarily convoluted logic of os_dup2_impl, I've took the opportunity to refactor it to clearly express what it wants to do (roughly "if not inheritable, try to do special stuff for atomicity; fallback to dup2()).


If *fd* is equal to *fd2*, the file descriptor is validated, but its
inheritability is not changed unless *inheritable* is ``False``.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding comment from PR #5713, I'm not sure if it should be versionchanged. The current mess is unintentional, so it's not like we had some clear behavior before and then we changed it.

Currently, os.dup2(fd, fd, False) behaves differently depending on the
platform:

* On Windows, Linux before 2.6.27, macOS, and FreeBSD it validates the
  fd and makes it non-inheritable.

* On Solaris it validates the fd, but doesn't change its inheritability.

* On modern Linux and Illumos it fails with OSError.

Fix this inconsistency by making it always behave as in the first point.
@izbyshev izbyshev force-pushed the gh-77043-dup2-eq-fd branch from 6a7cce6 to 9442ad8 Compare March 5, 2023 09:30
@izbyshev izbyshev force-pushed the gh-77043-dup2-eq-fd branch from 1123368 to 5f2f2e1 Compare April 5, 2023 18:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review extension-modules C modules in the Modules dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup