@@ -49,6 +49,14 @@ typedef struct {
49
49
50
50
/* Top level Exception; inherits from ArithmeticError */
51
51
PyObject * DecimalException ;
52
+
53
+ /* Template for creating new thread contexts, calling Context() without
54
+ * arguments and initializing the module_context on first access. */
55
+ PyObject * default_context_template ;
56
+
57
+ /* Basic and extended context templates */
58
+ PyObject * basic_context_template ;
59
+ PyObject * extended_context_template ;
52
60
} decimal_state ;
53
61
54
62
static decimal_state global_state ;
@@ -147,14 +155,6 @@ static PyDecContextObject *cached_context = NULL;
147
155
static PyObject * current_context_var = NULL ;
148
156
#endif
149
157
150
- /* Template for creating new thread contexts, calling Context() without
151
- * arguments and initializing the module_context on first access. */
152
- static PyObject * default_context_template = NULL ;
153
- /* Basic and extended context templates */
154
- static PyObject * basic_context_template = NULL ;
155
- static PyObject * extended_context_template = NULL ;
156
-
157
-
158
158
/* Error codes for functions that return signals or conditions */
159
159
#define DEC_INVALID_SIGNALS (MPD_Max_status+1U)
160
160
#define DEC_ERR_OCCURRED (DEC_INVALID_SIGNALS<<1)
@@ -1272,8 +1272,8 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
1272
1272
1273
1273
ctx = CTX (self );
1274
1274
1275
- if (default_context_template ) {
1276
- * ctx = * CTX (default_context_template );
1275
+ if (state -> default_context_template ) {
1276
+ * ctx = * CTX (state -> default_context_template );
1277
1277
}
1278
1278
else {
1279
1279
* ctx = dflt_ctx ;
@@ -1576,7 +1576,7 @@ current_context_from_dict(void)
1576
1576
}
1577
1577
1578
1578
/* Set up a new thread local context. */
1579
- tl_context = context_copy (default_context_template , NULL );
1579
+ tl_context = context_copy (state -> default_context_template , NULL );
1580
1580
if (tl_context == NULL ) {
1581
1581
return NULL ;
1582
1582
}
@@ -1649,9 +1649,9 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
1649
1649
1650
1650
/* If the new context is one of the templates, make a copy.
1651
1651
* This is the current behavior of decimal.py. */
1652
- if (v == default_context_template ||
1653
- v == basic_context_template ||
1654
- v == extended_context_template ) {
1652
+ if (v == state -> default_context_template ||
1653
+ v == state -> basic_context_template ||
1654
+ v == state -> extended_context_template ) {
1655
1655
v = context_copy (v , NULL );
1656
1656
if (v == NULL ) {
1657
1657
return NULL ;
@@ -1675,7 +1675,8 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
1675
1675
static PyObject *
1676
1676
init_current_context (void )
1677
1677
{
1678
- PyObject * tl_context = context_copy (default_context_template , NULL );
1678
+ decimal_state * state = GLOBAL_STATE ();
1679
+ PyObject * tl_context = context_copy (state -> default_context_template , NULL );
1679
1680
if (tl_context == NULL ) {
1680
1681
return NULL ;
1681
1682
}
@@ -1730,9 +1731,9 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
1730
1731
1731
1732
/* If the new context is one of the templates, make a copy.
1732
1733
* This is the current behavior of decimal.py. */
1733
- if (v == default_context_template ||
1734
- v == basic_context_template ||
1735
- v == extended_context_template ) {
1734
+ if (v == state -> default_context_template ||
1735
+ v == state -> basic_context_template ||
1736
+ v == state -> extended_context_template ) {
1736
1737
v = context_copy (v , NULL );
1737
1738
if (v == NULL ) {
1738
1739
return NULL ;
@@ -5980,10 +5981,10 @@ PyInit__decimal(void)
5980
5981
5981
5982
5982
5983
/* Init default context template first */
5983
- ASSIGN_PTR (default_context_template ,
5984
+ ASSIGN_PTR (state -> default_context_template ,
5984
5985
PyObject_CallObject ((PyObject * )state -> PyDecContext_Type , NULL ));
5985
5986
CHECK_INT (PyModule_AddObject (m , "DefaultContext" ,
5986
- Py_NewRef (default_context_template )));
5987
+ Py_NewRef (state -> default_context_template )));
5987
5988
5988
5989
#ifndef WITH_DECIMAL_CONTEXTVAR
5989
5990
ASSIGN_PTR (tls_context_key , PyUnicode_FromString ("___DECIMAL_CTX__" ));
@@ -5995,18 +5996,18 @@ PyInit__decimal(void)
5995
5996
CHECK_INT (PyModule_AddObject (m , "HAVE_THREADS" , Py_NewRef (Py_True )));
5996
5997
5997
5998
/* Init basic context template */
5998
- ASSIGN_PTR (basic_context_template ,
5999
+ ASSIGN_PTR (state -> basic_context_template ,
5999
6000
PyObject_CallObject ((PyObject * )state -> PyDecContext_Type , NULL ));
6000
- init_basic_context (basic_context_template );
6001
+ init_basic_context (state -> basic_context_template );
6001
6002
CHECK_INT (PyModule_AddObject (m , "BasicContext" ,
6002
- Py_NewRef (basic_context_template )));
6003
+ Py_NewRef (state -> basic_context_template )));
6003
6004
6004
6005
/* Init extended context template */
6005
- ASSIGN_PTR (extended_context_template ,
6006
+ ASSIGN_PTR (state -> extended_context_template ,
6006
6007
PyObject_CallObject ((PyObject * )state -> PyDecContext_Type , NULL ));
6007
- init_extended_context (extended_context_template );
6008
+ init_extended_context (state -> extended_context_template );
6008
6009
CHECK_INT (PyModule_AddObject (m , "ExtendedContext" ,
6009
- Py_NewRef (extended_context_template )));
6010
+ Py_NewRef (state -> extended_context_template )));
6010
6011
6011
6012
6012
6013
/* Init mpd_ssize_t constants */
@@ -6046,14 +6047,14 @@ PyInit__decimal(void)
6046
6047
Py_CLEAR (MutableMapping ); /* GCOV_NOT_REACHED */
6047
6048
Py_CLEAR (SignalTuple ); /* GCOV_NOT_REACHED */
6048
6049
Py_CLEAR (state -> DecimalTuple ); /* GCOV_NOT_REACHED */
6049
- Py_CLEAR (default_context_template ); /* GCOV_NOT_REACHED */
6050
+ Py_CLEAR (state -> default_context_template ); /* GCOV_NOT_REACHED */
6050
6051
#ifndef WITH_DECIMAL_CONTEXTVAR
6051
6052
Py_CLEAR (tls_context_key ); /* GCOV_NOT_REACHED */
6052
6053
#else
6053
6054
Py_CLEAR (current_context_var ); /* GCOV_NOT_REACHED */
6054
6055
#endif
6055
- Py_CLEAR (basic_context_template ); /* GCOV_NOT_REACHED */
6056
- Py_CLEAR (extended_context_template ); /* GCOV_NOT_REACHED */
6056
+ Py_CLEAR (state -> basic_context_template ); /* GCOV_NOT_REACHED */
6057
+ Py_CLEAR (state -> extended_context_template ); /* GCOV_NOT_REACHED */
6057
6058
Py_CLEAR (m ); /* GCOV_NOT_REACHED */
6058
6059
6059
6060
return NULL ; /* GCOV_NOT_REACHED */
0 commit comments