From baf705f34a8c8977d042ce43c71f508f9ca4f8ce Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Tue, 21 Nov 2023 15:11:58 +0000 Subject: [PATCH] Refs #34986 -- Fixed some test assertions for PyPy. These failures were due to minor inconsistencies or implementation differences between CPython and PyPy. --- tests/forms_tests/field_tests/test_emailfield.py | 2 +- tests/forms_tests/field_tests/test_urlfield.py | 2 +- tests/forms_tests/widget_tests/test_selectdatewidget.py | 7 ++++++- tests/model_forms/tests.py | 6 +++++- tests/utils_tests/test_encoding.py | 6 ++++-- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/forms_tests/field_tests/test_emailfield.py b/tests/forms_tests/field_tests/test_emailfield.py index 079267ca8b..601318bae3 100644 --- a/tests/forms_tests/field_tests/test_emailfield.py +++ b/tests/forms_tests/field_tests/test_emailfield.py @@ -71,6 +71,6 @@ class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase): self.assertIsNone(f.clean(None)) def test_emailfield_unable_to_set_strip_kwarg(self): - msg = "__init__() got multiple values for keyword argument 'strip'" + msg = "got multiple values for keyword argument 'strip'" with self.assertRaisesMessage(TypeError, msg): EmailField(strip=False) diff --git a/tests/forms_tests/field_tests/test_urlfield.py b/tests/forms_tests/field_tests/test_urlfield.py index 058a2992ed..2cd1a82694 100644 --- a/tests/forms_tests/field_tests/test_urlfield.py +++ b/tests/forms_tests/field_tests/test_urlfield.py @@ -136,7 +136,7 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase): self.assertIsNone(f.clean(None)) def test_urlfield_unable_to_set_strip_kwarg(self): - msg = "__init__() got multiple values for keyword argument 'strip'" + msg = "got multiple values for keyword argument 'strip'" with self.assertRaisesMessage(TypeError, msg): URLField(strip=False) diff --git a/tests/forms_tests/widget_tests/test_selectdatewidget.py b/tests/forms_tests/widget_tests/test_selectdatewidget.py index 215c41a809..017e90f5f6 100644 --- a/tests/forms_tests/widget_tests/test_selectdatewidget.py +++ b/tests/forms_tests/widget_tests/test_selectdatewidget.py @@ -5,6 +5,7 @@ from django.forms import DateField, Form, SelectDateWidget from django.test import override_settings from django.utils import translation from django.utils.dates import MONTHS_AP +from django.utils.version import PYPY from .base import WidgetTest @@ -611,7 +612,11 @@ class SelectDateWidgetTest(WidgetTest): ((None, "12", "1"), None), (("2000", None, "1"), None), (("2000", "12", None), None), - ((str(sys.maxsize + 1), "12", "1"), "0-0-0"), + ( + (str(sys.maxsize + 1), "12", "1"), + # PyPy does not raise OverflowError. + f"{sys.maxsize + 1}-12-1" if PYPY else "0-0-0", + ), ] for values, expected in tests: with self.subTest(values=values): diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index 43bb770f7e..f706271106 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -25,6 +25,7 @@ from django.test import SimpleTestCase, TestCase, ignore_warnings, skipUnlessDBF from django.test.utils import isolate_apps from django.utils.choices import BlankChoiceIterator from django.utils.deprecation import RemovedInDjango60Warning +from django.utils.version import PYPY from .models import ( Article, @@ -3017,7 +3018,10 @@ class OtherModelFormTests(TestCase): return ", ".join(c.name for c in obj.colours.all()) field = ColorModelChoiceField(ColourfulItem.objects.prefetch_related("colours")) - with self.assertNumQueries(3): # would be 4 if prefetch is ignored + # CPython calls ModelChoiceField.__len__() when coercing to tuple. PyPy + # doesn't call __len__() and so .count() isn't called on the QuerySet. + # The following would trigger an extra query if prefetch were ignored. + with self.assertNumQueries(2 if PYPY else 3): self.assertEqual( tuple(field.choices), ( diff --git a/tests/utils_tests/test_encoding.py b/tests/utils_tests/test_encoding.py index 2b52b1607c..e0ee190431 100644 --- a/tests/utils_tests/test_encoding.py +++ b/tests/utils_tests/test_encoding.py @@ -22,6 +22,7 @@ from django.utils.encoding import ( ) from django.utils.functional import SimpleLazyObject from django.utils.translation import gettext_lazy +from django.utils.version import PYPY class TestEncodingUtils(SimpleTestCase): @@ -43,9 +44,10 @@ class TestEncodingUtils(SimpleTestCase): self.assertIs(type(force_str(s)), str) def test_force_str_DjangoUnicodeDecodeError(self): + reason = "unexpected end of data" if PYPY else "invalid start byte" msg = ( - "'utf-8' codec can't decode byte 0xff in position 0: invalid " - "start byte. You passed in b'\\xff' ()" + f"'utf-8' codec can't decode byte 0xff in position 0: {reason}. " + "You passed in b'\\xff' ()" ) with self.assertRaisesMessage(DjangoUnicodeDecodeError, msg): force_str(b"\xff")