]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-114678: Fix incorrect deprecation warning for 'N' specifier in Decimal format...
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 29 Jan 2024 17:58:31 +0000 (19:58 +0200)
committerGitHub <noreply@github.com>
Mon, 29 Jan 2024 17:58:31 +0000 (19:58 +0200)
Co-authored-by: Stefan Krah <skrah@bytereef.org>
Lib/test/test_decimal.py
Misc/NEWS.d/next/Library/2024-01-28-19-40-40.gh-issue-114678.kYKcJw.rst [new file with mode: 0644]
Modules/_decimal/_decimal.c

index 7a5fe62b4673720049b6e81d1c06f43a9c52e165..1423bc61c7f69067d5f97b743a9b4326df64b9a8 100644 (file)
@@ -41,6 +41,7 @@ from test.support import (TestFailed,
                           darwin_malloc_err_warning, is_emscripten)
 from test.support.import_helper import import_fresh_module
 from test.support import threading_helper
+from test.support import warnings_helper
 import random
 import inspect
 import threading
@@ -1237,7 +1238,14 @@ class FormatTest:
         else:
             self.assertRaises(ValueError, format, h, 'N')
             self.assertRaises(ValueError, format, h, '010.3N')
-
+        with warnings_helper.check_no_warnings(self):
+            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')
+            self.assertRaises(ValueError, format, h, '>Nf')
+            self.assertRaises(ValueError, format, h, '10Nf')
+            self.assertRaises(ValueError, format, h, 'Nx')
 
     @run_with_locale('LC_ALL', 'ps_AF')
     def test_wide_char_separator_decimal_point(self):
diff --git a/Misc/NEWS.d/next/Library/2024-01-28-19-40-40.gh-issue-114678.kYKcJw.rst b/Misc/NEWS.d/next/Library/2024-01-28-19-40-40.gh-issue-114678.kYKcJw.rst
new file mode 100644 (file)
index 0000000..2306af4
--- /dev/null
@@ -0,0 +1,3 @@
+Ensure that deprecation warning for 'N' specifier in :class:`~decimal.Decimal`
+format is not raised for cases where 'N' appears in other places
+in the format specifier. Based on patch by Stefan Krah.
index 8b93f8e2cbcf0bbabcde63c7d7951f9d8c7ac5d1..127f5f2887d4cd125881d67f9638141c148a50de 100644 (file)
@@ -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(). */
@@ -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);