summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_gettext.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-10-09 14:27:01 (GMT)
committerGitHub <noreply@github.com>2023-10-09 14:27:01 (GMT)
commit1f43bc2f3ba9500d70afa9f471cb328fa3f8f4ec (patch)
tree80fcb2e446f5f3c982f6e6689a33101627021da2 /Lib/test/test_gettext.py
parent53122bcf825681487226c041d47763e82e081530 (diff)
downloadcpython-1f43bc2f3ba9500d70afa9f471cb328fa3f8f4ec.zip
cpython-1f43bc2f3ba9500d70afa9f471cb328fa3f8f4ec.tar.gz
cpython-1f43bc2f3ba9500d70afa9f471cb328fa3f8f4ec.tar.bz2
[3.12] gh-110519: Improve deprecation warning in the gettext module (GH-110520) (GH-110563)
Deprecation warning about non-integer numbers in gettext now always refers to the line in the user code where gettext function or method is used. Previously, it could refer to a line in gettext code. Also, increase test coverage for NullTranslations and domain-aware functions like dngettext(). (cherry picked from commit 326c6c4e07137b43c49b74bd5528619360080469) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/test/test_gettext.py')
-rw-r--r--Lib/test/test_gettext.py178
1 files changed, 130 insertions, 48 deletions
diff --git a/Lib/test/test_gettext.py b/Lib/test/test_gettext.py
index 2650ce5..dd33b9b 100644
--- a/Lib/test/test_gettext.py
+++ b/Lib/test/test_gettext.py
@@ -2,6 +2,7 @@ import os
import base64
import gettext
import unittest
+from functools import partial
from test import support
from test.support import os_helper
@@ -122,8 +123,9 @@ def reset_gettext():
class GettextBaseTest(unittest.TestCase):
- def setUp(self):
- self.addCleanup(os_helper.rmtree, os.path.split(LOCALEDIR)[0])
+ @classmethod
+ def setUpClass(cls):
+ cls.addClassCleanup(os_helper.rmtree, os.path.split(LOCALEDIR)[0])
if not os.path.isdir(LOCALEDIR):
os.makedirs(LOCALEDIR)
with open(MOFILE, 'wb') as fp:
@@ -136,6 +138,8 @@ class GettextBaseTest(unittest.TestCase):
fp.write(base64.decodebytes(UMO_DATA))
with open(MMOFILE, 'wb') as fp:
fp.write(base64.decodebytes(MMO_DATA))
+
+ def setUp(self):
self.env = self.enterContext(os_helper.EnvironmentVarGuard())
self.env['LANGUAGE'] = 'xx'
reset_gettext()
@@ -316,59 +320,137 @@ fhccbeg sbe lbhe Clguba cebtenzf ol cebivqvat na vagresnpr gb gur TAH
trggrkg zrffntr pngnybt yvoenel.''')
-class PluralFormsTestCase(GettextBaseTest):
+class PluralFormsTests:
+
+ def _test_plural_forms(self, ngettext, gettext,
+ singular, plural, tsingular, tplural,
+ numbers_only=True):
+ x = ngettext(singular, plural, 1)
+ self.assertEqual(x, tsingular)
+ x = ngettext(singular, plural, 2)
+ self.assertEqual(x, tplural)
+ x = gettext(singular)
+ self.assertEqual(x, tsingular)
+
+ 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)
+ self.assertEqual(x, tplural)
+
+ def test_plural_forms(self):
+ self._test_plural_forms(
+ self.ngettext, self.gettext,
+ 'There is %s file', 'There are %s files',
+ 'Hay %s fichero', 'Hay %s ficheros')
+ self._test_plural_forms(
+ self.ngettext, self.gettext,
+ '%d file deleted', '%d files deleted',
+ '%d file deleted', '%d files deleted')
+
+ def test_plural_context_forms(self):
+ ngettext = partial(self.npgettext, 'With context')
+ gettext = partial(self.pgettext, 'With context')
+ self._test_plural_forms(
+ ngettext, gettext,
+ 'There is %s file', 'There are %s files',
+ 'Hay %s fichero (context)', 'Hay %s ficheros (context)')
+ self._test_plural_forms(
+ ngettext, gettext,
+ '%d file deleted', '%d files deleted',
+ '%d file deleted', '%d files deleted')
+
+ def test_plural_wrong_context_forms(self):
+ self._test_plural_forms(
+ partial(self.npgettext, 'Unknown context'),
+ partial(self.pgettext, 'Unknown context'),
+ 'There is %s file', 'There are %s files',
+ 'There is %s file', 'There are %s files')
+
+
+class GNUTranslationsPluralFormsTestCase(PluralFormsTests, GettextBaseTest):
def setUp(self):
GettextBaseTest.setUp(self)
- self.localedir = os.curdir
# Set up the bindings
- gettext.bindtextdomain('gettext', self.localedir)
+ gettext.bindtextdomain('gettext', os.curdir)
gettext.textdomain('gettext')
- self.mofile = MOFILE
- def test_plural_forms1(self):
- eq = self.assertEqual
- x = gettext.ngettext('There is %s file', 'There are %s files', 1)
- eq(x, 'Hay %s fichero')
- x = gettext.ngettext('There is %s file', 'There are %s files', 2)
- eq(x, 'Hay %s ficheros')
- x = gettext.gettext('There is %s file')
- eq(x, 'Hay %s fichero')
-
- def test_plural_context_forms1(self):
- eq = self.assertEqual
- x = gettext.npgettext('With context',
- 'There is %s file', 'There are %s files', 1)
- eq(x, 'Hay %s fichero (context)')
- x = gettext.npgettext('With context',
- 'There is %s file', 'There are %s files', 2)
- eq(x, 'Hay %s ficheros (context)')
- x = gettext.pgettext('With context', 'There is %s file')
- eq(x, 'Hay %s fichero (context)')
-
- def test_plural_forms2(self):
- eq = self.assertEqual
- with open(self.mofile, 'rb') as fp:
- t = gettext.GNUTranslations(fp)
- x = t.ngettext('There is %s file', 'There are %s files', 1)
- eq(x, 'Hay %s fichero')
- x = t.ngettext('There is %s file', 'There are %s files', 2)
- eq(x, 'Hay %s ficheros')
- x = t.gettext('There is %s file')
- eq(x, 'Hay %s fichero')
-
- def test_plural_context_forms2(self):
- eq = self.assertEqual
- with open(self.mofile, 'rb') as fp:
+ self.gettext = gettext.gettext
+ self.ngettext = gettext.ngettext
+ self.pgettext = gettext.pgettext
+ self.npgettext = gettext.npgettext
+
+
+class GNUTranslationsWithDomainPluralFormsTestCase(PluralFormsTests, GettextBaseTest):
+ def setUp(self):
+ GettextBaseTest.setUp(self)
+ # Set up the bindings
+ gettext.bindtextdomain('gettext', os.curdir)
+
+ self.gettext = partial(gettext.dgettext, 'gettext')
+ self.ngettext = partial(gettext.dngettext, 'gettext')
+ self.pgettext = partial(gettext.dpgettext, 'gettext')
+ self.npgettext = partial(gettext.dnpgettext, 'gettext')
+
+ def test_plural_forms_wrong_domain(self):
+ self._test_plural_forms(
+ partial(gettext.dngettext, 'unknown'),
+ partial(gettext.dgettext, 'unknown'),
+ 'There is %s file', 'There are %s files',
+ 'There is %s file', 'There are %s files',
+ numbers_only=False)
+
+ def test_plural_context_forms_wrong_domain(self):
+ self._test_plural_forms(
+ partial(gettext.dnpgettext, 'unknown', 'With context'),
+ partial(gettext.dpgettext, 'unknown', 'With context'),
+ 'There is %s file', 'There are %s files',
+ 'There is %s file', 'There are %s files',
+ numbers_only=False)
+
+
+class GNUTranslationsClassPluralFormsTestCase(PluralFormsTests, GettextBaseTest):
+ def setUp(self):
+ GettextBaseTest.setUp(self)
+ with open(MOFILE, 'rb') as fp:
t = gettext.GNUTranslations(fp)
- x = t.npgettext('With context',
- 'There is %s file', 'There are %s files', 1)
- eq(x, 'Hay %s fichero (context)')
- x = t.npgettext('With context',
- 'There is %s file', 'There are %s files', 2)
- eq(x, 'Hay %s ficheros (context)')
- x = t.pgettext('With context', 'There is %s file')
- eq(x, 'Hay %s fichero (context)')
+ self.gettext = t.gettext
+ self.ngettext = t.ngettext
+ self.pgettext = t.pgettext
+ self.npgettext = t.npgettext
+
+ def test_plural_forms_null_translations(self):
+ t = gettext.NullTranslations()
+ self._test_plural_forms(
+ t.ngettext, t.gettext,
+ 'There is %s file', 'There are %s files',
+ 'There is %s file', 'There are %s files',
+ numbers_only=False)
+
+ def test_plural_context_forms_null_translations(self):
+ t = gettext.NullTranslations()
+ self._test_plural_forms(
+ partial(t.npgettext, 'With context'),
+ partial(t.pgettext, 'With context'),
+ 'There is %s file', 'There are %s files',
+ 'There is %s file', 'There are %s files',
+ numbers_only=False)
+
+
+class PluralFormsInternalTestCase:
# Examples from http://www.gnu.org/software/gettext/manual/gettext.html
def test_ja(self):