8000 gh-89902: Deprecate non-standard format specifier "N" for Decimal by serhiy-storchaka · Pull Request #110508 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-89902: Deprecate non-standard format specifier "N" for Decimal #110508

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
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ Deprecated
in :data:`~dis.hasarg` instead.
(Contributed by Irit Katriel in :gh:`109319`.)

* Deprecate non-standard format specifier "N" for :class:`decimal.Decimal`.
It was not documented and only supported in the C implementation.
(Contributed by Serhiy Storchaka in :gh:`89902`.)


Pending Removal in Python 3.14
------------------------------
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,23 @@ def get_fmt(x, override=None, fmt='n'):
self.assertEqual(get_fmt(Decimal('-1.5'), dotsep_wide, '020n'),
'-0\u00b4000\u00b4000\u00b4000\u00b4001\u00bf5')

def test_deprecated_N_format(self):
Decimal = self.decimal.Decimal
h = Decimal('6.62607015e-34')
if self.decimal == C:
with self.assertWarns(DeprecationWarning) as cm:
r = format(h, 'N')
self.assertEqual(cm.filename, __file__)
self.assertEqual(r, format(h, 'n').upper())
with self.assertWarns(DeprecationWarning) as cm:
r = format(h, '010.3N')
self.assertEqual(cm.filename, __file__)
self.assertEqual(r, format(h, '010.3n').upper())
else:
self.assertRaises(ValueError, format, h, 'N')
self.assertRaises(ValueError, format, h, '010.3N')


@run_with_locale('LC_ALL', 'ps_AF')
def test_wide_char_separator_decimal_point(self):
# locale with wide char separator and decimal point
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate non-standard format specifier "N" for :class:`decimal.Decimal`. It
was not documented and only supported in the C implementation.
6 changes: 6 additions & 0 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3593,6 +3593,12 @@ dec_format(PyObject *dec, PyObject *args)
if (replace_fillchar) {
dec_replace_fillchar(decstring);
}
if (strchr(fmt, 'N') != NULL) {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"Format specifier 'N' is deprecated", 1) < 0) {
goto finish;
}
}

result = PyUnicode_DecodeUTF8(decstring, size, NULL);

Expand Down
0