Index: Lib/gettext.py =================================================================== --- Lib/gettext.py (revision 87951) +++ Lib/gettext.py (working copy) @@ -48,17 +48,19 @@ import locale, copy, io, os, re, struct, sys from errno import ENOENT +from warnings import warn as _warn __all__ = ['NullTranslations', 'GNUTranslations', 'Catalog', 'find', 'translation', 'install', 'textdomain', 'bindtextdomain', - 'dgettext', 'dngettext', 'gettext', 'ngettext', + 'bind_textdomain_codeset', 'dgettext', 'ldgettext', 'dngettext', + 'ldngettext', 'gettext', 'lgettext', 'ngettext', 'lngettext', ] _default_localedir = os.path.join(sys.prefix, 'share', 'locale') -def c2py(plural): +def _c2py(plural): """Gets a C expression as used in PO files for plural forms and returns a Python lambda function that implements an equivalent expression. """ @@ -106,8 +108,6 @@ return eval('lambda n: int(%s)' % plural) - - def _expand_lang(loc): loc = locale.normalize(loc) COMPONENT_CODESET = 1 << 0 @@ -148,8 +148,6 @@ ret.reverse() return ret - - class NullTranslations: def __init__(self, fp=None): self._info = {} @@ -278,7 +276,7 @@ elif k == 'plural-forms': v = v.split(';') plural = v[1].split('plural=')[1] - self.plural = c2py(plural) + self.plural = _c2py(plural) # Note: we unconditionally convert both msgids and msgstrs to # Unicode using the character encoding specified in the charset # parameter of the Content-Type header. The gettext documentation @@ -385,8 +383,6 @@ return mofile return result - - # a mapping between absolute .mo file path and Translation object _translations = {} @@ -420,13 +416,10 @@ result.add_fallback(t) return result - def install(domain, localedir=None, codeset=None, names=None): t = translation(domain, localedir, fallback=True, codeset=codeset) t.install(names) - - # a mapping b/w domains and locale directories _localedirs = {} # a mapping b/w domains and codesets @@ -434,28 +427,24 @@ # current global domain, `messages' used for compatibility w/ GNU gettext _current_domain = 'messages' - def textdomain(domain=None): global _current_domain if domain is not None: _current_domain = domain return _current_domain - def bindtextdomain(domain, localedir=None): global _localedirs if localedir is not None: _localedirs[domain] = localedir return _localedirs.get(domain, _default_localedir) - def bind_textdomain_codeset(domain, codeset=None): global _localecodesets if codeset is not None: _localecodesets[domain] = codeset return _localecodesets.get(domain) - def dgettext(domain, message): try: t = translation(domain, _localedirs.get(domain, None), @@ -521,3 +510,8 @@ # gettext. Catalog = translation + +def c2py(pl): + _warn("The gettext.c2py() function is deprecated", + DeprecationWarning, 2) + return _no_underscore(pl) Index: Lib/test/test_gettext.py =================================================================== --- Lib/test/test_gettext.py (revision 87951) +++ Lib/test/test_gettext.py (working copy) @@ -232,63 +232,63 @@ def test_hu(self): eq = self.assertEqual - f = gettext.c2py('0') + f = gettext._c2py('0') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") def test_de(self): eq = self.assertEqual - f = gettext.c2py('n != 1') + f = gettext._c2py('n != 1') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "10111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111") def test_fr(self): eq = self.assertEqual - f = gettext.c2py('n>1') + f = gettext._c2py('n>1') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "00111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111") def test_gd(self): eq = self.assertEqual - f = gettext.c2py('n==1 ? 0 : n==2 ? 1 : 2') + f = gettext._c2py('n==1 ? 0 : n==2 ? 1 : 2') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222") def test_gd2(self): eq = self.assertEqual # Tests the combination of parentheses and "?:" - f = gettext.c2py('n==1 ? 0 : (n==2 ? 1 : 2)') + f = gettext._c2py('n==1 ? 0 : (n==2 ? 1 : 2)') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "20122222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222") def test_lt(self): eq = self.assertEqual - f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2') + f = gettext._c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && (n%100<10 || n%100>=20) ? 1 : 2') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "20111111112222222222201111111120111111112011111111201111111120111111112011111111201111111120111111112011111111222222222220111111112011111111201111111120111111112011111111201111111120111111112011111111") def test_ru(self): eq = self.assertEqual - f = gettext.c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2') + f = gettext._c2py('n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "20111222222222222222201112222220111222222011122222201112222220111222222011122222201112222220111222222011122222222222222220111222222011122222201112222220111222222011122222201112222220111222222011122222") def test_pl(self): eq = self.assertEqual - f = gettext.c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2') + f = gettext._c2py('n==1 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "20111222222222222222221112222222111222222211122222221112222222111222222211122222221112222222111222222211122222222222222222111222222211122222221112222222111222222211122222221112222222111222222211122222") def test_sl(self): eq = self.assertEqual - f = gettext.c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3') + f = gettext._c2py('n%100==1 ? 0 : n%100==2 ? 1 : n%100==3 || n%100==4 ? 2 : 3') s = ''.join([ str(f(x)) for x in range(200) ]) eq(s, "30122333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333012233333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333") def test_security(self): raises = self.assertRaises # Test for a dangerous expression - raises(ValueError, gettext.c2py, "os.chmod('/etc/passwd',0777)") + raises(ValueError, gettext._c2py, "os.chmod('/etc/passwd',0777)") class UnicodeTranslationsTest(GettextBaseTest):