Skip to content
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

Doc'd that HttpResponse accepts bytestrings. #11134

Merged
merged 2 commits into from
Mar 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions django/core/files/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ def write(self, data):


def endswith_cr(line):
"""Return True if line (a text or byte string) ends with '\r'."""
"""Return True if line (a text or bytestring) ends with '\r'."""
return line.endswith('\r' if isinstance(line, str) else b'\r')


def endswith_lf(line):
"""Return True if line (a text or byte string) ends with '\n'."""
"""Return True if line (a text or bytestring) ends with '\n'."""
return line.endswith('\n' if isinstance(line, str) else b'\n')


def equals_lf(line):
"""Return True if line (a text or byte string) equals '\n'."""
"""Return True if line (a text or bytestring) equals '\n'."""
return line == ('\n' if isinstance(line, str) else b'\n')
2 changes: 1 addition & 1 deletion django/db/backends/postgresql/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
sql_delete_procedure = 'DROP FUNCTION %(procedure)s(%(param_types)s)'

def quote_value(self, value):
# getquoted() returns a quoted byte string of the adapted value.
# getquoted() returns a quoted bytestring of the adapted value.
return psycopg2.extensions.adapt(value).getquoted().decode()

def _field_indexes_sql(self, model, field):
Expand Down
2 changes: 1 addition & 1 deletion django/utils/safestring.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SafeString(str, SafeData):
"""
def __add__(self, rhs):
"""
Concatenating a safe string with another safe byte string or
Concatenating a safe string with another safe bytestring or
safe string is safe. Otherwise, the result is no longer safe.
"""
t = super().__add__(rhs)
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/files/uploads.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ Custom file upload handlers **must** define the following methods:

Receives a "chunk" of data from the file upload.

``raw_data`` is a byte string containing the uploaded data.
``raw_data`` is a bytestring containing the uploaded data.

``start`` is the position in the file where this ``raw_data`` chunk
begins.
Expand Down
18 changes: 9 additions & 9 deletions docs/ref/request-response.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ All attributes should be considered read-only, unless stated otherwise.

.. attribute:: HttpRequest.body

The raw HTTP request body as a byte string. This is useful for processing
The raw HTTP request body as a bytestring. This is useful for processing
Copy link
Member

Choose a reason for hiding this comment

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

OK, should this not be just bytes, (a bytestring being the particular representation of it... b'some bytes here')?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure 🤔 It seems that in our documentation bytes is mainly used as a unit of size.

Copy link
Member

Choose a reason for hiding this comment

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

No, I'm not sure either. It's just that I think we're might be doing it wrong...

>>> a_bytestring = b'some content'
>>> type(a_bytestring)
bytes

So e.g. the change to 1.7.1.txt, the type returned is bytes, which yes we see as a bytestring (with the b'') when printed... (and so on for several of the changes here.)

Happy to not make a thing of it just now. 🙂 (It just came up from our discussion earlier and reviewing here.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Changes from first commit 2eaac75 I treat as a typo. IMO we can merge both commits, or only second if you prefer.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, fine. Agreed it's not byte strings 🙂

data in different ways than conventional HTML forms: binary images,
XML payload etc. For processing conventional form data, use
:attr:`HttpRequest.POST`.
Expand Down Expand Up @@ -631,12 +631,13 @@ Usage
Passing strings
~~~~~~~~~~~~~~~

Typical usage is to pass the contents of the page, as a string, to the
:class:`HttpResponse` constructor::
Typical usage is to pass the contents of the page, as a string or bytestring,
to the :class:`HttpResponse` constructor::

>>> from django.http import HttpResponse
>>> response = HttpResponse("Here's the text of the Web page.")
>>> response = HttpResponse("Text only, please.", content_type="text/plain")
>>> response = HttpResponse(b'Bytestrings are also accepted.')

But if you want to add content incrementally, you can use ``response`` as a
file-like object::
Expand Down Expand Up @@ -735,16 +736,15 @@ Attributes
Methods
-------

.. method:: HttpResponse.__init__(content='', content_type=None, status=200, reason=None, charset=None)
.. method:: HttpResponse.__init__(content=b'', content_type=None, status=200, reason=None, charset=None)

Instantiates an ``HttpResponse`` object with the given page content and
content type.

``content`` should be an iterator or a string. If it's an
iterator, it should return strings, and those strings will be
joined together to form the content of the response. If it is not
an iterator or a string, it will be converted to a string when
accessed.
``content`` is most commonly an iterator, bytestring, or string. Other
types will be converted to a bytestring by encoding their string
representation. Iterators should return strings or bytestrings and those
will be joined together to form the content of the response.

``content_type`` is the MIME type optionally completed by a character set
encoding and is used to fill the HTTP ``Content-Type`` header. If not
Expand Down
4 changes: 2 additions & 2 deletions docs/releases/1.5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ Python's copy of version 2.0.9. However, there are some incompatibilities
between other versions of ``simplejson``:

- While the ``simplejson`` API is documented as always returning unicode
strings, the optional C implementation can return a byte string. This was
strings, the optional C implementation can return a bytestring. This was
fixed in Python 2.7.
- ``simplejson.JSONEncoder`` gained a ``namedtuple_as_object`` keyword
argument in version 2.2.
Expand All @@ -525,7 +525,7 @@ String types of hasher method parameters
If you have written a :ref:`custom password hasher <auth_password_storage>`,
your ``encode()``, ``verify()`` or ``safe_summary()`` methods should accept
Unicode parameters (``password``, ``salt`` or ``encoded``). If any of the
hashing methods need byte strings, you can use the
hashing methods need bytestrings, you can use the
:func:`~django.utils.encoding.force_bytes` utility to encode the strings.

Validation of previous_page_number and next_page_number
Expand Down
2 changes: 1 addition & 1 deletion docs/releases/1.7.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ Bugfixes
causing ``IntegrityError`` (:ticket:`23611`).

* Made :func:`~django.utils.http.urlsafe_base64_decode` return the proper
type (byte string) on Python 3 (:ticket:`23333`).
type (bytestring) on Python 3 (:ticket:`23333`).

* :djadmin:`makemigrations` can now serialize timezone-aware values
(:ticket:`23365`).
Expand Down
2 changes: 1 addition & 1 deletion docs/releases/1.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ Miscellaneous
(or 200M, before 1.7.2) to 500M.

* ``reverse()`` and ``reverse_lazy()`` now return Unicode strings instead of
byte strings.
bytestrings.

* The ``CacheClass`` shim has been removed from all cache backends.
These aliases were provided for backwards compatibility with Django 1.3.
Expand Down