MCPcopy
hub / github.com/scrapy/scrapy / to_bytes

Function to_bytes

scrapy/utils/python.py:88–101  ·  view source on GitHub ↗

Return the binary representation of ``text``. If ``text`` is already a bytes object, return it as-is.

(
    text: str | bytes, encoding: str | None = None, errors: str = "strict"
)

Source from the content-addressed store, hash-verified

86
87
88def to_bytes(
89 text: str | bytes, encoding: str | None = None, errors: str = "strict"
90) -> bytes:
91 """Return the binary representation of ``text``. If ``text``
92 is already a bytes object, return it as-is."""
93 if isinstance(text, bytes):
94 return text
95 if not isinstance(text, str):
96 raise TypeError(
97 f"to_bytes must receive a str or bytes object, got {type(text).__name__}"
98 )
99 if encoding is None:
100 encoding = "utf-8"
101 return text.encode(encoding, errors)
102
103
104def _chunk_iter(text: str, chunk_size: int) -> Iterable[tuple[str, int]]:

Callers 15

export_itemMethod · 0.90
export_itemMethod · 0.90
export_itemMethod · 0.90
from_bodyMethod · 0.90
_to_bytes_or_noneFunction · 0.90
open_in_browserFunction · 0.90
fingerprintFunction · 0.90
request_httpreprFunction · 0.90
_set_bodyMethod · 0.90
_urlencodeFunction · 0.90
_tunnel_request_dataFunction · 0.90
_get_agentMethod · 0.90

Calls

no outgoing calls