@@ -16,6 +16,7 @@ _ensure_current_cause(PyThreadState *tstate, PyObject *cause)
1616 }
1717 assert (PyException_GetCause (exc ) == NULL );
1818 PyException_SetCause (exc , cause );
19+ _PyErr_SetRaisedException (tstate , exc );
1920}
2021
2122
@@ -80,41 +81,59 @@ get_notshareableerror_type(PyThreadState *tstate)
8081}
8182
8283static void
83- set_notshareableerror (PyThreadState * tstate , PyObject * cause , const char * msg )
84+ _ensure_notshareableerror (PyThreadState * tstate ,
85+ PyObject * cause , int force , PyObject * msgobj )
8486{
87+ PyObject * ctx = _PyErr_GetRaisedException (tstate );
8588 PyObject * exctype = get_notshareableerror_type (tstate );
86- if (exctype == NULL ) {
89+ if (exctype != NULL ) {
90+ if (!force && ctx != NULL && Py_TYPE (ctx ) == (PyTypeObject * )exctype ) {
91+ // A NotShareableError instance is already set.
92+ assert (cause == NULL );
93+ _PyErr_SetRaisedException (tstate , ctx );
94+ }
95+ }
96+ else {
8797 exctype = PyExc_ValueError ;
8898 }
89- // We have to set the context manually since _PyErr_SetObject() won't.
90- PyObject * ctx = _PyErr_GetRaisedException (tstate );
91- _PyErr_SetString (tstate , exctype , msg );
99+ _PyErr_SetObject (tstate , exctype , msgobj );
100+ // We have to set the context manually since _PyErr_SetObject() doesn't.
92101 _PyErr_ChainExceptions1Tstate (tstate , ctx );
93102 _ensure_current_cause (tstate , cause );
94103}
95104
96105static void
97- format_notshareableerror_v (PyThreadState * tstate , PyObject * cause ,
106+ set_notshareableerror (PyThreadState * tstate , PyObject * cause , int force , const char * msg )
107+ {
108+ PyObject * msgobj = PyUnicode_FromString (msg );
109+ if (msgobj == NULL ) {
110+ assert (_PyErr_Occurred (tstate ));
111+ }
112+ else {
113+ _ensure_notshareableerror (tstate , cause , force , msgobj );
114+ }
115+ }
116+
117+ static void
118+ format_notshareableerror_v (PyThreadState * tstate , PyObject * cause , int force ,
98119 const char * format , va_list vargs )
99120{
100- PyObject * exctype = get_notshareableerror_type (tstate );
101- if (exctype == NULL ) {
102- exctype = PyExc_ValueError ;
121+ PyObject * msgobj = PyUnicode_FromFormatV (format , vargs );
122+ if (msgobj == NULL ) {
123+ assert (_PyErr_Occurred (tstate ));
124+ }
125+ else {
126+ _ensure_notshareableerror (tstate , cause , force , msgobj );
103127 }
104- // We have to set the context manually since _PyErr_SetObject() won't.
105- PyObject * ctx = _PyErr_GetRaisedException (tstate );
106- _PyErr_FormatV (tstate , exctype , format , vargs );
107- _PyErr_ChainExceptions1Tstate (tstate , ctx );
108- _ensure_current_cause (tstate , cause );
109128}
110129
111130static void
112- format_notshareableerror (PyThreadState * tstate , PyObject * cause ,
131+ format_notshareableerror (PyThreadState * tstate , PyObject * cause , int force ,
113132 const char * format , ...)
114133{
115134 va_list vargs ;
116135 va_start (vargs , format );
117- format_notshareableerror_v (tstate , cause , format , vargs );
136+ format_notshareableerror_v (tstate , cause , force , format , vargs );
118137 va_end (vargs );
119138}
120139
0 commit comments