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
Refactoring Doc, using _format inisde locale module
Refactoring Doc, moving format doc below format_string,
this is to make sure people have lesser probability of using
format in future.
Also using _format method inside locale module, since we know
there is only one format function that is _format
  • Loading branch information
plusminushalf committed Mar 19, 2017
commit 795aa657033a6e20f86e54dc4364f5c7d8f6c211
28 changes: 11 additions & 17 deletions Doc/library/locale.rst
Original file line number Diff line number Diff line change
Expand Up @@ -352,23 +352,6 @@ The :mod:`locale` module defines the following exception and functions:
sequence of strings.


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

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,
also takes the grouping into account.

If *monetary* is true, the conversion uses monetary thousands separator and
grouping strings.

Please note that this function will only work for exactly one %char specifier.
For whole format strings, use :func:`format_string`.

.. deprecated:: 3.7
Use :meth:`format_string` instead


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

Formats a number *val* according to the current :const:`LC_NUMERIC` setting.
Expand All @@ -386,6 +369,17 @@ The :mod:`locale` module defines the following exception and functions:
The *monetary* keyword parameter was added.


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

Please note that this function works like format_string but will only work
for exactly one %char specifier.

For whole format strings, use :func:`format_string`.

.. deprecated:: 3.7
Use :meth:`format_string` instead


Copy link
Member

Choose a reason for hiding this comment

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

The description of the monetary parameter should be added to the regular docs, and the version changed phrase should just say "the monetary keyword parameter was added"

.. function:: currency(val, symbol=True, grouping=False, international=False)

Formats a number *val* according to the current :const:`LC_MONETARY` settings.
Expand Down
49 changes: 23 additions & 26 deletions Doc/whatsnew/3.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,14 @@ Other Language Changes
======================

* More than 255 arguments can now be passed to a function, and a function can
now have more than 255 parameters.
(Contributed by Serhiy Storchaka in :issue:`12844` and :issue:`18896`.)
now have more than 255 parameters. (Contributed by Serhiy Storchaka in
:issue:`12844` and :issue:`18896`.)

* :meth:`bytes.fromhex` and :meth:`bytearray.fromhex` now ignore all ASCII
whitespace, not only spa 10000 ces.
(Contributed by Robert Xiao in :issue:`28927`.)
whitespace, not only spaces. (Contributed by Robert Xiao in :issue:`28927`.)

* :exc:`ImportError` now displays module name and module ``__file__`` path when
``from ... import ...`` fails.
(Contributed by Matthias Bussonnier in :issue:`29546`.)
``from ... import ...`` fails. (Contributed by Matthias Bussonnier in :issue:`29546`.)


New Modules
Expand All @@ -102,28 +100,27 @@ locale

Added another argument *monetary* in :meth:`format_string` of :mod:`locale`.
If *monetary* is true, the conversion uses monetary thousands separator and
grouping strings.
(Contributed by Garvit in :issue:`10379`.)
grouping strings. (Contributed by Garvit in :issue:`10379`.)

Copy link
Member

Choose a reason for hiding this comment

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

Small nit: our convention appears to be to put the Contributed by as part of the paragraph when there's only one paragraph. Ends up formatted the same in html, but might as well make the ReST consistent as well.

os
--

Added support for :class:`bytes` paths in :func:`~os.fwalk`.
(Contributed by Serhiy Storchaka in :issue:`28682`.)
Added support for :class:`bytes` paths in :func:`~os.fwalk`. (Contributed by
Serhiy Storchaka in :issue:`28682`.)

unittest.mock
-------------

The :const:`~unittest.mock.sentinel` attributes now preserve their identity
when they are :mod:`copied <copy>` or :mod:`pickled <pickle>`.
(Contributed by Serhiy Storchaka in :issue:`20804`.)
when they are :mod:`copied <copy>` or :mod:`pickled <pickle>`. (Contributed by
Serhiy Storchaka in :issue:`20804`.)

xmlrpc.server
-------------

:meth:`register_function` of :class:`xmlrpc.server.SimpleXMLRPCDispatcher` and
its subclasses can be used as a decorator.
(Contributed by Xiang Zhang in :issue:`7769`.)
its subclasses can be used as a decorator. (Contributed by Xiang Zhang in
:issue:`7769`.)

urllib.parse
------------
Expand All @@ -138,13 +135,13 @@ Optimizations

