@@ -49,6 +49,14 @@ typedef struct {
4949
5050/* Top level Exception; inherits from ArithmeticError */
5151PyObject * 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 ;
5260}decimal_state ;
5361
5462static decimal_state global_state ;
@@ -147,14 +155,6 @@ static PyDecContextObject *cached_context = NULL;
147155static PyObject * current_context_var = NULL ;
148156#endif
149157
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-
158158/* Error codes for functions that return signals or conditions */
159159#define DEC_INVALID_SIGNALS (MPD_Max_status+1U)
160160#define DEC_ERR_OCCURRED (DEC_INVALID_SIGNALS<<1)
@@ -1272,8 +1272,8 @@ context_new(PyTypeObject *type, PyObject *args UNUSED, PyObject *kwds UNUSED)
12721272
12731273ctx = CTX (self );
12741274
1275- if (default_context_template ) {
1276- * ctx = * CTX (default_context_template );
1275+ if (state -> default_context_template ) {
1276+ * ctx = * CTX (state -> default_context_template );
12771277 }
12781278else {
12791279* ctx = dflt_ctx ;
@@ -1576,7 +1576,7 @@ current_context_from_dict(void)
15761576 }
15771577
15781578/* 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 );
15801580if (tl_context == NULL ) {
15811581return NULL ;
15821582 }
@@ -1649,9 +1649,9 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
16491649
16501650/* If the new context is one of the templates, make a copy.
16511651 * 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 ) {
16551655v = context_copy (v ,NULL );
16561656if (v == NULL ) {
16571657return NULL ;
@@ -1675,7 +1675,8 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
16751675static PyObject *
16761676init_current_context (void )
16771677{
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 );
16791680if (tl_context == NULL ) {
16801681return NULL ;
16811682 }
@@ -1730,9 +1731,9 @@ PyDec_SetCurrentContext(PyObject *self UNUSED, PyObject *v)
17301731
17311732/* If the new context is one of the templates, make a copy.
17321733 * 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 ) {
17361737v = context_copy (v ,NULL );
17371738if (v == NULL ) {
17381739return NULL ;
@@ -5980,10 +5981,10 @@ PyInit__decimal(void)
59805981
59815982
59825983/* Init default context template first */
5983- ASSIGN_PTR (default_context_template ,
5984+ ASSIGN_PTR (state -> default_context_template ,
59845985PyObject_CallObject ((PyObject * )state -> PyDecContext_Type ,NULL ));
59855986CHECK_INT (PyModule_AddObject (m ,"DefaultContext" ,
5986- Py_NewRef (default_context_template )));
5987+ Py_NewRef (state -> default_context_template )));
59875988
59885989#ifndef WITH_DECIMAL_CONTEXTVAR
59895990ASSIGN_PTR (tls_context_key ,PyUnicode_FromString ("___DECIMAL_CTX__" ));
@@ -5995,18 +5996,18 @@ PyInit__decimal(void)
59955996CHECK_INT (PyModule_AddObject (m ,"HAVE_THREADS" ,Py_NewRef (Py_True )));
59965997
59975998/* Init basic context template */
5998- ASSIGN_PTR (basic_context_template ,
5999+ ASSIGN_PTR (state -> basic_context_template ,
59996000PyObject_CallObject ((PyObject * )state -> PyDecContext_Type ,NULL ));
6000- init_basic_context (basic_context_template );
6001+ init_basic_context (state -> basic_context_template );
60016002CHECK_INT (PyModule_AddObject (m ,"BasicContext" ,
6002- Py_NewRef (basic_context_template )));
6003+ Py_NewRef (state -> basic_context_template )));
60036004
60046005/* Init extended context template */
6005- ASSIGN_PTR (extended_context_template ,
6006+ ASSIGN_PTR (state -> extended_context_template ,
60066007PyObject_CallObject ((PyObject * )state -> PyDecContext_Type ,NULL ));
6007- init_extended_context (extended_context_template );
6008+ init_extended_context (state -> extended_context_template );
60086009CHECK_INT (PyModule_AddObject (m ,"ExtendedContext" ,
6009- Py_NewRef (extended_context_template )));
6010+ Py_NewRef (state -> extended_context_template )));
60106011
60116012
60126013/* Init mpd_ssize_t constants */
@@ -6046,14 +6047,14 @@ PyInit__decimal(void)
60466047Py_CLEAR (MutableMapping );/* GCOV_NOT_REACHED */
60476048Py_CLEAR (SignalTuple );/* GCOV_NOT_REACHED */
60486049Py_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 */
60506051#ifndef WITH_DECIMAL_CONTEXTVAR
60516052Py_CLEAR (tls_context_key );/* GCOV_NOT_REACHED */
60526053#else
60536054Py_CLEAR (current_context_var );/* GCOV_NOT_REACHED */
60546055#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 */
60576058Py_CLEAR (m );/* GCOV_NOT_REACHED */
60586059
60596060return NULL ;/* GCOV_NOT_REACHED */