@@ -76,8 +76,9 @@ typedef struct {
76
76
#ifndef WITH_DECIMAL_CONTEXTVAR
77
77
/* Key for thread state dictionary */
78
78
PyObject * tls_context_key ;
79
- /* Invariant: NULL or the most recently accessed thread local context */
80
- struct PyDecContextObject * cached_context ;
79
+ /* Invariant: NULL or a strong reference to the most recently accessed
80
+ thread local context. */
81
+ struct PyDecContextObject * cached_context ; /* Not borrowed */
81
82
#else
82
83
PyObject * current_context_var ;
83
84
#endif
@@ -1419,12 +1420,6 @@ context_dealloc(PyDecContextObject *self)
1419
1420
{
1420
1421
PyTypeObject * tp = Py_TYPE (self );
1421
1422
PyObject_GC_UnTrack (self );
1422
- #ifndef WITH_DECIMAL_CONTEXTVAR
1423
- decimal_state * state = get_module_state_by_def (Py_TYPE (self ));
1424
- if (self == state -> cached_context ) {
1425
- state -> cached_context = NULL ;
1426
- }
1427
- #endif
1428
1423
(void )context_clear (self );
1429
1424
tp -> tp_free (self );
1430
1425
Py_DECREF (tp );
@@ -1701,7 +1696,8 @@ current_context_from_dict(decimal_state *modstate)
1701
1696
1702
1697
/* Cache the context of the current thread, assuming that it
1703
1698
* will be accessed several times before a thread switch. */
1704
- modstate -> cached_context = (PyDecContextObject * )tl_context ;
1699
+ Py_XSETREF (modstate -> cached_context ,
1700
+ (PyDecContextObject * )Py_NewRef (tl_context ));
1705
1701
modstate -> cached_context -> tstate = tstate ;
1706
1702
1707
1703
/* Borrowed reference with refcount==1 */
@@ -1769,7 +1765,7 @@ PyDec_SetCurrentContext(PyObject *self, PyObject *v)
1769
1765
Py_INCREF (v );
1770
1766
}
1771
1767
1772
- state -> cached_context = NULL ;
1768
+ Py_CLEAR ( state -> cached_context ) ;
1773
1769
if (PyDict_SetItem (dict , state -> tls_context_key , v ) < 0 ) {
1774
1770
Py_DECREF (v );
1775
1771
return NULL ;
@@ -6122,6 +6118,16 @@ decimal_traverse(PyObject *module, visitproc visit, void *arg)
6122
6118
Py_VISIT (state -> Rational );
6123
6119
Py_VISIT (state -> SignalTuple );
6124
6120
6121
+ if (state -> signal_map != NULL ) {
6122
+ for (DecCondMap * cm = state -> signal_map ; cm -> name != NULL ; cm ++ ) {
6123
+ Py_VISIT (cm -> ex );
6124
+ }
6125
+ }
6126
+ if (state -> cond_map != NULL ) {
6127
+ for (DecCondMap * cm = state -> cond_map + 1 ; cm -> name != NULL ; cm ++ ) {
6128
+ Py_VISIT (cm -> ex );
6129
+ }
6130
+ }
6125
6131
return 0 ;
6126
6132
}
6127
6133
0 commit comments