8000 gh-88434: Emit deprecation warnings for non-integer numbers in gettex… · python/cpython@38bd2c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 38bd2c5

Browse files
gh-88434: Emit deprecation warnings for non-integer numbers in gettext if translation not found (GH-110574)
1 parent 45cfabb commit 38bd2c5

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,10 @@ Deprecated
363363
It was not documented and only supported in the C implementation.
364364
(Contributed by Serhiy Storchaka in :gh:`89902`.)
365365

366+
* Emit deprecation warning for non-integer numbers in :mod:`gettext` functions
367+
and methods that consider plural forms even if the translation was not found.
368+
(Contributed by Serhiy Storchaka in :gh:`88434`.)
369+
366370

367371
Pending Removal in Python 3.14
368372
------------------------------

Lib/gettext.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ def _as_int(n):
171171
except TypeError:
172172
raise TypeError('Plural value must be an integer, got %s' %
173173
(n.__class__.__name__,)) from None
174+
return _as_int2(n)
175+
176+
def _as_int2(n):
177+
try:
178+
return operator.index(n)
179+
except TypeError:
180+
pass
174181

175182
import warnings
176183
frame = sys._getframe(1)
@@ -288,6 +295,7 @@ def gettext(self, message):
288295
def ngettext(self, msgid1, msgid2, n):
289296
if self._fallback:
290297
return self._fallback.ngettext(msgid1, msgid2, n)
298+
n = _as_int2(n)
291299
if n == 1:
292300
return msgid1
293301
else:
@@ -301,6 +309,7 @@ def pgettext(self, context, message):
301309
def npgettext(self, context, msgid1, msgid2, n):
302310
if self._fallback:
303311
return self._fallback.npgettext(context, msgid1, msgid2, n)
312+
n = _as_int2(n)
304313
if n == 1:
305314
return msgid1
306315
else:
@@ -587,6 +596,7 @@ def dngettext(domain, msgid1, msgid2, n):
587596
try:
588597
t = translation(domain, _localedirs.get(domain, None))
589598
except OSError:
599+
n = _as_int2(n)
590600
if n == 1:
591601
return msgid1
592602
else:
@@ -606,6 +616,7 @@ def dnpgettext(domain, context, msgid1, msgid2, n):
606616
try:
607617
t = translation(domain, _localedirs.get(domain, None))
608618
except OSError:
619+
n = _as_int2(n)
609620
if n == 1:
610621
return msgid1
611622
else:

Lib/test/test_gettext.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,24 @@ def _test_plural_forms(self, ngettext, gettext,
332332
x = gettext(singular)
333333
self.assertEqual(x, tsingular)
334334

335+
lineno = self._test_plural_forms.__code__.co_firstlineno + 12
336+
with self.assertWarns(DeprecationWarning) as cm:
337+
x = ngettext(singular, plural, 1.0)
338+
self.assertEqual(cm.filename, __file__)
339+
self.assertEqual(cm.lineno, lineno)
340+
self.assertEqual(x, tsingular)
341+
with self.assertWarns(DeprecationWarning) as cm:
342+
x = ngettext(singular, plural, 1.1)
343+
self.assertEqual(cm.filename, __file__)
344+
self.assertEqual(cm.lineno, lineno + 5)
345+
self.assertEqual(x, tplural)
346+
335347
if numbers_only:
336-
lineno = self._test_plural_forms.__code__.co_firstlineno + 9
337-
with self.assertWarns(DeprecationWarning) as cm:
338-
x = ngettext(singular, plural, 1.0)
339-
self.assertEqual(cm.filename, __file__)
340-
self.assertEqual(cm.lineno, lineno + 4)
341-
self.assertEqual(x, tsingular)
342-
with self.assertWarns(DeprecationWarning) as cm:
343-
x = ngettext(singular, plural, 1.1)
344-
self.assertEqual(cm.filename, __file__)
345-
self.assertEqual(cm.lineno, lineno + 9)
346-
self.assertEqual(x, tplural)
347348
with self.assertRaises(TypeError):
348349
ngettext(singular, plural, None)
349350
else:
350-
x = ngettext(singular, plural, None)
351+
with self.assertWarns(DeprecationWarning) as cm:
352+
x = ngettext(singular, plural, None)
351353
self.assertEqual(x, tplural)
352354

353355
def test_plural_forms(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Emit deprecation warning for non-integer numbers in :mod:`gettext` functions
2+
and methods that consider plural forms even if the translation was not
3+
found.

0 commit comments

Comments
 (0)
0