Skip to content

If the input stream is a text stream, use its binary buffer #125

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
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions multipart/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1937,6 +1937,10 @@ def parse_form(
content_length = float("inf")
bytes_read = 0

# If the input stream is a text stream, use its binary buffer
if isinstance (input_stream, io .TextIOBase):
input_stream = input_stream .buffer

while True:
# Read only up to the Content-Length given.
max_readable = min(content_length - bytes_read, 1048576)
Expand Down
35 changes: 33 additions & 2 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import sys
import tempfile
import unittest
from io import BytesIO
from io import BytesIO, TextIOBase
from typing import TYPE_CHECKING
from unittest.mock import Mock

Expand Down Expand Up @@ -1268,7 +1268,7 @@ def test_create_form_parser_error(self):
with self.assertRaises(ValueError):
create_form_parser(headers, None, None)

def test_parse_form(self):
def test_parse_form_bytes(self):
on_field = Mock()
on_file = Mock()

Expand All @@ -1280,6 +1280,37 @@ def test_parse_form(self):
# 15 - i.e. all data is written.
self.assertEqual(on_file.call_args[0][0].size, 15)

def test_parse_form_file(self):
on_field = Mock()
on_file = Mock()

with open ('12345678.txt', 'wt+') as f:
f .write ('123456789012345')
f .seek (0o0)
parse_form({"Content-Type": "application/octet-stream"}, f .buffer, on_field, on_file)

assert on_file.call_count == 1

# Assert that the first argument of the call (a File object) has size
# 15 - i.e. all data is written.
self.assertEqual(on_file.call_args[0][0].size, 15)

def test_parse_form_text(self):
on_field = Mock()
on_file = Mock()

with open ('12345678.txt', 'wt+') as f:
f .write ('123456789012345')
f .seek (0o0)
self .assertTrue (isinstance (f, TextIOBase))
parse_form({"Content-Type": "application/octet-stream"}, f, on_field, on_file)

assert on_file.call_count == 1

# Assert that the first argument of the call (a File object) has size
# 15 - i.e. all data is written.
self.assertEqual(on_file.call_args[0][0].size, 15)

def test_parse_form_content_length(self):
files = []

Expand Down