]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-89902: Deprecate non-standard format specifier "N" for Decimal (GH-110508)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 8 Oct 2023 07:01:39 +0000 (10:01 +0300)
committerGitHub <noreply@github.com>
Sun, 8 Oct 2023 07:01:39 +0000 (10:01 +0300)
It was not documented and only supported in the C implementation.

Doc/whatsnew/3.13.rst
Lib/test/test_decimal.py
Misc/NEWS.d/next/Library/2023-10-07-21-12-28.gh-issue-89902.dCokZj.rst [new file with mode: 0644]
Modules/_decimal/_decimal.c

index 73975b055c240bab38ea2edab2830970debb6e59..2fe1494a3d770ff308d7037d2aebf02ca91dcf08 100644 (file)
@@ -353,6 +353,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
 ------------------------------
index bd299483e7b0bd3e3e4c80563e6a5d1a07f5e590..7a5fe62b4673720049b6e81d1c06f43a9c52e165 100644 (file)
@@ -1222,6 +1222,23 @@ class FormatTest:
         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
diff --git a/Misc/NEWS.d/next/Library/2023-10-07-21-12-28.gh-issue-89902.dCokZj.rst b/Misc/NEWS.d/next/Library/2023-10-07-21-12-28.gh-issue-89902.dCokZj.rst
new file mode 100644 (file)
index 0000000..812ae95
--- /dev/null
@@ -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.
index 99ff9aa17b179ccddfc7f5c79cf60e9e89f14279..8b93f8e2cbcf0bbabcde63c7d7951f9d8c7ac5d1 100644 (file)
@@ -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);