Skip to content
Closed
15 changes: 15 additions & 0 deletions Lib/test/test_threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ def test_args_argument(self):
t.start()
t.join()

def test_args_invalid_arguments_raises_typeerror(self):
def task(x):
pass

invalid_args = (
"test string",
1,
b"Bytes"
)

for args in invalid_args:
with self.subTest(args=args):
with self.assertRaises(TypeError):
threading.Thread(target=task, args=args)

def test_lock_no_args(self):
threading.Lock() # works
self.assertRaises(TypeError, threading.Lock, 1)
Expand Down
2 changes: 2 additions & 0 deletions Lib/threading.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,8 @@ class is implemented.

self._target = target
self._name = name
if isinstance(args, (str, bytes)) or not hasattr(args, '__iter__'):
raise TypeError(f"'args' must be a non-string iterable like a tuple or list, not {type(args).__name__}")
self._args = args
self._kwargs = kwargs
if daemon is not None:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added validation in threading.Thread to ensure that 'args' is a tuple or list. Raises TypeError if a string, bytes, or non-iterable is passed.
Loading