Skip to content

Commit

Permalink
Fix c_locale context manager
Browse files Browse the repository at this point in the history
The context can raise an exception and in that case we would not
properly reset the locale.

Issue #2581

Signed-off-by: Michal Čihař <[email protected]>
  • Loading branch information
nijel committed Mar 1, 2019
1 parent 72888b5 commit 2a7f55f
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions weblate/utils/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,31 @@
@contextmanager
def c_locale():
"""Context to execute something in C locale."""
# List of locales to reset
locales = [
('C', 'UTF-8'),
('en_US', 'UTF-8'),
'',
]
try:
# If locale is set, insert it to the top
currlocale = getlocale()
print('Orig locale {}'.format(currlocale))
if currlocale[0]:
locales.insert(0, currlocale)
except Error:
pass
# Set C locale for the execution
setlocale(LC_ALL, 'C')
print('Locale reset to C')
yield
for currlocale in locales:
try:
print('SET {} -> {}'.format(currlocale, setlocale(LC_ALL, currlocale)))
except Error:
continue
if getlocale()[0]:
print('Reverted locale to {}'.format(getlocale()))
break
try:
# Here the context gets executed
yield
finally:
for currlocale in locales:
try:
setlocale(LC_ALL, currlocale)
except Error:
continue
# If getlocale returns None, the locale is most
# likely not working properly
if getlocale()[0]:
break

0 comments on commit 2a7f55f

Please sign in to comment.