Locale('de', territory='DE')
If the `identifier` parameter is neither of these, such as `None`
- e.g. because a default locale identifier could not be determined,
- a `TypeError` is raised:
+ or an empty string, e.g. because a default locale identifier
+ could not be determined, a `TypeError` is raised:
>>> Locale.parse(None)
Traceback (most recent call last):
:raise `UnknownLocaleError`: if no locale data is available for the
requested locale
:raise `TypeError`: if the identifier is not a string or a `Locale`
+ :raise `ValueError`: if the identifier is not a valid string
"""
if isinstance(identifier, Locale):
return identifier
- elif not isinstance(identifier, str):
+
+ if not identifier:
+ msg = (
+ f"Empty locale identifier value: {identifier!r}\n\n"
+ f"If you didn't explicitly pass an empty value to a Babel function, "
+ f"this could be caused by there being no suitable locale environment "
+ f"variables for the API you tried to use.",
+ )
+ if isinstance(identifier, str):
+ raise ValueError(msg) # `parse_locale` would raise a ValueError, so let's do that here
+ raise TypeError(msg)
+
+ if not isinstance(identifier, str):
raise TypeError(f"Unexpected value for identifier: {identifier!r}")
parts = parse_locale(identifier, sep=sep)
:raise `ValueError`: if the string does not appear to be a valid locale
identifier
"""
+ if not identifier:
+ raise ValueError("empty locale identifier")
identifier, _, modifier = identifier.partition('@')
if '.' in identifier:
# this is probably the charset/encoding, which we don't care about
assert (core.parse_locale('de_DE.iso885915@euro') ==
('de', 'DE', None, None, 'euro'))
+ with pytest.raises(ValueError, match="empty"):
+ core.parse_locale("")
+
@pytest.mark.parametrize('filename', [
'babel/global.dat',
locale = Locale('mus')
assert locale.get_display_name() == 'Mvskoke'
assert locale.get_display_name(Locale('en')) == 'Muscogee'
+
+
+def test_locale_parse_empty():
+ with pytest.raises(ValueError, match="Empty"):
+ Locale.parse("")
+ with pytest.raises(TypeError, match="Empty"):
+ Locale.parse(None)
+ with pytest.raises(TypeError, match="Empty"):
+ Locale.parse(False) # weird...!
assert numbers.format_currency(0, "USD", locale=None) == "0,00\xa0$"
+def test_format_currency_with_none_locale(monkeypatch):
+ """Test that the API raises the "Empty locale identifier" error when locale is None, and the default is too."""
+ monkeypatch.setattr(numbers, "LC_NUMERIC", None) # Pretend we couldn't find any locale when importing the module
+ with pytest.raises(TypeError, match="Empty"):
+ numbers.format_currency(0, "USD", locale=None)
+
+
def test_format_currency_format_type():
assert (numbers.format_currency(1099.98, 'USD', locale='en_US',
format_type="standard")