Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7c5e24f

Browse files
Issue #19591: Use specific asserts in ctype tests.
1 parent 328cf3c commit 7c5e24f

16 files changed

Lines changed: 84 additions & 85 deletions

Lib/ctypes/test/test_arrays.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def test_numeric_arrays(self):
8484
self.assertEqual(values, [1, 2, 3, 4, 5])
8585

8686
def test_classcache(self):
87-
self.assertTrue(not ARRAY(c_int, 3) is ARRAY(c_int, 4))
88-
self.assertTrue(ARRAY(c_int, 3) is ARRAY(c_int, 3))
87+
self.assertIsNot(ARRAY(c_int, 3), ARRAY(c_int, 4))
88+
self.assertIs(ARRAY(c_int, 3), ARRAY(c_int, 3))
8989

9090
def test_from_address(self):
9191
# Failed with 0.9.8, reported by JUrner
@@ -125,7 +125,7 @@ class my_int(c_int):
125125
# Create a new array type based on it:
126126
t1 = my_int * 1
127127
t2 = my_int * 1
128-
self.assertTrue(t1 is t2)
128+
self.assertIs(t1, t2)
129129

130130
def test_subclass(self):
131131
class T(Array):

Lib/ctypes/test/test_as_parameter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def test_longlong_callbacks(self):
134134
f.argtypes = [c_longlong, MyCallback]
135135

136136
def callback(value):
137-
self.assertTrue(isinstance(value, int))
137+
self.assertIsInstance(value, int)
138138
return value & 0x7FFFFFFF
139139

140140
cb = MyCallback(callback)

Lib/ctypes/test/test_buffers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ def test_buffer(self):
77
b = create_string_buffer(32)
88
self.assertEqual(len(b), 32)
99
self.assertEqual(sizeof(b), 32 * sizeof(c_char))
10-
self.assertTrue(type(b[0]) is bytes)
10+
self.assertIs(type(b[0]), bytes)
1111

1212
b = create_string_buffer(b"abc")
1313
self.assertEqual(len(b), 4) # trailing nul char
1414
self.assertEqual(sizeof(b), 4 * sizeof(c_char))
15-
self.assertTrue(type(b[0]) is bytes)
15+
self.assertIs(type(b[0]), bytes)
1616
self.assertEqual(b[0], b"a")
1717
self.assertEqual(b[:], b"abc\0")
1818
self.assertEqual(b[::], b"abc\0")
@@ -33,12 +33,12 @@ def test_unicode_buffer(self):
3333
b = create_unicode_buffer(32)
3434
self.assertEqual(len(b), 32)
3535
self.assertEqual(sizeof(b), 32 * sizeof(c_wchar))
36-
self.assertTrue(type(b[0]) is str)
36+
self.assertIs(type(b[0]), str)
3737

3838
b = create_unicode_buffer("abc")
3939
self.assertEqual(len(b), 4) # trailing nul char
4040
self.assertEqual(sizeof(b), 4 * sizeof(c_wchar))
41-
self.assertTrue(type(b[0]) is str)
41+
self.assertIs(type(b[0]), str)
4242
self.assertEqual(b[0], "a")
4343
self.assertEqual(b[:], "abc\0")
4444
self.assertEqual(b[::], "abc\0")
@@ -50,7 +50,7 @@ def test_unicode_conversion(self):
5050
b = create_unicode_buffer("abc")
5151
self.assertEqual(len(b), 4) # trailing nul char
5252
self.assertEqual(sizeof(b), 4 * sizeof(c_wchar))
53-
self.assertTrue(type(b[0]) is str)
53+
self.assertIs(type(b[0]), str)
5454
self.assertEqual(b[0], "a")
5555
self.assertEqual(b[:], "abc\0")
5656
self.assertEqual(b[::], "abc\0")

Lib/ctypes/test/test_byteswap.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ def X_test(self):
2323

2424
def test_endian_short(self):
2525
if sys.byteorder == "little":
26-
self.assertTrue(c_short.__ctype_le__ is c_short)
27-
self.assertTrue(c_short.__ctype_be__.__ctype_le__ is c_short)
26+
self.assertIs(c_short.__ctype_le__, c_short)
27+
self.assertIs(c_short.__ctype_be__.__ctype_le__, c_short)
2828
else:
29-
self.assertTrue(c_short.__ctype_be__ is c_short)
30-
self.assertTrue(c_short.__ctype_le__.__ctype_be__ is c_short)
29+
self.assertIs(c_short.__ctype_be__, c_short)
30+
self.assertIs(c_short.__ctype_le__.__ctype_be__, c_short)
3131
s = c_short.__ctype_be__(0x1234)
3232
self.assertEqual(bin(struct.pack(">h", 0x1234)), "1234")
3333
self.assertEqual(bin(s), "1234")
@@ -50,11 +50,11 @@ def test_endian_short(self):
5050

