-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
GH-134453: Fix subprocess memoryview input handling on POSIX #134949
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
Open
gpshead
wants to merge
4
commits into
python:main
Choose a base branch
from
gpshead:subprocess-posix-input-memoryview
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -957,6 +957,47 @@ def test_communicate(self): | |||||||||
self.assertEqual(stdout, b"banana") | ||||||||||
self.assertEqual(stderr, b"pineapple") | ||||||||||
|
||||||||||
def test_communicate_memoryview_input(self): | ||||||||||
# Test memoryview input with byte elements | ||||||||||
test_data = b"Hello, memoryview!" | ||||||||||
mv = memoryview(test_data) | ||||||||||
p = subprocess.Popen([sys.executable, "-c", | ||||||||||
'import sys; sys.stdout.write(sys.stdin.read())'], | ||||||||||
stdin=subprocess.PIPE, | ||||||||||
stdout=subprocess.PIPE) | ||||||||||
self.addCleanup(p.stdout.close) | ||||||||||
self.addCleanup(p.stdin.close) | ||||||||||
(stdout, stderr) = p.communicate(mv) | ||||||||||
self.assertEqual(stdout, test_data) | ||||||||||
self.assertEqual(stderr, None) | ||||||||||
|
||||||||||
def test_communicate_memoryview_input_nonbyte(self): | ||||||||||
# Test memoryview input with non-byte elements (e.g., int32) | ||||||||||
# This tests the fix for gh-134453 where non-byte memoryviews | ||||||||||
# had incorrect length tracking on POSIX | ||||||||||
import array | ||||||||||
# Create an array of 32-bit integers that's large enough to trigger | ||||||||||
# the chunked writing behavior (> PIPE_BUF) | ||||||||||
pipe_buf = getattr(select, 'PIPE_BUF', 512) | ||||||||||
# Each 'i' element is 4 bytes, so we need more than pipe_buf/4 elements | ||||||||||
# Add some extra to ensure we exceed the buffer size | ||||||||||
num_elements = (pipe_buf // 4) + 100 | ||||||||||
test_array = array.array('i', range(num_elements)) # 'i' = signed int (4 bytes each) | ||||||||||
Comment on lines
+984
to
+985
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, this test passes on the main branch. I think we want something like this:
Suggested change
From what I can tell, the pipe is taking each integer as a single byte. |
||||||||||
expected_bytes = test_array.tobytes() | ||||||||||
mv = memoryview(test_array) | ||||||||||
|
||||||||||
p = subprocess.Popen([sys.executable, "-c", | ||||||||||
'import sys; ' | ||||||||||
'data = sys.stdin.buffer.read(); ' | ||||||||||
'sys.stdout.buffer.write(data)'], | ||||||||||
stdin=subprocess.PIPE, | ||||||||||
stdout=subprocess.PIPE) | ||||||||||
self.addCleanup(p.stdout.close) | ||||||||||
self.addCleanup(p.stdin.close) | ||||||||||
(stdout, stderr) = p.communicate(mv) | ||||||||||
self.assertEqual(stdout, expected_bytes) | ||||||||||
self.assertEqual(stderr, None) | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
def test_communicate_timeout(self): | ||||||||||
p = subprocess.Popen([sys.executable, "-c", | ||||||||||
'import sys,os,time;' | ||||||||||
|
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Library/2025-05-30-18-37-44.gh-issue-134453.kxkA-o.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Fixed :func:`subprocess.Popen.communicate` ``input=`` handling of :class:`memoryview` | ||
instances that were non-byte shaped on POSIX platforms. Those are now properly | ||
cast to a byte shaped view instead of truncating the input. Windows platforms | ||
did not have this bug. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
assertIsNone