Opened 19 months ago

Last modified 6 months ago

#27795 assigned Cleanup/optimization

Audit force_text invocations

Reported by: Claude Paroz Owned by: Claude Paroz
Component: Utilities Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

django.utils.encoding.force_text is called many times in Django code for several use cases:

  • force the evaluation of a lazy string
  • convert bytes or a bytestring (PY2) to str
  • convert an object to its string representation

A global audit is needed to clean that usage, also considering that Python 3 native strings are more wildly used.
I'm working on it currently.

Change History (21)

comment:1 Changed 19 months ago by Claude Paroz <claude@…>

In ccfd1295:

Refs #27795 -- Prevented SafeText from losing safe status on str()

This will allow to replace force_text() by str() in several places (as one of
the features of force_text is to keep the safe status).

comment:2 Changed 18 months ago by Claude Paroz <claude@…>

In 2757209c:

Refs #27795 -- Removed force_text from templatize function

comment:3 Changed 18 months ago by Claude Paroz <claude@…>

In 3a148f95:

Refs #27795 -- Removed force_text from the template layer

Thanks Tim Graham for the review.

comment:4 Changed 18 months ago by Claude Paroz <claude@…>

In 8346680e:

Refs #27795 -- Removed unneeded force_text calls

Thanks Tim Graham for the review.

comment:5 Changed 16 months ago by GitHub <noreply@…>

In e5dce7b0:

Refs #27795 -- Removed unneeded force_text calls from the Oracle backend.

comment:6 Changed 16 months ago by Claude Paroz <claude@…>

In 301de77:

Refs #27795 -- Replaced many force_text() with str()

Thanks Tim Graham for the review.

comment:7 Changed 16 months ago by Claude Paroz <claude@…>

In a8343fe7:

Refs #27795 -- Replaced force_text() with str() in feed generators

comment:8 Changed 16 months ago by Tim Graham <timograham@…>

In b39aabc:

Refs #27795 -- Reworked get_or_create test erroneously mixing bytes and str.

As CharField.to_python() now always calls str(), assigning bytes to a
CharField is no longer correct usage. Doing so results in a warning:

django/db/models/fields/init__.py:1061: BytesWarning: str() on a bytes instance

Use a unique constraint violation to trigger the database error instead.

Warning introduced in 301de774c21d055e9e5a7073e5bffdb52bc71079.

comment:9 Changed 15 months ago by Claude Paroz <claude@…>

In d842ada:

Refs #27795 -- Stopped converting integer format settings to str in JS/JSON i18n views

Thanks Tim Graham for the review.

comment:10 Changed 15 months ago by Claude Paroz <claude@…>

In f87256b1:

Refs #27795 -- Replaced force_text() with str() in contrib.messages

Thanks Tim Graham for the review.

comment:11 Changed 15 months ago by Claude Paroz <claude@…>

In 45bbb9aa:

Refs #27795 -- Removed force_text() usage in serialization framework

comment:12 Changed 7 months ago by Tim Graham <timograham@…>

In 6b2f8fb9:

Refs #27795 -- Replaced force_text() usage in django.core.management.

Use decode() since Popen.communicate() always returns bytes.

comment:13 Changed 6 months ago by Tim Graham <timograham@…>

In 16c5a334:

Refs #27795 -- Replaced force_text/bytes() with decode()/encode() in password hashers.

comment:14 Changed 6 months ago by Carlton Gibson <carlton.gibson@…>

In 6a039800:

Refs #27795 -- Replaced force_bytes() usage in django.core.cache.

comment:15 Changed 6 months ago by Tim Graham <timograham@…>

In b38532c:

Refs #27795 -- Replaced force_bytes() usage in django.core.signing.

comment:16 Changed 6 months ago by GitHub <noreply@…>

In fa75b2c:

Refs #27795 -- Removed force_bytes/text() usage in tests.

comment:17 Changed 6 months ago by Tim Graham <timograham@…>

In 7d96f0c4:

Refs #27795 -- Replaced force_bytes() usage in django.http.

comment:18 Changed 6 months ago by Jon Dufresne <jon.dufresne@…>

In 968329b:

Refs #27795 -- Replaced force_text() calls with str() in django.utils.html.

comment:19 Changed 6 months ago by Tim Graham <timograham@…>

In 1f3852b:

Refs #27795 -- Removed force_bytes() usage in django/template/loaders/cached.py.

comment:20 Changed 6 months ago by Tim Graham <timograham@…>

In 919d5981:

Refs #27795 -- Removed force_text() usage in django/test/testcases.py.

comment:21 Changed 6 months ago by Tim Graham <timograham@…>

In b8a41a28:

Refs #27795 -- Removed force_bytes() in django.test.client where possible.

Note: See TracTickets for help on using tickets.
Back to Top