From 62d6671aab12b4a31bcf6b0a5ebdd981db14923e Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sat, 26 Apr 2025 15:18:06 +0200 Subject: [PATCH 1/4] Improve invalid typecode message --- Lib/multiprocessing/sharedctypes.py | 7 ++++++- Lib/test/_test_multiprocessing.py | 12 ++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Lib/multiprocessing/sharedctypes.py b/Lib/multiprocessing/sharedctypes.py index 6071707027bea4..ce4e539ad44c16 100644 --- a/Lib/multiprocessing/sharedctypes.py +++ b/Lib/multiprocessing/sharedctypes.py @@ -37,7 +37,12 @@ # 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) diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 4dc9a31d22f771..1b690cb88bfc57 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -2463,6 +2463,12 @@ def test_getobj_getlock(self): 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 @@ def test_getobj_getlock_obj(self): 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', []) # # # From 3ae6818d697d7edae33197363ca7f996d453ad85 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Sat, 26 Apr 2025 15:19:41 +0200 Subject: [PATCH 2/4] Add news entry --- .../Library/2025-05-01-16-03-11.gh-issue-133017.k7RLQp.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-05-01-16-03-11.gh-issue-133017.k7RLQp.rst 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 index 00000000000000..df23949442c5ae --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-05-01-16-03-11.gh-issue-133017.k7RLQp.rst @@ -0,0 +1,4 @@ +Improve the error message of :func:`multiprocessing.Array`, +:func:`multiprocessing.RawArray`, :func:`multiprocessing.Value` and +:func:`multiprocessing.RawValue` when an invalid typecode is passed. Patch +by Tomas Roun From 9ec59900748fafdcc92c98a30c75b95885a6705d Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Thu, 1 May 2025 16:55:57 +0200 Subject: [PATCH 3/4] Make linter happy --- Lib/multiprocessing/sharedctypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/multiprocessing/sharedctypes.py b/Lib/multiprocessing/sharedctypes.py index ce4e539ad44c16..eee1172e6e9135 100644 --- a/Lib/multiprocessing/sharedctypes.py +++ b/Lib/multiprocessing/sharedctypes.py @@ -42,7 +42,7 @@ def _new_value(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) From 03776cca10ce647e61ca80a00bc29e82ec568182 Mon Sep 17 00:00:00 2001 From: Tomas Roun Date: Thu, 1 May 2025 22:13:48 +0200 Subject: [PATCH 4/4] Fix news entry --- .../Library/2025-05-01-16-03-11.gh-issue-133017.k7RLQp.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index df23949442c5ae..1b5bf74fb47e33 100644 --- 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 @@ -1,4 +1,4 @@ -Improve the error message of :func:`multiprocessing.Array`, -:func:`multiprocessing.RawArray`, :func:`multiprocessing.Value` and -:func:`multiprocessing.RawValue` when an invalid typecode is passed. Patch +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