Skip to content

Commit 901e6b3

Browse files
committed
Remove DEFAULT_CHARSET setting
UTF-8 has been the universal web standard for over a decade. Hardcode "utf-8" at each usage site instead of routing through a setting no one would ever change.
1 parent efe5d4d commit 901e6b3

File tree

8 files changed

+15
-21
lines changed

8 files changed

+15
-21
lines changed

plain-email/plain/email/backends/smtp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def _send(self, email_message: EmailMessage) -> bool:
152152
"""A helper method that does the actual sending."""
153153
if not email_message.recipients():
154154
return False
155-
encoding = email_message.encoding or settings.DEFAULT_CHARSET
155+
encoding = email_message.encoding or "utf-8"
156156
from_email = _sanitize_address(email_message.from_email, encoding)
157157
recipients = [
158158
_sanitize_address(addr, encoding) for addr in email_message.recipients()

plain-email/plain/email/message.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _forbid_multi_line_headers(
7070
name: str, val: str, encoding: str | None
7171
) -> tuple[str, str]:
7272
"""Forbid multi-line headers to prevent header injection."""
73-
encoding = encoding or settings.DEFAULT_CHARSET
73+
encoding = encoding or "utf-8"
7474
val = str(val) # val may be lazy
7575
if "\n" in val or "\r" in val:
7676
raise BadHeaderError(
@@ -281,7 +281,7 @@ def get_connection(self, fail_silently: bool = False) -> BaseEmailBackend:
281281
return self.connection
282282

283283
def message(self) -> SafeMIMEText | SafeMIMEMultipart:
284-
encoding = self.encoding or settings.DEFAULT_CHARSET
284+
encoding = self.encoding or "utf-8"
285285
msg = SafeMIMEText(self.body, self.content_subtype, encoding)
286286
msg = self._create_message(msg)
287287
msg["Subject"] = self.subject
@@ -391,7 +391,7 @@ def _create_attachments(
391391
self, msg: SafeMIMEText | SafeMIMEMultipart
392392
) -> SafeMIMEText | SafeMIMEMultipart:
393393
if self.attachments:
394-
encoding = self.encoding or settings.DEFAULT_CHARSET
394+
encoding = self.encoding or "utf-8"
395395
body_msg = msg
396396
msg = SafeMIMEMultipart(_subtype=self.mixed_subtype, encoding=encoding)
397397
if self.body or body_msg.is_multipart():
@@ -414,7 +414,7 @@ def _create_mime_attachment(
414414
"""
415415
basetype, subtype = mimetype.split("/", 1)
416416
if basetype == "text":
417-
encoding = self.encoding or settings.DEFAULT_CHARSET
417+
encoding = self.encoding or "utf-8"
418418
if not isinstance(content, str):
419419
content = force_str(content)
420420
attachment = SafeMIMEText(content, subtype, encoding)
@@ -528,7 +528,7 @@ def _create_message(self, msg: SafeMIMEText) -> SafeMIMEText | SafeMIMEMultipart
528528
def _create_alternatives(
529529
self, msg: SafeMIMEText | SafeMIMEMultipart
530530
) -> SafeMIMEText | SafeMIMEMultipart:
531-
encoding = self.encoding or settings.DEFAULT_CHARSET
531+
encoding = self.encoding or "utf-8"
532532
if self.alternatives:
533533
body_msg = msg
534534
msg = SafeMIMEMultipart(

plain/plain/http/multipartparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def __init__(self, request: Request):
109109
possible_sizes = [x.chunk_size for x in self._upload_handlers if x.chunk_size]
110110
self._chunk_size = min([2**31 - 4] + possible_sizes)
111111

112-
self._encoding = request.encoding or settings.DEFAULT_CHARSET
112+
self._encoding = request.encoding or "utf-8"
113113
self._content_length = content_length
114114

115115
def parse(self) -> tuple[Any, MultiValueDict]:

plain/plain/http/request.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ class QueryDict(MultiValueDict):
513513
will always return a mutable copy.
514514
515515
Both keys and values set on this class are converted from the given encoding
516-
(DEFAULT_CHARSET by default) to str.
516+
(UTF-8 by default) to str.
517517
"""
518518

519519
# These are both reset in __init__, but is specified here at the class
@@ -528,7 +528,7 @@ def __init__(
528528
encoding: str | None = None,
529529
):
530530
super().__init__()
531-
self.encoding = encoding or settings.DEFAULT_CHARSET
531+
self.encoding = encoding or "utf-8"
532532
query_string = query_string or ""
533533
parse_qsl_kwargs: dict[str, Any] = {
534534
"keep_blank_values": True,
@@ -579,7 +579,7 @@ def fromkeys( # type: ignore[override]
579579
@property
580580
def encoding(self) -> str:
581581
if self._encoding is None:
582-
self._encoding = settings.DEFAULT_CHARSET
582+
self._encoding = "utf-8"
583583
return self._encoding
584584

585585
@encoding.setter

plain/plain/http/response.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from plain import signals
1818
from plain.http.cookie import sign_cookie_value
1919
from plain.json import PlainJSONEncoder
20-
from plain.runtime import settings
2120
from plain.utils import timezone
2221
from plain.utils.datastructures import CaseInsensitiveMapping
2322
from plain.utils.encoding import iri_to_uri
@@ -178,7 +177,7 @@ def charset(self) -> str:
178177
# store it back into the _charset for later intentionally, to
179178
# allow for the Content-Type to be switched again later.
180179
return matched["charset"].replace('"', "")
181-
return settings.DEFAULT_CHARSET
180+
return "utf-8"
182181

183182
@charset.setter
184183
def charset(self, value: str) -> None:

plain/plain/runtime/global_settings.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@
9393
# user time zone.
9494
TIME_ZONE: str = "UTC"
9595

96-
# Default charset to use for all Response objects, if a MIME type isn't
97-
# manually specified. It's used to construct the Content-Type header.
98-
DEFAULT_CHARSET: str = "utf-8"
9996

10097
# MARK: URL Configuration
10198

plain/plain/test/client.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from plain.internal.handlers.base import BaseHandler
1212
from plain.internal.handlers.wsgi import WSGIRequest
1313
from plain.json import PlainJSONEncoder
14-
from plain.runtime import settings
1514
from plain.signals import request_started
1615
from plain.urls import get_resolver
1716
from plain.utils.encoding import force_bytes
@@ -293,7 +292,7 @@ def _encode_data(self, data: dict[str, Any] | str, content_type: str) -> bytes:
293292
if match:
294293
charset = match[1]
295294
else:
296-
charset = settings.DEFAULT_CHARSET
295+
charset = "utf-8"
297296
return force_bytes(data, encoding=charset)
298297

299298
def _encode_json(self, data: Any, content_type: str) -> Any:
@@ -472,7 +471,7 @@ def generic(
472471
) -> WSGIRequest:
473472
"""Construct an arbitrary HTTP request."""
474473
parsed = urlparse(str(path)) # path can be lazy
475-
data = force_bytes(data, settings.DEFAULT_CHARSET)
474+
data = force_bytes(data, "utf-8")
476475
r: dict[str, Any] = {
477476
"PATH_INFO": self._get_path(parsed),
478477
"REQUEST_METHOD": method,

plain/plain/test/encoding.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
from typing import Any
66

7-
from plain.runtime import settings
87
from plain.utils.encoding import force_bytes
98
from plain.utils.itercompat import is_iterable
109

@@ -20,7 +19,7 @@ def encode_multipart(boundary: str, data: dict[str, Any]) -> bytes:
2019
lines: list[bytes] = []
2120

2221
def to_bytes(s: str) -> bytes:
23-
return force_bytes(s, settings.DEFAULT_CHARSET)
22+
return force_bytes(s, "utf-8")
2423

2524
# Not by any means perfect, but good enough for our purposes.
2625
def is_file(thing: Any) -> bool:
@@ -73,7 +72,7 @@ def is_file(thing: Any) -> bool:
7372

7473
def encode_file(boundary: str, key: str, file: Any) -> list[bytes]:
7574
def to_bytes(s: str) -> bytes:
76-
return force_bytes(s, settings.DEFAULT_CHARSET)
75+
return force_bytes(s, "utf-8")
7776

7877
# file.name might not be a string. For example, it's an int for
7978
# tempfile.TemporaryFile().

0 commit comments

Comments
 (0)