Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 1 | from ctypes import * |
| 2 | import unittest |
| 3 | |
| 4 | class SimpleTestCase(unittest.TestCase): |
| 5 | def test_cint(self): |
| 6 | x = c_int() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 7 | self.assertEqual(x._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 8 | x.value = 42 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 9 | self.assertEqual(x._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 10 | x = c_int(99) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 11 | self.assertEqual(x._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 12 | |
| 13 | def test_ccharp(self): |
| 14 | x = c_char_p() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 15 | self.assertEqual(x._objects, None) |
Victor Stinner | 42746df | 2010-07-27 23:36:41 +0000 | [diff] [blame] | 16 | x.value = b"abc" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 17 | self.assertEqual(x._objects, b"abc") |
Victor Stinner | 42746df | 2010-07-27 23:36:41 +0000 | [diff] [blame] | 18 | x = c_char_p(b"spam") |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 19 | self.assertEqual(x._objects, b"spam") |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 20 | |
| 21 | class StructureTestCase(unittest.TestCase): |
| 22 | def test_cint_struct(self): |
| 23 | class X(Structure): |
| 24 | _fields_ = [("a", c_int), |
| 25 | ("b", c_int)] |
| 26 | |
| 27 | x = X() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 28 | self.assertEqual(x._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 29 | x.a = 42 |
| 30 | x.b = 99 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 31 | self.assertEqual(x._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 32 | |
| 33 | def test_ccharp_struct(self): |
| 34 | class X(Structure): |
| 35 | _fields_ = [("a", c_char_p), |
| 36 | ("b", c_char_p)] |
| 37 | x = X() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 38 | self.assertEqual(x._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 39 | |
Victor Stinner | 42746df | 2010-07-27 23:36:41 +0000 | [diff] [blame] | 40 | x.a = b"spam" |
| 41 | x.b = b"foo" |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 42 | self.assertEqual(x._objects, {"0": b"spam", "1": b"foo"}) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 43 | |
| 44 | def test_struct_struct(self): |
| 45 | class POINT(Structure): |
| 46 | _fields_ = [("x", c_int), ("y", c_int)] |
| 47 | class RECT(Structure): |
| 48 | _fields_ = [("ul", POINT), ("lr", POINT)] |
| 49 | |
| 50 | r = RECT() |
| 51 | r.ul.x = 0 |
| 52 | r.ul.y = 1 |
| 53 | r.lr.x = 2 |
| 54 | r.lr.y = 3 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 55 | self.assertEqual(r._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 56 | |
| 57 | r = RECT() |
| 58 | pt = POINT(1, 2) |
| 59 | r.ul = pt |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 60 | self.assertEqual(r._objects, {'0': {}}) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 61 | r.ul.x = 22 |
| 62 | r.ul.y = 44 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 63 | self.assertEqual(r._objects, {'0': {}}) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 64 | r.lr = POINT() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 65 | self.assertEqual(r._objects, {'0': {}, '1': {}}) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 66 | |
| 67 | class ArrayTestCase(unittest.TestCase): |
| 68 | def test_cint_array(self): |
| 69 | INTARR = c_int * 3 |
| 70 | |
| 71 | ia = INTARR() |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 72 | self.assertEqual(ia._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 73 | ia[0] = 1 |
| 74 | ia[1] = 2 |
| 75 | ia[2] = 3 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 76 | self.assertEqual(ia._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 77 | |
| 78 | class X(Structure): |
| 79 | _fields_ = [("x", c_int), |
| 80 | ("a", INTARR)] |
| 81 | |
| 82 | x = X() |
| 83 | x.x = 1000 |
| 84 | x.a[0] = 42 |
| 85 | x.a[1] = 96 |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 86 | self.assertEqual(x._objects, None) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 87 | x.a = ia |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame^] | 88 | self.assertEqual(x._objects, {'1': {}}) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 89 | |
| 90 | class PointerTestCase(unittest.TestCase): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 91 | def test_p_cint(self): |
| 92 | i = c_int(42) |
| 93 | x = pointer(i) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 94 | self.assertEqual(x._objects, {'1': i}) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 95 | |
| 96 | class DeletePointerTestCase(unittest.TestCase): |
| 97 | def X_test(self): |
| 98 | class X(Structure): |
| 99 | _fields_ = [("p", POINTER(c_char_p))] |
| 100 | x = X() |
| 101 | i = c_char_p("abc def") |
| 102 | from sys import getrefcount as grc |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 103 | print("2?", grc(i)) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 104 | x.p = pointer(i) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 105 | print("3?", grc(i)) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 106 | for i in range(320): |
| 107 | c_int(99) |
| 108 | x.p[0] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 109 | print(x.p[0]) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 110 | ## del x |
| 111 | ## print "2?", grc(i) |
| 112 | ## del i |
| 113 | import gc |
| 114 | gc.collect() |
| 115 | for i in range(320): |
| 116 | c_int(99) |
| 117 | x.p[0] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 118 | print(x.p[0]) |
| 119 | print(x.p.contents) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 120 | ## print x._objects |
| 121 | |
| 122 | x.p[0] = "spam spam" |
| 123 | ## print x.p[0] |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 124 | print("+" * 42) |
| 125 | print(x._objects) |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 126 | |
| 127 | class PointerToStructure(unittest.TestCase): |
| 128 | def test(self): |
| 129 | class POINT(Structure): |
| 130 | _fields_ = [("x", c_int), ("y", c_int)] |
| 131 | class RECT(Structure): |
| 132 | _fields_ = [("a", POINTER(POINT)), |
| 133 | ("b", POINTER(POINT))] |
| 134 | r = RECT() |
| 135 | p1 = POINT(1, 2) |
Tim Peters | e8d09e5 | 2006-03-09 01:15:05 +0000 | [diff] [blame] | 136 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 137 | r.a = pointer(p1) |
| 138 | r.b = pointer(p1) |
| 139 | ## from pprint import pprint as pp |
| 140 | ## pp(p1._objects) |
| 141 | ## pp(r._objects) |
| 142 | |
| 143 | r.a[0].x = 42 |
| 144 | r.a[0].y = 99 |
| 145 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 146 | # to avoid leaking when tests are run several times |
| 147 | # clean up the types left in the cache. |
| 148 | from ctypes import _pointer_type_cache |
| 149 | del _pointer_type_cache[POINT] |
| 150 | |
Thomas Heller | babddfc | 2006-03-08 19:56:54 +0000 | [diff] [blame] | 151 | if __name__ == "__main__": |
| 152 | unittest.main() |