Skip to content

Commit

Permalink
pythongh-114678: Fix incorrect deprecation warning for 'N' specifier …
Browse files Browse the repository at this point in the history
…in Decimal format

Co-authored-by: Stefan Krah <skrah@bytereef.org>
  • Loading branch information
serhiy-storchaka and skrah committed Jan 28, 2024
1 parent 5ecfd75 commit 19c363c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,10 @@ def test_deprecated_N_format(self):
else:
self.assertRaises(ValueError, format, h, 'N')
self.assertRaises(ValueError, format, h, '010.3N')

self.assertEqual(format(h, 'N>10.3'), 'NN6.63E-34')
self.assertEqual(format(h, 'N>10.3n'), 'NN6.63e-34')
self.assertEqual(format(h, 'N>10.3e'), 'N6.626e-34')
self.assertEqual(format(h, 'N>10.3f'), 'NNNNN0.000')

@run_with_locale('LC_ALL', 'ps_AF')
def test_wide_char_separator_decimal_point(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix incorrect deprecation warning for 'N' specifier in
:class:`~decimal.Decimal` format. Based on patch by Stefan Krah.
14 changes: 8 additions & 6 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3446,6 +3446,14 @@ dec_format(PyObject *dec, PyObject *args)
if (fmt == NULL) {
return NULL;
}

if (size > 0 && fmt[size-1] == 'N') {
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"Format specifier 'N' is deprecated", 1) < 0) {
return NULL;
}
}

/* NOTE: If https://github.com/python/cpython/pull/29438 lands, the
* format string manipulation below can be eliminated by enhancing
* the forked mpd_parse_fmt_str(). */
Expand Down Expand Up @@ -3593,12 +3601,6 @@ 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 comments on commit 19c363c

Please sign in to comment.