gh-133017: Improve error message for invalid typecodes in multiprocessing.{Array,Value} (GH-133252)

This commit is contained in:
Tomas R. 2025-05-09 10:46:45 +02:00 committed by GitHub
parent 2cd24ebfe9
commit f52de8a937
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 1 deletions

View 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)

View 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', [])
#
#
#

View file

@ -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