33
33
34
34
// Forward declarations
35
35
static struct PyModuleDef _testcapimodule ;
36
- static PyObject * TestError ; /* set to exception object in init */
37
36
37
+ // Module state
38
+ typedef struct {
39
+ PyObject * error ; // _testcapi.error object
40
+ } testcapistate_t ;
41
+
42
+ static testcapistate_t *
43
+ get_testcapi_state (PyObject * module )
44
+ {
45
+ void * state = PyModule_GetState (module );
46
+ assert (state != NULL );
47
+ return (testcapistate_t * )state ;
48
+ }
38
49
39
- /* Raise TestError with test_name + ": " + msg, and return NULL. */
50
+ static PyObject *
51
+ get_testerror (PyObject * self ) {
52
+ testcapistate_t * state = get_testcapi_state ((PyObject * )Py_TYPE (self ));
53
+ return state -> error ;
54
+ }
55
+
56
+ /* Raise _testcapi.error with test_name + ": " + msg, and return NULL. */
40
57
41
58
static PyObject *
42
- raiseTestError (const char * test_name , const char * msg )
59
+ raiseTestError (PyObject * self , const char * test_name , const char * msg )
43
60
{
44
- PyErr_Format (TestError , "%s: %s" , test_name , msg );
61
+ PyErr_Format (get_testerror ( self ) , "%s: %s" , test_name , msg );
45
62
return NULL ;
46
63
}
47
64
@@ -52,10 +69,10 @@ raiseTestError(const char* test_name, const char* msg)
52
69
platforms have these hardcoded. Better safe than sorry.
53
70
*/
54
71
static PyObject *
55
- sizeof_error (const char * fatname , const char * typname ,
72
+ sizeof_error (PyObject * self , const char * fatname , const char * typname ,
56
73
int expected , int got )
57
74
{
58
- PyErr_Format (TestError ,
75
+ PyErr_Format (get_testerror ( self ) ,
59
76
"%s #define == %d but sizeof(%s) == %d" ,
60
77
fatname , expected , typname , got );
61
78
return (PyObject * )NULL ;
@@ -66,7 +83,7 @@ test_config(PyObject *self, PyObject *Py_UNUSED(ignored))
66
83
{
67
84
#define CHECK_SIZEOF (FATNAME , TYPE ) \
68
85
if (FATNAME != sizeof(TYPE)) \
69
- return sizeof_error(#FATNAME, #TYPE, FATNAME, sizeof(TYPE))
86
+ return sizeof_error(self, #FATNAME, #TYPE, FATNAME, sizeof(TYPE))
70
87
71
88
CHECK_SIZEOF (SIZEOF_SHORT , short );
72
89
CHECK_SIZEOF (SIZEOF_INT , int );
@@ -89,15 +106,15 @@ test_sizeof_c_types(PyObject *self, PyObject *Py_UNUSED(ignored))
89
106
#endif
90
107
#define CHECK_SIZEOF (TYPE , EXPECTED ) \
91
108
if (EXPECTED != sizeof(TYPE)) { \
92
- PyErr_Format(TestError, \
109
+ PyErr_Format(get_testerror(self), \
93
110
"sizeof(%s) = %u instead of %u", \
94
111
#TYPE, sizeof(TYPE), EXPECTED); \
95
112
return (PyObject*)NULL; \
96
113
}
97
114
#define IS_SIGNED (TYPE ) (((TYPE)-1) < (TYPE)0)
98
115
#define CHECK_SIGNNESS (TYPE , SIGNED ) \
99
116
if (IS_SIGNED(TYPE) != SIGNED) { \
100
- PyErr_Format(TestError, \
117
+ PyErr_Format(get_testerror(self), \
101
118
"%s signness is, instead of %i", \
102
119
#TYPE, IS_SIGNED(TYPE), SIGNED); \
103
120
return (PyObject*)NULL; \
@@ -170,7 +187,7 @@ test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored))
170
187
for (i = 0 ; i < NLIST ; ++ i ) {
171
188
PyObject * anint = PyList_GET_ITEM (list , i );
172
189
if (PyLong_AS_LONG (anint ) != NLIST - 1 - i ) {
173
- PyErr_SetString (TestError ,
190
+ PyErr_SetString (get_testerror ( self ) ,
174
191
"test_list_api: reverse screwed up" );
175
192
Py_DECREF (list );
176
193
return (PyObject * )NULL ;
@@ -183,7 +200,7 @@ test_list_api(PyObject *self, PyObject *Py_UNUSED(ignored))
183
200
}
184
201
185
202
static int
186
- test_dict_inner (int count )
203
+ test_dict_inner (PyObject * self , int count )
187
204
{
188
205
Py_ssize_t pos = 0 , iterations = 0 ;
189
206
int i ;
@@ -231,7 +248,7 @@ test_dict_inner(int count)
231
248
232
249
if (iterations != count ) {
233
250
PyErr_SetString (
234
- TestError ,
251
+ get_testerror ( self ) ,
235
252
"test_dict_iteration: dict iteration went wrong " );
236
253
return -1 ;
237
254
} else {
@@ -250,7 +267,7 @@ test_dict_iteration(PyObject* self, PyObject *Py_UNUSED(ignored))
250
267
int i ;
251
268
252
269
for (i = 0 ; i < 200 ; i ++ ) {
253
- if (test_dict_inner (i ) < 0 ) {
270
+ if (test_dict_inner (self , i ) < 0 ) {
254
271
return NULL ;
255
272
}
256
273
}
@@ -334,14 +351,14 @@ test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
334
351
if (obj == NULL ) {
335
352
PyErr_Clear ();
336
353
PyErr_SetString (
337
- TestError ,
354
+ get_testerror ( self ) ,
338
355
"test_lazy_hash_inheritance: failed to create object" );
339
356
return NULL ;
340
357
}
341
358
342
359
if (type -> tp_dict != NULL ) {
343
360
PyErr_SetString (
344
- TestError ,
361
+ get_testerror ( self ) ,
345
362
"test_lazy_hash_inheritance: type initialised too soon" );
346
363
Py_DECREF (obj );
347
364
return NULL ;
@@ -351,23 +368,23 @@ test_lazy_hash_inheritance(PyObject* self, PyObject *Py_UNUSED(ignored))
351
368
if ((hash == -1 ) && PyErr_Occurred ()) {
352
369
PyErr_Clear ();
353
370
PyErr_SetString (
354
- TestError ,
371
+ get_testerror ( self ) ,
355
372
"test_lazy_hash_inheritance: could not hash object" );
356
373
Py_DECREF (obj );
357
374
return NULL ;
358
375
}
359
376
360
377
if (type -> tp_dict == NULL ) {
361
378
PyErr_SetString (
362
- TestError ,
379
+ get_testerror ( self ) ,
363
380
"test_lazy_hash_inheritance: type not initialised by hash()" );
364
381
Py_DECREF (obj );
365
382
return NULL ;
366
383
}
367
384
368
385
if (type -> tp_hash != PyType_Type .tp_hash ) {
369
386
PyErr_SetString (
370
- TestError ,
387
+ get_testerror ( self ) ,
371
388
"test_lazy_hash_inheritance: unexpected hash function" );
372
389
Py_DECREF (obj );
373
390
return NULL ;
@@ -427,7 +444,7 @@ py_buildvalue_ints(PyObject *self, PyObject *args)
427
444
}
428
445
429
446
static int
430
- test_buildvalue_N_error (const char * fmt )
447
+ test_buildvalue_N_error (PyObject * self , const char * fmt )
431
448
{
432
449
PyObject * arg , * res ;
433
450
@@ -443,7 +460,7 @@ test_buildvalue_N_error(const char *fmt)
443
460
}
444
461
Py_DECREF (res );
445
462
if (Py_REFCNT (arg ) != 1 ) {
446
- PyErr_Format (TestError , "test_buildvalue_N: "
463
+ PyErr_Format (get_testerror ( self ) , "test_buildvalue_N: "
447
464
"arg was not decrefed in successful "
448
465
"Py_BuildValue(\"%s\")" , fmt );
449
466
return -1 ;
@@ -452,13 +469,13 @@ test_buildvalue_N_error(const char *fmt)
452
469
Py_INCREF (arg );
453
470
res = Py_BuildValue (fmt , raise_error , NULL , arg );
454
471
if (res != NULL || !PyErr_Occurred ()) {
455
- PyErr_Format (TestError , "test_buildvalue_N: "
472
+ PyErr_Format (get_testerror ( self ) , "test_buildvalue_N: "
456
473
"Py_BuildValue(\"%s\") didn't complain" , fmt );
457
474
return -1 ;
458
475
}
459
476
PyErr_Clear ();
460
477
if (Py_REFCNT (arg ) != 1 ) {
461
- PyErr_Format (TestError , "test_buildvalue_N: "
478
+ PyErr_Format (get_testerror ( self ) , "test_buildvalue_N: "
462
479
"arg was not decrefed in failed "
463
480
"Py_BuildValue(\"%s\")" , fmt );
464
481
return -1 ;
@@ -482,25 +499,25 @@ test_buildvalue_N(PyObject *self, PyObject *Py_UNUSED(ignored))
482
499
return NULL ;
483
500
}
484
501
if (res != arg ) {
485
- return raiseTestError ("test_buildvalue_N" ,
502
+ return raiseTestError (self , "test_buildvalue_N" ,
486
503
"Py_BuildValue(\"N\") returned wrong result" );
487
504
}
488
505
if (Py_REFCNT (arg ) != 2 ) {
489
- return raiseTestError ("test_buildvalue_N" ,
506
+ return raiseTestError (self , "test_buildvalue_N" ,
490
507
"arg was not decrefed in Py_BuildValue(\"N\")" );
491
508
}
492
509
Py_DECREF (res );
493
510
Py_DECREF (arg );
494
511
495
- if (test_buildvalue_N_error ("O&N" ) < 0 )
512
+ if (test_buildvalue_N_error (self , "O&N" ) < 0 )
496
513
return NULL ;
497
- if (test_buildvalue_N_error ("(O&N)" ) < 0 )
514
+ if (test_buildvalue_N_error (self , "(O&N)" ) < 0 )
498
515
return NULL ;
499
- if (test_buildvalue_N_error ("[O&N]" ) < 0 )
516
+ if (test_buildvalue_N_error (self , "[O&N]" ) < 0 )
500
517
return NULL ;
501
- if (test_buildvalue_N_error ("{O&N}" ) < 0 )
518
+ if (test_buildvalue_N_error (self , "{O&N}" ) < 0 )
502
519
return NULL ;
503
- if (test_buildvalue_N_error ("{()O&(())N}" ) < 0 )
520
+ if (test_buildvalue_N_error (self , "{()O&(())N}" ) < 0 )
504
521
return NULL ;
505
522
506
523
Py_RETURN_NONE ;
@@ -910,7 +927,7 @@ test_string_to_double(PyObject *self, PyObject *Py_UNUSED(ignored)) {
910
927
911
928
Py_RETURN_NONE ;
912
929
fail :
913
- return raiseTestError ("test_string_to_double" , msg );
930
+ return raiseTestError (self , "test_string_to_double" , msg );
914
931
#undef CHECK_STRING
915
932
#undef CHECK_INVALID
916
933
}
@@ -1061,7 +1078,7 @@ test_capsule(PyObject *self, PyObject *Py_UNUSED(ignored))
1061
1078
1062
1079
exit :
1063
1080
if (error ) {
1064
- return raiseTestError ("test_capsule" , error );
1081
+ return raiseTestError (self , "test_capsule" , error );
1065
1082
}
1066
1083
Py_RETURN_NONE ;
1067
1084
#undef FAIL
@@ -1272,7 +1289,7 @@ test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored))
1272
1289
ptr = view .buf ;
1273
1290
for (i = 0 ; i < 5 ; i ++ ) {
1274
1291
if (ptr [2 * i ] != i ) {
1275
- PyErr_SetString (TestError ,
1292
+ PyErr_SetString (get_testerror ( self ) ,
1276
1293
"test_from_contiguous: incorrect result" );
1277
1294
return NULL ;
1278
1295
}
@@ -1285,7 +1302,7 @@ test_from_contiguous(PyObject* self, PyObject *Py_UNUSED(ignored))
1285
1302
ptr = view .buf ;
1286
1303
for (i = 0 ; i < 5 ; i ++ ) {
1287
1304
if (* (ptr - 2 * i ) != i ) {
1288
- PyErr_SetString (TestError ,
1305
+ PyErr_SetString (get_testerror ( self ) ,
1289
1306
"test_from_contiguous: incorrect result" );
1290
1307
return NULL ;
1291
1308
}
@@ -1338,7 +1355,7 @@ test_pep3118_obsolete_write_locks(PyObject* self, PyObject *Py_UNUSED(ignored))
1338
1355
Py_RETURN_NONE ;
1339
1356
1340
1357
error :
1341
- PyErr_SetString (TestError ,
1358
+ PyErr_SetString (get_testerror ( self ) ,
1342
1359
"test_pep3118_obsolete_write_locks: failure" );
1343
1360
return NULL ;
1344
1361
}
@@ -1959,7 +1976,7 @@ test_pythread_tss_key_state(PyObject *self, PyObject *args)
1959
1976
{
1960
1977
Py_tss_t tss_key = Py_tss_NEEDS_INIT ;
1961
1978
if (PyThread_tss_is_created (& tss_key )) {
1962
- return raiseTestError ("test_pythread_tss_key_state" ,
1979
+ return raiseTestError (self , "test_pythread_tss_key_state" ,
1963
1980
"TSS key not in an uninitialized state at "
1964
1981
"creation time" );
1965
1982
}
@@ -1968,27 +1985,27 @@ test_pythread_tss_key_state(PyObject *self, PyObject *args)
1968
1985
return NULL ;
1969
1986
}
1970
1987
if (!PyThread_tss_is_created (& tss_key )) {
1971
- return raiseTestError ("test_pythread_tss_key_state" ,
1988
+ return raiseTestError (self , "test_pythread_tss_key_state" ,
1972
1989
"PyThread_tss_create succeeded, "
1973
1990
"but with TSS key in an uninitialized state" );
1974
1991
}
1975
1992
if (PyThread_tss_create (& tss_key ) != 0 ) {
1976
- return raiseTestError ("test_pythread_tss_key_state" ,
1993
+ return raiseTestError (self , "test_pythread_tss_key_state" ,
1977
1994
"PyThread_tss_create unsuccessful with "
1978
1995
"an already initialized key" );
1979
1996
}
1980
1997
#define CHECK_TSS_API (expr ) \
1981
1998
(void)(expr); \
1982
1999
if (!PyThread_tss_is_created(&tss_key)) { \
1983
- return raiseTestError("test_pythread_tss_key_state", \
2000
+ return raiseTestError(self, "test_pythread_tss_key_state", \
1984
2001
"TSS key initialization state was not " \
1985
2002
"preserved after calling " #expr); }
1986
2003
CHECK_TSS_API (PyThread_tss_set (& tss_key , NULL ));
1987
2004
CHECK_TSS_API (PyThread_tss_get (& tss_key ));
1988
2005
#undef CHECK_TSS_API
1989
2006
PyThread_tss_delete (& tss_key );
1990
2007
if (PyThread_tss_is_created (& tss_key )) {
1991
- return raiseTestError ("test_pythread_tss_key_state" ,
2008
+ return raiseTestError (self , "test_pythread_tss_key_state" ,
1992
2009
"PyThread_tss_delete called, but did not "
1993
2010
"set the key state to uninitialized" );
1994
2011
}
@@ -1999,7 +2016,7 @@ test_pythread_tss_key_state(PyObject *self, PyObject *args)
1999
2016
return NULL ;
2000
2017
}
2001
2018
if (PyThread_tss_is_created (ptr_key )) {
2002
- return raiseTestError ("test_pythread_tss_key_state" ,
2019
+ return raiseTestError (self , "test_pythread_tss_key_state" ,
2003
2020
"TSS key not in an uninitialized state at "
2004
2021
"allocation time" );
2005
2022
}
@@ -3831,14 +3848,9 @@ static PyTypeObject ContainerNoGC_type = {
3831
3848
3832
3849
static struct PyModuleDef _testcapimodule = {
3833
3850
PyModuleDef_HEAD_INIT ,
3834
- "_testcapi" ,
3835
- NULL ,
3836
- -1 ,
3837
- TestMethods ,
3838
- NULL ,
3839
- NULL ,
3840
- NULL ,
3841
- NULL
3851
+ .m_name = "_testcapi" ,
3852
+ .m_size = sizeof (testcapistate_t ),
3853
+ .m_methods = TestMethods ,
3842
3854
};
3843
3855
3844
3856
/* Per PEP 489, this module will not be converted to multi-phase initialization
@@ -3933,9 +3945,10 @@ PyInit__testcapi(void)
3933
3945
PyModule_AddIntConstant (m , "the_number_three" , 3 );
3934
3946
PyModule_AddIntMacro (m , Py_C_RECURSION_LIMIT );
3935
3947
3936
- TestError = PyErr_NewException ("_testcapi.error" , NULL , NULL );
3937
- Py_INCREF (TestError );
3938
- PyModule_AddObject (m , "error" , TestError );
3948
+ testcapistate_t * state = get_testcapi_state (m );
3949
+ state -> error = PyErr_NewException ("_testcapi.error" , NULL , NULL );
3950
+ Py_INCREF (state -> error );
3951
+ PyModule_AddObject (m , "error" , state -> error );
3939
3952
3940
3953
if (PyType_Ready (& ContainerNoGC_type ) < 0 ) {
3941
3954
return NULL ;
0 commit comments