Skip to content

Commit 85ff1a0

Browse files
Add few more tests
1 parent 5b9891f commit 85ff1a0

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Lib/test/test_ctypes/test_pointers.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def test_bug_1467852(self):
183183
q = pointer(y)
184184
pp[0] = q # <==
185185
self.assertEqual(p[0], 6)
186+
186187
def test_c_void_p(self):
187188
# http://sourceforge.net/tracker/?func=detail&aid=1518190&group_id=5470&atid=105470
188189
if sizeof(c_void_p) == 4:
@@ -201,6 +202,30 @@ def test_c_void_p(self):
201202
self.assertRaises(TypeError, c_void_p, 3.14) # make sure floats are NOT accepted
202203
self.assertRaises(TypeError, c_void_p, object()) # nor other objects
203204

205+
def test_read_null_pointer(self):
206+
null_ptr = POINTER(c_int)()
207+
with self.assertRaisesRegex(ValueError, "NULL pointer access"):
208+
null_ptr[0]
209+
210+
def test_write_null_pointer(self):
211+
null_ptr = POINTER(c_int)()
212+
with self.assertRaisesRegex(ValueError, "NULL pointer access"):
213+
null_ptr[0] = 1
214+
215+
def test_set_pointer_to_null_and_read(self):
216+
class Bar(Structure):
217+
_fields_ = [("values", POINTER(c_int))]
218+
219+
bar = Bar()
220+
bar.values = (c_int * 3)(1, 2, 3)
221+
222+
values = [bar.values[0], bar.values[1], bar.values[2]]
223+
self.assertEqual(values, [1, 2, 3])
224+
225+
bar.values = None
226+
with self.assertRaisesRegex(ValueError, "NULL pointer access"):
227+
bar.values[0]
228+
204229
def test_pointers_bool(self):
205230
# NULL pointers have a boolean False value, non-NULL pointers True.
206231
self.assertEqual(bool(POINTER(c_int)()), False)

0 commit comments

Comments
 (0)