Skip to content

Commit afa4728

Browse files
authored
Merge branch 'main' into main
2 parents 6250ebf + 1706d14 commit afa4728

6 files changed

Lines changed: 39 additions & 25 deletions

File tree

Doc/library/struct.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The module defines the following exception and functions:
6868
Pack the values *v1*, *v2*, ... according to the format string *format* and
6969
write the packed bytes into the writable buffer *buffer* starting at
7070
position *offset*. Note that *offset* is a required argument.
71+
A negative *offset* counts from the end of *buffer*.
7172

7273

7374
.. function:: unpack(format, buffer)
@@ -84,6 +85,7 @@ The module defines the following exception and functions:
8485
string *format*. The result is a tuple even if it contains exactly one
8586
item. The buffer's size in bytes, starting at position *offset*, must be at
8687
least the size required by the format, as reflected by :func:`calcsize`.
88+
A negative *offset* counts from the end of *buffer*.
8789

8890

8991
.. function:: iter_unpack(format, buffer)

Doc/whatsnew/3.16.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,9 @@ imaplib
263263
(Contributed by Przemysław Buczkowski and Serhiy Storchaka in :gh:`89869`.)
264264

265265
* Non-ASCII mailbox names are now automatically encoded as modified UTF-7
266-
(:rfc:`3501`, section 5.1.3), so international mailbox names can be passed
267-
as ordinary :class:`str`.
266+
(:rfc:`3501`, section 5.1.3) in the default mode, so international mailbox
267+
names can be passed as ordinary :class:`str` without enabling ``UTF8=ACCEPT``
268+
(under which they are sent as UTF-8).
268269
(Contributed by Serhiy Storchaka in :gh:`49555`.)
269270

270271
* The :meth:`~imaplib.IMAP4.copy`, :meth:`~imaplib.IMAP4.move`,

Lib/imaplib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ def _encode_mailbox(self, arg, safe):
16231623
# UTF-7 token -- one that needs no quoting, or an explicitly quoted
16241624
# string -- is passed through unchanged, so that a name obtained as raw
16251625
# bytes from LIST (and decoded as ASCII) round-trips without being
1626-
# encoded again. Pass bytes to bypass encoding entirely.
1626+
# encoded again. self._encoding is only ever 'ascii' or 'utf-8'.
16271627
if self._encoding != 'ascii':
16281628
return bytes(arg, self._encoding)
16291629
try:
@@ -1639,11 +1639,15 @@ def _encode_mailbox(self, arg, safe):
16391639
return arg.encode('utf-7-imap')
16401640

16411641
def _mailbox(self, arg):
1642+
# A str name is encoded for the wire; pass bytes to send the name
1643+
# verbatim, bypassing encoding entirely.
16421644
if isinstance(arg, str):
16431645
arg = self._encode_mailbox(arg, _non_astring_char)
16441646
return self._astring(arg)
16451647

16461648
def _list_mailbox(self, arg):
1649+
# As _mailbox(), but for a LIST/LSUB pattern; pass bytes to bypass
1650+
# encoding.
16471651
if isinstance(arg, str):
16481652
arg = self._encode_mailbox(arg, _non_list_char)
16491653
if _quoted.fullmatch(arg):
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
:mod:`imaplib` now encodes non-ASCII mailbox names as modified UTF-7
2-
(:rfc:`3501`, section 5.1.3), so international mailbox names can be passed as
3-
ordinary :class:`str`. A ``str`` that is already valid modified UTF-7, or a
4-
:class:`bytes` object, is sent unchanged.
2+
(:rfc:`3501`, section 5.1.3) in the default mode, so international mailbox
3+
names can be passed as ordinary :class:`str`; under ``UTF8=ACCEPT`` they are
4+
sent as UTF-8 instead. A name that is not valid modified UTF-7, such as one
5+
with a bare ``&``, is now encoded correctly instead of being sent as is.
6+
A ``str`` that is already valid modified UTF-7, or a :class:`bytes` object,
7+
is sent unchanged.

