-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
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
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d0f9863
bpo-10379: deprecate locale.format in lue of locale.format_string
plusminushalf 820e289
Adding tests and documentation changes
plusminushalf 4cfd737
Adding version changed info for locale.format
plusminushalf a08419f
Deprecating but not changing the function defination
plusminushalf 4e24a3f
Adding documentation inside what's new in 3.7
plusminushalf 6097e47
Adding whatsnew documentation in proper segments
plusminushalf 04577c2
Merge branch 'master' of github.com:python/cpython into fix-bpo-10379
plusminushalf 2039406
fixing documentation bug
plusminushalf 5b100b8
Adding locale subsection
plusminushalf 041349f
Merge branch 'master' of github.com:python/cpython into fix-bpo-10379
plusminushalf 795aa65
Refactoring Doc, using _format inisde locale module
plusminushalf 8190fa9
Adding entry to Misc/NEWS about changes
plusminushalf 7e0bfcf
CR fixes
plusminushalf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Adding tests and documentation changes
- Loading branch information
commit 820e28962048c3e4243542ab34744cdc0aa27f6d
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.""" | ||
|
@@ -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,) | ||
|
@@ -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) | ||
|
@@ -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) | ||
# '<' and '>' are markers if the sign must be inserted between symbol and value | ||
s = '<' + s + '>' | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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." | ||
|
@@ -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) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
import locale | ||
import sys | ||
import codecs | ||
import warnings | ||
|
||
class BaseLocalizedTest(unittest.TestCase): | ||
# | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.