bpo-10379: add 'monetary' to format_string, deprecate format
Add the 'monetary' parameter to format_string so that all
uses of format can be converted to format_string. Adjust
the documentation accordingly, and add a deprecation
warning when format is used.
diff --git a/Lib/locale.py b/Lib/locale.py
index f8d1d78..50cd652 100644
--- a/Lib/locale.py
+++ b/Lib/locale.py
@@ -17,6 +17,7 @@
import collections
from builtins import str as _builtin_str
import functools
+import warnings
# Try importing the _locale module.
#
@@ -180,19 +181,6 @@
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')
-def format(percent, value, grouping=False, monetary=False, *additional):
- """Returns the locale-aware substitution of a %? specifier
- (percent).
-
- additional is for format strings which contain one or more
- '*' modifiers."""
- # this is only for one-percent-specifier strings and this should be checked
- match = _percent_re.match(percent)
- if not match or len(match.group())!= len(percent):
- raise ValueError(("format() must be given exactly one %%char "
- "format specifier, %s not valid") % repr(percent))
- return _format(percent, value, grouping, monetary, *additional)
-
def _format(percent, value, grouping=False, monetary=False, *additional):
if additional:
formatted = percent % ((value,) + additional)
@@ -217,10 +205,13 @@
formatted = _strip_padding(formatted, seps)
return formatted
-def format_string(f, val, grouping=False):
+def format_string(f, val, grouping=False, monetary=False):
"""Formats a string in the same way that the % formatting would use,
but takes the current locale into account.
- Grouping is applied if the third parameter is true."""
+
+ Grouping is applied if the third parameter is true.
+ Conversion uses monetary thousands separator and grouping strings if
+ forth parameter monetary is true."""
percents = list(_percent_re.finditer(f))
new_f = _percent_re.sub('%s', f)
@@ -230,7 +221,7 @@
if perc.group()[-1]=='%':
new_val.append('%')
else:
- new_val.append(format(perc.group(), val, grouping))
+ new_val.append(_format(perc.group(), val, grouping, monetary))
else:
if not isinstance(val, tuple):
val = (val,)
@@ -244,13 +235,27 @@
new_val.append(_format(perc.group(),
val[i],
grouping,
- False,
+ monetary,
*val[i+1:i+1+starcount]))
i += (1 + starcount)
val = tuple(new_val)
return new_f % val
+def format(percent, value, grouping=False, monetary=False, *additional):
+ """Deprecated, use format_string instead."""
+ warnings.warn(
+ "This method will be removed in a future version of Python."
+ "Use 'locale.format_string()' instead.",
+ DeprecationWarning, stacklevel=2
+ )
+
+ match = _percent_re.match(percent)
+ if not match or len(match.group())!= len(percent):
+ raise ValueError(("format() must be given exactly one %%char "
+ "format specifier, %s not valid") % repr(percent))
+ return _format(percent, value, grouping, monetary, *additional)
+
def currency(val, symbol=True, grouping=False, international=False):
"""Formats val according to the currency settings
in the current locale."""
@@ -262,7 +267,7 @@
raise ValueError("Currency formatting is not possible using "
"the 'C' locale.")
- s = format('%%.%if' % digits, abs(val), grouping, monetary=True)
+ s = _format('%%.%if' % digits, abs(val), grouping, monetary=True)
# '<' and '>' are markers if the sign must be inserted between symbol and value
s = '<' + s + '>'
@@ -298,7 +303,7 @@
def str(val):
"""Convert float to string, taking the locale into account."""
- return format("%.12g", val)
+ return _format("%.12g", val)
def delocalize(string):
"Parses a string as a normalized number according to the locale settings."
@@ -327,7 +332,7 @@
def _test():
setlocale(LC_ALL, "")
#do grouping
- s1 = format("%d", 123456789,1)
+ s1 = format_string("%d", 123456789,1)
print(s1, "is", atoi(s1))
#standard formatting
s1 = str(3.14)
diff --git a/Lib/test/test_locale.py b/Lib/test/test_locale.py
index 99fab58..06d2866 100644
--- a/Lib/test/test_locale.py
+++ b/Lib/test/test_locale.py
@@ -3,6 +3,7 @@
import locale
import sys
import codecs
+import warnings
class BaseLocalizedTest(unittest.TestCase):
#
@@ -197,6 +198,10 @@
self._test_format("%+10.f", -4200, grouping=0, out='-4200'.rjust(10))
self._test_format("%-10.f", 4200, grouping=0, out='4200'.ljust(10))
+ def test_format_deprecation(self):
+ with self.assertWarns(DeprecationWarning):
+ locale.format("%-10.f", 4200, grouping=True)
+
def test_complex_formatting(self):
# Spaces in formatting string
self._test_format_string("One million is %i", 1000000, grouping=1,
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index 4a9fcba..67d3281 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -381,8 +381,8 @@
for i in range(-10, 10):
x = 1234567890.0 * (10.0 ** i)
- self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n'))
- self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n'))
+ self.assertEqual(locale.format_string('%g', x, grouping=True), format(x, 'n'))
+ self.assertEqual(locale.format_string('%.10g', x, grouping=True), format(x, '.10n'))
@run_with_locale('LC_NUMERIC', 'en_US.UTF8')
def test_int__format__locale(self):
@@ -390,7 +390,7 @@
x = 123456789012345678901234567890
for i in range(0, 30):
- self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n'))
+ self.assertEqual(locale.format_string('%d', x, grouping=True), format(x, 'n'))
# move to the next integer to test
x = x // 10