Modules/_struct.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,14 +2156,14 @@ Return a tuple containing unpacked values.
21562156
21572157
Values are unpacked according to the struct format string. The
21582158
buffer's size in bytes, starting at position offset, must be at
2159-
least the struct size. See help(struct) for more on format
2160-
strings.
2159+
least the struct size. A negative offset counts from the end of
2160+
the buffer. See help(struct) for more on format strings.
21612161
[clinic start generated code]*/
21622162

21632163
static PyObject *
21642164
Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
21652165
Py_ssize_t offset)
2166-
/*[clinic end generated code: output=57fac875e0977316 input=57cfcf84c088faa4]*/
2166+
/*[clinic end generated code: output=57fac875e0977316 input=49b7c90dd8faeb97]*/
21672167
{
21682168
_structmodulestate *state = get_struct_state_structinst(self);
21692169
ENSURE_STRUCT_IS_READY(self);
@@ -2497,15 +2497,16 @@ Pack values and write the packed bytes into the buffer.
24972497
24982498
Pack the provided values according to the struct format string
24992499
and write the packed bytes into the writable buffer starting at
2500-
offset. Note that the offset is a required argument. See
2501-
help(struct) for more on format strings.
2500+
offset. Note that the offset is a required argument. A negative
2501+
offset counts from the end of the buffer. See help(struct) for
2502+
more on format strings.
25022503
[clinic start generated code]*/
25032504

25042505
static PyObject *
25052506
Struct_pack_into_impl(PyStructObject *self, Py_buffer *buffer,
25062507
Py_ssize_t offset, PyObject * const *values,
25072508
Py_ssize_t values_length)
2508-
/*[clinic end generated code: output=aa9d9a93f5f8f77b input=9d842a368ee14245]*/
2509+
/*[clinic end generated code: output=aa9d9a93f5f8f77b input=a2b8749e3843f01b]*/
25092510
{
25102511
_structmodulestate *state = get_struct_state_structinst(self);
25112512

@@ -2735,15 +2736,15 @@ Pack values and write the packed bytes into the buffer.
27352736
27362737
Pack the provided values according to the format string and write the
27372738
packed bytes into the writable buffer starting at offset. Note that the
2738-
offset is a required argument. See help(struct) for more on format
2739-
strings.
2739+
offset is a required argument. A negative offset counts from the end of
2740+
the buffer. See help(struct) for more on format strings.
27402741
[clinic start generated code]*/
27412742

27422743
static PyObject *
27432744
pack_into_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer,
27442745
Py_ssize_t offset, PyObject * const *values,
27452746
Py_ssize_t values_length)
2746-
/*[clinic end generated code: output=e8bf7d422b2088ef input=086867c0f5d8a8e4]*/
2747+
/*[clinic end generated code: output=e8bf7d422b2088ef input=548c35c57db7436a]*/
27472748
{
27482749
return Struct_pack_into_impl(s_object, buffer, offset,
27492750
values, values_length);
@@ -2781,14 +2782,15 @@ unpack_from
27812782
27822783
Return a tuple containing values unpacked according to the format string.
27832784
2784-
The buffer's size, minus offset, must be at least calcsize(format). See
2785+
The buffer must contain at least calcsize(format) bytes starting at
2786+
offset. A negative offset counts from the end of the buffer. See
27852787
help(struct) for more on format strings.
27862788
[clinic start generated code]*/
27872789

27882790
static PyObject *
27892791
unpack_from_impl(PyObject *module, PyStructObject *s_object,
27902792
Py_buffer *buffer, Py_ssize_t offset)
2791-
/*[clinic end generated code: output=1042631674c6e0d3 input=3e46619756fb0293]*/
2793+
/*[clinic end generated code: output=1042631674c6e0d3 input=fb755400a7a47a51]*/
27922794
{
27932795
return Struct_unpack_from_impl(s_object, buffer, offset);
27942796
}

Modules/clinic/_struct.c.h

Lines changed: 10 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)