Skip to content

Commit aa3402a

Browse files
serhiy-storchakaStefan Krah
andauthored
gh-114678: Fix incorrect deprecation warning for 'N' specifier in Decimal format (GH-114683)
Co-authored-by: Stefan Krah <[email protected]>
1 parent 0cd9bac commit aa3402a

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

Lib/test/test_decimal.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
darwin_malloc_err_warning, is_emscripten)
4242
from test.support.import_helper import import_fresh_module
4343
from test.support import threading_helper
44+
from test.support import warnings_helper
4445
import random
4546
import inspect
4647
import threading
@@ -1237,7 +1238,14 @@ def test_deprecated_N_format(self):
12371238
else:
12381239
self.assertRaises(ValueError, format, h, 'N')
12391240
self.assertRaises(ValueError, format, h, '010.3N')
1240-
1241+
with warnings_helper.check_no_warnings(self):
1242+
self.assertEqual(format(h, 'N>10.3'), 'NN6.63E-34')
1243+
self.assertEqual(format(h, 'N>10.3n'), 'NN6.63e-34')
1244+
self.assertEqual(format(h, 'N>10.3e'), 'N6.626e-34')
1245+
self.assertEqual(format(h, 'N>10.3f'), 'NNNNN0.000')
1246+
self.assertRaises(ValueError, format, h, '>Nf')
1247+
self.assertRaises(ValueError, format, h, '10Nf')
1248+
self.assertRaises(ValueError, format, h, 'Nx')
12411249

12421250
@run_with_locale('LC_ALL', 'ps_AF')
12431251
def test_wide_char_separator_decimal_point(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ensure that deprecation warning for 'N' specifier in :class:`~decimal.Decimal`
2+
format is not raised for cases where 'N' appears in other places
3+
in the format specifier. Based on patch by Stefan Krah.

Modules/_decimal/_decimal.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,6 +3446,14 @@ dec_format(PyObject *dec, PyObject *args)
34463446
if (fmt == NULL) {
34473447
return NULL;
34483448
}
3449+
3450+
if (size > 0 && fmt[size-1] == 'N') {
3451+
if (PyErr_WarnEx(PyExc_DeprecationWarning,
3452+
"Format specifier 'N' is deprecated", 1) < 0) {
3453+
return NULL;
3454+
}
3455+
}
3456+
34493457
/* NOTE: If https://github.com/python/cpython/pull/29438 lands, the
34503458
* format string manipulation below can be eliminated by enhancing
34513459
* the forked mpd_parse_fmt_str(). */
@@ -3593,12 +3601,6 @@ dec_format(PyObject *dec, PyObject *args)
35933601
if (replace_fillchar) {
35943602
dec_replace_fillchar(decstring);
35953603
}
3596-
if (strchr(fmt, 'N') != NULL) {
3597-
if (PyErr_WarnEx(PyExc_DeprecationWarning,
3598-
"Format specifier 'N' is deprecated", 1) < 0) {
3599-
goto finish;
3600-
}
3601-
}
36023604

36033605
result = PyUnicode_DecodeUTF8(decstring, size, NULL);
36043606

0 commit comments

Comments
 (0)