@@ -819,9 +819,19 @@ dir_fd_converter(PyObject *o, void *p)
819
819
}
820
820
}
821
821
822
+ #ifdef HAVE_PUTENV
823
+ # define PY_PUTENV_DICT
824
+ #endif
825
+
822
826
typedef struct {
823
827
PyObject * billion ;
824
- PyObject * posix_putenv_garbage ;
828
+ #ifdef PY_PUTENV_DICT
829
+ /* putenv() and _wputenv() requires that the caller manages the environment
830
+ variable memory. Use a Python dictionary for that: name => env, where
831
+ env is a string like "name=value". On Windows, dict keys and values are
832
+ Unicode strings. On Unix, they are bytes strings. */
833
+ PyObject * putenv_dict ;
834
+ #endif
825
835
PyObject * DirEntryType ;
826
836
PyObject * ScandirIteratorType ;
827
837
#if defined(HAVE_SCHED_SETPARAM ) || defined(HAVE_SCHED_SETSCHEDULER ) || defined(POSIX_SPAWN_SETSCHEDULER ) || defined(POSIX_SPAWN_SETSCHEDPARAM )
@@ -2105,7 +2115,9 @@ static int
2105
2115
_posix_clear (PyObject * module )
2106
2116
{
2107
2117
Py_CLEAR (_posixstate (module )-> billion );
2108
- Py_CLEAR (_posixstate (module )-> posix_putenv_garbage );
2118
+ #ifdef PY_PUTENV_DICT
2119
+ Py_CLEAR (_posixstate (module )-> putenv_dict );
2120
+ #endif
2109
2121
Py_CLEAR (_posixstate (module )-> DirEntryType );
2110
2122
Py_CLEAR (_posixstate (module )-> ScandirIteratorType );
2111
2123
#if defined(HAVE_SCHED_SETPARAM ) || defined(HAVE_SCHED_SETSCHEDULER ) || defined(POSIX_SPAWN_SETSCHEDULER ) || defined(POSIX_SPAWN_SETSCHEDPARAM )
@@ -2130,7 +2142,9 @@ static int
2130
2142
_posix_traverse (PyObject * module , visitproc visit , void * arg )
2131
2143
{
2132
2144
Py_VISIT (_posixstate (module )-> billion );
2133
- Py_VISIT (_posixstate (module )-> posix_putenv_garbage );
2145
+ #ifdef PY_PUTENV_DICT
2146
+ Py_VISIT (_posixstate (module )-> putenv_dict );
2147
+ #endif
2134
2148
Py_VISIT (_posixstate (module )-> DirEntryType );
2135
2149
Py_VISIT (_posixstate (module )-> ScandirIteratorType );
2136
2150
#if defined(HAVE_SCHED_SETPARAM ) || defined(HAVE_SCHED_SETSCHEDULER ) || defined(POSIX_SPAWN_SETSCHEDULER ) || defined(POSIX_SPAWN_SETSCHEDPARAM )
@@ -10047,23 +10061,26 @@ os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
10047
10061
}
10048
10062
#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
10049
10063
10050
- #ifdef HAVE_PUTENV
10051
10064
10065
+ #ifdef PY_PUTENV_DICT
10052
10066
static void
10053
- posix_putenv_garbage_setitem (PyObject * name , PyObject * value )
10067
+ posix_putenv_dict_setitem (PyObject * name , PyObject * value )
10054
10068
{
10055
- /* Install the first arg and newstr in posix_putenv_garbage ;
10069
+ /* Install the first arg and newstr in putenv_dict ;
10056
10070
* this will cause previous value to be collected. This has to
10057
10071
* happen after the real putenv() call because the old value
10058
10072
* was still accessible until then. */
10059
- if (PyDict_SetItem (_posixstate_global -> posix_putenv_garbage , name , value ))
10073
+ if (PyDict_SetItem (_posixstate_global -> putenv_dict , name , value ))
10060
10074
/* really not much we can do; just leak */
10061
10075
PyErr_Clear ();
10062
10076
else
10063
10077
Py_DECREF (value );
10064
10078
}
10079
+ #endif /* PY_PUTENV_DICT */
10065
10080
10066
10081
10082
+ #ifdef HAVE_PUTENV
10083
+
10067
10084
#ifdef MS_WINDOWS
10068
10085
/*[clinic input]
10069
10086
os.putenv
@@ -10114,7 +10131,7 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10114
10131
goto error ;
10115
10132
}
10116
10133
10117
- posix_putenv_garbage_setitem (name , unicode );
10134
+ posix_putenv_dict_setitem (name , unicode );
10118
10135
Py_RETURN_NONE ;
10119
10136
10120
10137
error :
@@ -10156,7 +10173,7 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10156
10173
return posix_error ();
10157
10174
}
10158
10175
10159
- posix_putenv_garbage_setitem (name , bytes );
10176
+ posix_putenv_dict_setitem (name , bytes );
10160
10177
Py_RETURN_NONE ;
10161
10178
}
10162
10179
#endif /* MS_WINDOWS */
@@ -10189,18 +10206,20 @@ os_unsetenv_impl(PyObject *module, PyObject *name)
10189
10206
return PyErr_SetFromWindowsErr (0 );
10190
10207
}
10191
10208
10192
- /* Remove the key from posix_putenv_garbage;
10209
+ #ifdef PY_PUTENV_DICT
10210
+ /* Remove the key from putenv_dict;
10193
10211
* this will cause it to be collected. This has to
10194
10212
* happen after the real unsetenv() call because the
10195
10213
* old value was still accessible until then.
10196
10214
*/
10197
- if (PyDict_DelItem (_posixstate (module )-> posix_putenv_garbage , name )) {
10215
+ if (PyDict_DelItem (_posixstate (module )-> putenv_dict , name )) {
10198
10216
/* really not much we can do; just leak */
10199
10217
if (!PyErr_ExceptionMatches (PyExc_KeyError )) {
10200
10218
return NULL ;
10201
10219
}
10202
10220
PyErr_Clear ();
10203
10221
}
10222
+ #endif
10204
10223
10205
10224
Py_RETURN_NONE ;
10206
10225
}
@@ -10230,18 +10249,21 @@ os_unsetenv_impl(PyObject *module, PyObject *name)
10230
10249
return posix_error ();
10231
10250
#endif
10232
10251
10233
- /* Remove the key from posix_putenv_garbage;
10252
+ #ifdef PY_PUTENV_DICT
10253
+ /* Remove the key from putenv_dict;
10234
10254
* this will cause it to be collected. This has to
10235
10255
* happen after the real unsetenv() call because the
10236
10256
* old value was still accessible until then.
10237
10257
*/
10238
- if (PyDict_DelItem (_posixstate (module )-> posix_putenv_garbage , name )) {
10258
+ if (PyDict_DelItem (_posixstate (module )-> putenv_dict , name )) {
10239
10259
/* really not much we can do; just leak */
10240
10260
if (!PyErr_ExceptionMatches (PyExc_KeyError )) {
10241
10261
return NULL ;
10242
10262
}
10243
10263
PyErr_Clear ();
10244
10264
}
10265
+ #endif
10266
+
10245
10267
Py_RETURN_NONE ;
10246
10268
}
10247
10269
#endif /* HAVE_UNSETENV */
@@ -14538,10 +14560,10 @@ INITFUNC(void)
14538
14560
Py_INCREF (PyExc_OSError );
14539
14561
PyModule_AddObject (m , "error" , PyExc_OSError );
14540
14562
14541
- #ifdef HAVE_PUTENV
14563
+ #ifdef PY_PUTENV_DICT
14542
14564
/* Save putenv() parameters as values here, so we can collect them when they
14543
14565
* get re-set with another call for the same key. */
14544
- _posixstate (m )-> posix_putenv_garbage = PyDict_New ();
14566
+ _posixstate (m )-> putenv_dict = PyDict_New ();
14545
14567
#endif
14546
14568
14547
14569
#if defined(HAVE_WAITID ) && !defined(__APPLE__ )
0 commit comments