@@ -207,34 +207,49 @@ static char *CURSES_SCREEN_ENCODING = NULL;
207
207
#define PyCursesInitialisedColor \
208
208
CHECK_STATE_FLAG("start_color", CURSES_INITIALISED_COLORS)
209
209
210
- #define CHECK_NOT_NULL_OR_ERROR (VALUE ) \
211
- do { \
212
- if ((VALUE) == NULL) { \
213
- goto error; \
214
- } \
215
- } while (0)
216
-
217
- #define CHECK_RET_CODE_OR_ERROR (STATUS ) \
210
+ /* Jump to the 'error' label if STATUS < 0. */
211
+ #define CHECK_RET_CODE (STATUS ) \
218
212
do { \
219
213
if ((STATUS) < 0) { \
214
+ assert(PyErr_Occurred()); \
220
215
goto error; \
221
216
} \
222
217
} while (0)
223
218
224
- #define CHECK_RET_FLAG_OR_ERROR (STATUS ) \
225
- do { \
226
- if (!(STATUS)) { \
227
- goto error; \
228
- } \
219
+ /*
220
+ * Equivalent to DICT[NAME] = VALUE; on error, jump to the 'error' label.
221
+ *
222
+ * Parameters
223
+ *
224
+ * PyObject * DICT The Python dict to alter.
225
+ * const char * NAME The constant name.
226
+ * int or long VALUE The constant value.
227
+ */
228
+ #define DICT_ADD_INT_VALUE (DICT , NAME , VALUE ) \
229
+ do { \
230
+ PyObject *value = PyLong_FromLong((long)(VALUE)); \
231
+ if (value == NULL) { \
232
+ goto error; \
233
+ } \
234
+ int rc = PyDict_SetItemString((DICT), (NAME), value); \
235
+ Py_DECREF(value); \
236
+ CHECK_RET_CODE(rc); \
229
237
} while (0)
230
238
231
- #define DICT_ADD_INT_VALUE_OR_ERROR (DICT_OBJECT , STRING , VALUE ) \
232
- do { \
233
- PyObject *value = PyLong_FromLong((long)(VALUE)); \
234
- CHECK_NOT_NULL_OR_ERROR(value); \
235
- int rc = PyDict_SetItemString((DICT_OBJECT), (STRING), value); \
236
- Py_DECREF(value); \
237
- CHECK_RET_CODE_OR_ERROR(rc); \
239
+ /*
240
+ * Add an integral constant to a module; on error, jump to the 'error' label.
241
+ *
242
+ * Parameters
243
+ *
244
+ * PyObject * MODULE The module object to alter.
245
+ * const char * NAME The constant name.
246
+ * int or long VALUE The constant value.
247
+ */
248
+ #define MODULE_ADD_INT_CONSTANT (MODULE , NAME , VALUE ) \
249
+ do { \
250
+ long value = (long)(VALUE); \
251
+ int rc = PyModule_AddIntConstant((MODULE), (NAME), value); \
252
+ CHECK_RET_CODE(rc); \
238
253
} while (0)
239
254
240
255
/* Utility Functions */
@@ -754,13 +769,13 @@ PyCursesWindow_New(WINDOW *win, const char *encoding)
754
769
static void
755
770
PyCursesWindow_Dealloc (PyCursesWindowObject * wo )
756
771
{
757
- if (wo -> win != stdscr && wo -> win != NULL ) {
758
- delwin (wo -> win );
759
- wo -> win = NULL ;
772
+ if (wo -> win != stdscr ) {
773
+ // Silently ignore errors in delwin(3) (e.g., passing
774
+ // a NULL pointer results in delwin() returning ERR).
775
+ (void )delwin (wo -> win );
760
776
}
761
777
if (wo -> encoding != NULL ) {
762
778
PyMem_Free (wo -> encoding );
763
- wo -> encoding = NULL ;
764
779
}
765
780
PyObject_Free (wo );
766
781
}
@@ -3325,9 +3340,10 @@ _curses_initscr_impl(PyObject *module)
3325
3340
3326
3341
/* Here are some graphic symbols you can use */
3327
3342
PyObject * module_dict = PyModule_GetDict (module );
3328
- CHECK_NOT_NULL_OR_ERROR (module_dict );
3329
- #define SetDictInt (NAME , VALUE ) \
3330
- DICT_ADD_INT_VALUE_OR_ERROR(module_dict, (NAME), (VALUE))
3343
+ if (module_dict == NULL ) {
3344
+ goto error ;
3345
+ }
3346
+ #define SetDictInt (NAME , VALUE ) DICT_ADD_INT_VALUE(module_dict, (NAME), (VALUE))
3331
3347
3332
3348
SetDictInt ("ACS_ULCORNER" , (ACS_ULCORNER ));
3333
3349
SetDictInt ("ACS_LLCORNER" , (ACS_LLCORNER ));
@@ -3400,7 +3416,9 @@ _curses_initscr_impl(PyObject *module)
3400
3416
#undef SetDictInt
3401
3417
3402
3418
PyCursesWindowObject * winobj = (PyCursesWindowObject * )PyCursesWindow_New (win , NULL );
3403
- CHECK_NOT_NULL_OR_ERROR (winobj );
3419
+ if (winobj == NULL ) {
3420
+ goto error ;
3421
+ }
3404
3422
CURSES_SCREEN_ENCODING = winobj -> encoding ;
3405
3423
return (PyObject * )winobj ;
3406
3424
@@ -4008,27 +4026,21 @@ _curses_qiflush_impl(PyObject *module, int flag)
4008
4026
/* Internal helper used for updating curses.LINES, curses.COLS, _curses.LINES
4009
4027
* and _curses.COLS */
4010
4028
#if defined(HAVE_CURSES_RESIZETERM ) || defined(HAVE_CURSES_RESIZE_TERM )
4011
- static int
4029
+ static int /* 1 on success, 0 on failure */
4012
4030
update_lines_cols (PyObject * private_module )
4013
4031
{
4014
- PyObject * exposed_module = NULL , * o = NULL ;
4015
- exposed_module = PyImport_ImportModule ("curses" );
4016
- CHECK_NOT_NULL_OR_ERROR (exposed_module );
4017
- o = PyLong_FromLong (LINES );
4018
- CHECK_NOT_NULL_OR_ERROR (o );
4019
- CHECK_RET_CODE_OR_ERROR (PyObject_SetAttrString (exposed_module , "LINES" , o ));
4020
- CHECK_RET_CODE_OR_ERROR (PyObject_SetAttrString (private_module , "LINES" , o ));
4021
- Py_DECREF (o );
4022
- o = PyLong_FromLong (COLS );
4023
- CHECK_NOT_NULL_OR_ERROR (o );
4024
- CHECK_RET_CODE_OR_ERROR (PyObject_SetAttrString (exposed_module , "COLS" , o ));
4025
- CHECK_RET_CODE_OR_ERROR (PyObject_SetAttrString (private_module , "COLS" , o ));
4026
- Py_DECREF (o );
4032
+ PyObject * exposed_module = PyImport_ImportModule ("curses" );
4033
+ if (exposed_module == NULL ) {
4034
+ return 0 ;
4035
+ }
4036
+ MODULE_ADD_INT_CONSTANT (exposed_module , "LINES" , LINES );
4037
+ MODULE_ADD_INT_CONSTANT (private_module , "LINES" , LINES );
4038
+ MODULE_ADD_INT_CONSTANT (exposed_module , "COLS" , COLS );
4039
+ MODULE_ADD_INT_CONSTANT (private_module , "COLS" , COLS );
4027
4040
Py_DECREF (exposed_module );
4028
4041
return 1 ;
4029
4042
4030
4043
error :
4031
- Py_XDECREF (o );
4032
4044
Py_XDECREF (exposed_module );
4033
4045
return 0 ;
4034
4046
}
@@ -4122,16 +4134,18 @@ static PyObject *
4122
4134
_curses_resizeterm_impl (PyObject * module , int nlines , int ncols )
4123
4135
/*[clinic end generated code: output=56d6bcc5194ad055 input=0fca02ebad5ffa82]*/
4124
4136
{
4137
+ PyObject * result ;
4138
+
4125
4139
PyCursesInitialised ;
4126
4140
4127
- PyObject * result = PyCursesCheckERR (resizeterm (nlines , ncols ), "resizeterm" );
4128
- CHECK_NOT_NULL_OR_ERROR (result );
4129
- CHECK_RET_FLAG_OR_ERROR (update_lines_cols (module ));
4141
+ result = PyCursesCheckERR (resizeterm (nlines , ncols ), "resizeterm" );
4142
+ if (!result )
4143
+ return NULL ;
4144
+ if (!update_lines_cols (module )) {
4145
+ Py_DECREF (result );
4146
+ return NULL ;
4147
+ }
4130
4148
return result ;
4131
-
4132
- error :
4133
- Py_XDECREF (result );
4134
- return NULL ;
4135
4149
}
4136
4150
4137
4151
#endif
@@ -4159,16 +4173,18 @@ static PyObject *
4159
4173
_curses_resize_term_impl (PyObject * module , int nlines , int ncols )
4160
4174
/*[clinic end generated code: output=9e26d8b9ea311ed2 input=2197edd05b049ed4]*/
4161
4175
{
4176
+ PyObject * result ;
4177
+
4162
4178
PyCursesInitialised ;
4163
4179
4164
- PyObject * result = PyCursesCheckERR (resize_term (nlines , ncols ), "resize_term" );
4165
- CHECK_NOT_NULL_OR_ERROR (result );
4166
- CHECK_RET_FLAG_OR_ERROR (update_lines_cols (module ));
4180
+ result = PyCursesCheckERR (resize_term (nlines , ncols ), "resize_term" );
4181
+ if (!result )
4182
+ return NULL ;
4183
+ if (!update_lines_cols (module )) {
4184
+ Py_DECREF (result );
4185
+ return NULL ;
4186
+ }
4167
4187
return result ;
4168
-
4169
- error :
4170
- Py_XDECREF (result );
4171
- return NULL ;
4172
4188
}
4173
4189
#endif /* HAVE_CURSES_RESIZE_TERM */
4174
4190
@@ -4232,9 +4248,11 @@ _curses_start_color_impl(PyObject *module)
4232
4248
if (start_color () != ERR ) {
4233
4249
CURSES_INITIALISED_COLORS = TRUE;
4234
4250
PyObject * module_dict = PyModule_GetDict (module );
4235
- CHECK_NOT_NULL_OR_ERROR (module_dict );
4236
- DICT_ADD_INT_VALUE_OR_ERROR (module_dict , "COLORS" , COLORS );
4237
- DICT_ADD_INT_VALUE_OR_ERROR (module_dict , "COLOR_PAIRS" , COLOR_PAIRS );
4251
+ if (module_dict == NULL ) {
4252
+ return NULL ;
4253
+ }
4254
+ DICT_ADD_INT_VALUE (module_dict , "COLORS" , COLORS );
4255
+ DICT_ADD_INT_VALUE (module_dict , "COLOR_PAIRS" , COLOR_PAIRS );
4238
4256
Py_RETURN_NONE ;
4239
4257
}
4240
4258
else {
@@ -4615,7 +4633,9 @@ make_ncurses_version(PyTypeObject *type)
4615
4633
#define SET_VERSION_COMPONENT (INDEX , VALUE ) \
4616
4634
do { \
4617
4635
PyObject *o = PyLong_FromLong(VALUE); \
4618
- CHECK_NOT_NULL_OR_ERROR(o); \
4636
+ if (o == NULL) { \
4637
+ goto error; \
4638
+ } \
4619
4639
PyStructSequence_SET_ITEM(ncurses_version, INDEX, o); \
4620
4640
} while (0)
4621
4641
@@ -4764,18 +4784,22 @@ PyInit__curses(void)
4764
4784
PyObject * mod = NULL ;
4765
4785
4766
4786
/* Initialize object type */
4767
- CHECK_RET_CODE_OR_ERROR (PyType_Ready (& PyCursesWindow_Type ));
4787
+ CHECK_RET_CODE (PyType_Ready (& PyCursesWindow_Type ));
4768
4788
4769
4789
/* Create the module and add the functions */
4770
4790
mod = PyModule_Create (& _cursesmodule );
4771
- CHECK_NOT_NULL_OR_ERROR (mod );
4791
+ if (mod == NULL ) {
4792
+ goto error ;
4793
+ }
4772
4794
#ifdef Py_GIL_DISABLED
4773
4795
CHECK_RET_CODE_OR_ERROR (PyUnstable_Module_SetGIL (mod , Py_MOD_GIL_NOT_USED ));
4774
4796
#endif
4775
4797
4776
4798
/* Add some symbolic constants to the module */
4777
4799
PyObject * module_dict = PyModule_GetDict (mod );
4778
-
F438
CHECK_NOT_NULL_OR_ERROR (module_dict );
4800
+ if (module_dict == NULL ) {
4801
+ goto error ;
4802
+ }
4779
4803
4780
4804
void * * PyCurses_API = PyMem_Calloc (PyCurses_API_pointers , sizeof (void * ));
4781
4805
if (PyCurses_API == NULL ) {
@@ -4798,42 +4822,49 @@ PyInit__curses(void)
4798
4822
}
4799
4823
int rc = PyDict_SetItemString (module_dict , "_C_API" , c_api_object );
4800
4824
Py_DECREF (c_api_object );
4801
- CHECK_RET_CODE_OR_ERROR (rc );
4825
+ CHECK_RET_CODE (rc );
4802
4826
4803
4827
/* For exception curses.error */
4804
4828
PyCursesError = PyErr_NewException ("_curses.error" , NULL , NULL );
4805
- CHECK_NOT_NULL_OR_ERROR (PyCursesError );
4829
+ if (PyCursesError == NULL ) {
4830
+ goto error ;
4831
+ }
4806
4832
rc = PyDict_SetItemString (module_dict , "error" , PyCursesError );
4807
4833
Py_DECREF (PyCursesError );
4808
- CHECK_RET_CODE_OR_ERROR (rc );
4834
+ CHECK_RET_CODE (rc );
4809
4835
4810
4836
/* Make the version available */
4811
4837
PyObject * curses_version = PyBytes_FromString (PyCursesVersion );
4812
- CHECK_NOT_NULL_OR_ERROR (curses_version );
4838
+ if (curses_version == NULL ) {
4839
+ goto error ;
4840
+ }
4813
4841
rc = PyDict_SetItemString (module_dict , "version" , curses_version );
4814
4842
Py_DECREF (curses_version );
4815
- CHECK_RET_CODE_OR_ERROR (rc );
4843
+ CHECK_RET_CODE (rc );
4816
4844
Py_INCREF (curses_version );
4817
4845
rc = PyDict_SetItemString (module_dict , "__version__" , curses_version );
4818
4846
Py_CLEAR (curses_version );
4819
- CHECK_RET_CODE_OR_ERROR (rc );
4847
+ CHECK_RET_CODE (rc );
4820
4848
4821
4849
#ifdef NCURSES_VERSION
4822
4850
/* ncurses_version */
4823
4851
PyTypeObject * version_type ;
4824
4852
version_type = _PyStructSequence_NewType (& ncurses_version_desc ,
4825
4853
Py_TPFLAGS_DISALLOW_INSTANTIATION );
4826
- CHECK_NOT_NULL_OR_ERROR (version_type );
4854
+ if (version_type == NULL ) {
4855
+ goto error ;
4856
+ }
4827
4857
PyObject * ncurses_version = make_ncurses_version (version_type );
4828
4858
Py_DECREF (version_type );
4829
- CHECK_NOT_NULL_OR_ERROR (ncurses_version );
4859
+ if (ncurses_version == NULL ) {
4860
+ goto error ;
4861
+ }
4830
4862
rc = PyDict_SetItemString (module_dict , "ncurses_version" , ncurses_version );
4831
4863
Py_CLEAR (ncurses_version );
4832
- CHECK_RET_CODE_OR_ERROR (rc );
4864
+ CHECK_RET_CODE (rc );
4833
4865
#endif /* NCURSES_VERSION */
4834
4866
4835
- #define SetDictInt (NAME , VALUE ) \
4836
- DICT_ADD_INT_VALUE_OR_ERROR(module_dict, (NAME), (VALUE))
4867
+ #define SetDictInt (NAME , VALUE ) DICT_ADD_INT_VALUE(module_dict, (NAME), (VALUE))
4837
4868
4838
4869
SetDictInt ("ERR" , ERR );
4839
4870
SetDictInt ("OK" , OK );
@@ -4952,11 +4983,13 @@ PyInit__curses(void)
4952
4983
}
4953
4984
* p2 = (char )0 ;
4954
4985
PyObject * p_keycode = PyLong_FromLong ((long )keycode );
4955
- CHECK_NOT_NULL_OR_ERROR (p_keycode );
4986
+ if (p_keycode == NULL ) {
4987
+ goto error ;
4988
+ }
4956
4989
int rc = PyDict_SetItemString (module_dict , fn_key_name , p_keycode );
4957
4990
Py_DECREF (p_keycode );
4958
4991
PyMem_Free (fn_key_name );
4959
- CHECK_RET_CODE_OR_ERROR (rc );
4992
+ CHECK_RET_CODE (rc );
4960
4993
}
4961
4994
else {
4962
4995
SetDictInt (key_name , keycode );
@@ -4966,16 +4999,16 @@ PyInit__curses(void)
4966
4999
SetDictInt ("KEY_MAX" , KEY_MAX );
4967
5000
#undef SetDictInt
4968
5001
4969
- CHECK_RET_CODE_OR_ERROR (PyModule_AddType (mod , & PyCursesWindow_Type ));
5002
+ CHECK_RET_CODE (PyModule_AddType (mod , & PyCursesWindow_Type ));
4970
5003
return mod ;
4971
5004
4972
5005
error :
4973
5006
Py_XDECREF (mod );
4974
5007
return NULL ;
4975
5008
}
4976
5009
4977
- #undef DICT_ADD_INT_VALUE_OR_ERROR
5010
+ #undef DICT_ADD_INT_VALUE
4978
5011
#undef CHECK_RET_FLAG_OR_ERROR
4979
- #undef CHECK_RET_CODE_OR_ERROR
5012
+ #undef CHECK_RET_CODE
4980
5013
#undef CHECK_NOT_NULL_OR_ERROR
4981
5014
#undef CHECK_STATE_FLAG
0 commit comments