]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-133017: Improve error message for invalid typecodes in multiprocessing.{Array...
authorTomas R. <tomas.roun8@gmail.com>
Fri, 9 May 2025 08:46:45 +0000 (10:46 +0200)
committerGitHub <noreply@github.com>
Fri, 9 May 2025 08:46:45 +0000 (11:46 +0300)
Lib/multiprocessing/sharedctypes.py
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2025-05-01-16-03-11.gh-issue-133017.k7RLQp.rst [new file with mode: 0644]

index 6071707027bea46dccfc4d92fb3195060faf07ce..eee1172e6e9135c764a2c6f231cdf61aa3cce000 100644 (file)
@@ -37,7 +37,12 @@ typecode_to_type = {
 #
 
 def _new_value(type_):
-    size = ctypes.sizeof(type_)
+    try:
+        size = ctypes.sizeof(type_)
+    except TypeError as e:
+        raise TypeError("bad typecode (must be a ctypes type or one of "
+                        "c, b, B, u, h, H, i, I, l, L, q, Q, f or d)") from e
+
     wrapper = heap.BufferWrapper(size)
     return rebuild_ctype(type_, wrapper, None)
 
index 4dc9a31d22f7716a4199a69c31413ef04a9fbedf..1b690cb88bfc57815100fd67142782f7a5997bcc 100644 (file)
@@ -2463,6 +2463,12 @@ class _TestValue(BaseTestCase):
         self.assertNotHasAttr(arr5, 'get_lock')
         self.assertNotHasAttr(arr5, 'get_obj')
 
+    @unittest.skipIf(c_int is None, "requires _ctypes")
+    def test_invalid_typecode(self):
+        with self.assertRaisesRegex(TypeError, 'bad typecode'):
+            self.Value('x', None)
+        with self.assertRaisesRegex(TypeError, 'bad typecode'):
+            self.RawValue('x', None)
 
 class _TestArray(BaseTestCase):
 
@@ -2543,6 +2549,12 @@ class _TestArray(BaseTestCase):
         self.assertNotHasAttr(arr5, 'get_lock')
         self.assertNotHasAttr(arr5, 'get_obj')
 
+    @unittest.skipIf(c_int is None, "requires _ctypes")
+    def test_invalid_typecode(self):
+        with self.assertRaisesRegex(TypeError, 'bad typecode'):
+            self.Array('x', [])
+        with self.assertRaisesRegex(TypeError, 'bad typecode'):
+            self.RawArray('x', [])
 #
 #
 #
diff --git a/Misc/NEWS.d/next/Library/2025-05-01-16-03-11.gh-issue-133017.k7RLQp.rst b/Misc/NEWS.d/next/Library/2025-05-01-16-03-11.gh-issue-133017.k7RLQp.rst
new file mode 100644 (file)
index 0000000..1b5bf74
--- /dev/null
@@ -0,0 +1,4 @@
+Improve the error message of :func:`multiprocessing.sharedctypes.Array`,
+:func:`multiprocessing.sharedctypes.RawArray`, :func:`multiprocessing.sharedctypes.Value` and
+:func:`multiprocessing.sharedctypes.RawValue` when an invalid typecode is passed. Patch
+by Tomas Roun