@@ -239,9 +239,11 @@ static const PyConfigSpec PYPRECONFIG_SPEC[] = {
239239
240240
241241// Forward declarations
242- static PyObject *
243- config_get (const PyConfig * config , const PyConfigSpec * spec ,
244- int use_sys );
242+ static PyObject * config_get (const PyConfig * config , const PyConfigSpec * spec ,
243+ int use_sys );
244+ static void initconfig_free_wstr (wchar_t * member );
245+ static void initconfig_free_wstr_list (PyWideStringList * list );
246+ static void initconfig_free_config (const PyConfig * config );
245247
246248
247249/* --- Command line options --------------------------------------- */
@@ -3725,6 +3727,8 @@ PyInitConfig_Free(PyInitConfig *config)
37253727 if (config == NULL ) {
37263728 return ;
37273729 }
3730+
3731+ initconfig_free_config (& config -> config );
37283732 free (config -> err_msg );
37293733 free (config );
37303734}
@@ -4093,9 +4097,46 @@ PyInitConfig_SetStr(PyInitConfig *config, const char *name, const char* value)
40934097}
40944098
40954099
4100+ static void
4101+ initconfig_free_wstr (wchar_t * member )
4102+ {
4103+ if (member ) {
4104+ free (member );
4105+ }
4106+ }
4107+
4108+
4109+ static void
4110+ initconfig_free_wstr_list (PyWideStringList * list )
4111+ {
4112+ for (Py_ssize_t i = 0 ; i < list -> length ; i ++ ) {
4113+ initconfig_free_wstr (list -> items [i ]);
4114+ }
4115+ }
4116+
4117+
4118+ static void
4119+ initconfig_free_config (const PyConfig * config )
4120+ {
4121+ const PyConfigSpec * spec = PYCONFIG_SPEC ;
4122+ for (; spec -> name != NULL ; spec ++ ) {
4123+ void * member = config_get_spec_member (config , spec );
4124+ if (spec -> type == PyConfig_MEMBER_WSTR
4125+ || spec -> type == PyConfig_MEMBER_WSTR_OPT )
4126+ {
4127+ wchar_t * wstr = * (wchar_t * * )member ;
4128+ initconfig_free_wstr (wstr );
4129+ }
4130+ else if (spec -> type == PyConfig_MEMBER_WSTR_LIST ) {
4131+ initconfig_free_wstr_list (member );
4132+ }
4133+ }
4134+ }
4135+
4136+
40964137static int
4097- _PyWideStringList_FromUTF8 (PyInitConfig * config , PyWideStringList * list ,
4098- Py_ssize_t length , char * const * items )
4138+ initconfig_set_str_list (PyInitConfig * config , PyWideStringList * list ,
4139+ Py_ssize_t length , char * const * items )
40994140{
41004141 PyWideStringList wlist = _PyWideStringList_INIT ;
41014142 size_t size = sizeof (wchar_t * ) * length ;
@@ -4108,14 +4149,14 @@ _PyWideStringList_FromUTF8(PyInitConfig *config, PyWideStringList *list,
41084149 for (Py_ssize_t i = 0 ; i < length ; i ++ ) {
41094150 wchar_t * arg = utf8_to_wstr (config , items [i ]);
41104151 if (arg == NULL ) {
4111- _PyWideStringList_Clear (& wlist );
4152+ initconfig_free_wstr_list (& wlist );
41124153 return -1 ;
41134154 }
41144155 wlist .items [i ] = arg ;
41154156 wlist .length ++ ;
41164157 }
41174158
4118- _PyWideStringList_Clear (list );
4159+ initconfig_free_wstr_list (list );
41194160 * list = wlist ;
41204161 return 0 ;
41214162}
@@ -4136,7 +4177,7 @@ PyInitConfig_SetStrList(PyInitConfig *config, const char *name,
41364177 return -1 ;
41374178 }
41384179 PyWideStringList * list = raw_member ;
4139- if (_PyWideStringList_FromUTF8 (config , list , length , items ) < 0 ) {
4180+ if (initconfig_set_str_list (config , list , length , items ) < 0 ) {
41404181 return -1 ;
41414182 }
41424183
0 commit comments