* Added two new opcodes: ``LOAD_METHOD`` and ``CALL_METHOD`` to avoid
instantiation of bound method objects for method calls, which results
in method calls being faster up to 20%.
(Contributed by Yury Selivanov and INADA Naoki in :issue:`26110`.)
in method calls being faster up to 20%. (Contributed by Yury Selivanov and
INADA Naoki in :issue:`26110`.)

* Fast implementation from standard C library is now used for functions
:func:`~math.tgamma`, :func:`~math.lgamma`, :func:`~math.erf` and
:func:`~math.erfc` in the :mod:`math` module.
(Contributed by Serhiy Storchaka in :issue:`26121`.)
:func:`~math.erfc` in the :mod:`math` module. (Contributed by Serhiy
Storchaka in :issue:`26121`.)


Build and C API Changes
Expand All @@ -162,8 +159,8 @@ Build and C API Changes
``char *``. (Contributed by Serhiy Storchaka in :issue:`28761`.)

* The result of :c:func:`PyUnicode_AsUTF8AndSize` and :c:func:`PyUnicode_AsUTF8`
is now of type ``const char *`` rather of ``char *``.
(Contributed by Serhiy Storchaka in :issue:`28769`.)
is now of type ``const char *`` rather of ``char *``. (Contributed by Serhiy
Storchaka in :issue:`28769`.)

* Added functions :c:func:`PySlice_Unpack` and :c:func:`PySlice_AdjustIndices`.
(Contributed by Serhiy Storchaka in :issue:`27867`.)
Expand Down Expand Up @@ -192,8 +189,8 @@ Deprecated
by Matthias Bussonnier in :issue:`29576`)

- Using non-integer value for selecting a plural form in :mod:`gettext` is
now deprecated. It never correctly worked.
(Contributed by Serhiy Storchaka in :issue:`28692`.)
now deprecated. It never correctly worked. (Contributed by Serhiy Storchaka
in :issue:`28692`.)


Removed
Expand Down Expand Up @@ -230,8 +227,8 @@ Changes in the Python API

* A format string argument for :meth:`string.Formatter.format`
is now :ref:`positional-only <positional-only_parameter>`.
Passing it as a keyword argument was deprecated in Python 3.5.
(Contributed by Serhiy Storchaka in :issue:`29193`.)
Passing it as a keyword argument was deprecated in Python 3.5. (Contributed
by Serhiy Storchaka in :issue:`29193`.)

* Attributes :attr:`~http.cookies.Morsel.key`,
:attr:`~http.cookies.Morsel.value` and
Expand All @@ -245,8 +242,8 @@ Changes in the Python API
``ClassDef`` AST nodes now have a new ``docstring`` field.
The first statement in their body is not considered as a docstring
anymore. ``co_firstlineno`` and ``co_lnotab`` of code object for class
and module are affected by this change.
(Contributed by INADA Naoki and Eugene Toder in :issue:`29463`.)
and module are affected by this change. (Contributed by INADA Naoki and
Eugene Toder in :issue:`29463`.)

CPython bytecode changes
------------------------
Expand Down
33 changes: 17 additions & 16 deletions Lib/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,20 +181,6 @@ def _strip_padding(s, amount):
_percent_re = re.compile(r'%(?:\((?P<key>.*?)\))?'
r'(?P<modifiers>[-#0-9 +*.hlL]*?)[eEfFgGdiouxXcrs%]')

def format(percent, value, grouping=False, monetary=False, *additional):
"""Deprecated, use format_string instead."""
warnings.warn(
"This method will be removed in future versions. "
"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 _format(percent, value, grouping=False, monetary=False, *additional):
if additional:
formatted = percent % ((value,) + additional)
Expand Down Expand Up @@ -222,6 +208,7 @@ def _format(percent, value, grouping=False, monetary=False, *additional):
def format_string(f, val, grouping=False, monetary=False):
&quo 1E79 t;""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.
Copy link
Member

Choose a reason for hiding this comment

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

As long as we are editing this, I think there should be a blank line after the first sentence, before the 'Grouping' sentence.

Conversion uses monetary thousands separator and grouping strings if
forth parameter monetary is true."""
Expand Down Expand Up @@ -255,6 +242,20 @@ def format_string(f, val, grouping=False, monetary=False):

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."""
Expand All @@ -266,7 +267,7 @@ def currency(val, symbol=True, grouping=False, international=False):
raise ValueError("Currency formatting is not possible using "
"the 'C' locale.")

s = format_string('%%.%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 + '>'

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

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

def delocalize(string):
"Parses a string as a normalized number according to the locale settings."
Expand Down
0