[Python-checkins] gh-106078: Move DecimalException to _decimal state (#106301)

erlend-aasland webhook-mailer at python.org
Sun Jul 2 12:50:43 EDT 2023


https://github.com/python/cpython/commit/dbefa88b27ee1f124f782363b139aee3f1ccf590
commit: dbefa88b27ee1f124f782363b139aee3f1ccf590
branch: main
author: Charlie Zhao <zhaoyu_hit at qq.com>
committer: erlend-aasland <erlend.aasland at protonmail.com>
date: 2023-07-02T16:50:40Z
summary:

gh-106078: Move DecimalException to _decimal state (#106301)

files:
M Modules/_decimal/_decimal.c
M Tools/c-analyzer/cpython/globals-to-fix.tsv

diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c
index b7cb19515b300..da62372500342 100644
--- a/Modules/_decimal/_decimal.c
+++ b/Modules/_decimal/_decimal.c
@@ -46,6 +46,9 @@ typedef struct {
     PyTypeObject *PyDec_Type;
     PyTypeObject *PyDecSignalDict_Type;
     PyTypeObject *DecimalTuple;
+
+    /* Top level Exception; inherits from ArithmeticError */
+    PyObject *DecimalException;
 } decimal_state;
 
 static decimal_state global_state;
@@ -164,9 +167,6 @@ typedef struct {
     PyObject *ex;       /* corresponding exception */
 } DecCondMap;
 
-/* Top level Exception; inherits from ArithmeticError */
-static PyObject *DecimalException = NULL;
-
 /* Exceptions that correspond to IEEE signals */
 #define SUBNORMAL 5
 #define INEXACT 6
@@ -5902,10 +5902,10 @@ PyInit__decimal(void)
     CHECK_INT(PyModule_AddType(m, state->DecimalTuple));
 
     /* Create top level exception */
-    ASSIGN_PTR(DecimalException, PyErr_NewException(
+    ASSIGN_PTR(state->DecimalException, PyErr_NewException(
                                      "decimal.DecimalException",
                                      PyExc_ArithmeticError, NULL));
-    CHECK_INT(PyModule_AddObject(m, "DecimalException", Py_NewRef(DecimalException)));
+    CHECK_INT(PyModule_AddType(m, (PyTypeObject *)state->DecimalException));
 
     /* Create signal tuple */
     ASSIGN_PTR(SignalTuple, PyTuple_New(SIGNAL_MAP_LEN));
@@ -5918,10 +5918,11 @@ PyInit__decimal(void)
 
         switch (cm->flag) {
         case MPD_Float_operation:
-            base = PyTuple_Pack(2, DecimalException, PyExc_TypeError);
+            base = PyTuple_Pack(2, state->DecimalException, PyExc_TypeError);
             break;
         case MPD_Division_by_zero:
-            base = PyTuple_Pack(2, DecimalException, PyExc_ZeroDivisionError);
+            base = PyTuple_Pack(2, state->DecimalException,
+                                PyExc_ZeroDivisionError);
             break;
         case MPD_Overflow:
             base = PyTuple_Pack(2, signal_map[INEXACT].ex,
@@ -5933,7 +5934,7 @@ PyInit__decimal(void)
                                    signal_map[SUBNORMAL].ex);
             break;
         default:
-            base = PyTuple_Pack(1, DecimalException);
+            base = PyTuple_Pack(1, state->DecimalException);
             break;
         }
 
diff --git a/Tools/c-analyzer/cpython/globals-to-fix.tsv b/Tools/c-analyzer/cpython/globals-to-fix.tsv
index 1131edff265ee..8fdc54df2b072 100644
--- a/Tools/c-analyzer/cpython/globals-to-fix.tsv
+++ b/Tools/c-analyzer/cpython/globals-to-fix.tsv
@@ -393,7 +393,6 @@ Modules/xxlimited_35.c	-	Xxo_Type	-
 ## exception types
 Modules/_ctypes/_ctypes.c	-	PyExc_ArgError	-
 Modules/_cursesmodule.c	-	PyCursesError	-
-Modules/_decimal/_decimal.c	-	DecimalException	-
 Modules/_tkinter.c	-	Tkinter_TclError	-
 Modules/xxlimited_35.c	-	ErrorObject	-
 Modules/xxmodule.c	-	ErrorObject	-



More information about the Python-checkins mailing list