8000 gh-89902: Deprecate non-standard format specifier "N" for Decimal (GH… · python/cpython@d96a8cd · GitHub
[go: up one dir, main page]

Skip to content

Commit d96a8cd

Browse files
gh-89902: Deprecate non-standard format specifier "N" for Decimal (GH-110508)
It was not documented and only supported in the C implementation.
1 parent 6780d63 commit d96a8cd

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

Doc/whatsnew/3.13.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,10 @@ Deprecated
353353
in :data:`~dis.hasarg` instead.
354354
(Contributed by Irit Katriel in :gh:`109319`.)
355355

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

357361
Pending Removal in Python 3.14
358362
------------------------------

Lib/test/test_decimal.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,23 @@ def get_fmt(x, override=None, fmt='n'):
12221222
self.assertEqual(get_fmt(Decimal('-1.5'), dotsep_wide, '020n'),
12231223
'-0\u00b4000\u00b4000\u00b4000\u00b4001\u00bf5')
12241224

1225+
def test_deprecated_N_format(self):
1226+
Decimal = self.decimal.Decimal
1227+
h = Decimal('6.62607015e-34')
1228+
if self.decimal == C:
1229+
with self.assertWarns(DeprecationWarning) as cm:
1230+
r = format(h, 'N')
1231+
self.assertEqual(cm.filename, __file__)
1232+
self.assertEqual(r, format(h, 'n').upper())
1233+
with self.assertWarns(DeprecationWarning) as cm:
1234+
r = format(h, '010.3N')
1235+
self.assertEqual(cm.filename, __file__)
1236+
self.assertEqual(r, format(h, '010.3n').upper())
1237+
else:
1238+
self.assertRaises(ValueError, format, h, 'N')
1239+
self.assertRaises(ValueError, format, h, '010.3N')
1240+
1241+
12251242
@run_with_locale('LC_ALL', 'ps_AF')
12261243
def test_wide_char_separator_decimal_point(self):
12271244
# locale with wide char separator and decimal point
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Deprecate non-standard format specifier "N" for :class:`decimal.Decimal`. It
2+
was not documented and only supported in the C implementation.

Modules/_decimal/_decimal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,6 +3593,12 @@ dec_format(PyObject *dec, PyObject *args)
35933593
if (replace_fillchar) {
35943594
dec_replace_fillchar(decstring);
35953595
}
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+
}
35963602

35973603
result = PyUnicode_DecodeUTF8(decstring, size, NULL);
35983604

0 commit comments

Comments
 (0)
0