@@ -21,13 +21,19 @@ safely sent by email, used as parts of URLs, or included as part of an HTTP
2121POST request. The encoding algorithm is not the same as the
2222:program: `uuencode ` program.
2323
24- There are two :rfc: `3548 ` interfaces provided by this module. The modern
25- interface supports encoding and decoding ASCII byte string objects using all
26- three :rfc: `3548 ` defined alphabets (normal, URL-safe, and filesystem-safe).
27- Additionally, the decoding functions of the modern interface also accept
28- Unicode strings containing only ASCII characters. The legacy interface provides
29- for encoding and decoding to and from file-like objects as well as byte
30- strings, but only using the Base64 standard alphabet.
24+ There are two interfaces provided by this module. The modern interface
25+ supports encoding :term: `bytes-like objects <bytes-like object> ` to ASCII
26+ :class: `bytes `, and decoding :term: `bytes-like objects <bytes-like object> ` or
27+ strings containing ASCII to :class: `bytes `. All three :rfc: `3548 ` defined
28+ alphabets (normal, URL-safe, and filesystem-safe) are supported.
29+
30+ The legacy interface does not support decoding from strings, but it does
31+ provide functions for encoding and decoding to and from :term: `file objects
32+ <file object> `. It only supports the Base64 standard alphabet, and it adds
33+ newlines every 76 characters as per :rfc: `2045 `. Note that if you are looking
34+ for :rfc: `2045 ` support you probably want to be looking at the :mod: `email `
35+ package instead.
36+
3137
3238.. versionchanged :: 3.3
3339 ASCII-only Unicode strings are now accepted by the decoding functions of
@@ -41,26 +47,26 @@ The modern interface provides:
4147
4248.. function :: b64encode(s, altchars=None)
4349
44- Encode a byte string using Base64.
50+ Encode the :term: `bytes-like object ` *s * using Base64 and return the encoded
51+ :class: `bytes `.
4552
46- * s * is the string to encode. Optional *altchars * must be a string of at least
53+ Optional *altchars * must be a :term: ` bytes-like object ` of at least
4754 length 2 (additional characters are ignored) which specifies an alternative
4855 alphabet for the ``+ `` and ``/ `` characters. This allows an application to e.g.
4956 generate URL or filesystem safe Base64 strings. The default is ``None ``, for
5057 which the standard Base64 alphabet is used.
5158
52- The encoded byte string is returned.
53-
5459
5560.. function :: b64decode(s, altchars=None, validate=False)
5661
57- Decode a Base64 encoded byte string.
62+ Decode the Base64 encoded :term: `bytes-like object ` or ASCII string
63+ *s * and return the decoded :class: `bytes `.
5864
59- * s * is the byte string to decode. Optional *altchars * must be a string of
65+ Optional *altchars * must be a :term: ` bytes-like object ` or ASCII string of
6066 at least length 2 (additional characters are ignored) which specifies the
6167 alternative alphabet used instead of the ``+ `` and ``/ `` characters.
6268
63- The decoded string is returned. A :exc: `binascii.Error ` exception is raised
69+ A :exc: `binascii.Error ` exception is raised
6470 if *s * is incorrectly padded.
6571
6672 If *validate * is ``False `` (the default), non-base64-alphabet characters are
@@ -71,38 +77,44 @@ The modern interface provides:
7177
7278.. function :: standard_b64encode(s)
7379
74- Encode byte string *s * using the standard Base64 alphabet.
80+ Encode :term: `bytes-like object ` *s * using the standard Base64 alphabet
81+ and return the encoded :class: `bytes `.
7582
7683
7784.. function :: standard_b64decode(s)
7885
79- Decode byte string *s * using the standard Base64 alphabet.
86+ Decode :term: `bytes-like object ` or ASCII string *s * using the standard
87+ Base64 alphabet and return the decoded :class: `bytes `.
8088
8189
8290.. function :: urlsafe_b64encode(s)
8391
84- Encode byte string *s * using a URL-safe alphabet, which substitutes ``- `` instead of
85- ``+ `` and ``_ `` instead of ``/ `` in the standard Base64 alphabet. The result
92+ Encode :term: `bytes-like object ` *s * using a URL-safe alphabet, which
93+ substitutes ``- `` instead of ``+ `` and ``_ `` instead of ``/ `` in the
94+ standard Base64 alphabet, and return the encoded :class: `bytes `. The result
8695 can still contain ``= ``.
8796
8897
8998.. function :: urlsafe_b64decode(s)
9099
91- Decode byte string *s * using a URL-safe alphabet, which substitutes ``- `` instead of
92- ``+ `` and ``_ `` instead of ``/ `` in the standard Base64 alphabet.
100+ Decode :term: `bytes-like object ` or ASCII string *s * using a URL-safe
101+ alphabet, which substitutes ``- `` instead of ``+ `` and ``_ `` instead of
102+ ``/ `` in the standard Base64 alphabet, and return the decoded
103+ :class: `bytes `.
93104
94105
95106.. function :: b32encode(s)
96107
97- Encode a byte string using Base32. *s * is the string to encode. The encoded string
98- is returned .
108+ Encode the :term: ` bytes-like object ` *s * using Base32 and return the
109+ encoded :class: ` bytes ` .
99110
100111
101112.. function :: b32decode(s, casefold=False, map01=None)
102113
103- Decode a Base32 encoded byte string.
114+ Decode the Base32 encoded :term: `bytes-like object ` or ASCII string *s * and
115+ return the decoded :class: `bytes `.
104116
105- * s * is the byte string to decode. Optional *casefold * is a flag specifying
117+ Optional *casefold * is a flag specifying
106118 whether a lowercase alphabet is acceptable as input. For security purposes,
107119 the default is ``False ``.
108120
@@ -113,46 +125,45 @@ The modern interface provides:
113125 digit 0 is always mapped to the letter O). For security purposes the default is
114126 ``None ``, so that 0 and 1 are not allowed in the input.
115127
116- The decoded byte string is returned. A :exc: `binascii.Error ` is raised if *s * is
128+ A :exc: `binascii.Error ` is raised if *s * is
117129 incorrectly padded or if there are non-alphabet characters present in the
118- string .
130+ input .
119131
120132
121133.. function :: b16encode(s)
122134
123- Encode a byte string using Base16.
124-
125- *s * is the string to encode. The encoded byte string is returned.
135+ Encode the :term: `bytes-like object ` *s * using Base16 and return the
136+ encoded :class: `bytes `.
126137
127138
128139.. function :: b16decode(s, casefold=False)
129140
130- Decode a Base16 encoded byte string.
141+ Decode the Base16 encoded :term: `bytes-like object ` or ASCII string *s * and
142+ return the decoded :class: `bytes `.
131143
132- * s * is the string to decode. Optional *casefold * is a flag specifying whether a
144+ Optional *casefold * is a flag specifying whether a
133145 lowercase alphabet is acceptable as input. For security purposes, the default
134146 is ``False ``.
135147
136- The decoded byte string is returned. A :exc: `TypeError ` is raised if *s * were
148+ A :exc: `TypeError ` is raised if *s * is
137149 incorrectly padded or if there are non-alphabet characters present in the
138- string .
150+ input .
139151
140152
141153.. function :: a85encode(s, *, foldspaces=False, wrapcol=0, pad=False, adobe=False)
142154
143- Encode a byte string using Ascii85.
144-
145- *s * is the string to encode. The encoded byte string is returned.
155+ Encode the :term: `bytes-like object ` *s * using Ascii85 and return the
156+ encoded :class: `bytes `.
146157
147158 *foldspaces * is an optional flag that uses the special short sequence 'y'
148159 instead of 4 consecutive spaces (ASCII 0x20) as supported by 'btoa'. This
149160 feature is not supported by the "standard" Ascii85 encoding.
150161
151- *wrapcol * controls whether the output should have newline (``'\n' ``)
162+ *wrapcol * controls whether the output should have newline (``b '\n' ``)
152163 characters added to it. If this is non-zero, each output line will be
153164 at most this many characters long.
154165
155- *pad * controls whether the input string is padded to a multiple of 4
166+ *pad * controls whether the input is padded to a multiple of 4
156167 before encoding. Note that the ``btoa `` implementation always pads.
157168
158169 *adobe * controls whether the encoded byte sequence is framed with ``<~ ``
@@ -163,9 +174,8 @@ The modern interface provides:
163174
164175.. function :: a85decode(s, *, foldspaces=False, adobe=False, ignorechars=b' \\t\\n\\r\\v')
165176
166- Decode an Ascii85 encoded byte string.
167-
168- *s * is the byte string to decode.
177+ Decode the Ascii85 encoded :term: `bytes-like object ` or ASCII string *s * and
178+ return the decoded :class: `bytes `.
169179
170180 *foldspaces * is a flag that specifies whether the 'y' short sequence
171181 should be accepted as shorthand for 4 consecutive spaces (ASCII 0x20).
@@ -174,7 +184,8 @@ The modern interface provides:
174184 *adobe * controls whether the input sequence is in Adobe Ascii85 format
175185 (i.e. is framed with <~ and ~>).
176186
177- *ignorechars * should be a byte string containing characters to ignore
187+ *ignorechars * should be a :term: `bytes-like object ` or ASCII string
188+ containing characters to ignore
178189 from the input. This should only contain whitespace characters, and by
179190 default contains all whitespace characters in ASCII.
180191
@@ -183,18 +194,19 @@ The modern interface provides:
183194
184195.. function :: b85encode(s, pad=False)
185196
186- Encode a byte string using base85, as used in e.g. git-style binary
187- diffs.
197+ Encode the :term: ` bytes-like object ` * s * using base85 ( as used in e.g.
198+ git-style binary diffs) and return the encoded :class: ` bytes ` .
188199
189- If *pad * is true, the input is padded with " \\ 0" so its length is a
190- multiple of 4 characters before encoding.
200+ If *pad * is true, the input is padded with `` b'\0' `` so its length is a
201+ multiple of 4 bytes before encoding.
191202
192203 .. versionadded :: 3.4
193204
194205
195206.. function :: b85decode(b)
196207
197- Decode base85-encoded byte string. Padding is implicitly removed, if
208+ Decode the base85-encoded :term: `bytes-like object ` or ASCII string *b * and
209+ return the decoded :class: `bytes `. Padding is implicitly removed, if
198210 necessary.
199211
200212 .. versionadded :: 3.4
@@ -214,15 +226,15 @@ The legacy interface:
214226
215227 Decode the contents of the binary *input * file and write the resulting binary
216228 data to the *output * file. *input * and *output * must be :term: `file objects
217- <file object> `. *input * will be read until ``input.read () `` returns an empty
218- bytes object.
229+ <file object> `. *input * will be read until ``input.readline () `` returns an
230+ empty bytes object.
219231
220232
221233.. function :: decodebytes(s)
222234 decodestring(s)
223235
224- Decode the byte string *s *, which must contain one or more lines of base64
225- encoded data, and return a byte string containing the resulting binary data .
236+ Decode the :term: ` bytes-like object ` *s *, which must contain one or more
237+ lines of base64 encoded data, and return the decoded :class: ` bytes ` .
226238 ``decodestring `` is a deprecated alias.
227239
228240 .. versionadded :: 3.1
@@ -233,17 +245,19 @@ The legacy interface:
233245 Encode the contents of the binary *input * file and write the resulting base64
234246 encoded data to the *output * file. *input * and *output * must be :term: `file
235247 objects <file object> `. *input * will be read until ``input.read() `` returns
236- an empty bytes object. :func: `encode ` returns the encoded data plus a trailing
237- newline character (``b'\n' ``).
248+ an empty bytes object. :func: `encode ` inserts a newline character (``b'\n' ``)
249+ after every 76 bytes of the output, as well as ensuring that the output
250+ always ends with a newline, as per :rfc: `2045 ` (MIME).
238251
239252
240253.. function :: encodebytes(s)
241254 encodestring(s)
242255
243- Encode the byte string *s *, which can contain arbitrary binary data, and
244- return a byte string containing one or more lines of base64-encoded data.
245- :func: `encodebytes ` returns a string containing one or more lines of
246- base64-encoded data always including an extra trailing newline (``b'\n' ``).
256+ Encode the :term: `bytes-like object ` *s *, which can contain arbitrary binary
257+ data, and return :class: `bytes ` containing the base64-encoded data, with newlines
258+ (``b'\n' ``) inserted after every 76 bytes of output, and ensuring that
259+ there is a trailing newline, as per :rfc: `2045 ` (MIME).
260+
247261 ``encodestring `` is a deprecated alias.
248262
249263
0 commit comments