FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-88434: Emit deprecation warnings for non-integer numbers in gettext if translation not found by serhiy-storchaka · Pull Request #110574 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,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
------------------------------
Expand Down
11 changes: 11 additions & 0 deletions Lib/gettext.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -288,6 +295,7 @@ def gettext(self, message):
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:
Expand All @@ -301,6 +309,7 @@ def pgettext(self, context, message):
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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
26 changes: 14 additions & 12 deletions Lib/test/test_gettext.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -332,22 +332,24 @@ def _test_plural_forms(self, ngettext, gettext,
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):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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.

Back | FazBrowse Home | New Git URL