summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2023-06-14 21:29:05 (GMT)
committerGitHub <noreply@github.com>2023-06-14 21:29:05 (GMT)
commit5ab13c5f97aa5226c49052f1ad19a8c97a3d7cdf (patch)
treef5e20900e6d348fff79c9b28c31a63fbb4871d8b /Lib
parente7507bd131fbfbb49a6819a0d5ad5dd1e21b48cd (diff)
downloadcpython-5ab13c5f97aa5226c49052f1ad19a8c97a3d7cdf.zip
cpython-5ab13c5f97aa5226c49052f1ad19a8c97a3d7cdf.tar.gz
cpython-5ab13c5f97aa5226c49052f1ad19a8c97a3d7cdf.tar.bz2
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.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_ctypes/test_arrays.py8
-rw-r--r--Lib/test/test_ctypes/test_callbacks.py8
-rw-r--r--Lib/test/test_ctypes/test_delattr.py14
-rw-r--r--Lib/test/test_ctypes/test_internals.py10
-rw-r--r--Lib/test/test_ctypes/test_keeprefs.py11
-rw-r--r--Lib/test/test_ctypes/test_pointers.py4
-rw-r--r--Lib/test/test_ctypes/test_python_api.py22
-rw-r--r--Lib/test/test_ctypes/test_refcounts.py32
-rw-r--r--Lib/test/test_ctypes/test_slicing.py50
-rw-r--r--Lib/test/test_ctypes/test_stringptr.py6
10 files changed, 85 insertions, 80 deletions
diff --git a/Lib/test/test_ctypes/test_arrays.py b/Lib/test/test_ctypes/test_arrays.py
index 7c35a7e..f7a4b5d 100644
--- a/Lib/test/test_ctypes/test_arrays.py
+++ b/Lib/test/test_ctypes/test_arrays.py
@@ -46,9 +46,9 @@ class ArrayTestCase(unittest.TestCase):
with self.assertRaises(IndexError): ia[-alen-1]
# change the items
- from operator import setitem
new_values = list(range(42, 42+alen))
- [setitem(ia, n, new_values[n]) for n in range(alen)]
+ for n in range(alen):
+ ia[n] = new_values[n]
values = [ia[i] for i in range(alen)]
self.assertEqual(values, new_values)
@@ -78,8 +78,8 @@ class ArrayTestCase(unittest.TestCase):
self.assertEqual(len(ca), 3)
# cannot delete items
- from operator import delitem
- self.assertRaises(TypeError, delitem, ca, 0)
+ with self.assertRaises(TypeError):
+ del ca[0]
def test_step_overflow(self):
a = (c_int * 5)()
diff --git a/Lib/test/test_ctypes/test_callbacks.py b/Lib/test/test_ctypes/test_callbacks.py
index 45829b5..50c2650 100644
--- a/Lib/test/test_ctypes/test_callbacks.py
+++ b/Lib/test/test_ctypes/test_callbacks.py
@@ -1,4 +1,5 @@
import functools
+import sys
import unittest
from test import support
@@ -106,15 +107,14 @@ class Callbacks(unittest.TestCase):
def test_pyobject(self):
o = ()
- from sys import getrefcount as grc
for o in (), [], object():
- initial = grc(o)
+ initial = sys.getrefcount(o)
# This call leaks a reference to 'o'...
self.check_type(py_object, o)
- before = grc(o)
+ before = sys.getrefcount(o)
# ...but this call doesn't leak any more. Where is the refcount?
self.check_type(py_object, o)
- after = grc(o)
+ after = sys.getrefcount(o)
self.assertEqual((after, o), (before, o))
def test_unsupported_restype_1(self):
diff --git a/Lib/test/test_ctypes/test_delattr.py b/Lib/test/test_ctypes/test_delattr.py
index 10d2fe0..e80b5fa 100644
--- a/Lib/test/test_ctypes/test_delattr.py
+++ b/Lib/test/test_ctypes/test_delattr.py
@@ -8,16 +8,18 @@ class X(Structure):
class TestCase(unittest.TestCase):
def test_simple(self):
- self.assertRaises(TypeError,
- delattr, c_int(42), "value")
+ with self.assertRaises(TypeError):
+ del c_int(42).value
def test_chararray(self):
- self.assertRaises(TypeError,
- delattr, (c_char * 5)(), "value")
+ chararray = (c_char * 5)()
+ with self.assertRaises(TypeError):
+ del chararray.value
def test_struct(self):
- self.assertRaises(TypeError,
- delattr, X(), "foo")
+ struct = X()
+ with self.assertRaises(TypeError):
+ del struct.foo
if __name__ == "__main__":
diff --git a/Lib/test/test_ctypes/test_internals.py b/Lib/test/test_ctypes/test_internals.py
index 1290f80..06826eb 100644
--- a/Lib/test/test_ctypes/test_internals.py
+++ b/Lib/test/test_ctypes/test_internals.py
@@ -1,7 +1,7 @@
# This tests the internal _objects attribute
+import sys
import unittest
from ctypes import Structure, POINTER, c_char_p, c_int
-from sys import getrefcount as grc
# XXX This test must be reviewed for correctness!!!
@@ -20,16 +20,16 @@ class ObjectsTestCase(unittest.TestCase):
def test_ints(self):
i = 42000123
- refcnt = grc(i)
+ refcnt = sys.getrefcount(i)
ci = c_int(i)
- self.assertEqual(refcnt, grc(i))
+ self.assertEqual(refcnt, sys.getrefcount(i))
self.assertEqual(ci._objects, None)
def test_c_char_p(self):
s = b"Hello, World"
- refcnt = grc(s)
+ refcnt = sys.getrefcount(s)
cs = c_char_p(s)
- self.assertEqual(refcnt + 1, grc(s))
+ self.assertEqual(refcnt + 1, sys.getrefcount(s))
self.assertSame(cs._objects, s)
def test_simple_struct(self):
diff --git a/Lib/test/test_ctypes/test_keeprefs.py b/Lib/test/test_ctypes/test_keeprefs.py
index ecfcda1..6fb0628 100644
--- a/Lib/test/test_ctypes/test_keeprefs.py
+++ b/Lib/test/test_ctypes/test_keeprefs.py
@@ -1,5 +1,7 @@
-from ctypes import Structure, POINTER, pointer, c_char_p, c_int
+import sys
import unittest
+from ctypes import Structure, POINTER, pointer, c_char_p, c_int
+
class SimpleTestCase(unittest.TestCase):
def test_cint(self):
@@ -100,16 +102,15 @@ class DeletePointerTestCase(unittest.TestCase):
_fields_ = [("p", POINTER(c_char_p))]
x = X()
i = c_char_p("abc def")
- from sys import getrefcount as grc
- print("2?", grc(i))
+ print("2?", sys.getrefcount(i))
x.p = pointer(i)
- print("3?", grc(i))
+ print("3?", sys.getrefcount(i))
for i in range(320):
c_int(99)
x.p[0]
print(x.p[0])
## del x
-## print "2?", grc(i)
+## print "2?", sys.getrefcount(i)
## del i
import gc
gc.collect()
diff --git a/Lib/test/test_ctypes/test_pointers.py b/Lib/test/test_ctypes/test_pointers.py
index 7d13aeb..7e25d82 100644
--- a/Lib/test/test_ctypes/test_pointers.py
+++ b/Lib/test/test_ctypes/test_pointers.py
@@ -97,7 +97,6 @@ class PointersTestCase(unittest.TestCase):
## print self.result
def test_basics(self):
- from operator import delitem
for ct, pt in zip(ctype_types, python_types):
i = ct(42)
p = pointer(i)
@@ -108,7 +107,8 @@ class PointersTestCase(unittest.TestCase):
## self.assertEqual(p.contents, 42)
## self.assertEqual(p[0], 42)
- self.assertRaises(TypeError, delitem, p, 0)
+ with self.assertRaises(TypeError):
+ del p[0]
def test_from_address(self):
from array import array
diff --git a/Lib/test/test_ctypes/test_python_api.py b/Lib/test/test_ctypes/test_python_api.py
index 3799311..c8b8119 100644
--- a/Lib/test/test_ctypes/test_python_api.py
+++ b/Lib/test/test_ctypes/test_python_api.py
@@ -1,4 +1,5 @@
import unittest
+import sys
from test import support
from ctypes import (pythonapi, POINTER, c_buffer, sizeof,
py_object, c_char_p, c_char, c_long, c_size_t)
@@ -11,7 +12,6 @@ from _ctypes import PyObj_FromPtr
################################################################
-from sys import getrefcount as grc
class PythonAPITestCase(unittest.TestCase):
@@ -29,41 +29,41 @@ class PythonAPITestCase(unittest.TestCase):
pythonapi.PyBytes_FromString.argtypes = (c_char_p,)
s = b"abc"
- refcnt = grc(s)
+ refcnt = sys.getrefcount(s)
pyob = pythonapi.PyBytes_FromString(s)
- self.assertEqual(grc(s), refcnt)
+ self.assertEqual(sys.getrefcount(s), refcnt)
self.assertEqual(s, pyob)
del pyob
- self.assertEqual(grc(s), refcnt)
+ self.assertEqual(sys.getrefcount(s), refcnt)
@support.refcount_test
def test_PyLong_Long(self):
- ref42 = grc(42)
+ ref42 = sys.getrefcount(42)
pythonapi.PyLong_FromLong.restype = py_object
self.assertEqual(pythonapi.PyLong_FromLong(42), 42)
- self.assertEqual(grc(42), ref42)
+ self.assertEqual(sys.getrefcount(42), ref42)
pythonapi.PyLong_AsLong.argtypes = (py_object,)
pythonapi.PyLong_AsLong.restype = c_long
res = pythonapi.PyLong_AsLong(42)
# Small int refcnts don't change
- self.assertEqual(grc(res), ref42)
+ self.assertEqual(sys.getrefcount(res), ref42)
del res
- self.assertEqual(grc(42), ref42)
+ self.assertEqual(sys.getrefcount(42), ref42)
@support.refcount_test
def test_PyObj_FromPtr(self):
s = "abc def ghi jkl"
- ref = grc(s)
+ ref = sys.getrefcount(s)
# id(python-object) is the address
pyobj = PyObj_FromPtr(id(s))
self.assertIs(s, pyobj)
- self.assertEqual(grc(s), ref + 1)
+ self.assertEqual(sys.getrefcount(s), ref + 1)
del pyobj
- self.assertEqual(grc(s), ref)
+ self.assertEqual(sys.getrefcount(s), ref)
def test_PyOS_snprintf(self):
PyOS_snprintf = pythonapi.PyOS_snprintf
diff --git a/Lib/test/test_ctypes/test_refcounts.py b/Lib/test/test_ctypes/test_refcounts.py
index 48958cd..b05cad0 100644
--- a/Lib/test/test_ctypes/test_refcounts.py
+++ b/Lib/test/test_ctypes/test_refcounts.py
@@ -1,7 +1,8 @@
-import unittest
-from test import support
import ctypes
import gc
+import sys
+import unittest
+from test import support
MyCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
OtherCallback = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_ulonglong)
@@ -13,8 +14,6 @@ class RefcountTestCase(unittest.TestCase):
@support.refcount_test
def test_1(self):
- from sys import getrefcount as grc
-
f = dll._testfunc_callback_i_if
f.restype = ctypes.c_int
f.argtypes = [ctypes.c_int, MyCallback]
@@ -23,38 +22,37 @@ class RefcountTestCase(unittest.TestCase):
#print "called back with", value
return value
- self.assertEqual(grc(callback), 2)
+ self.assertEqual(sys.getrefcount(callback), 2)
cb = MyCallback(callback)
- self.assertGreater(grc(callback), 2)
+ self.assertGreater(sys.getrefcount(callback), 2)
result = f(-10, cb)
self.assertEqual(result, -18)
cb = None
gc.collect()
- self.assertEqual(grc(callback), 2)
+ self.assertEqual(sys.getrefcount(callback), 2)
@support.refcount_test
def test_refcount(self):
- from sys import getrefcount as grc
def func(*args):
pass
# this is the standard refcount for func
- self.assertEqual(grc(func), 2)
+ self.assertEqual(sys.getrefcount(func), 2)
# the CFuncPtr instance holds at least one refcount on func:
f = OtherCallback(func)
- self.assertGreater(grc(func), 2)
+ self.assertGreater(sys.getrefcount(func), 2)
# and may release it again
del f
- self.assertGreaterEqual(grc(func), 2)
+ self.assertGreaterEqual(sys.getrefcount(func), 2)
# but now it must be gone
gc.collect()
- self.assertEqual(grc(func), 2)
+ self.assertEqual(sys.getrefcount(func), 2)
class X(ctypes.Structure):
_fields_ = [("a", OtherCallback)]
@@ -62,27 +60,27 @@ class RefcountTestCase(unittest.TestCase):
x.a = OtherCallback(func)
# the CFuncPtr instance holds at least one refcount on func:
- self.assertGreater(grc(func), 2)
+ self.assertGreater(sys.getrefcount(func), 2)
# and may release it again
del x
- self.assertGreaterEqual(grc(func), 2)
+ self.assertGreaterEqual(sys.getrefcount(func), 2)
# and now it must be gone again
gc.collect()
- self.assertEqual(grc(func), 2)
+ self.assertEqual(sys.getrefcount(func), 2)
f = OtherCallback(func)
# the CFuncPtr instance holds at least one refcount on func:
- self.assertGreater(grc(func), 2)
+ self.assertGreater(sys.getrefcount(func), 2)
# create a cycle
f.cycle = f
del f
gc.collect()
- self.assertEqual(grc(func), 2)
+ self.assertEqual(sys.getrefcount(func), 2)
class AnotherLeak(unittest.TestCase):
def test_callback(self):
diff --git a/Lib/test/test_ctypes/test_slicing.py b/Lib/test/test_ctypes/test_slicing.py
index 91385e9..30db90c 100644
--- a/Lib/test/test_ctypes/test_slicing.py
+++ b/Lib/test/test_ctypes/test_slicing.py
@@ -46,18 +46,21 @@ class SlicesTestCase(unittest.TestCase):
b[33::-3] = range(12)
self.assertEqual(a[:], b)
- from operator import setitem
-
# TypeError: int expected instead of str instance
- self.assertRaises(TypeError, setitem, a, slice(0, 5), "abcde")
+ with self.assertRaises(TypeError):
+ a[:5] = "abcde"
+
# TypeError: int expected instead of str instance
- self.assertRaises(TypeError, setitem, a, slice(0, 5),
- ["a", "b", "c", "d", "e"])
+ with self.assertRaises(TypeError):
+ a[:5] = ["a", "b", "c", "d", "e"]
+
# TypeError: int expected instead of float instance
- self.assertRaises(TypeError, setitem, a, slice(0, 5),
- [1, 2, 3, 4, 3.14])
+ with self.assertRaises(TypeError):
+ a[:5] = [1, 2, 3, 4, 3.14]
+
# ValueError: Can only assign sequence of same size
- self.assertRaises(ValueError, setitem, a, slice(0, 5), range(32))
+ with self.assertRaises(ValueError):
+ a[:5] = range(32)
def test_char_ptr(self):
s = b"abcdefghijklmnopqrstuvwxyz"
@@ -73,18 +76,20 @@ class SlicesTestCase(unittest.TestCase):
self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
self.assertEqual(res[0:-1:-1], s[0::-1])
- import operator
- self.assertRaises(ValueError, operator.getitem,
- res, slice(None, None, None))
- self.assertRaises(ValueError, operator.getitem,
- res, slice(0, None, None))
- self.assertRaises(ValueError, operator.getitem,
- res, slice(None, 5, -1))
- self.assertRaises(ValueError, operator.getitem,
- res, slice(-5, None, None))
-
- self.assertRaises(TypeError, operator.setitem,
- res, slice(0, 5), "abcde")
+ # get items
+ with self.assertRaises(ValueError):
+ res[:]
+ with self.assertRaises(ValueError):
+ res[0:]
+ with self.assertRaises(ValueError):
+ res[:5:-1]
+ with self.assertRaises(ValueError):
+ res[-5:]
+
+ # set items
+ with self.assertRaises(TypeError):
+ res[:5] = "abcde"
+
dll.my_free(res)
dll.my_strdup.restype = POINTER(c_byte)
@@ -139,9 +144,8 @@ class SlicesTestCase(unittest.TestCase):
self.assertEqual(res[len(s)-1:-1:-1], s[::-1])
self.assertEqual(res[len(s)-1:5:-7], s[:5:-7])
- import operator
- self.assertRaises(TypeError, operator.setitem,
- res, slice(0, 5), "abcde")
+ with self.assertRaises(TypeError):
+ res[:5] = "abcde"
dll.my_free(res)
if sizeof(c_wchar) == sizeof(c_short):
diff --git a/Lib/test/test_ctypes/test_stringptr.py b/Lib/test/test_ctypes/test_stringptr.py
index 1eae154..8bf0433 100644
--- a/Lib/test/test_ctypes/test_stringptr.py
+++ b/Lib/test/test_ctypes/test_stringptr.py
@@ -1,4 +1,5 @@
import unittest
+import sys
from test import support
from ctypes import CDLL, Structure, POINTER, c_buffer, c_char, c_char_p
@@ -17,10 +18,9 @@ class StringPtrTestCase(unittest.TestCase):
# NULL pointer access
self.assertRaises(ValueError, getattr, x.str, "contents")
b = c_buffer(b"Hello, World")
- from sys import getrefcount as grc
- self.assertEqual(grc(b), 2)
+ self.assertEqual(sys.getrefcount(b), 2)
x.str = b
- self.assertEqual(grc(b), 3)
+ self.assertEqual(sys.getrefcount(b), 3)
# POINTER(c_char) and Python string is NOT compatible
# POINTER(c_char) and c_buffer() is compatible