Skip to content
Myr

Merge pull request #2825 from Lukasa/universal_newlines

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add warnings about text/binary mode files.

authored by Ian Cordasco · committed by Ian Cordasco · parents 1dff6c9, f1fd11e · Browse files · 5 files+66 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+66 added−4 removed

  • other:+66 added−4 removed

5 files+66 added−4 removed

modified:docs/user/advanced.rst+19 added-1 removed
313 unmodified lines
314315 with open('massive-body', 'rb') as f:316 requests.post('http://some.url/streamed', data=f)317318.. warning:: It is strongly recommended that you open files in `binary mode`_.319 This is because Requests may attempt to provide the320 ``Content-Length`` header for you, and if it does this value will321 be set to the number of *bytes* in the file. Errors may occur if322 you open the file in *text mode*.323324.. _binary mode: https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files325326327.. _chunk-encoding:328329Chunk-Encoded Requests330----------------------
39 unmodified lines
370 'Content-Type': 'multipart/form-data; boundary=3131623adb2043caaeb5538cc7aa0b3a',371 ...372 }373374.. warning:: It is strongly recommended that you open files in `binary mode`_.375 This is because Requests may attempt to provide the376 ``Content-Length`` header for you, and if it does this value will377 be set to the number of *bytes* in the file. Errors may occur if378 you open the file in *text mode*.379380.. _binary mode: https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files381382383.. _event-hooks:384385Event Hooks386-----------
149 unmodified lines
536 "http://10.20.1.128": "http://10.10.1.10:5323",537 }538539Note that proxy URLs must include the scheme.522 540541.. _compliance:542543Compliance544----------
351 unmodified lines
modified:docs/user/quickstart.rst+8 added
300 unmodified lines
301302For sending multiple files in one request refer to the :ref:`advanced <advanced>`303section.304305.. warning:: It is strongly recommended that you open files in `binary mode`_.306 This is because Requests may attempt to provide the307 ``Content-Length`` header for you, and if it does this value will308 be set to the number of *bytes* in the file. Errors may occur if309 you open the file in *text mode*.310311.. _binary mode: https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files312313314Response Status Codes315---------------------316317We can check the response status code::
177 unmodified lines
modified:requests/__init__.py+7 added-1 removed
60 unmodified lines
61from .sessions import session, Session62from .status_codes import codes63from .exceptions import (64 RequestException, Timeout, URLRequired,65 TooManyRedirects, HTTPError, ConnectionError65 TooManyRedirects, HTTPError, ConnectionError,66 FileModeWarning,67)6869# Set default logging handler to avoid "No handler found" warnings.70import logging
4 unmodified lines
75 def emit(self, record):76 pass7778logging.getLogger(__name__).addHandler(NullHandler())7980import warnings8182# FileModeWarnings go off per the default.83warnings.simplefilter('default', FileModeWarning, append=True)
modified:requests/exceptions.py+15 added
95 unmodified lines
969798class RetryError(RequestException):99 """Custom retries logic failed"""100101102# Warnings103104105class RequestsWarning(Warning):106 """Base warning for Requests."""107 pass108109110class FileModeWarning(RequestsWarning, DeprecationWarning):111 """112 A file was opened in text mode, but Requests determined its binary length.113 """114 pass
modified:requests/utils.py+17 added-2 removed
27 unmodified lines
28 builtin_str, getproxies, proxy_bypass, urlunparse,29 basestring)30from .cookies import RequestsCookieJar, cookiejar_from_dict31from .structures import CaseInsensitiveDict32from .exceptions import InvalidURL32from .exceptions import InvalidURL, FileModeWarning3334_hush_pyflakes = (RequestsCookieJar,)3536NETRC_FILES = ('.netrc', '_netrc')
22 unmodified lines
59 fileno = o.fileno()60 except io.UnsupportedOperation:61 pass62 else:63 return os.fstat(fileno).st_size63 filesize = os.fstat(fileno).st_size6465 # Having used fstat to determine the file length, we need to66 # confirm that this file was opened up in binary mode.67 if 'b' not in o.mode:68 warnings.warn((69 "Requests has determined the content-length for this "70 "request using the binary size of the file: however, the "71 "file has been opened in text mode (i.e. without the 'b' "72 "flag in the mode). This may lead to an incorrect "73 "content-length. In Requests 3.0, support will be removed "74 "for files in text mode."),75 FileModeWarning76 )7778 return filesize7980 if hasattr(o, 'getvalue'):81 # e.g. BytesIO, cStringIO.StringIO82 return len(o.getvalue())83
628 unmodified lines