|  | 
| 5 | 5 | import unittest | 
| 6 | 6 | from ctypes import (CDLL, CFUNCTYPE, Structure, | 
| 7 | 7 |                     POINTER, pointer, _Pointer, | 
| 8 |  | -                    byref, sizeof, | 
|  | 8 | +                    addressof, byref, sizeof, | 
| 9 | 9 |                     c_void_p, c_char_p, | 
| 10 | 10 |                     c_byte, c_ubyte, c_short, c_ushort, c_int, c_uint, | 
| 11 | 11 |                     c_long, c_ulong, c_longlong, c_ulonglong, | 
| @@ -472,6 +472,105 @@ class C(Structure): | 
| 472 | 472 |         ptr.set_type(c_int) | 
| 473 | 473 |         self.assertIs(ptr._type_, c_int) | 
| 474 | 474 | 
 | 
|  | 475 | +    def test_pointer_lifecycle_basic(self): | 
|  | 476 | +        i = c_long(1010) | 
|  | 477 | +        p = pointer(i) | 
|  | 478 | +        self.assertEqual(p[0], 1010) | 
|  | 479 | +        self.assertIsNone(p._b_base_) | 
|  | 480 | +        self.assertEqual(addressof(i), addressof(p.contents)) | 
|  | 481 | + | 
|  | 482 | +    def test_pointer_lifecycle_set_contents(self): | 
|  | 483 | +        i = c_long(2020) | 
|  | 484 | +        p = pointer(c_long(1010)) | 
|  | 485 | +        p.contents = i | 
|  | 486 | +        self.assertEqual(p[0], 2020) | 
|  | 487 | +        self.assertIsNone(p._b_base_) | 
|  | 488 | +        self.assertEqual(addressof(i), addressof(p.contents)) | 
|  | 489 | + | 
|  | 490 | +    def test_pointer_lifecycle_set_pointer_contents(self): | 
|  | 491 | +        i = c_long(3030) | 
|  | 492 | +        p = pointer(c_long(1010)) | 
|  | 493 | +        pointer(p).contents.contents = i | 
|  | 494 | +        self.assertEqual(p.contents.value, 3030) | 
|  | 495 | +        self.assertEqual(addressof(i), addressof(p.contents)) | 
|  | 496 | + | 
|  | 497 | +    def test_pointer_lifecycle_array_set_contents(self): | 
|  | 498 | +        arr_type = POINTER(c_long) * 3 | 
|  | 499 | +        arr_obj = arr_type() | 
|  | 500 | +        i = c_long(300300) | 
|  | 501 | +        arr_obj[0] = pointer(c_long(100100)) | 
|  | 502 | +        arr_obj[1] = pointer(c_long(200200)) | 
|  | 503 | +        arr_obj[2] = pointer(i) | 
|  | 504 | +        self.assertEqual(arr_obj[0].contents.value, 100100) | 
|  | 505 | +        self.assertEqual(arr_obj[1].contents.value, 200200) | 
|  | 506 | +        self.assertEqual(arr_obj[2].contents.value, 300300) | 
|  | 507 | +        self.assertEqual(addressof(i), addressof(arr_obj[2].contents)) | 
|  | 508 | + | 
|  | 509 | +    def test_pointer_lifecycle_array_set_pointer_contents(self): | 
|  | 510 | +        arr_type = POINTER(c_long) * 3 | 
|  | 511 | +        arr_obj = arr_type() | 
|  | 512 | +        i = c_long(200003) | 
|  | 513 | +        arr_obj[0].contents = c_long(100001) | 
|  | 514 | +        arr_obj[1].contents = c_long(200002) | 
|  | 515 | +        arr_obj[2].contents = i | 
|  | 516 | +        self.assertEqual(arr_obj[0].contents.value, 100001) | 
|  | 517 | +        self.assertEqual(arr_obj[1].contents.value, 200002) | 
|  | 518 | +        self.assertEqual(arr_obj[2].contents.value, 200003) | 
|  | 519 | +        self.assertEqual(addressof(i), addressof(arr_obj[2].contents)) | 
|  | 520 | + | 
|  | 521 | +    def test_pointer_lifecycle_array_set_pointer_contents_pointer(self): | 
|  | 522 | +        arr_type = POINTER(c_long) * 3 | 
|  | 523 | +        arr_obj = arr_type() | 
|  | 524 | +        i = c_long(200003) | 
|  | 525 | +        pointer(arr_obj[0]).contents.contents = c_long(100001) | 
|  | 526 | +        pointer(arr_obj[1]).contents.contents = c_long(200002) | 
|  | 527 | +        pointer(arr_obj[2]).contents.contents = i | 
|  | 528 | +        self.assertEqual(arr_obj[0].contents.value, 100001) | 
|  | 529 | +        self.assertEqual(arr_obj[1].contents.value, 200002) | 
|  | 530 | +        self.assertEqual(arr_obj[2].contents.value, 200003) | 
|  | 531 | +        self.assertEqual(addressof(i), addressof(arr_obj[2].contents)) | 
|  | 532 | + | 
|  | 533 | +    def test_pointer_lifecycle_struct_set_contents(self): | 
|  | 534 | +        class S(Structure): | 
|  | 535 | +            _fields_ = (("s", POINTER(c_long)),) | 
|  | 536 | +        s = S(s=pointer(c_long(1111111))) | 
|  | 537 | +        s.s.contents = c_long(2222222) | 
|  | 538 | +        self.assertEqual(s.s.contents.value, 2222222) | 
|  | 539 | + | 
|  | 540 | +    def test_pointer_lifecycle_struct_set_contents_pointer(self): | 
|  | 541 | +        class S(Structure): | 
|  | 542 | +            _fields_ = (("s", POINTER(c_long)),) | 
|  | 543 | +        s = S(s=pointer(c_long(1111111))) | 
|  | 544 | +        pointer(s.s).contents.contents = c_long(2222222) | 
|  | 545 | +        self.assertEqual(s.s.contents.value, 2222222) | 
|  | 546 | + | 
|  | 547 | +    def test_pointer_lifecycle_struct_set_pointer_contents(self): | 
|  | 548 | +        class S(Structure): | 
|  | 549 | +            _fields_ = (("s", POINTER(c_long)),) | 
|  | 550 | +        s = S(s=pointer(c_long(1111111))) | 
|  | 551 | +        s.s = pointer(c_long(3333333)) | 
|  | 552 | +        self.assertEqual(s.s.contents.value, 3333333) | 
|  | 553 | + | 
|  | 554 | +    def test_pointer_lifecycle_struct_with_extra_field(self): | 
|  | 555 | +        class U(Structure): | 
|  | 556 | +            _fields_ = ( | 
|  | 557 | +                ("s", POINTER(c_long)), | 
|  | 558 | +                ("u", c_long), | 
|  | 559 | +            ) | 
|  | 560 | +        u = U(s=pointer(c_long(1010101))) | 
|  | 561 | +        u.s.contents = c_long(202020202) | 
|  | 562 | +        self.assertEqual(u.s.contents.value, 202020202) | 
|  | 563 | + | 
|  | 564 | +    def test_pointer_lifecycle_struct_with_extra_field_pointer(self): | 
|  | 565 | +        class U(Structure): | 
|  | 566 | +            _fields_ = ( | 
|  | 567 | +                ("s", POINTER(c_uint)), | 
|  | 568 | +                ("u", c_uint), | 
|  | 569 | +            ) | 
|  | 570 | +        u = U(s=pointer(c_uint(1010101))) | 
|  | 571 | +        pointer(u.s).contents.contents = c_uint(202020202) | 
|  | 572 | +        self.assertEqual(u.s.contents.value, 202020202) | 
|  | 573 | + | 
| 475 | 574 | 
 | 
| 476 | 575 | if __name__ == '__main__': | 
| 477 | 576 |     unittest.main() | 
0 commit comments