diff options
| author | Serhiy Storchaka <storchaka@gmail.com> | 2023-10-14 06:07:02 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-10-14 06:07:02 (GMT) |
| commit | 38bd2c520a2df7904e32093da2166eec2c039802 (patch) | |
| tree | 38c31ad1a4b3ec189658ccc9023520df6912f88e | |
| parent | 45cfabb8429b5ecee78ca60c1ca8f7f5820afc5c (diff) | |
| download | cpython-38bd2c520a2df7904e32093da2166eec2c039802.zip cpython-38bd2c520a2df7904e32093da2166eec2c039802.tar.gz cpython-38bd2c520a2df7904e32093da2166eec2c039802.tar.bz2 | |
gh-88434: Emit deprecation warnings for non-integer numbers in gettext if translation not found (GH-110574)
| -rw-r--r-- | Doc/whatsnew/3.13.rst | 4 | ||||
| -rw-r--r-- | Lib/gettext.py | 11 | ||||
| -rw-r--r-- | Lib/test/test_gettext.py | 26 | ||||
| -rw-r--r-- | Misc/NEWS.d/next/Library/2023-10-08-18-38-09.gh-issue-88434.2Q_IkG.rst | 3 |
4 files changed, 32 insertions, 12 deletions
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst index eb49e01..f43ebc4 100644 --- a/Doc/whatsnew/3.13.rst +++ b/Doc/whatsnew/3.13.rst @@ -363,6 +363,10 @@ Deprecated It was not documented and only supported in the C implementation. (Contributed by Serhiy Storchaka in :gh:`89902`.) +* Emit deprecation warning for non-integer numbers in :mod:`gettext` functions + and methods that consider plural forms even if the translation was not found. + (Contributed by Serhiy Storchaka in :gh:`88434`.) + Pending Removal in Python 3.14 ------------------------------ diff --git a/Lib/gettext.py b/Lib/gettext.py index e84765b..62cff81 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -171,6 +171,13 @@ def _as_int(n): except TypeError: raise TypeError('Plural value must be an integer, got %s' % (n.__class__.__name__,)) from None + return _as_int2(n) + +def _as_int2(n): + try: + return operator.index(n) + except TypeError: + pass import warnings frame = sys._getframe(1) @@ -288,6 +295,7 @@ class NullTranslations: def ngettext(self, msgid1, msgid2, n): if self._fallback: return self._fallback.ngettext(msgid1, msgid2, n) + n = _as_int2(n) if n == 1: return msgid1 else: @@ -301,6 +309,7 @@ class NullTranslations: def npgettext(self, context, msgid1, msgid2, n): if self._fallback: return self._fallback.npgettext(context, msgid1, msgid2, n) + n = _as_int2(n) if n == 1: return msgid1 else: @@ -587,6 +596,7 @@ def dngettext(domain, msgid1, msgid2, n): try: t = translation(domain, _localedirs.get(domain, None)) except OSError: + n = _as_int2(n) if n == 1: return msgid1 else: @@ -606,6 +616,7 @@ def dnpgettext(domain, context, msgid1, msgid2, n): try: t = translation(domain, _localedirs.get(domain, None)) except OSError: + n = _as_int2(n) if n == 1: return msgid1 else: diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py index dd33b9b..b2fe3e2 100644 --- a/Lib/test/test_gettext.py +++ b/Lib/test/test_gettext.py @@ -332,22 +332,24 @@ class PluralFormsTests: x = gettext(singular) self.assertEqual(x, tsingular) + lineno = self._test_plural_forms.__code__.co_firstlineno + 12 + with self.assertWarns(DeprecationWarning) as cm: + x = ngettext(singular, plural, 1.0) + self.assertEqual(cm.filename, __file__) + self.assertEqual(cm.lineno, lineno) + self.assertEqual(x, tsingular) + with self.assertWarns(DeprecationWarning) as cm: + x = ngettext(singular, plural, 1.1) + self.assertEqual(cm.filename, __file__) + self.assertEqual(cm.lineno, lineno + 5) + self.assertEqual(x, tplural) + if numbers_only: - lineno = self._test_plural_forms.__code__.co_firstlineno + 9 - with self.assertWarns(DeprecationWarning) as cm: - x = ngettext(singular, plural, 1.0) - self.assertEqual(cm.filename, __file__) - self.assertEqual(cm.lineno, lineno + 4) - self.assertEqual(x, tsingular) - with self.assertWarns(DeprecationWarning) as cm: - x = ngettext(singular, plural, 1.1) - self.assertEqual(cm.filename, __file__) - self.assertEqual(cm.lineno, lineno + 9) - self.assertEqual(x, tplural) with self.assertRaises(TypeError): ngettext(singular, plural, None) else: - x = ngettext(singular, plural, None) + with self.assertWarns(DeprecationWarning) as cm: + x = ngettext(singular, plural, None) self.assertEqual(x, tplural) def test_plural_forms(self): diff --git a/Misc/NEWS.d/next/Library/2023-10-08-18-38-09.gh-issue-88434.2Q_IkG.rst b/Misc/NEWS.d/next/Library/2023-10-08-18-38-09.gh-issue-88434.2Q_IkG.rst new file mode 100644 index 0000000..9722be2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-10-08-18-38-09.gh-issue-88434.2Q_IkG.rst @@ -0,0 +1,3 @@ +Emit deprecation warning for non-integer numbers in :mod:`gettext` functions +and methods that consider plural forms even if the translation was not +found. |
