Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,9 @@ def _test_pack_into(self, pack_into):
pack_into(writable_buf, None, test_string)
with self.assertRaises(TypeError):
pack_into(writable_buf, 0.0, test_string)
with self.assertRaises((IndexError, OverflowError)):
with self.assertRaises(OverflowError):
pack_into(writable_buf, 2**1000, test_string)
with self.assertRaises((IndexError, OverflowError)):
with self.assertRaises(OverflowError):
pack_into(writable_buf, -2**1000, test_string)

def test_pack_into(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:func:`struct.pack_into` now raises OverflowError instead of IndexError for
too large *offset* argument.
21 changes: 7 additions & 14 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -2275,7 +2275,7 @@ Struct_pack_impl(PyStructObject *self, PyObject * const *values,
Struct.pack_into

buffer: Py_buffer(accept={rwbuffer})
offset as offset_obj: object
offset: Py_ssize_t
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This long-time behavior, I think we need a deprecation.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior is uncommon, it was not documented and not covered by tests, it should not occur in normal code. This looks like a long-time bug, we just do not backport its fix.

It is not easy to deprecate a raised exception, and deprecation will have effect comparable with simple changing the exception type. We usually do not do this.

/
*values: array

Expand All @@ -2289,11 +2289,10 @@ help(struct) for more on format strings.

static PyObject *
Struct_pack_into_impl(PyStructObject *self, Py_buffer *buffer,
PyObject *offset_obj, PyObject * const *values,
Py_ssize_t offset, PyObject * const *values,
Py_ssize_t values_length)
/*[clinic end generated code: output=b0c2ef496135dad3 input=d0de9b9f138c782d]*/
/*[clinic end generated code: output=aa9d9a93f5f8f77b input=9d842a368ee14245]*/
{
Py_ssize_t offset;
_structmodulestate *state = get_struct_state_structinst(self);

ENSURE_STRUCT_IS_READY(self);
Expand All @@ -2304,12 +2303,6 @@ Struct_pack_into_impl(PyStructObject *self, Py_buffer *buffer,
return NULL;
}

/* Extract the offset from the first argument */
offset = PyNumber_AsSsize_t(offset_obj, PyExc_IndexError);
if (offset == -1 && PyErr_Occurred()) {
return NULL;
}

/* Support negative offsets. */
if (offset < 0) {
/* Check that negative offset is low enough to fit data */
Expand Down Expand Up @@ -2546,7 +2539,7 @@ pack_into

format as s_object: cache_struct
buffer: Py_buffer(accept={rwbuffer})
offset as offset_obj: object
offset: Py_ssize_t
/
*values: array

Expand All @@ -2560,11 +2553,11 @@ strings.

static PyObject *
pack_into_impl(PyObject *module, PyStructObject *s_object, Py_buffer *buffer,
PyObject *offset_obj, PyObject * const *values,
Py_ssize_t offset, PyObject * const *values,
Py_ssize_t values_length)
/*[clinic end generated code: output=148ef659a490eec3 input=3c5fe5bd3b6fd396]*/
/*[clinic end generated code: output=e8bf7d422b2088ef input=086867c0f5d8a8e4]*/
{
return Struct_pack_into_impl(s_object, buffer, offset_obj,
return Struct_pack_into_impl(s_object, buffer, offset,
values, values_length);
}

Expand Down
40 changes: 31 additions & 9 deletions Modules/clinic/_struct.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading