Skip to content

Commit 5ab13c5

Browse files
authored
gh-105751: test_ctypes avoids the operator module (GH-105797)
* Replace operator.delitem(obj, index) with "del obj[index]". * Replace operator.setitem(obj, index, value) with "obj[index] = value". * Replace delattr(obj, "attr) with "del obj.attr". * Replace grc() with sys.getrefcount() for readability.
1 parent e7507bd commit 5ab13c5

10 files changed

+85
-80
lines changed

Lib/test/test_ctypes/test_arrays.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ def test_simple(self):
4646
with self.assertRaises(IndexError): ia[-alen-1]
4747

4848
# change the items
49-
from operator import setitem
5049
new_values = list(range(42, 42+alen))
51-
[setitem(ia, n, new_values[n]) for n in range(alen)]
50+
for n in range(alen):
51+
ia[n] = new_values[n]
5252
values = [ia[i] for i in range(alen)]
5353
self.assertEqual(values, new_values)
5454

@@ -78,8 +78,8 @@ def test_simple(self):
7878
self.assertEqual(len(ca), 3)
7979

8080
# cannot delete items
81-
from operator import delitem
82-
self.assertRaises(TypeError, delitem, ca, 0)
81+
with self.assertRaises(TypeError):
82+
del ca[0]
8383

8484
def test_step_overflow(self):
8585
a = (c_int * 5)()

Lib/test/test_ctypes/test_callbacks.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import sys
23
import unittest
34
from test import support
45

@@ -106,15 +107,14 @@ def test_char_p(self):
106107

107108
def test_pyobject(self):
108109
o = ()
109-
from sys import getrefcount as grc
110110
for o in (), [], object():
111-
initial = grc(o)
111+
initial = sys.getrefcount(o)
112112
# This call leaks a reference to 'o'...
113113
self.check_type(py_object, o)
114-
before = grc(o)
114+
before = sys.getrefcount(o)
115115
# ...but this call doesn't leak any more. Where is the refcount?
116116
self.check_type(py_object, o)
117-
after = grc(o)
117+
after = sys.getrefcount(o)
118118
self.assertEqual((after, o), (before, o))
119119

120120
def test_unsupported_restype_1(self):

Lib/test/test_ctypes/test_delattr.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ class X(Structure):
88

99
class TestCase(unittest.TestCase):
1010
def test_simple(self):
11-
self.assertRaises(TypeError,
12-
delattr, c_int(42), "value")
11+
with self.assertRaises(TypeError):
12+
del c_int(42).value
1313

1414
def test_chararray(self):
15-
self.assertRaises(TypeError,
16-
delattr, (c_char * 5)(), "value")
15+
chararray = (c_char * 5)()
16+
with self.assertRaises(TypeError):
17+
del chararray.value
1718

1819
def test_struct(self):
19-
self.assertRaises(TypeError,
20-
delattr, X(), "foo")
20+
struct = X()
21+
with self.assertRaises(TypeError):
22+
del struct.foo
2123

2224

2325
if __name__ == "__main__":

Lib/test/test_ctypes/test_internals.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This tests the internal _objects attribute
2+
import sys
23
import unittest
34
from ctypes import Structure, POINTER, c_char_p, c_int
4-
from sys import getrefcount as grc
55

66
# XXX This test must be reviewed for correctness!!!
77

@@ -20,16 +20,16 @@ def assertSame(self, a, b):
2020

2121
def test_ints(self):
2222
i = 42000123
23-
refcnt = grc(i)
23+
refcnt = sys.getrefcount(i)
2424
ci = c_int(i)
25-
self.assertEqual(refcnt, grc(i))
25+
self.assertEqual(refcnt, sys.getrefcount(i))
2626
self.assertEqual(ci._objects, None)
2727

2828
def test_c_char_p(self):
2929
s = b"Hello, World"
30-
refcnt = grc(s)
30+
refcnt = sys.getrefcount(s)
3131
cs = c_char_p(s)
32-
self.assertEqual(refcnt + 1, grc(s))
32+
self.assertEqual(refcnt + 1, sys.getrefcount(s))
3333
self.assertSame(cs._objects, s)
3434

3535
def test_simple_struct(self):

Lib/test/test_ctypes/test_keeprefs.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
from ctypes import Structure, POINTER, pointer, c_char_p, c_int
1+
import sys
22
import unittest
3+
from ctypes import Structure, POINTER, pointer, c_char_p, c_int
4+
35

46
class SimpleTestCase(unittest.TestCase):
57
def test_cint(self):
@@ -100,16 +102,15 @@ class X(Structure):
100102
_fields_ = [("p", POINTER(c_char_p))]
101103
x = X()
102104
i = c_char_p("abc def")
103-
from sys import getrefcount as grc
104-
print("2?", grc(i))
105+
print("2?", sys.getrefcount(i))
105106
x.p = pointer(i)
106-
print("3?", grc(i))
107+
print("3?", sys.getrefcount(i))
107108
for i in range(320):
108109
c_int(99)
109110
x.p[0]
110111
print(x.p[0])
111112
## del x
112-
## print "2?", grc(i)
113+
## print "2?", sys.getrefcount(i)
113114
## del i
114115
import gc
115116
gc.collect()

Lib/test/test_ctypes/test_pointers.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def func(arg):
9797
## print self.result
9898

9999
def test_basics(self):
100-
from operator import delitem
101100
for ct, pt in zip(ctype_types, python_types):
102101
i = ct(42)
103102
p = pointer(i)
@@ -108,7 +107,8 @@ def test_basics(self):
108107
## self.assertEqual(p.contents, 42)
109108
## self.assertEqual(p[0], 42)
110109

111-
self.assertRaises(TypeError, delitem, p, 0)
110+
with self.assertRaises(TypeError):
111+
del p[0]
112112

113113
def test_from_address(self):
114114
from array import array

Lib/test/test_ctypes/test_python_api.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
import sys
23
from test import support
34
from ctypes import (pythonapi, POINTER, c_buffer, sizeof,
45
py_object, c_char_p, c_char, c_long, c_size_t)
@@ -11,7 +12,6 @@
1112

1213
################################################################
1314

14-
from sys import getrefcount as grc
1515

1616
class PythonAPITestCase(unittest.TestCase):
1717

@@ -29,41 +29,41 @@ def test_PyString_FromString(self):
2929
pythonapi.PyBytes_FromString.argtypes = (c_char_p,)
3030

3131
s = b"abc"
32-
refcnt = grc(s)
32+
refcnt = sys.getrefcount(s)
3333
pyob = pythonapi.PyBytes_FromString(s)
34-
self.assertEqual(grc(s), refcnt)
34+
self.assertEqual(sys.getrefcount(s), refcnt)
3535
self.assertEqual(s, pyob)
3636
del pyob
37-
self.assertEqual(grc(s), refcnt)
37+
self.assertEqual(sys.getrefcount(s), refcnt)
3838

3939
@support.refcount_test
4040
def test_PyLong_Long(self):
41-
ref42 = grc(42)
41+
ref42 = sys.getrefcount(42)
4242
pythonapi.PyLong_FromLong.restype = py_object
4343
self.assertEqual(pythonapi.PyLong_FromLong(42), 42)
4444

45-
self.assertEqual(grc(42), ref42)
45+
self.assertEqual(sys.getrefcount(42), ref42)
4646

4747
pythonapi.PyLong_AsLong.argtypes = (py_object,)
4848
pythonapi.PyLong_AsLong.restype = c_long
4949

5050
res = pythonapi.PyLong_AsLong(42)
5151
# Small int refcnts don't change
52-
self.assertEqual(grc(res), ref42)
52+
self.assertEqual(sys.getrefcount(res), ref42)
5353
del res
54-
self.assertEqual(grc(42), ref42)
54+
self.assertEqual(sys.getrefcount(42), ref42)
5555

5656
@support.refcount_test
5757
def test_PyObj_FromPtr(self):
5858
s = "abc def ghi jkl"
59-
ref = grc(s)
59+
ref = sys.getrefcount(s)
6060
# id(python-object) is the address
6161
pyobj = PyObj_FromPtr(id(s))
6262
self.assertIs(s, pyobj)
6363

64-
self.assertEqual(grc(s), ref + 1)
64+
self.assertEqual(sys.getrefcount(s), ref + 1)
6565
del pyobj
66-
self.assertEqual(grc(s), ref)
66+
self.assertEqual(sys.getrefcount(s), ref)
6767

6868
def test_PyOS_snprintf(self):
6969
PyOS_snprintf = pythonapi.PyOS_snprintf

Lib/test/test_ctypes/test_refcounts.py

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import unittest
2-
from test import support
31
import ctypes
42
import gc
3+
import sys
4+
import unittest
5+
from test import support
56

67
MyCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
78
OtherCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_ulonglong)
@@ -13,8 +14,6 @@ class RefcountTestCase(unittest.TestCase):
1314

1415
@support.refcount_test
1516
def test_1(self):
16-
from sys import getrefcount as grc
17-
1817
f = dll._testfunc_callback_i_if
1918
f.restype = ctypes.c_int
2019
f.argtypes = [ctypes.c_int, MyCallback]
@@ -23,66 +22,65 @@ def callback(value):
2322
#print "called back with", value
2423
return value
2524

26-
self.assertEqual(grc(callback), 2)
25+
self.assertEqual(sys.getrefcount(callback), 2)
2726
cb = MyCallback(callback)
2827

29-
self.assertGreater(grc(callback), 2)
28+
self.assertGreater(sys.getrefcount(callback), 2)
3029
result = f(-10, cb)
3130
self.assertEqual(result, -18)
3231
cb = None
3332

3433
gc.collect()
3534

36-
self.assertEqual(grc(callback), 2)
35+
self.assertEqual(sys.getrefcount(callback), 2)
3736

3837

3938
@support.refcount_test
4039
def test_refcount(self):
41-
from sys import getrefcount as grc
4240
def func(*args):
4341
pass
4442
# this is the standard refcount for func
45-
self.assertEqual(grc(func), 2)
43+
self.assertEqual(sys.getrefcount(func), 2)
4644

4745
# the CFuncPtr instance holds at least one refcount on func:
4846
f = OtherCallback(func)
49-
self.assertGreater(grc(func), 2)
47+
self.assertGreater(sys.getrefcount(func), 2)
5048

5149
# and may release it again
5250
del f
53-
self.assertGreaterEqual(grc(func), 2)
51+
self.assertGreaterEqual(sys.getrefcount(func), 2)
5452

5553
# but now it must be gone
5654
gc.collect()
57-
self.assertEqual(grc(func), 2)
55+
self.assertEqual(sys.getrefcount(func), 2)
5856

5957
class X(ctypes.Structure):
6058
_fields_ = [("a", OtherCallback)]
6159
x = X()
6260
x.a = OtherCallback(func)
6361

6462
# the CFuncPtr instance holds at least one refcount on func:
65-
self.assertGreater(grc(func), 2)
63+
self.assertGreater(sys.getrefcount(func), 2)
6664

6765
# and may release it again
6866
del x
69-
self.assertGreaterEqual(grc(func), 2)
67+
self.assertGreaterEqual(sys.getrefcount(func), 2)
7068

7169
# and now it must be gone again
7270
gc.collect()
73-
self.assertEqual(grc(func), 2)
71+
self.assertEqual(sys.getrefcount(func), 2)
7472

7573
f = OtherCallback(func)
7674

7775
# the CFuncPtr instance holds at least one refcount on func:
78-
self.assertGreater(grc(func), 2)
76+
self.assertGreater(sys.getrefcount(func), 2)
7977

8078
# create a cycle
8179
f.cycle = f
8280

8381
del f
8482
gc.collect()
85-
self.assertEqual(grc(func), 2)
83+
self.assertEqual(sys.getrefcount(func), 2)
8684

8785
class AnotherLeak(unittest.TestCase):
8886
def test_callback(self):

Lib/test/test_ctypes/test_slicing.py

+27-23
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,21 @@ def test_setslice_cint(self):
4646
b[33::-3] = range(12)
4747
self.assertEqual(a[:], b)
4848

49-
from operator import setitem
50-
5149
# TypeError: int expected instead of str instance
52-
self.assertRaises(TypeError, setitem, a, slice(0, 5), "abcde")
50+
with self.assertRaises(TypeError):
51+
a[:5] = "abcde"
52+
5353
# TypeError: int expected instead of str instance
54-
self.assertRaises(TypeError, setitem, a, slice(0, 5),
55-
["a", "b", "c", "d", "e"])
54+
with self.assertRaises(TypeError):
55+
a[:5] = ["a", "b", "c", "d", "e"]
56+
5657
# TypeError: int expected instead of float instance
57-
self.assertRaises(TypeError, setitem, a, slice(0, 5),
58-
[1, 2, 3, 4, 3.14])
58+
with self.assertRaises(TypeError):
59+
a[:5] = [1, 2, 3, 4, 3.14]
60+
5961
# ValueError: Can only assign sequence of same size
60-
self.assertRaises(ValueError, setitem, a, slice(0, 5), range(32))
62+
with self.assertRaises(ValueError):
63+
a[:5] = range(32)
6164

6265
def test_char_ptr(self):
6366
s = b"abcdefghijklmnopqrstuvwxyz"
@@ -73,18 +76,20 @@ def test_char_ptr(self):
7376
self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
7477
self.assertEqual(res[0:-1:-1], s[0::-1])
7578

76-
import operator
77-
self.assertRaises(ValueError, operator.getitem,
78-
res, slice(None, None, None))
79-
self.assertRaises(ValueError, operator.getitem,
80-
res, slice(0, None, None))
81-
self.assertRaises(ValueError, operator.getitem,
82-
res, slice(None, 5, -1))
83-
self.assertRaises(ValueError, operator.getitem,
84-
res, slice(-5, None, None))
85-
86-
self.assertRaises(TypeError, operator.setitem,
87-
res, slice(0, 5), "abcde")
79+
# get items
80+
with self.assertRaises(ValueError):
81+
res[:]
82+
with self.assertRaises(ValueError):
83+
res[0:]
84+
with self.assertRaises(ValueError):
85+
res[:5:-1]
86+
with self.assertRaises(ValueError):
87+
res[-5:]
88+
89+
# set items
90+
with self.assertRaises(TypeError):
91+
res[:5] = "abcde"
92+
8893
dll.my_free(res)
8994

9095
dll.my_strdup.restype = POINTER(c_byte)
@@ -139,9 +144,8 @@ def test_wchar_ptr(self):
139144
self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
140145
self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
141146

142-
import operator
143-
self.assertRaises(TypeError, operator.setitem,
144-
res, slice(0, 5), "abcde")
147+
with self.assertRaises(TypeError):
148+
res[:5] = "abcde"
145149
dll.my_free(res)
146150

147151
if sizeof(c_wchar) == sizeof(c_short):

0 commit comments

Comments
 (0)