Skip to content

Conversation

izbyshev
Copy link
Contributor

@izbyshev izbyshev commented Feb 14, 2018

When redirecting, subprocess attempts to achieve the following state:
each fd to be redirected to is less than or equal to the fd
it is redirected from, which is necessary because redirection
occurs in the ascending order of destination descriptors.
It fails to do so in a couple of corner cases,
for example, if 1 is redirected to 2 and 0 is closed in the parent.

https://bugs.python.org/issue32844

…stderr

When redirecting, subprocess attempts to achieve the following state:
each fd to be redirected to is less than or equal to the fd
it is redirected from, which is necessary because redirection
occurs in the ascending order of destination descriptors.
It fails to do so in a couple of corner cases,
for example, if 1 is redirected to 2 and 0 is closed in the parent.
os.lseek(from_fd, 0, os.SEEK_SET)
exp_bytes = str(to_fd).encode('ascii')
read_bytes = os.read(from_fd, 1024)
self.assertEqual(read_bytes, exp_bytes)
Copy link
Member

Choose a reason for hiding this comment

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

To make debugging failures easier, add a msg= parameter to this assertion that contains the an explanation of the from_fds, to_fds, and skipped_fd configuration that was being tested.

os.write(fd, str(fd).encode('ascii'))
''')

skipped_fd = next(fd for fd in range(3) if fd not in to_fds)
Copy link
Member

Choose a reason for hiding this comment

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

I believe this works, but it feels like something using sets might be easier to understand?

({0, 1, 2} - set(to_fds)).pop() perhaps?

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@@ -2195,7 +2193,7 @@ def _check_swap_std_fds_with_one_closed(self, from_fds, to_fds):
os.write(fd, str(fd).encode('ascii'))
''')

skipped_fd = next(fd for fd in range(3) if fd not in to_fds)
skipped_fd = (set(range(3)) - set(to_fds)).pop()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've used the suggested construct but with range(3) instead of {0, 1, 2} for consistency with the surrounding code.

parent descriptor {from_fd} got redirected
to descriptor(s) {read_fds} instead of descriptor {to_fd}.
""")
self.assertEqual([to_fd], read_fds, msg)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that the assertion comparing bytes would be not very friendly even with a message, so I've decided it's not too dangerous to decode them.

@izbyshev
Copy link
Contributor Author

Thank you for the review! I have made the requested changes; please review again.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@gpshead: please review the changes made to this pull request.

@gpshead gpshead merged commit 0e7144b into python:master Mar 26, 2018
@miss-islington
Copy link
Contributor

Thanks @izbyshev for the PR, and @gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-6262 is a backport of this pull request to the 3.7 branch.

@bedevere-bot
Copy link

GH-6263 is a backport of this pull request to the 3.6 branch.

@izbyshev izbyshev deleted the bpo-32844 branch March 26, 2018 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants