mirror of
				https://github.com/python/cpython.git
				synced 2025-10-29 20:51:26 +00:00 
			
		
		
		
	Issue #15424: Add a __sizeof__ implementation for array objects.
Patch by Ludwig Hähne.
This commit is contained in:
		
							parent
							
								
									7dbee38564
								
							
						
					
					
						commit
						03b4d5072a
					
				
					 4 changed files with 32 additions and 0 deletions
				
			
		|  | @ -988,6 +988,19 @@ def test_create_from_bytes(self): | |||
|         a = array.array('H', b"1234") | ||||
|         self.assertEqual(len(a) * a.itemsize, 4) | ||||
| 
 | ||||
|     @support.cpython_only | ||||
|     def test_sizeof_with_buffer(self): | ||||
|         a = array.array(self.typecode, self.example) | ||||
|         basesize = support.calcvobjsize('4Pi') | ||||
|         buffer_size = a.buffer_info()[1] * a.itemsize | ||||
|         support.check_sizeof(self, a, basesize + buffer_size) | ||||
| 
 | ||||
|     @support.cpython_only | ||||
|     def test_sizeof_without_buffer(self): | ||||
|         a = array.array(self.typecode) | ||||
|         basesize = support.calcvobjsize('4Pi') | ||||
|         support.check_sizeof(self, a, basesize) | ||||
| 
 | ||||
| 
 | ||||
| class StringTest(BaseTest): | ||||
| 
 | ||||
|  |  | |||
|  | @ -434,6 +434,7 @@ Jim Hugunin | |||
| Greg Humphreys | ||||
| Eric Huss | ||||
| Jeremy Hylton | ||||
| Ludwig Hähne | ||||
| Gerhard Häring | ||||
| Fredrik Håård | ||||
| Mihai Ibanescu | ||||
|  |  | |||
|  | @ -101,6 +101,9 @@ Core and Builtins | |||
| Library | ||||
| ------- | ||||
| 
 | ||||
| - Issue #15424: Add a __sizeof__ implementation for array objects. | ||||
|   Patch by Ludwig Hähne. | ||||
| 
 | ||||
| - Issue #13052: Fix IDLE crashing when replace string in Search/Replace dialog | ||||
|   ended with '\'. Patch by Roger Serwy. | ||||
| 
 | ||||
|  |  | |||
|  | @ -1510,6 +1510,19 @@ array.tobytes().decode() to obtain a unicode string from\n\ | |||
| an array of some other type."); | ||||
| 
 | ||||
| 
 | ||||
| static PyObject * | ||||
| array_sizeof(arrayobject *self, PyObject *unused) | ||||
| { | ||||
|     Py_ssize_t res; | ||||
|     res = sizeof(arrayobject) + self->allocated * self->ob_descr->itemsize; | ||||
|     return PyLong_FromSsize_t(res); | ||||
| } | ||||
| 
 | ||||
| PyDoc_STRVAR(sizeof_doc, | ||||
| "__sizeof__() -> int\n\
 | ||||
| \n\ | ||||
| Size of the array in memory, in bytes."); | ||||
| 
 | ||||
| 
 | ||||
| /*********************** Pickling support ************************/ | ||||
| 
 | ||||
|  | @ -2077,6 +2090,8 @@ static PyMethodDef array_methods[] = { | |||
|      tobytes_doc}, | ||||
|     {"tounicode",   (PyCFunction)array_tounicode,       METH_NOARGS, | ||||
|      tounicode_doc}, | ||||
|     {"__sizeof__",      (PyCFunction)array_sizeof,      METH_NOARGS, | ||||
|      sizeof_doc}, | ||||
|     {NULL,              NULL}           /* sentinel */ | ||||
| }; | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Meador Inge
						Meador Inge