Skip to content

Commit d68d539

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 70d6c85 commit d68d539

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
@@ -1758,15 +1758,16 @@ Return a tuple containing unpacked values.
17581758
Values are unpacked according to the format string Struct.format.
17591759
17601760
The buffer's size in bytes, starting at position offset, must be
1761-
at least Struct.size.
1761+
at least Struct.size. A negative offset counts from the end of the
1762+
buffer.
17621763
17631764
See help(struct) for more on format strings.
17641765
[clinic start generated code]*/
17651766

17661767
static PyObject *
17671768
Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
17681769
Py_ssize_t offset)
1769-
/*[clinic end generated code: output=57fac875e0977316 input=cafd4851d473c894]*/
1770+
/*[clinic end generated code: output=57fac875e0977316 input=3451f778ed8a5345]*/
17701771
{
17711772
_structmodulestate *state = get_struct_state_structinst(self);
17721773
ENSURE_STRUCT_IS_READY(self);
@@ -2085,8 +2086,9 @@ PyDoc_STRVAR(s_pack_into__doc__,
20852086
\n\
20862087
Pack the values v1, v2, ... according to the format string S.format\n\
20872088
and write the packed bytes into the writable buffer buf starting at\n\
2088-
offset. Note that the offset is a required argument. See\n\
2089-
help(struct) for more on format strings.");
2089+
offset. Note that the offset is a required argument. A negative\n\
2090+
offset counts from the end of the buffer. See help(struct) for more\n\
2091+
on format strings.");
20902092

20912093
static PyObject *
20922094
s_pack_into(PyObject *self, PyObject *const *args, Py_ssize_t nargs)
@@ -2378,8 +2380,8 @@ PyDoc_STRVAR(pack_into_doc,
23782380
\n\
23792381
Pack the values v1, v2, ... according to the format string and write\n\
23802382
the packed bytes into the writable buffer buf starting at offset. Note\n\
2381-
that the offset is a required argument. See help(struct) for more\n\
2382-
on format strings.");
2383+
that the offset is a required argument. A negative offset counts from\n\
2384+
the end of the buffer. See help(struct) for more on format strings.");
23832385

23842386
static PyObject *
23852387
pack_into(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
@@ -2433,14 +2435,15 @@ unpack_from
24332435
Return a tuple containing values unpacked according to the format string.
24342436
24352437
The buffer's size, minus offset, must be at least calcsize(format).
2438+
A negative offset counts from the end of the buffer.
24362439
24372440
See help(struct) for more on format strings.
24382441
[clinic start generated code]*/
24392442

24402443
static PyObject *
24412444
unpack_from_impl(PyObject *module, PyStructObject *s_object,
24422445
Py_buffer *buffer, Py_ssize_t offset)
2443-
/*[clinic end generated code: output=1042631674c6e0d3 input=6e80a5398e985025]*/
2446+
/*[clinic end generated code: output=1042631674c6e0d3 input=e383ecb97728be2b]*/
24442447
{
24452448
return Struct_unpack_from_impl(s_object, buffer, offset);
24462449
}

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)