Skip to content

Commit f52de8a

Browse files
authored
gh-133017: Improve error message for invalid typecodes in multiprocessing.{Array,Value} (GH-133252)
1 parent 2cd24eb commit f52de8a

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

Lib/multiprocessing/sharedctypes.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@
3737
#
3838

3939
def _new_value(type_):
40-
size = ctypes.sizeof(type_)
40+
try:
41+
size = ctypes.sizeof(type_)
42+
except TypeError as e:
43+
raise TypeError("bad typecode (must be a ctypes type or one of "
44+
"c, b, B, u, h, H, i, I, l, L, q, Q, f or d)") from e
45+
4146
wrapper = heap.BufferWrapper(size)
4247
return rebuild_ctype(type_, wrapper, None)
4348

Lib/test/_test_multiprocessing.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2463,6 +2463,12 @@ def test_getobj_getlock(self):
24632463
self.assertNotHasAttr(arr5, 'get_lock')
24642464
self.assertNotHasAttr(arr5, 'get_obj')
24652465

2466+
@unittest.skipIf(c_int is None, "requires _ctypes")
2467+
def test_invalid_typecode(self):
2468+
with self.assertRaisesRegex(TypeError, 'bad typecode'):
2469+
self.Value('x', None)
2470+
with self.assertRaisesRegex(TypeError, 'bad typecode'):
2471+
self.RawValue('x', None)
24662472

24672473
class _TestArray(BaseTestCase):
24682474

@@ -2543,6 +2549,12 @@ def test_getobj_getlock_obj(self):
25432549
self.assertNotHasAttr(arr5, 'get_lock')
25442550
self.assertNotHasAttr(arr5, 'get_obj')
25452551

2552+
@unittest.skipIf(c_int is None, "requires _ctypes")
2553+
def test_invalid_typecode(self):
2554+
with self.assertRaisesRegex(TypeError, 'bad typecode'):
2555+
self.Array('x', [])
2556+
with self.assertRaisesRegex(TypeError, 'bad typecode'):
2557+
self.RawArray('x', [])
25462558
#
25472559
#
25482560
#
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Improve the error message of :func:`multiprocessing.sharedctypes.Array`,
2+
:func:`multiprocessing.sharedctypes.RawArray`, :func:`multiprocessing.sharedctypes.Value` and
3+
:func:`multiprocessing.sharedctypes.RawValue` when an invalid typecode is passed. Patch
4+
by Tomas Roun

0 commit comments

Comments
 (0)