Skip to content

Commit c30d567

Browse files
gh-71086: Document negative offset support in struct.pack_into() and unpack_from()
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent dd2faeb commit c30d567

3 files changed

Lines changed: 25 additions & 19 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: 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)