mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	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.
This commit is contained in:
		
							parent
							
								
									a0e454b69d
								
							
						
					
					
						commit
						c0f59ad145
					
				
					 3 changed files with 19 additions and 15 deletions
				
			
		|  | @ -530,13 +530,13 @@ def test_trailing_counter(self): | ||||||
| 
 | 
 | ||||||
|         # format lists containing only count spec should result in an error |         # format lists containing only count spec should result in an error | ||||||
|         self.assertRaises(struct.error, struct.pack, '12345') |         self.assertRaises(struct.error, struct.pack, '12345') | ||||||
|         self.assertRaises(struct.error, struct.unpack, '12345', '') |         self.assertRaises(struct.error, struct.unpack, '12345', b'') | ||||||
|         self.assertRaises(struct.error, struct.pack_into, '12345', store, 0) |         self.assertRaises(struct.error, struct.pack_into, '12345', store, 0) | ||||||
|         self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0) |         self.assertRaises(struct.error, struct.unpack_from, '12345', store, 0) | ||||||
| 
 | 
 | ||||||
|         # Format lists with trailing count spec should result in an error |         # Format lists with trailing count spec should result in an error | ||||||
|         self.assertRaises(struct.error, struct.pack, 'c12345', 'x') |         self.assertRaises(struct.error, struct.pack, 'c12345', 'x') | ||||||
|         self.assertRaises(struct.error, struct.unpack, 'c12345', 'x') |         self.assertRaises(struct.error, struct.unpack, 'c12345', b'x') | ||||||
|         self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0, |         self.assertRaises(struct.error, struct.pack_into, 'c12345', store, 0, | ||||||
|                            'x') |                            'x') | ||||||
|         self.assertRaises(struct.error, struct.unpack_from, 'c12345', store, |         self.assertRaises(struct.error, struct.unpack_from, 'c12345', store, | ||||||
|  | @ -545,7 +545,7 @@ def test_trailing_counter(self): | ||||||
|         # Mixed format tests |         # Mixed format tests | ||||||
|         self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs') |         self.assertRaises(struct.error, struct.pack, '14s42', 'spam and eggs') | ||||||
|         self.assertRaises(struct.error, struct.unpack, '14s42', |         self.assertRaises(struct.error, struct.unpack, '14s42', | ||||||
|                           'spam and eggs') |                           b'spam and eggs') | ||||||
|         self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0, |         self.assertRaises(struct.error, struct.pack_into, '14s42', store, 0, | ||||||
|                           'spam and eggs') |                           'spam and eggs') | ||||||
|         self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0) |         self.assertRaises(struct.error, struct.unpack_from, '14s42', store, 0) | ||||||
|  |  | ||||||
|  | @ -2162,7 +2162,7 @@ pack_into(PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | ||||||
| unpack | unpack | ||||||
| 
 | 
 | ||||||
|     format: object |     format: object | ||||||
|     inputstr: object |     buffer: Py_buffer | ||||||
|     / |     / | ||||||
| 
 | 
 | ||||||
| Return a tuple containing values unpacked according to the format string. | Return a tuple containing values unpacked according to the format string. | ||||||
|  | @ -2173,8 +2173,8 @@ See help(struct) for more on format strings. | ||||||
| [clinic start generated code]*/ | [clinic start generated code]*/ | ||||||
| 
 | 
 | ||||||
| static PyObject * | static PyObject * | ||||||
| unpack_impl(PyObject *module, PyObject *format, PyObject *inputstr) | unpack_impl(PyObject *module, PyObject *format, Py_buffer *buffer) | ||||||
| /*[clinic end generated code: output=06951d66eae6d63b input=4b81d54988890f5e]*/ | /*[clinic end generated code: output=f75ada02aaa33b3b input=654078e6660c2df0]*/ | ||||||
| { | { | ||||||
|     PyStructObject *s_object; |     PyStructObject *s_object; | ||||||
|     PyObject *result; |     PyObject *result; | ||||||
|  | @ -2182,7 +2182,7 @@ unpack_impl(PyObject *module, PyObject *format, PyObject *inputstr) | ||||||
|     s_object = cache_struct(format); |     s_object = cache_struct(format); | ||||||
|     if (s_object == NULL) |     if (s_object == NULL) | ||||||
|         return NULL; |         return NULL; | ||||||
|     result = Struct_unpack(s_object, inputstr); |     result = Struct_unpack_impl(s_object, buffer); | ||||||
|     Py_DECREF(s_object); |     Py_DECREF(s_object); | ||||||
|     return result; |     return result; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -156,7 +156,7 @@ PyDoc_STRVAR(calcsize__doc__, | ||||||
|     {"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__}, |     {"calcsize", (PyCFunction)calcsize, METH_O, calcsize__doc__}, | ||||||
| 
 | 
 | ||||||
| PyDoc_STRVAR(unpack__doc__, | PyDoc_STRVAR(unpack__doc__, | ||||||
| "unpack($module, format, inputstr, /)\n" | "unpack($module, format, buffer, /)\n" | ||||||
| "--\n" | "--\n" | ||||||
| "\n" | "\n" | ||||||
| "Return a tuple containing values unpacked according to the format string.\n" | "Return a tuple containing values unpacked according to the format string.\n" | ||||||
|  | @ -169,27 +169,31 @@ PyDoc_STRVAR(unpack__doc__, | ||||||
|     {"unpack", (PyCFunction)unpack, METH_FASTCALL, unpack__doc__}, |     {"unpack", (PyCFunction)unpack, METH_FASTCALL, unpack__doc__}, | ||||||
| 
 | 
 | ||||||
| static PyObject * | static PyObject * | ||||||
| unpack_impl(PyObject *module, PyObject *format, PyObject *inputstr); | unpack_impl(PyObject *module, PyObject *format, Py_buffer *buffer); | ||||||
| 
 | 
 | ||||||
| static PyObject * | static PyObject * | ||||||
| unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) | ||||||
| { | { | ||||||
|     PyObject *return_value = NULL; |     PyObject *return_value = NULL; | ||||||
|     PyObject *format; |     PyObject *format; | ||||||
|     PyObject *inputstr; |     Py_buffer buffer = {NULL, NULL}; | ||||||
| 
 | 
 | ||||||
|     if (!_PyArg_UnpackStack(args, nargs, "unpack", |     if (!_PyArg_ParseStack(args, nargs, "Oy*:unpack", | ||||||
|         2, 2, |         &format, &buffer)) { | ||||||
|         &format, &inputstr)) { |  | ||||||
|         goto exit; |         goto exit; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (!_PyArg_NoStackKeywords("unpack", kwnames)) { |     if (!_PyArg_NoStackKeywords("unpack", kwnames)) { | ||||||
|         goto exit; |         goto exit; | ||||||
|     } |     } | ||||||
|     return_value = unpack_impl(module, format, inputstr); |     return_value = unpack_impl(module, format, &buffer); | ||||||
| 
 | 
 | ||||||
| exit: | exit: | ||||||
|  |     /* Cleanup for buffer */ | ||||||
|  |     if (buffer.obj) { | ||||||
|  |        PyBuffer_Release(&buffer); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     return return_value; |     return return_value; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | @ -273,4 +277,4 @@ iter_unpack(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnam | ||||||
| exit: | exit: | ||||||
|     return return_value; |     return return_value; | ||||||
| } | } | ||||||
| /*[clinic end generated code: output=db8152ad222fa3d0 input=a9049054013a1b77]*/ | /*[clinic end generated code: output=0714090a5d0ea8ce input=a9049054013a1b77]*/ | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Victor Stinner
						Victor Stinner