gh-115801: Only allow sequence of strings as input for difflib.unifie… · python/cpython@c3b6dbf

GitHub

@@ -295,7 +295,7 @@ def test_close_matches_aligned(self):

295295296296classTestOutputFormat(unittest.TestCase):

297297deftest_tab_delimiter(self):

298-args= ['one', 'two', 'Original', 'Current',

298+args= [['one'], ['two'], 'Original', 'Current',

299299'2005-01-26 23:30:50', '2010-04-02 10:20:52']

300300ud=difflib.unified_diff(*args, lineterm='')

301301self.assertEqual(list(ud)[0:2], [

@@ -307,7 +307,7 @@ def test_tab_delimiter(self):

307307"--- Current\t2010-04-02 10:20:52"])

308308309309deftest_no_trailing_tab_on_empty_filedate(self):

310-args= ['one', 'two', 'Original', 'Current']

310+args= [['one'], ['two'], 'Original', 'Current']

311311ud=difflib.unified_diff(*args, lineterm='')

312312self.assertEqual(list(ud)[0:2], ["--- Original", "+++ Current"])

313313@@ -447,6 +447,28 @@ def assertDiff(expect, actual):

447447lineterm=b'')

448448assertDiff(expect, actual)

449449450+451+classTestInputTypes(unittest.TestCase):

452+def_assert_type_error(self, msg, generator, *args):

453+withself.assertRaises(TypeError) asctx:

454+list(generator(*args))

455+self.assertEqual(msg, str(ctx.exception))

456+457+deftest_input_type_checks(self):

458+unified=difflib.unified_diff

459+context=difflib.context_diff

460+461+expect="input must be a sequence of strings, not str"

462+self._assert_type_error(expect, unified, 'a', ['b'])

463+self._assert_type_error(expect, context, 'a', ['b'])

464+465+self._assert_type_error(expect, unified, ['a'], 'b')

466+self._assert_type_error(expect, context, ['a'], 'b')

467+468+expect="lines to compare must be str, not NoneType (None)"

469+self._assert_type_error(expect, unified, ['a'], [None])

470+self._assert_type_error(expect, context, ['a'], [None])

471+450472deftest_mixed_types_content(self):

451473# type of input content must be consistent: all str or all bytes

452474a= [b'hello']

@@ -495,10 +517,6 @@ def test_mixed_types_dates(self):

495517b= ['bar\n']

496518list(difflib.unified_diff(a, b, 'a', 'b', datea, dateb))

497519498-def_assert_type_error(self, msg, generator, *args):

499-withself.assertRaises(TypeError) asctx:

500-list(generator(*args))

501-self.assertEqual(msg, str(ctx.exception))

502520503521classTestJunkAPIs(unittest.TestCase):

504522deftest_is_line_junk_true(self):