Skip to content

Commit 29e733a

Browse files
miss-islingtonserhiy-storchakaclaude
authored
[3.15] gh-71086: Document negative offset support in struct.pack_into() and unpack_from() (GH-153650) (GH-153699)
(cherry picked from commit 1706d14) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 1268235 commit 29e733a

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
@@ -2127,14 +2127,14 @@ Return a tuple containing unpacked values.
21272127
21282128
Values are unpacked according to the struct format string. The
21292129
buffer's size in bytes, starting at position offset, must be at
2130-
least the struct size. See help(struct) for more on format
2131-
strings.
2130+
least the struct size. A negative offset counts from the end of
2131+
the buffer. See help(struct) for more on format strings.
21322132
[clinic start generated code]*/
21332133

21342134
static PyObject *
21352135
Struct_unpack_from_impl(PyStructObject *self, Py_buffer *buffer,
21362136
Py_ssize_t offset)
2137-
/*[clinic end generated code: output=57fac875e0977316 input=57cfcf84c088faa4]*/
2137+
/*[clinic end generated code: output=57fac875e0977316 input=49b7c90dd8faeb97]*/
21382138
{
21392139
_structmodulestate *state = get_struct_state_structinst(self);
21402140
ENSURE_STRUCT_IS_READY(self);
@@ -2468,15 +2468,16 @@ Pack values and write the packed bytes into the buffer.
24682468
24692469
Pack the provided values according to the struct format string
24702470
and write the packed bytes into the writable buffer starting at
2471-
offset. Note that the offset is a required argument. See
2472-
help(struct) for more on format strings.
2471+
offset. Note that the offset is a required argument. A negative
2472+
offset counts from the end of the buffer. See help(struct) for
2473+
more on format strings.
24732474
[clinic start generated code]*/
24742475

24752476
static PyObject *
24762477
Struct_pack_into_impl(PyStructObject *self, Py_buffer *buffer,
24772478
Py_ssize_t offset, PyObject * const *values,
24782479
Py_ssize_t values_length)
2479-
/*[clinic end generated code: output=aa9d9a93f5f8f77b input=9d842a368ee14245]*/
2480+
/*[clinic end generated code: output=aa9d9a93f5f8f77b input=a2b8749e3843f01b]*/
24802481
{
24812482
_structmodulestate *state = get_struct_state_structinst(self);
24822483

@@ -2706,15 +2707,15 @@ Pack values and write the packed bytes into the buffer.
27062707
27072708
Pack the provided values according to the format string and write the
27082709
packed bytes into the writable buffer starting at offset. Note that the
2709-
offset is a required argument. See help(struct) for more on format
2710-
strings.
2710+
offset is a required argument. A negative offset counts from the end of
2711+
the buffer. See help(struct) for more on format strings.
27112712
[clinic start generated code]*/
27122713

27132714
static PyObject *
27142715
pack_into_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer,
27152716
Py_ssize_t offset, PyObject * const *values,
27162717
Py_ssize_t values_length)
2717-
/*[clinic end generated code: output=e8bf7d422b2088ef input=086867c0f5d8a8e4]*/
2718+
/*[clinic end generated code: output=e8bf7d422b2088ef input=548c35c57db7436a]*/
27182719
{
27192720
return Struct_pack_into_impl(s_object, buffer, offset,
27202721
values, values_length);
@@ -2752,14 +2753,15 @@ unpack_from
27522753
27532754
Return a tuple containing values unpacked according to the format string.
27542755
2755-
The buffer's size, minus offset, must be at least calcsize(format). See
2756+
The buffer must contain at least calcsize(format) bytes starting at
2757+
offset. A negative offset counts from the end of the buffer. See
27562758
help(struct) for more on format strings.
27572759
[clinic start generated code]*/
27582760

27592761
static PyObject *
27602762
unpack_from_impl(PyObject *module, PyStructObject *s_object,
27612763
Py_buffer *buffer, Py_ssize_t offset)
2762-
/*[clinic end generated code: output=1042631674c6e0d3 input=3e46619756fb0293]*/
2764+
/*[clinic end generated code: output=1042631674c6e0d3 input=fb755400a7a47a51]*/
27632765
{
27642766
return Struct_unpack_from_impl(s_object, buffer, offset);
27652767
}

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)