@@ -147,8 +147,19 @@ readline_free(void *m)
147
147
148
148
static PyModuleDef readlinemodule ;
149
149
150
- #define readlinestate_global ((readlinestate *)PyModule_GetState(PyState_FindModule(&readlinemodule)))
151
-
150
+ static inline readlinestate *
151
+ get_hook_module_state (void )
152
+ {
153
+ PyObject * mod = PyState_FindModule (& readlinemodule );
154
+ if (mod == NULL ){
155
+ PyErr_Clear ();
156
+ return NULL ;
157
+ }
158
+ Py_INCREF (mod );
159
+ readlinestate * state = get_readline_state (mod );
160
+ Py_DECREF (mod );
161
+ return state ;
162
+ }
152
163
153
164
/* Convert to/from multibyte C strings */
154
165
@@ -438,14 +449,15 @@ readline_set_completion_display_matches_hook_impl(PyObject *module,
438
449
PyObject * function )
439
450
/*[clinic end generated code: output=516e5cb8db75a328 input=4f0bfd5ab0179a26]*/
440
451
{
452
+ readlinestate * state = get_readline_state (module );
441
453
PyObject * result = set_hook ("completion_display_matches_hook" ,
442
- & readlinestate_global -> completion_display_matches_hook ,
454
+ & state -> completion_display_matches_hook ,
443
455
function );
444
456
#ifdef HAVE_RL_COMPLETION_DISPLAY_MATCHES_HOOK
445
457
/* We cannot set this hook globally, since it replaces the
446
458
default completion display. */
447
459
rl_completion_display_matches_hook =
448
- readlinestate_global -> completion_display_matches_hook ?
460
+ state -> completion_display_matches_hook ?
449
461
#if defined(HAVE_RL_COMPDISP_FUNC_T )
450
462
(rl_compdisp_func_t * )on_completion_display_matches_hook : 0 ;
451
463
#else
@@ -472,7 +484,8 @@ static PyObject *
472
484
readline_set_startup_hook_impl (PyObject * module , PyObject * function )
473
485
/*[clinic end generated code: output=02cd0e0c4fa082ad input=7783b4334b26d16d]*/
474
486
{
475
- return set_hook ("startup_hook" , & readlinestate_global -> startup_hook ,
487
+ readlinestate * state = get_readline_state (module );
488
+ return set_hook ("startup_hook" , & state -> startup_hook ,
476
489
function );
477
490
}
478
491
@@ -497,7 +510,8 @@ static PyObject *
497
510
readline_set_pre_input_hook_impl (PyObject * module , PyObject * function )
498
511
/*[clinic end generated code: output=fe1a96505096f464 input=4f3eaeaf7ce1fdbe]*/
499
512
{
500
- return set_hook ("pre_input_hook" , & readlinestate_global -> pre_input_hook ,
513
+ readlinestate * state = get_readline_state (module );
514
+ return set_hook ("pre_input_hook" , & state -> pre_input_hook ,
501
515
function );
502
516
}
503
517
#endif
@@ -530,7 +544,8 @@ static PyObject *
530
544
readline_get_begidx_impl (PyObject * module )
531
545
/*[clinic end generated code: output=362616ee8ed1b2b1 input=e083b81c8eb4bac3]*/
532
546
{
533
- return Py_NewRef (readlinestate_global -> begidx );
547
+ readlinestate * state = get_readline_state (module );
548
+ return Py_NewRef (state -> begidx );
534
549
}
535
550
536
551
/* Get the ending index for the scope of the tab-completion */
@@ -545,7 +560,8 @@ static PyObject *
545
560
readline_get_endidx_impl (PyObject * module )
546
561
/*[clinic end generated code: output=7f763350b12d7517 input=d4c7e34a625fd770]*/
547
562
{
548
- return Py_NewRef (readlinestate_global -> endidx );
563
+ readlinestate * state = get_readline_state (module );
564
+ return Py_NewRef (state -> endidx );
549
565
}
550
566
551
567
/* Set the tab-completion word-delimiters that readline uses */
@@ -772,7 +788,8 @@ static PyObject *
772
788
readline_set_completer_impl (PyObject * module , PyObject * function )
773
789
/*[clinic end generated code: output=171a2a60f81d3204 input=51e81e13118eb877]*/
774
790
{
775
- return set_hook ("completer" , & readlinestate_global -> completer , function );
791
+ readlinestate * state = get_readline_state (module );
792
+ return set_hook ("completer" , & state -> completer , function );
776
793
}
777
794
778
795
/*[clinic input]
@@ -785,10 +802,11 @@ static PyObject *
785
802
readline_get_completer_impl (PyObject * module )
786
803
/*[clinic end generated code: output=6e6bbd8226d14475 input=6457522e56d70d13]*/
787
804
{
788
- if (readlinestate_global -> completer == NULL ) {
805
+ readlinestate * state = get_readline_state (module );
806
+ if (state -> completer == NULL ) {
789
807
Py_RETURN_NONE ;
790
808
}
791
- return Py_NewRef (readlinestate_global -> completer );
809
+ return Py_NewRef (state -> completer );
792
810
}
793
811
794
812
/* Private function to get current length of history. XXX It may be
@@ -1026,7 +1044,12 @@ on_startup_hook(void)
1026
1044
{
1027
1045
int r ;
1028
1046
PyGILState_STATE gilstate = PyGILState_Ensure ();
1029
- r = on_hook (readlinestate_global -> startup_hook );
1047
+ readlinestate * state = get_hook_module_state ();
1048
+ if (state == NULL ) {
1049
+ PyGILState_Release (gilstate );
1050
+ return -1 ;
1051
+ }
1052
+ r = on_hook (state -> startup_hook );
1030
1053
PyGILState_Release (gilstate );
1031
1054
return r ;
1032
1055
}
@@ -1043,7 +1066,12 @@ on_pre_input_hook(void)
1043
1066
{
1044
1067
int r ;
1045
1068
PyGILState_STATE gilstate = PyGILState_Ensure ();
1046
- r = on_hook (readlinestate_global -> pre_input_hook );
1069
+ readlinestate * state = get_hook_module_state ();
1070
+ if (state == NULL ) {
1071
+ PyGILState_Release (gilstate );
1072
+ return -1 ;
1073
+ }
1074
+ r = on_hook (state -> pre_input_hook );
1047
1075
PyGILState_Release (gilstate );
1048
1076
return r ;
1049
1077
}
@@ -1060,6 +1088,11 @@ on_completion_display_matches_hook(char **matches,
1060
1088
int i ;
1061
1089
PyObject * sub , * m = NULL , * s = NULL , * r = NULL ;
1062
1090
PyGILState_STATE gilstate = PyGILState_Ensure ();
1091
+ readlinestate * state = get_hook_module_state ();
1092
+ if (state == NULL ) {
1093
+ PyGILState_Release (gilstate );
1094
+ return ;
1095
+ }
1063
1096
m = PyList_New (num_matches );
1064
1097
if (m == NULL )
1065
1098
goto error ;
@@ -1070,7 +1103,7 @@ on_completion_display_matches_hook(char **matches,
1070
1103
PyList_SET_ITEM (m , i , s );
1071
1104
}
1072
1105
sub = decode (matches [0 ]);
1073
- r = PyObject_CallFunction (readlinestate_global -> completion_display_matches_hook ,
1106
+ r = PyObject_CallFunction (state -> completion_display_matches_hook ,
1074
1107
"NNi" , sub , m , max_length );
1075
1108
1076
1109
m = NULL ;
@@ -1118,12 +1151,17 @@ static char *
1118
1151
on_completion (const char * text , int state )
1119
1152
{
1120
1153
char * result = NULL ;
1121
- if (readlinestate_global -> completer != NULL ) {
1154
+ PyGILState_STATE gilstate = PyGILState_Ensure ();
1155
+ readlinestate * module_state = get_hook_module_state ();
1156
+ if (module_state == NULL ) {
1157
+ PyGILState_Release (gilstate );
1158
+ return NULL ;
1159
+ }
1160
+ if (module_state -> completer != NULL ) {
1122
1161
PyObject * r = NULL , * t ;
1123
- PyGILState_STATE gilstate = PyGILState_Ensure ();
1124
1162
rl_attempted_completion_over = 1 ;
1125
1163
t = decode (text );
1126
- r = PyObject_CallFunction (readlinestate_global -> completer , "Ni" , t , state );
1164
+ r = PyObject_CallFunction (module_state -> completer , "Ni" , t , state );
1127
1165
if (r == NULL )
1128
1166
goto error ;
1129
1167
if (r == Py_None ) {
@@ -1145,6 +1183,7 @@ on_completion(const char *text, int state)
1145
1183
PyGILState_Release (gilstate );
1146
1184
return result ;
1147
1185
}
1186
+ PyGILState_Release (gilstate );
1148
1187
return result ;
1149
1188
}
1150
1189
@@ -1160,6 +1199,7 @@ flex_complete(const char *text, int start, int end)
1160
1199
size_t start_size , end_size ;
1161
1200
wchar_t * s ;
1162
1201
PyGILState_STATE gilstate = PyGILState_Ensure ();
1202
+ readlinestate * state = get_hook_module_state ();
1163
1203
#ifdef HAVE_RL_COMPLETION_APPEND_CHARACTER
1164
1204
rl_completion_append_character = '\0' ;
1165
1205
#endif
@@ -1187,10 +1227,12 @@ flex_complete(const char *text, int start, int end)
1187
1227
end = start + (int )end_size ;
1188
1228
1189
1229
done :
1190
- Py_XDECREF (readlinestate_global -> begidx );
1191
- Py_XDECREF (readlinestate_global -> endidx );
1192
- readlinestate_global -> begidx = PyLong_FromLong ((long ) start );
1193
- readlinestate_global -> endidx = PyLong_FromLong ((long ) end );
1230
+ if (state ) {
1231
+ Py_XDECREF (state -> begidx );
1232
+ Py_XDECREF (state -> endidx );
1233
+ state -> begidx = PyLong_FromLong ((long ) start );
1234
+ state -> endidx = PyLong_FromLong ((long ) end );
1235
+ }
1194
1236
result = completion_matches ((char * )text , * on_completion );
1195
1237
PyGILState_Release (gilstate );
1196
1238
return result ;
@@ -1511,12 +1553,17 @@ PyInit_readline(void)
1511
1553
}
1512
1554
1513
1555
mod_state = (readlinestate * ) PyModule_GetState (m );
1556
+ if (mod_state == NULL ){
1557
+ goto error ;
1558
+ }
1514
1559
PyOS_ReadlineFunctionPointer = call_readline ;
1515
1560
if (setup_readline (mod_state ) < 0 ) {
1516
1561
PyErr_NoMemory ();
1517
1562
goto error ;
1518
1563
}
1519
-
1564
+ if (PyErr_Occurred ()){
1565
+ goto error ;
1566
+ }
1520
1567
return m ;
1521
1568
1522
1569
error :
0 commit comments