pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/commit/c0f59ad1455a53b6a8a9f09620ae4283e4df9f26

aa60c69660fa.css" /> Rename struct.unpack() 2nd parameter to "buffer" · python/cpython@c0f59ad · GitHub
Skip to content

Commit c0f59ad

Browse files
committed
Rename struct.unpack() 2nd parameter to "buffer"
Issue #29300: Rename struct.unpack() second parameter from "inputstr" to "buffer", and use the Py_buffer type. Fix also unit tests on struct.unpack() which passed a Unicode string instead of a bytes string as struct.unpack() second parameter. The purpose of test_trailing_counter() is to test invalid format strings, not to test the buffer parameter.
1 parent a0e454b commit c0f59ad

3 files changed

Lines changed: 19 additions & 15 deletions

File tree

Lib/test/test_struct.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,13 +530,13 @@ def test_trailing_counter(self):
530530

531531
# format lists containing only count spec should result in an error
532532
self.assertRaises(struct.error, struct.pack, '12345')
533-
self.assertRaises(struct.error, struct.unpack, '12345', '')
533+
self.assertRaises(struct.error, struct.unpack, '12345', b'')
534534
self.assertRaises(struct.error, struct.pack_into, '12345', store, 0)
535535
self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0)
536536

537537
# Format lists with trailing count spec should result in an error
538538
self.assertRaises(struct.error, struct.pack, 'c12345', 'x')
539-
self.assertRaises(struct.error, struct.unpack, 'c12345', 'x')
539+
self.assertRaises(struct.error, struct.unpack, 'c12345', b'x')
540540
self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0,
541541
'x')
542542
self.assertRaises(struct.error, struct.unpack_from, 'c12345', store,
@@ -545,7 +545,7 @@ def test_trailing_counter(self):
545545
# Mixed format tests
546546
self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs')
547547
self.assertRaises(struct.error, struct.unpack, '14s42',
548-
'spam and eggs')
548+
b'spam and eggs')
549549
self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0,
550550
'spam and eggs')
551551
self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0)

Modules/_struct.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,7 +2162,7 @@ pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
21622162
unpack
21632163
21642164
format: object
2165-
inputstr: object
2165+
buffer: Py_buffer
21662166
/
21672167
21682168
Return a tuple containing values unpacked according to the format string.
@@ -2173,16 +2173,16 @@ See help(struct) for more on format strings.
21732173
[clinic start generated code]*/
21742174

21752175
static PyObject *
2176-
unpack_impl(PyObject *module, PyObject *format, PyObject *inputstr)
2177-
/*[clinic end generated code: output=06951d66eae6d63b input=4b81d54988890f5e]*/
2176+
unpack_impl(PyObject *module, PyObject *format, Py_buffer *buffer)
2177+
/*[clinic end generated code: output=f75ada02aaa33b3b input=654078e6660c2df0]*/
21782178
{
21792179
PyStructObject *s_object;
21802180
PyObject *result;
21812181

21822182
s_object = cache_struct(format);
21832183
if (s_object == NULL)
21842184
return NULL;
2185-
result = Struct_unpack(s_object, inputstr);
2185+
result = Struct_unpack_impl(s_object, buffer);
21862186
Py_DECREF(s_object);
21872187
return result;
21882188
}

Modules/clinic/_struct.c.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ PyDoc_STRVAR(calcsize__doc__,
156156
{"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__},
157157

158158
PyDoc_STRVAR(unpack__doc__,
159-
"unpack($module, format, inputstr, /)\n"
159+
"unpack($module, format, buffer, /)\n"
160160
"--\n"
161161
"\n"
162162
"Return a tuple containing values unpacked according to the format string.\n"
@@ -169,27 +169,31 @@ PyDoc_STRVAR(unpack__doc__,
169169
{"unpack", (PyCFunction)unpack, METH_FASTCALL, unpack__doc__},
170170

171171
static PyObject *
172-
unpack_impl(PyObject *module, PyObject *format, PyObject *inputstr);
172+
unpack_impl(PyObject *module, PyObject *format, Py_buffer *buffer);
173173

174174
static PyObject *
175175
unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
176176
{
177177
PyObject *return_value = NULL;
178178
PyObject *format;
179-
PyObject *inputstr;
179+
Py_buffer buffer = {NULL, NULL};
180180

181-
if (!_PyArg_UnpackStack(args, nargs, "unpack",
182-
2, 2,
183-
&format, &inputstr)) {
181+
if (!_PyArg_ParseStack(args, nargs, "Oy*:unpack",
182+
&format, &buffer)) {
184183
goto exit;
185184
}
186185

187186
if (!_PyArg_NoStackKeywords("unpack", kwnames)) {
188187
goto exit;
189188
}
190-
return_value = unpack_impl(module, format, inputstr);
189+
return_value = unpack_impl(module, format, &buffer);
191190

192191
exit:
192+
/* Cleanup for buffer */
193+
if (buffer.obj) {
194+
PyBuffer_Release(&buffer);
195+
}
196+
193197
return return_value;
194198
}
195199

@@ -273,4 +277,4 @@ iter_unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnam
273277
exit:
274278
return return_value;
275279
}
276-
/*[clinic end generated code: output=db8152ad222fa3d0 input=a9049054013a1b77]*/
280+
/*[clinic end generated code: output=0714090a5d0ea8ce input=a9049054013a1b77]*/

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy