15
15
#define MODULE_NAME "_xxsubinterpreters"
16
16
17
17
18
- static char *
18
+ static const char *
19
19
_copy_raw_string (PyObject * strobj )
20
20
{
21
21
const char * str = PyUnicode_AsUTF8 (strobj );
22
22
if (str == NULL ) {
23
23
return NULL ;
24
24
}
25
- char * copied = PyMem_Malloc (strlen (str )+ 1 );
25
+ char * copied = PyMem_RawMalloc (strlen (str )+ 1 );
26
26
if (copied == NULL ) {
27
27
PyErr_NoMemory ();
28
28
return NULL ;
@@ -128,7 +128,7 @@ clear_module_state(module_state *state)
128
128
/* data-sharing-specific code ***********************************************/
129
129
130
130
struct _sharednsitem {
131
- char * name ;
131
+ const char * name ;
132
132
_PyCrossInterpreterData data ;
133
133
};
134
134
@@ -152,7 +152,7 @@ static void
152
152
_sharednsitem_clear (struct _sharednsitem * item )
153
153
{
154
154
if (item -> name != NULL ) {
155
- PyMem_Free ( item -> name );
155
+ PyMem_RawFree (( void * ) item -> name );
156
156
item -> name = NULL ;
157
157
}
158
158
(void )_release_xid_data (& item -> data , 1 );
@@ -258,96 +258,74 @@ _sharedns_apply(_sharedns *shared, PyObject *ns)
258
258
// of the exception in the calling interpreter.
259
259
260
260
typedef struct _sharedexception {
261
- char * name ;
262
- char * msg ;
261
+ const char * name ;
262
+ const char * msg ;
263
263
} _sharedexception ;
264
264
265
- static _sharedexception *
266
- _sharedexception_new (void )
267
- {
268
- _sharedexception * err = PyMem_NEW (_sharedexception , 1 );
269
- if (err == NULL ) {
270
- PyErr_NoMemory ();
271
- return NULL ;
272
- }
273
- err -> name = NULL ;
274
- err -> msg = NULL ;
275
- return err ;
276
- }
265
+ static const struct _sharedexception no_exception = {
266
+ .name = NULL ,
267
+ .msg = NULL ,
268
+ };
277
269
278
270
static void
279
271
_sharedexception_clear (_sharedexception * exc )
280
272
{
281
273
if (exc -> name != NULL ) {
282
- PyMem_Free ( exc -> name );
274
+ PyMem_RawFree (( void * ) exc -> name );
283
275
}
284
276
if (exc -> msg != NULL ) {
285
- PyMem_Free ( exc -> msg );
277
+ PyMem_RawFree (( void * ) exc -> msg );
286
278
}
287
279
}
288
280
289
- static void
290
- _sharedexception_free (_sharedexception * exc )
291
- {
292
- _sharedexception_clear (exc );
293
- PyMem_Free (exc );
294
- }
295
-
296
- static _sharedexception *
297
- _sharedexception_bind (PyObject * exc )
281
+ static const char *
282
+ _sharedexception_bind (PyObject * exc , _sharedexception * sharedexc )
298
283
{
299
284
assert (exc != NULL );
300
- char * failure = NULL ;
301
-
302
- _sharedexception * err = _sharedexception_new ();
303
- if (err == NULL ) {
304
- goto finally ;
305
- }
285
+ const char * failure = NULL ;
306
286
307
- PyObject * name = PyUnicode_FromFormat ("%S" , Py_TYPE (exc ));
308
- if (name == NULL ) {
287
+ PyObject * nameobj = PyUnicode_FromFormat ("%S" , Py_TYPE (exc ));
288
+ if (nameobj == NULL ) {
309
289
failure = "unable to format exception type name" ;
310
- goto finally ;
290
+ goto error ;
311
291
}
312
- err -> name = _copy_raw_string (name );
313
- Py_DECREF (name );
314
- if (err -> name == NULL ) {
292
+ sharedexc -> name = _copy_raw_string (nameobj );
293
+ Py_DECREF (nameobj );
294
+ if (sharedexc -> name == NULL ) {
315
295
if (PyErr_ExceptionMatches (PyExc_MemoryError )) {
316
296
failure = "out of memory copying exception type name" ;
317
297
} else {
318
298
failure = "unable to encode and copy exception type name" ;
319
299
}
320
- goto finally ;
300
+ goto error ;
321
301
}
322
302
323
303
if (exc != NULL ) {
324
- PyObject * msg = PyUnicode_FromFormat ("%S" , exc );
325
- if (msg == NULL ) {
304
+ PyObject * msgobj = PyUnicode_FromFormat ("%S" , exc );
305
+ if (msgobj == NULL ) {
326
306
failure = "unable to format exception message" ;
327
- goto finally ;
307
+ goto error ;
328
308
}
329
- err -> msg = _copy_raw_string (msg );
330
- Py_DECREF (msg );
331
- if (err -> msg == NULL ) {
309
+ sharedexc -> msg = _copy_raw_string (msgobj );
310
+ Py_DECREF (msgobj );
311
+ if (sharedexc -> msg == NULL ) {
332
312
if (PyErr_ExceptionMatches (PyExc_MemoryError )) {
333
313
failure = "out of memory copying exception message" ;
334
314
} else {
335
315
failure = "unable to encode and copy exception message" ;
336
316
}
337
- goto finally ;
317
+ goto error ;
338
318
}
339
319
}
340
320
341
- finally :
342
- if (failure != NULL ) {
343
- PyErr_Clear ();
344
- if (err -> name != NULL ) {
345
- PyMem_Free (err -> name );
346
- err -> name = NULL ;
347
- }
348
- err -> msg = failure ;
349
- }
350
- return err ;
321
+ return NULL ;
322
+
323
+ error :
324
+ assert (failure != NULL );
325
+ PyErr_Clear ();
326
+ _sharedexception_clear (sharedexc );
327
+ * sharedexc = no_exception ;
328
+ return failure ;
351
329
}
352
330
353
331
static void
@@ -430,7 +408,7 @@ _ensure_not_running(PyInterpreterState *interp)
430
408
431
409
static int
432
410
_run_script (PyInterpreterState * interp , const char * codestr ,
433
- _sharedns * shared , _sharedexception * * exc )
411
+ _sharedns * shared , _sharedexception * sharedexc )
434
412
{
435
413
PyObject * excval = NULL ;
436
414
PyObject * main_mod = _PyInterpreterState_GetMainModule (interp );
@@ -462,22 +440,20 @@ _run_script(PyInterpreterState *interp, const char *codestr,
462
440
Py_DECREF (result ); // We throw away the result.
463
441
}
464
442
465
- * exc = NULL ;
443
+ * sharedexc = no_exception ;
466
444
return 0 ;
467
445
468
446
error :
469
447
excval = PyErr_GetRaisedException ();
470
- _sharedexception * sharedexc = _sharedexception_bind (excval );
471
- Py_XDECREF (excval );
472
- if (sharedexc == NULL ) {
473
- fprintf (stderr , "RunFailedError: script raised an uncaught exception" );
448
+ const char * failure = _sharedexception_bind (excval , sharedexc );
449
+ if (failure != NULL ) {
450
+ fprintf (stderr ,
451
+ "RunFailedError: script raised an uncaught exception (%s)" ,
452
+ failure );
474
453
PyErr_Clear ();
475
- sharedexc = NULL ;
476
- }
477
- else {
478
- assert (!PyErr_Occurred ());
479
454
}
480
- * exc = sharedexc ;
455
+ Py_XDECREF (excval );
456
+ assert (!PyErr_Occurred ());
481
457
return -1 ;
482
458
}
483
459
@@ -505,7 +481,7 @@ _run_script_in_interpreter(PyObject *mod, PyInterpreterState *interp,
505
481
}
506
482
507
483
// Run the script.
508
- _sharedexception * exc = NULL ;
484
+ _sharedexception exc ;
509
485
int result = _run_script (interp , codestr , shared , & exc );
510
486
511
487
// Switch back.
@@ -514,10 +490,9 @@ _run_script_in_interpreter(PyObject *mod, PyInterpreterState *interp,
514
490
}
515
491
516
492
// Propagate any exception out to the caller.
517
- if (exc != NULL ) {
493
+ if (exc . name != NULL ) {
518
494
assert (state != NULL );
519
- _sharedexception_apply (exc , state -> RunFailedError );
520
- _sharedexception_free (exc );
495
+ _sharedexception_apply (& exc , state -> RunFailedError );
521
496
}
522
497
else if (result != 0 ) {
523
498
// We were unable to allocate a shared exception.
0 commit comments