Skip to contentSkip to conversation

gettext: handle unsupported languages properly

PATCH In review Vit Mojzis 3 participants 6 messages Jun 24, 2022 – Jun 29, 2022 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

PATCH 1/1 gettext: handle unsupported languages properly

With "fallback=True" gettext.translation behaves the same as gettext.install and uses NullTranslations in case the translation file for given language was not found (as opposed to throwing an exception).

Fixes:
# LANG is set to any "unsupported" language, e.g. en_US.UTF-8

  $ chcat --help
  Traceback (most recent call last):
  File "/usr/bin/chcat", line 39, in <module>
    t = gettext.translation(PROGNAME,
  File "/usr/lib64/python3.9/gettext.py", line 592, in translation
    raise FileNotFoundError(ENOENT,
  FileNotFoundError: [Errno 2] No translation file found for domain: 'selinux-python'
---
 gui/booleansPage.py                          | 3 ++-
 gui/domainsPage.py                           | 3 ++-
 gui/fcontextPage.py                          | 3 ++-
 gui/loginsPage.py                            | 3 ++-
 gui/modulesPage.py                           | 3 ++-
 gui/polgengui.py                             | 3 ++-
 gui/portsPage.py                             | 3 ++-
 gui/semanagePage.py                          | 3 ++-
 gui/statusPage.py                            | 3 ++-
 gui/system-config-selinux.py                 | 3 ++-
 gui/usersPage.py                             | 3 ++-
 python/chcat/chcat                           | 5 +++--
 python/semanage/semanage                     | 3 ++-
 python/semanage/seobject.py                  | 3 ++-
 python/sepolgen/src/sepolgen/sepolgeni18n.py | 4 +++-
 python/sepolicy/sepolicy.py                  | 3 ++-
 python/sepolicy/sepolicy/__init__.py         | 3 ++-
 python/sepolicy/sepolicy/generate.py         | 3 ++-
 python/sepolicy/sepolicy/gui.py              | 3 ++-
 python/sepolicy/sepolicy/interface.py        | 3 ++-
 sandbox/sandbox                              | 3 ++-
 21 files changed, 44 insertions(+), 22 deletions(-)
gui/booleansPage.py +2 -1
index 5beec58b..ad11a9b2 100644
@@ -46,7 +46,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/domainsPage.py +2 -1
index e08f34b4..e6eadd61 100644
@@ -38,7 +38,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/fcontextPage.py +2 -1
index bac2bec3..767664f2 100644
@@ -55,7 +55,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/loginsPage.py +2 -1
index 18b93d8c..7e08232a 100644
@@ -37,7 +37,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/modulesPage.py +2 -1
index c546d455..02b79f15 100644
@@ -38,7 +38,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/polgengui.py +2 -1
index a18f1cba..7a3ecd50 100644
@@ -71,7 +71,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/portsPage.py +2 -1
index 54aa80de..bee2bdf1 100644
@@ -43,7 +43,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/semanagePage.py +2 -1
index 1371d4e7..efad14d9 100644
@@ -30,7 +30,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/statusPage.py +2 -1
index c241ef83..832849e6 100644
@@ -43,7 +43,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/system-config-selinux.py +2 -1
index 1b460c99..9f53b7fe 100644
@@ -53,7 +53,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
gui/usersPage.py +2 -1
index d51bd968..9acd3b84 100644
@@ -37,7 +37,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
python/chcat/chcat +3 -2
index e779fcc6..952cb818 100755
@@ -38,9 +38,10 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
-except ImportError:
+except:
     try:
         import builtins
         builtins.__dict__['_'] = str
python/semanage/semanage +2 -1
index 1d828128..4e8d64d6 100644
@@ -38,7 +38,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
python/semanage/seobject.py +2 -1
index ff8f4e9c..0782c082 100644
@@ -42,7 +42,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
python/sepolgen/src/sepolgen/sepolgeni18n.py +3 -1
index 56ebd807..1ff307d9 100644
@@ -19,7 +19,9 @@
 
 try: 
     import gettext
-    t = gettext.translation( 'selinux-python' )
+    t = gettext.translation("selinux-python",
+                        localedir="/usr/share/locale",
+                        fallback=True)
     _ = t.gettext
 except:
     def _(str):
python/sepolicy/sepolicy.py +2 -1
index 7ebe0efa..c7a70e09 100755
@@ -36,7 +36,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
python/sepolicy/sepolicy/__init__.py +2 -1
index 7208234b..9c3caa05 100644
@@ -31,7 +31,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
python/sepolicy/sepolicy/generate.py +2 -1
index 67189fc3..3717d5d4 100644
@@ -56,7 +56,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
python/sepolicy/sepolicy/gui.py +2 -1
index b0263740..5bdbfeba 100644
@@ -49,7 +49,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
python/sepolicy/sepolicy/interface.py +2 -1
index 599f97fd..43f86443 100644
@@ -38,7 +38,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
sandbox/sandbox +2 -1
index cd5709fb..789621e1 100644
@@ -45,7 +45,8 @@ try:
         kwargs['unicode'] = True
     t = gettext.translation(PROGNAME,
                     localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
     _ = t.gettext
 except:
     try:
Signature · 1 line
2.35.3
#1 lore raw
Quoted 199 lines · Vit Mojzis

With "fallback=True" gettext.translation behaves the same as gettext.install and uses NullTranslations in case the translation file for given language was not found (as opposed to throwing an exception).

Fixes:
# LANG is set to any "unsupported" language, e.g. en_US.UTF-8

   $ chcat --help
   Traceback (most recent call last):
   File "/usr/bin/chcat", line 39, in <module>
     t = gettext.translation(PROGNAME,
   File "/usr/lib64/python3.9/gettext.py", line 592, in translation
     raise FileNotFoundError(ENOENT,
   FileNotFoundError: [Errno 2] No translation file found for domain: 'selinux-python'
---
  gui/booleansPage.py                          | 3 ++-
  gui/domainsPage.py                           | 3 ++-
  gui/fcontextPage.py                          | 3 ++-
  gui/loginsPage.py                            | 3 ++-
  gui/modulesPage.py                           | 3 ++-
  gui/polgengui.py                             | 3 ++-
  gui/portsPage.py                             | 3 ++-
  gui/semanagePage.py                          | 3 ++-
  gui/statusPage.py                            | 3 ++-
  gui/system-config-selinux.py                 | 3 ++-
  gui/usersPage.py                             | 3 ++-
  python/chcat/chcat                           | 5 +++--
  python/semanage/semanage                     | 3 ++-
  python/semanage/seobject.py                  | 3 ++-
  python/sepolgen/src/sepolgen/sepolgeni18n.py | 4 +++-
  python/sepolicy/sepolicy.py                  | 3 ++-
  python/sepolicy/sepolicy/__init__.py         | 3 ++-
  python/sepolicy/sepolicy/generate.py         | 3 ++-
  python/sepolicy/sepolicy/gui.py              | 3 ++-
  python/sepolicy/sepolicy/interface.py        | 3 ++-
  sandbox/sandbox                              | 3 ++-
  21 files changed, 44 insertions(+), 22 deletions(-)
gui/booleansPage.py +2 -1
index 5beec58b..ad11a9b2 100644
@@ -46,7 +46,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/domainsPage.py +2 -1
index e08f34b4..e6eadd61 100644
@@ -38,7 +38,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/fcontextPage.py +2 -1
index bac2bec3..767664f2 100644
@@ -55,7 +55,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/loginsPage.py +2 -1
index 18b93d8c..7e08232a 100644
@@ -37,7 +37,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/modulesPage.py +2 -1
index c546d455..02b79f15 100644
@@ -38,7 +38,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/polgengui.py +2 -1
index a18f1cba..7a3ecd50 100644
@@ -71,7 +71,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/portsPage.py +2 -1
index 54aa80de..bee2bdf1 100644
@@ -43,7 +43,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/semanagePage.py +2 -1
index 1371d4e7..efad14d9 100644
@@ -30,7 +30,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/statusPage.py +2 -1
index c241ef83..832849e6 100644
@@ -43,7 +43,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/system-config-selinux.py +2 -1
index 1b460c99..9f53b7fe 100644
@@ -53,7 +53,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
gui/usersPage.py +2 -1
index d51bd968..9acd3b84 100644
@@ -37,7 +37,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
python/chcat/chcat +0 -0
index e779fcc6..952cb818 100755

python/chcat/chcat try: · line 44

Quoted 9 lines
python/chcat/chcat +3 -2
@@ -38,9 +38,10 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
-except ImportError:
+except:

Isn't the point of the overall change that gettext.translation() doesn't throw an exception anymore? So we don't need to handle OSError/IOError here once fallback=True. I see that this standardizes with the other call sites, but I wonder if standardizing on the more specific exceptions (or just leaving as is) wouldn't be better?

-Daniel

Quoted 130 lines
python/chcat/chcat +0 -0
      try:
          import builtins
          builtins.__dict__['_'] = str
python/semanage/semanage +2 -1
index 1d828128..4e8d64d6 100644
@@ -38,7 +38,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
python/semanage/seobject.py +2 -1
index ff8f4e9c..0782c082 100644
@@ -42,7 +42,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
python/sepolgen/src/sepolgen/sepolgeni18n.py +3 -1
index 56ebd807..1ff307d9 100644
@@ -19,7 +19,9 @@
 
  try:
      import gettext
-    t = gettext.translation( 'selinux-python' )
+    t = gettext.translation("selinux-python",
+                        localedir="/usr/share/locale",
+                        fallback=True)
      _ = t.gettext
  except:
      def _(str):
python/sepolicy/sepolicy.py +2 -1
index 7ebe0efa..c7a70e09 100755
@@ -36,7 +36,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
python/sepolicy/sepolicy/__init__.py +2 -1
index 7208234b..9c3caa05 100644
@@ -31,7 +31,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
python/sepolicy/sepolicy/generate.py +2 -1
index 67189fc3..3717d5d4 100644
@@ -56,7 +56,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
python/sepolicy/sepolicy/gui.py +2 -1
index b0263740..5bdbfeba 100644
@@ -49,7 +49,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
python/sepolicy/sepolicy/interface.py +2 -1
index 599f97fd..43f86443 100644
@@ -38,7 +38,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
sandbox/sandbox +2 -1
index cd5709fb..789621e1 100644
@@ -45,7 +45,8 @@ try:
          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME,
                      localedir="/usr/share/locale",
-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:
author
#2 lore raw
Quoted 203 lines · Daniel Burgener
Quoted 202 lines · Vit Mojzis

With "fallback=True" gettext.translation behaves the same as gettext.install and uses NullTranslations in case the translation file for given language was not found (as opposed to throwing an exception).

Fixes:
   # LANG is set to any "unsupported" language, e.g. en_US.UTF-8

   $ chcat --help
   Traceback (most recent call last):
   File "/usr/bin/chcat", line 39, in <module>
     t = gettext.translation(PROGNAME,
   File "/usr/lib64/python3.9/gettext.py", line 592, in translation
     raise FileNotFoundError(ENOENT,
   FileNotFoundError: [Errno 2] No translation file found for domain: 'selinux-python'

---
  gui/booleansPage.py                          | 3 ++- gui/domainsPage.py                           | 3 ++- gui/fcontextPage.py                          | 3 ++- gui/loginsPage.py                            | 3 ++- gui/modulesPage.py                           | 3 ++- gui/polgengui.py                             | 3 ++- gui/portsPage.py                             | 3 ++- gui/semanagePage.py                          | 3 ++- gui/statusPage.py                            | 3 ++- gui/system-config-selinux.py                 | 3 ++- gui/usersPage.py                             | 3 ++- python/chcat/chcat                           | 5 +++-- python/semanage/semanage                     | 3 ++- python/semanage/seobject.py                  | 3 ++- python/sepolgen/src/sepolgen/sepolgeni18n.py | 4 +++- python/sepolicy/sepolicy.py                  | 3 ++- python/sepolicy/sepolicy/__init__.py         | 3 ++- python/sepolicy/sepolicy/generate.py         | 3 ++- python/sepolicy/sepolicy/gui.py              | 3 ++- python/sepolicy/sepolicy/interface.py        | 3 ++- sandbox/sandbox                              | 3 ++-

  21 files changed, 44 insertions(+), 22 deletions(-)
gui/booleansPage.py +0 -0
index 5beec58b..ad11a9b2 100644
@@ -46,7 +46,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/domainsPage.py +0 -0
index e08f34b4..e6eadd61 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/fcontextPage.py +0 -0
index bac2bec3..767664f2 100644
@@ -55,7 +55,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/loginsPage.py +0 -0
index 18b93d8c..7e08232a 100644
@@ -37,7 +37,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/modulesPage.py +0 -0
index c546d455..02b79f15 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/polgengui.py +0 -0
index a18f1cba..7a3ecd50 100644
@@ -71,7 +71,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/portsPage.py +0 -0
index 54aa80de..bee2bdf1 100644
@@ -43,7 +43,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/semanagePage.py +0 -0
index 1371d4e7..efad14d9 100644
@@ -30,7 +30,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/statusPage.py +0 -0
index c241ef83..832849e6 100644
@@ -43,7 +43,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/system-config-selinux.py +0 -0
index 1b460c99..9f53b7fe 100644
@@ -53,7 +53,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/usersPage.py +0 -0
index d51bd968..9acd3b84 100644
@@ -37,7 +37,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/chcat/chcat +0 -0
index e779fcc6..952cb818 100755
@@ -38,9 +38,10 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale",

Quoted 8 lines

python/chcat/chcat try: · line 44

Quoted 6 lines

-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext

python/chcat/chcat +1 -1
-except ImportError:
+except:

Isn't the point of the overall change that gettext.translation() doesn't throw an exception anymore?  So we don't need to handle OSError/IOError here once fallback=True.  I see that this standardizes with the other call sites, but I wonder if standardizing on the more specific exceptions (or just leaving as is) wouldn't be better?

Yes, we do not need to handle OSError/IOError exceptions any more.
I agree that standardizing on the more specific exception handling would be more proper. However, translation handling is probably the least important feature of this tool (most people probably use it in English and those who don't wish they did) -- we don't want people to be unable to use the tool at all because of some translation issue and "catch all" exception handling is the easiest way to avoid that. As this bug showed, not settling on a single approach is probably the worst alternative (I tested the previous patch with multiple tools, but apparently missed the one that was different).

That being said, either approach is fine by me. Please let me know if I should change the patch. Vit

Quoted 133 lines

-Daniel

Quoted 129 lines

      try:
          import builtins
          builtins.__dict__['_'] = str

python/semanage/semanage +0 -0
index 1d828128..4e8d64d6 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/semanage/seobject.py +0 -0
index ff8f4e9c..0782c082 100644
@@ -42,7 +42,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolgen/src/sepolgen/sepolgeni18n.py +0 -0
index 56ebd807..1ff307d9 100644
@@ -19,7 +19,9 @@

    try:
      import gettext
-    t = gettext.translation( 'selinux-python' ) +    t = gettext.translation("selinux-python", +                        localedir="/usr/share/locale", +                        fallback=True)
      _ = t.gettext
  except:
      def _(str):

python/sepolicy/sepolicy.py +0 -0
index 7ebe0efa..c7a70e09 100755
@@ -36,7 +36,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/__init__.py +0 -0
index 7208234b..9c3caa05 100644
@@ -31,7 +31,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/generate.py +0 -0
index 67189fc3..3717d5d4 100644
@@ -56,7 +56,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/gui.py +0 -0
index b0263740..5bdbfeba 100644
@@ -49,7 +49,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/interface.py +0 -0
index 599f97fd..43f86443 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

sandbox/sandbox +0 -0
index cd5709fb..789621e1 100644
@@ -45,7 +45,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

Reviewed-by
#3 lore raw
Quoted 215 lines · Vit Mojzis
Quoted 203 lines · Daniel Burgener
Quoted 202 lines · Vit Mojzis

With "fallback=True" gettext.translation behaves the same as gettext.install and uses NullTranslations in case the translation file for given language was not found (as opposed to throwing an exception).

Fixes:
   # LANG is set to any "unsupported" language, e.g. en_US.UTF-8

   $ chcat --help
   Traceback (most recent call last):
   File "/usr/bin/chcat", line 39, in <module>
     t = gettext.translation(PROGNAME,
   File "/usr/lib64/python3.9/gettext.py", line 592, in translation
     raise FileNotFoundError(ENOENT,
   FileNotFoundError: [Errno 2] No translation file found for domain: 'selinux-python'

---
  gui/booleansPage.py                          | 3 ++- gui/domainsPage.py                           | 3 ++- gui/fcontextPage.py                          | 3 ++- gui/loginsPage.py                            | 3 ++- gui/modulesPage.py                           | 3 ++- gui/polgengui.py                             | 3 ++- gui/portsPage.py                             | 3 ++- gui/semanagePage.py                          | 3 ++- gui/statusPage.py                            | 3 ++- gui/system-config-selinux.py                 | 3 ++- gui/usersPage.py                             | 3 ++- python/chcat/chcat                           | 5 +++-- python/semanage/semanage                     | 3 ++- python/semanage/seobject.py                  | 3 ++- python/sepolgen/src/sepolgen/sepolgeni18n.py | 4 +++- python/sepolicy/sepolicy.py                  | 3 ++- python/sepolicy/sepolicy/__init__.py         | 3 ++- python/sepolicy/sepolicy/generate.py         | 3 ++- python/sepolicy/sepolicy/gui.py              | 3 ++- python/sepolicy/sepolicy/interface.py        | 3 ++- sandbox/sandbox                              | 3 ++-

  21 files changed, 44 insertions(+), 22 deletions(-)
gui/booleansPage.py +0 -0
index 5beec58b..ad11a9b2 100644
@@ -46,7 +46,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/domainsPage.py +0 -0
index e08f34b4..e6eadd61 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/fcontextPage.py +0 -0
index bac2bec3..767664f2 100644
@@ -55,7 +55,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/loginsPage.py +0 -0
index 18b93d8c..7e08232a 100644
@@ -37,7 +37,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/modulesPage.py +0 -0
index c546d455..02b79f15 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/polgengui.py +0 -0
index a18f1cba..7a3ecd50 100644
@@ -71,7 +71,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/portsPage.py +0 -0
index 54aa80de..bee2bdf1 100644
@@ -43,7 +43,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/semanagePage.py +0 -0
index 1371d4e7..efad14d9 100644
@@ -30,7 +30,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/statusPage.py +0 -0
index c241ef83..832849e6 100644
@@ -43,7 +43,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/system-config-selinux.py +0 -0
index 1b460c99..9f53b7fe 100644
@@ -53,7 +53,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/usersPage.py +0 -0
index d51bd968..9acd3b84 100644
@@ -37,7 +37,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/chcat/chcat +0 -0
index e779fcc6..952cb818 100755
@@ -38,9 +38,10 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale",

Quoted 8 lines

python/chcat/chcat try: · line 44

Quoted 6 lines

-                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext

python/chcat/chcat +1 -1
-except ImportError:
+except:

Isn't the point of the overall change that gettext.translation() doesn't throw an exception anymore?  So we don't need to handle OSError/IOError here once fallback=True.  I see that this standardizes with the other call sites, but I wonder if standardizing on the more specific exceptions (or just leaving as is) wouldn't be better?

Quoted 6 lines

Yes, we do not need to handle OSError/IOError exceptions any more.
I agree that standardizing on the more specific exception handling would be more proper. However, translation handling is probably the least important feature of this tool (most people probably use it in English and those who don't wish they did) -- we don't want people to be unable to use the tool at all because of some translation issue and "catch all" exception handling is the easiest way to avoid that. As this bug showed, not settling on a single approach is probably the worst alternative (I tested the previous patch with multiple tools, but apparently missed the one that was different).

That being said, either approach is fine by me. Please let me know if I should change the patch. Vit

Well, I'll leave questions of what you *should* do up to the maintainers, but I think as far as I'm concerned your point that we really don't want to fail hard on translations makes a lot of sense (and I definitely agree that inconsistency is a pretty bad alternative. So I'm on board with this approach. I was initially worried about "what if in some future version gettext adds an exception?", but to your point, we want the same fallback logic in that case, so the general except seems reasonable.

Quoted 134 lines
Quoted 134 lines

-Daniel

Quoted 129 lines

      try:
          import builtins
          builtins.__dict__['_'] = str

python/semanage/semanage +0 -0
index 1d828128..4e8d64d6 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/semanage/seobject.py +0 -0
index ff8f4e9c..0782c082 100644
@@ -42,7 +42,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolgen/src/sepolgen/sepolgeni18n.py +0 -0
index 56ebd807..1ff307d9 100644
@@ -19,7 +19,9 @@

    try:
      import gettext
-    t = gettext.translation( 'selinux-python' ) +    t = gettext.translation("selinux-python", +                        localedir="/usr/share/locale", +                        fallback=True)
      _ = t.gettext
  except:
      def _(str):

python/sepolicy/sepolicy.py +0 -0
index 7ebe0efa..c7a70e09 100755
@@ -36,7 +36,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/__init__.py +0 -0
index 7208234b..9c3caa05 100644
@@ -31,7 +31,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/generate.py +0 -0
index 67189fc3..3717d5d4 100644
@@ -56,7 +56,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/gui.py +0 -0
index b0263740..5bdbfeba 100644
@@ -49,7 +49,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/interface.py +0 -0
index 599f97fd..43f86443 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

sandbox/sandbox +0 -0
index cd5709fb..789621e1 100644
@@ -45,7 +45,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

#4 lore raw
Quoted 245 lines · Daniel Burgener
Quoted 231 lines · Vit Mojzis
Quoted 208 lines · Daniel Burgener
Quoted 207 lines · Vit Mojzis

With "fallback=True" gettext.translation behaves the same as gettext.install and uses NullTranslations in case the translation file for given language was not found (as opposed to throwing an exception).

Fixes:
   # LANG is set to any "unsupported" language, e.g. en_US.UTF-8

   $ chcat --help
   Traceback (most recent call last):
   File "/usr/bin/chcat", line 39, in <module>
     t = gettext.translation(PROGNAME,
   File "/usr/lib64/python3.9/gettext.py", line 592, in translation
     raise FileNotFoundError(ENOENT,
   FileNotFoundError: [Errno 2] No translation file found for domain: 
'selinux-python'

---
  gui/booleansPage.py                          | 3 ++- gui/domainsPage.py                           | 3 ++- gui/fcontextPage.py                          | 3 ++- gui/loginsPage.py                            | 3 ++- gui/modulesPage.py                           | 3 ++- gui/polgengui.py                             | 3 ++- gui/portsPage.py                             | 3 ++- gui/semanagePage.py                          | 3 ++- gui/statusPage.py                            | 3 ++- gui/system-config-selinux.py                 | 3 ++- gui/usersPage.py                             | 3 ++- python/chcat/chcat                           | 5 +++-- python/semanage/semanage                     | 3 ++- python/semanage/seobject.py                  | 3 ++- python/sepolgen/src/sepolgen/sepolgeni18n.py | 4 +++- python/sepolicy/sepolicy.py                  | 3 ++- python/sepolicy/sepolicy/__init__.py         | 3 ++- python/sepolicy/sepolicy/generate.py         | 3 ++- python/sepolicy/sepolicy/gui.py              | 3 ++- python/sepolicy/sepolicy/interface.py        | 3 ++- sandbox/sandbox                              | 3 ++-

  21 files changed, 44 insertions(+), 22 deletions(-)
gui/booleansPage.py +0 -0
index 5beec58b..ad11a9b2 100644
@@ -46,7 +46,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/domainsPage.py +0 -0
index e08f34b4..e6eadd61 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/fcontextPage.py +0 -0
index bac2bec3..767664f2 100644
@@ -55,7 +55,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/loginsPage.py +0 -0
index 18b93d8c..7e08232a 100644
@@ -37,7 +37,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/modulesPage.py +0 -0
index c546d455..02b79f15 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/polgengui.py +0 -0
index a18f1cba..7a3ecd50 100644
@@ -71,7 +71,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/portsPage.py +0 -0
index 54aa80de..bee2bdf1 100644
@@ -43,7 +43,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/semanagePage.py +0 -0
index 1371d4e7..efad14d9 100644
@@ -30,7 +30,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/statusPage.py +0 -0
index c241ef83..832849e6 100644
@@ -43,7 +43,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/system-config-selinux.py +0 -0
index 1b460c99..9f53b7fe 100644
@@ -53,7 +53,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

gui/usersPage.py +0 -0
index d51bd968..9acd3b84 100644
@@ -37,7 +37,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/chcat/chcat +0 -0
index e779fcc6..952cb818 100755
@@ -38,9 +38,10 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext

Quoted 8 lines

python/chcat/chcat try: · line 44

Quoted 2 lines
python/chcat/chcat +1 -1
-except ImportError:
+except:

Isn't the point of the overall change that gettext.translation() doesn't throw an exception anymore?  So we don't need to handle OSError/IOError here once fallback=True.  I see that this standardizes with the other call sites, but I wonder if standardizing on the more specific exceptions (or just leaving as is) wouldn't be better?

Yes, we do not need to handle OSError/IOError exceptions any more. I agree that standardizing on the more specific exception handling would be more proper. However, translation handling is probably the least important feature of this tool (most people probably use it in English and those who don't wish they did) -- we don't want people to be unable to use the tool at all because of some translation issue and "catch all" exception handling is the easiest way to avoid that.
As this bug showed, not settling on a single approach is probably the worst alternative (I tested the previous patch with multiple tools, but apparently missed the one that was different).

Quoted 3 lines

That being said, either approach is fine by me. Please let me know if I should change the patch.
Vit

Well, I'll leave questions of what you *should* do up to the maintainers, but I think as far as I'm concerned your point that we really don't want to fail hard on translations makes a lot of sense (and I definitely agree that inconsistency is a pretty bad alternative. So I'm on board with this approach. I was initially worried about "what if in some future version gettext adds an exception?", but to your point, we want the same fallback logic in that case, so the general except seems reasonable.

Quoted 2 lines

Thanks!

Quoted 139 lines
Quoted 139 lines
Quoted 139 lines

-Daniel

Quoted 134 lines

      try:
          import builtins
          builtins.__dict__['_'] = str

python/semanage/semanage +0 -0
index 1d828128..4e8d64d6 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/semanage/seobject.py +0 -0
index ff8f4e9c..0782c082 100644
@@ -42,7 +42,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolgen/src/sepolgen/sepolgeni18n.py +0 -0
index 56ebd807..1ff307d9 100644
@@ -19,7 +19,9 @@

    try:
      import gettext
-    t = gettext.translation( 'selinux-python' ) +    t = gettext.translation("selinux-python", +                        localedir="/usr/share/locale", +                        fallback=True)
      _ = t.gettext
  except:
      def _(str):

python/sepolicy/sepolicy.py +0 -0
index 7ebe0efa..c7a70e09 100755
@@ -36,7 +36,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/__init__.py +0 -0
index 7208234b..9c3caa05 100644
@@ -31,7 +31,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/generate.py +0 -0
index 67189fc3..3717d5d4 100644
@@ -56,7 +56,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/gui.py +0 -0
index b0263740..5bdbfeba 100644
@@ -49,7 +49,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

python/sepolicy/sepolicy/interface.py +0 -0
index 599f97fd..43f86443 100644
@@ -38,7 +38,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

sandbox/sandbox +0 -0
index cd5709fb..789621e1 100644
@@ -45,7 +45,8 @@ try:

          kwargs['unicode'] = True
      t = gettext.translation(PROGNAME, localedir="/usr/share/locale", -                    **kwargs)
+                    **kwargs,
+                    fallback=True)
      _ = t.gettext
  except:
      try:

#5 lore raw
Quoted 25 lines · Petr Lautrbach
Quoted 23 lines · Daniel Burgener
Quoted 22 lines · Vit Mojzis
Quoted 19 lines · Daniel Burgener
Quoted 18 lines · Vit Mojzis

With "fallback=True" gettext.translation behaves the same as gettext.install and uses NullTranslations in case the translation file for given language was not found (as opposed to throwing an exception).

Fixes:
   # LANG is set to any "unsupported" language, e.g. en_US.UTF-8

   $ chcat --help
   Traceback (most recent call last):
   File "/usr/bin/chcat", line 39, in <module>
     t = gettext.translation(PROGNAME,
   File "/usr/lib64/python3.9/gettext.py", line 592, in translation
     raise FileNotFoundError(ENOENT,
   FileNotFoundError: [Errno 2] No translation file found for domain: 
'selinux-python'

---

[...]

Quoted 31 lines
Quoted 31 lines
Quoted 18 lines
Quoted 6 lines

Isn't the point of the overall change that gettext.translation() doesn't throw an exception anymore?  So we don't need to handle OSError/IOError here once fallback=True.  I see that this standardizes with the other call sites, but I wonder if standardizing on the more specific exceptions (or just leaving as is) wouldn't be better?

Yes, we do not need to handle OSError/IOError exceptions any more. I agree that standardizing on the more specific exception handling would be more proper. However, translation handling is probably the least important feature of this tool (most people probably use it in English and those who don't wish they did) -- we don't want people to be unable to use the tool at all because of some translation issue and "catch all" exception handling is the easiest way to avoid that.
As this bug showed, not settling on a single approach is probably the worst alternative (I tested the previous patch with multiple tools, but apparently missed the one that was different).

Quoted 3 lines

That being said, either approach is fine by me. Please let me know if I should change the patch.
Vit

Well, I'll leave questions of what you *should* do up to the maintainers, but I think as far as I'm concerned your point that we really don't want to fail hard on translations makes a lot of sense (and I definitely agree that inconsistency is a pretty bad alternative. So I'm on board with this approach. I was initially worried about "what if in some future version gettext adds an exception?", but to your point, we want the same fallback logic in that case, so the general except seems reasonable.

Quoted 8 lines
Quoted 2 lines

Thanks!

Merged.