@@ -478,6 +478,8 @@ def set_locale(new_locale, lc_var=locale.LC_ALL):
478
478
A string of the form <language_country>.<encoding>. For example to set
479
479
the current locale to US English with a UTF8 encoding, you would pass
480
480
"en_US.UTF-8".
481
+ lc_var : int, default `locale.LC_ALL`
482
+ The category of the locale being set.
481
483
482
484
Notes
483
485
-----
@@ -489,37 +491,37 @@ def set_locale(new_locale, lc_var=locale.LC_ALL):
489
491
490
492
try :
491
493
locale .setlocale (lc_var , new_locale )
492
-
493
- try :
494
- normalized_locale = locale .getlocale ()
495
- except ValueError :
496
- yield new_locale
494
+ normalized_locale = locale .getlocale ()
495
+ if com ._all_not_none (* normalized_locale ):
496
+ yield '.' .join (normalized_locale )
497
497
else :
498
- if com ._all_not_none (* normalized_locale ):
499
- yield '.' .join (normalized_locale )
500
- else :
501
- yield new_locale
498
+ yield new_locale
502
499
finally :
503
500
locale .setlocale (lc_var , current_locale )
504
501
505
502
506
- def _can_set_locale (lc ):
507
- """Check to see if we can set a locale without throwing an exception.
503
+ def can_set_locale (lc , lc_var = locale .LC_ALL ):
504
+ """
505
+ Check to see if we can set a locale, and subsequently get the locale,
506
+ without raising an Exception.
508
507
509
508
Parameters
510
509
----------
511
510
lc : str
512
511
The locale to attempt to set.
512
+ lc_var : int, default `locale.LC_ALL`
513
+ The category of the locale being set.
513
514
514
515
Returns
515
516
-------
516
- isvalid : bool
517
+ is_valid : bool
517
518
Whether the passed locale can be set
518
519
"""
519
520
try :
520
- with set_locale (lc ):
521
+ with set_locale (lc , lc_var = lc_var ):
521
522
pass
522
- except locale .Error : # horrible name for a Exception subclass
523
+ except (ValueError ,
524
+ locale .Error ): # horrible name for a Exception subclass
523
525
return False
524
526
else :
525
527
return True
@@ -546,7 +548,7 @@ def _valid_locales(locales, normalize):
546
548
else :
547
549
normalizer = lambda x : x .strip ()
548
550
549
- return list (filter (_can_set_locale , map (normalizer , locales )))
551
+ return list (filter (can_set_locale , map (normalizer , locales )))
550
552
551
553
# -----------------------------------------------------------------------------
552
554
# Stdout / stderr decorators
0 commit comments