8000 bpo-10379: deprecate locale.format in lieu of locale.format_string by plusminushalf · Pull Request #259 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-10379: deprecate locale.format in lieu of locale.format_string #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adding tests and documentation changes
  • Loading branch information
plusminushalf committed Feb 24, 2017
commit 820e28962048c3e4243542ab34744cdc0aa27f6d
5 changes: 4 additions & 1 deletion Doc/library/locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ The :mod:`locale` module defines the following exception and functions:

.. function:: format(format, val, grouping=False, monetary=False)

.. deprecated:: 3.7
Use `format_string` instead

Formats a number *val* according to the current :const:`LC_NUMERIC` setting.
The format follows the conventions of the ``%`` operator. For floating point
values, the decimal point is modified if appropriate. If *grouping* is true,
Expand All @@ -366,7 +369,7 @@ The :mod:`locale` module defines the following exception and functions:
For whole format strings, use :func:`format_string`.


.. function:: format_string(format, val, grouping=False)
.. function:: format_string(format, val, grouping=False, monetary=False)

Processes formatting specifiers as in ``format % val``, but takes the current
locale settings into account.
Expand Down
12 changes: 6 additions & 6 deletions Lib/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
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."""
Expand All @@ -228,7 +228,7 @@ def format_string(f, val, grouping=False):
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,)
Expand All @@ -242,7 +242,7 @@ def format_string(f, val, grouping=False):
new_val.append(_format(perc.group(),
val[i],
grouping,
False,
monetary,
*val[i+1:i+1+starcount]))
i += (1 + starcount)
val = tuple(new_val)
Expand All @@ -260,7 +260,7 @@ def currency(val, symbol=True, grouping=False, international=False):
raise ValueError("Currency formatting is not possible using "
"the 'C' locale.")

s = format('%%.%if' % digits, abs(val), grouping, monetary=True)
s = format_string('%%.%if' % digits, abs(val), grouping, monetary=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we know there's only one format string here, I think this should be a call to _format.

# '<' and '>' are markers if the sign must be inserted between symbol and value
s = '<' + s + '>'

Expand Down Expand Up @@ -296,7 +296,7 @@ def currency(val, symbol=True, grouping=False, international=False):

def str(val):
"""Convert float to string, taking the locale into account."""
return format("%.12g", val)
return format_string("%.12g", val)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, I think this should be _format.


def delocalize(string):
"Parses a string as a normalized number according to the locale settings."
Expand Down Expand Up @@ -325,7 +325,7 @@ def atoi(string):
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)
Expand Down
24 changes: 17 additions & 7 deletions Lib/test/test_locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import locale
import sys
import codecs
import warnings

class BaseLocalizedTest(unittest.TestCase):
#
Expand Down Expand Up @@ -197,6 +198,15 @@ def test_padding(self):
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 warnings.catch_warnings(record=True) as w:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use assertWarns here instead.

warnings.simplefilter("always", DeprecationWarning)
locale.format("%.0f KB", 100, grouping=True)
for warning in w:
self.assertTrue(warning.category is DeprecationWarning)
self.assertEqual(
locale.format("%.0f KB", 100, grouping=True), "100 KB")

def test_complex_formatting(self):
# Spaces in formatting string
self._test_format_string("One million is %i", 1000000, grouping=1,
Expand Down Expand Up @@ -228,13 +238,13 @@ class TestFormatPatternArg(unittest.TestCase):

def test_onlyOnePattern(self):
# Issue 2522: accept exactly one % pattern, and no extra chars.
self.assertRaises(ValueError, locale.format, "%f\n", 'foo')
self.assertRaises(ValueError, locale.format, "%f\r", 'foo')
self.assertRaises(ValueError, locale.format, "%f\r\n", 'foo')
self.assertRaises(ValueError, locale.format, " %f", 'foo')
self.assertRaises(ValueError, locale.format, "%fg", 'foo')
self.assertRaises(ValueError, locale.format, "%^g", 'foo')
self.assertRaises(ValueError, locale.format, "%f%%", 'foo')
self.assertRaises(TypeError, locale.format, "%f\n", 'foo')
self.assertRaises(TypeError, locale.format, "%f\r", 'foo')
self.assertRaises(TypeError, locale.format, "%f\r\n", 'foo')
self.assertRaises(TypeError, locale.format, " %f", 'foo')
self.assertRaises(TypeError, locale.format, "%fg", 'foo')
self.assertRaises(TypeError, locale.format, "%^g", 'foo')
self.assertRaises(TypeError, locale.format, "%f%%", 'foo')


class TestLocaleFormatString(unittest.TestCase):
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,16 +381,16 @@ def test_float__format__locale(self):

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):
# test locale support for __format__ code 'n' for integers

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
Expand Down
0