5151
def test_endian_int(self):
5252
if sys.byteorder == "little":
53-
self.assertTrue(c_int.__ctype_le__ is c_int)
54-
self.assertTrue(c_int.__ctype_be__.__ctype_le__ is c_int)
53+
self.assertIs(c_int.__ctype_le__, c_int)
54+
self.assertIs(c_int.__ctype_be__.__ctype_le__, c_int)
5555
else:
56-
self.assertTrue(c_int.__ctype_be__ is c_int)
57-
self.assertTrue(c_int.__ctype_le__.__ctype_be__ is c_int)
56+
self.assertIs(c_int.__ctype_be__, c_int)
57+
self.assertIs(c_int.__ctype_le__.__ctype_be__, c_int)
5858

5959
s = c_int.__ctype_be__(0x12345678)
6060
self.assertEqual(bin(struct.pack(">i", 0x12345678)), "12345678")
@@ -78,11 +78,11 @@ def test_endian_int(self):
7878

7979
def test_endian_longlong(self):
8080
if sys.byteorder == "little":
81-
self.assertTrue(c_longlong.__ctype_le__ is c_longlong)
82-
self.assertTrue(c_longlong.__ctype_be__.__ctype_le__ is c_longlong)
81+
self.assertIs(c_longlong.__ctype_le__, c_longlong)
82+
self.assertIs(c_longlong.__ctype_be__.__ctype_le__, c_longlong)
8383
else:
84-
self.assertTrue(c_longlong.__ctype_be__ is c_longlong)
85-
self.assertTrue(c_longlong.__ctype_le__.__ctype_be__ is c_longlong)
84+
self.assertIs(c_longlong.__ctype_be__, c_longlong)
85+
self.assertIs(c_longlong.__ctype_le__.__ctype_be__, c_longlong)
8686

8787
s = c_longlong.__ctype_be__(0x1234567890ABCDEF)
8888
self.assertEqual(bin(struct.pack(">q", 0x1234567890ABCDEF)), "1234567890ABCDEF")
@@ -106,11 +106,11 @@ def test_endian_longlong(self):
106106

107107
def test_endian_float(self):
108108
if sys.byteorder == "little":
109-
self.assertTrue(c_float.__ctype_le__ is c_float)
110-
self.assertTrue(c_float.__ctype_be__.__ctype_le__ is c_float)
109+
self.assertIs(c_float.__ctype_le__, c_float)
110+
self.assertIs(c_float.__ctype_be__.__ctype_le__, c_float)
111111
else:
112-
self.assertTrue(c_float.__ctype_be__ is c_float)
113-
self.assertTrue(c_float.__ctype_le__.__ctype_be__ is c_float)
112+
self.assertIs(c_float.__ctype_be__, c_float)
113+
self.assertIs(c_float.__ctype_le__.__ctype_be__, c_float)
114114
s = c_float(math.pi)
115115
self.assertEqual(bin(struct.pack("f", math.pi)), bin(s))
116116
# Hm, what's the precision of a float compared to a double?
@@ -124,11 +124,11 @@ def test_endian_float(self):
124124

125125
def test_endian_double(self):
126126
if sys.byteorder == "little":
127-
self.assertTrue(c_double.__ctype_le__ is c_double)
128-
self.assertTrue(c_double.__ctype_be__.__ctype_le__ is c_double)
127+
self.assertIs(c_double.__ctype_le__, c_double)
128+
self.assertIs(c_double.__ctype_be__.__ctype_le__, c_double)
129129
else:
130-
self.assertTrue(c_double.__ctype_be__ is c_double)
131-
self.assertTrue(c_double.__ctype_le__.__ctype_be__ is c_double)
130+
self.assertIs(c_double.__ctype_be__, c_double)
131+
self.assertIs(c_double.__ctype_le__.__ctype_be__, c_double)
132132
s = c_double(math.pi)
133133
self.assertEqual(s.value, math.pi)
134134
self.assertEqual(bin(struct.pack("d", math.pi)), bin(s))
@@ -140,14 +140,14 @@ def test_endian_double(self):
140140
self.assertEqual(bin(struct.pack(">d", math.pi)), bin(s))
141141

142142
def test_endian_other(self):
143-
self.assertTrue(c_byte.__ctype_le__ is c_byte)
144-
self.assertTrue(c_byte.__ctype_be__ is c_byte)
143+
self.assertIs(c_byte.__ctype_le__, c_byte)
144+
self.assertIs(c_byte.__ctype_be__, c_byte)
145145

146-
self.assertTrue(c_ubyte.__ctype_le__ is c_ubyte)
147-
self.assertTrue(c_ubyte.__ctype_be__ is c_ubyte)
146+
self.assertIs(c_ubyte.__ctype_le__, c_ubyte)
147+
self.assertIs(c_ubyte.__ctype_be__, c_ubyte)
148148

149-
self.assertTrue(c_char.__ctype_le__ is c_char)
150-
self.assertTrue(c_char.__ctype_be__ is c_char)
149+
self.assertIs(c_char.__ctype_le__, c_char)
150+
self.assertIs(c_char.__ctype_be__, c_char)
151151

152152
def test_struct_fields_1(self):
153153
if sys.byteorder == "little":

Lib/ctypes/test/test_cast.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def test_p2a_objects(self):
3838

3939
p = cast(array, POINTER(c_char_p))
4040
# array and p share a common _objects attribute
41-
self.assertTrue(p._objects is array._objects)
41+
self.assertIs(p._objects, array._objects)
4242
self.assertEqual(array._objects, {'0': b"foo bar", id(array): array})
4343
p[0] = b"spam spam"
4444
self.assertEqual(p._objects, {'0': b"spam spam", id(array): array})
45-
self.assertTrue(array._objects is p._objects)
45+
self.assertIs(array._objects, p._objects)
4646
p[1] = b"foo bar"
4747
self.assertEqual(p._objects, {'1': b'foo bar', '0': b"spam spam", id(array): array})
48-
self.assertTrue(array._objects is p._objects)
48+
self.assertIs(array._objects, p._objects)
4949

5050
def test_other(self):
5151
p = cast((c_int * 4)(1, 2, 3, 4), POINTER(c_int))

Lib/ctypes/test/test_frombuffer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_fom_buffer(self):
2323
a[0], a[-1] = 200, -200
2424
self.assertEqual(x[:], a.tolist())
2525

26-
self.assertTrue(a in x._objects.values())
26+
self.assertIn(a, x._objects.values())
2727

2828
self.assertRaises(ValueError,
2929
c_int.from_buffer, a, -1)

Lib/ctypes/test/test_funcptr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class WNDCLASS(Structure):
7575
## "lpfnWndProc", WNDPROC_2(wndproc))
7676
# instead:
7777

78-
self.assertTrue(WNDPROC is WNDPROC_2)
78+
self.assertIs(WNDPROC, WNDPROC_2)
7979
# 'wndclass.lpfnWndProc' leaks 94 references. Why?
8080
self.assertEqual(wndclass.lpfnWndProc(1, 2, 3, 4), 10)
8181

Lib/ctypes/test/test_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def test_longlong_callbacks(self):
306306
f.argtypes = [c_longlong, MyCallback]
307307

308308
def callback(value):
309-
self.assertTrue(isinstance(value, int))
309+
self.assertIsInstance(value, int)
310310
return value & 0x7FFFFFFF
311311

312312
cb = MyCallback(callback)

Lib/ctypes/test/test_loading.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_find(self):
4343

4444
if os.name in ("nt", "ce"):
4545
def test_load_library(self):
46-
self.assertFalse(libc_name is None)
46+
self.assertIsNotNone(libc_name)
4747
if is_resource_enabled("printing"):
4848
print(find_library("kernel32"))
4949
print(find_library("user32"))

Lib/ctypes/test/test_numbers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ def test_float_from_address(self):
181181
a = array(t._type_, [3.14])
182182
v = t.from_address(a.buffer_info()[0])
183183
self.assertEqual(v.value, a[0])
184-
self.assertTrue(type(v) is t)
184+
self.assertIs(type(v), t)
185185
a[0] = 2.3456e17
186186
self.assertEqual(v.value, a[0])
187-
self.assertTrue(type(v) is t)
187+
self.assertIs(type(v), t)
188188

189189
def test_char_from_address(self):
190190
from ctypes import c_char
@@ -194,7 +194,7 @@ def test_char_from_address(self):
194194
a[0] = ord('x')
195195
v = c_char.from_address(a.buffer_info()[0])
196196
self.assertEqual(v.value, b'x')
197-
self.assertTrue(type(v) is c_char)
197+
self.assertIs(type(v), c_char)
198198

199199
a[0] = ord('?')
200200
self.assertEqual(v.value, b'?')

0 commit comments

Comments
 (0)