Skip to content

Commit 854e126

Browse files
gh-71086: Document negative offset support in struct.pack_into() and unpack_from() (GH-153650)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> (cherry picked from commit 1706d14)
1 parent 2ec8fcf commit 854e126

3 files changed

Lines changed: 16 additions & 9 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)

Modules/_struct.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,15 +1960,16 @@ Return a tuple containing unpacked values.
19601960
Values are unpacked according to the format string Struct.format.
19611961
19621962
The buffer's size in bytes, starting at position offset, must be
1963-
at least Struct.size.
1963+
at least Struct.size. A negative offset counts from the end of the
1964+
buffer.
19641965
19651966
See help(struct) for more on format strings.
19661967
[clinic start generated code]*/
19671968

19681969
static PyObject *
19691970
Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
19701971
Py_ssize_t offset)
1971-
/*[clinic end generated code: output=57fac875e0977316 input=cafd4851d473c894]*/
1972+
/*[clinic end generated code: output=57fac875e0977316 input=3451f778ed8a5345]*/
19721973
{
19731974
_structmodulestate *state = get_struct_state_structinst(self);
19741975
ENSURE_STRUCT_IS_READY(self);
@@ -2296,8 +2297,9 @@ PyDoc_STRVAR(s_pack_into__doc__,
22962297
\n\
22972298
Pack the values v1, v2, ... according to the format string S.format\n\
22982299
and write the packed bytes into the writable buffer buf starting at\n\
2299-
offset. Note that the offset is a required argument. See\n\
2300-
help(struct) for more on format strings.");
2300+
offset. Note that the offset is a required argument. A negative\n\
2301+
offset counts from the end of the buffer. See help(struct) for more\n\
2302+
on format strings.");
23012303

23022304
static PyObject *
23032305
s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
@@ -2593,8 +2595,8 @@ PyDoc_STRVAR(pack_into_doc,
25932595
\n\
25942596
Pack the values v1, v2, ... according to the format string and write\n\
25952597
the packed bytes into the writable buffer buf starting at offset. Note\n\
2596-
that the offset is a required argument. See help(struct) for more\n\
2597-
on format strings.");
2598+
that the offset is a required argument. A negative offset counts from\n\
2599+
the end of the buffer. See help(struct) for more on format strings.");
25982600

25992601
static PyObject *
26002602
pack_into(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
@@ -2648,14 +2650,15 @@ unpack_from
26482650
Return a tuple containing values unpacked according to the format string.
26492651
26502652
The buffer's size, minus offset, must be at least calcsize(format).
2653+
A negative offset counts from the end of the buffer.
26512654
26522655
See help(struct) for more on format strings.
26532656
[clinic start generated code]*/
26542657

26552658
static PyObject *
26562659
unpack_from_impl(PyObject *module, PyStructObject *s_object,
26572660
Py_buffer *buffer, Py_ssize_t offset)
2658-
/*[clinic end generated code: output=1042631674c6e0d3 input=6e80a5398e985025]*/
2661+
/*[clinic end generated code: output=1042631674c6e0d3 input=e383ecb97728be2b]*/
26592662
{
26602663
return Struct_unpack_from_impl(s_object, buffer, offset);
26612664
}

Modules/clinic/_struct.c.h

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

0 commit comments

Comments
 (0)