diff --git a/Lib/asynchat.py b/Lib/asynchat.py index 91f0bb2fd9c..fc6cabe7f25 100644 --- a/Lib/asynchat.py +++ b/Lib/asynchat.py @@ -99,6 +99,8 @@ def set_terminator(self, term): """ if isinstance(term, str) and self.use_encoding: term = bytes(term, self.encoding) + elif isinstance(term, int) and term < 0: + raise ValueError('the number of received bytes must be positive') self.terminator = term def get_terminator(self): diff --git a/Lib/test/test_asynchat.py b/Lib/test/test_asynchat.py index f6b17f9f36e..9e305731ed3 100644 --- a/Lib/test/test_asynchat.py +++ b/Lib/test/test_asynchat.py @@ -311,5 +311,13 @@ def test_given_list(self): self.assertEqual(f.pop(), (0, None)) +class TestNotConnected(unittest.TestCase): + def test_disallow_negative_terminator(self): + # Issue #11259 + client = asynchat.async_chat() + self.assertRaises(ValueError, client.set_terminator, -1) + + + if __name__ == "__main__": unittest.main() diff --git a/Misc/NEWS b/Misc/NEWS index ce35e3eb39d..4fec3c35295 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -108,6 +108,9 @@ Core and Builtins Library ------- +- Issue #11259: asynchat.async_chat().set_terminator() now raises a ValueError + if the number of received bytes is negative. + - Issue #12523: asynchat.async_chat.push() now raises a TypeError if it doesn't get a bytes string