mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	Issue #24731: Fixed crash on converting objects with special methods
__bytes__, __trunc__, and __float__ returning instances of subclasses of bytes, int, and float to subclasses of bytes, int, and float correspondingly.
This commit is contained in:
		
						commit
						f9afda57ad
					
				
					 8 changed files with 50 additions and 10 deletions
				
			
		|  | @ -779,6 +779,14 @@ def __bytes__(self): | |||
|             def __index__(self): | ||||
|                 return 42 | ||||
|         self.assertEqual(bytes(A()), b'a') | ||||
|         # Issue #24731 | ||||
|         class A: | ||||
|             def __bytes__(self): | ||||
|                 return OtherBytesSubclass(b'abc') | ||||
|         self.assertEqual(bytes(A()), b'abc') | ||||
|         self.assertIs(type(bytes(A())), OtherBytesSubclass) | ||||
|         self.assertEqual(BytesSubclass(A()), b'abc') | ||||
|         self.assertIs(type(BytesSubclass(A())), BytesSubclass) | ||||
| 
 | ||||
|     # Test PyBytes_FromFormat() | ||||
|     def test_from_format(self): | ||||
|  | @ -1552,6 +1560,9 @@ class ByteArraySubclass(bytearray): | |||
| class BytesSubclass(bytes): | ||||
|     pass | ||||
| 
 | ||||
| class OtherBytesSubclass(bytes): | ||||
|     pass | ||||
| 
 | ||||
| class ByteArraySubclassTest(SubclassTest, unittest.TestCase): | ||||
|     type2test = bytearray | ||||
|     subclass2test = ByteArraySubclass | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Serhiy Storchaka
						Serhiy Storchaka