Skip to content

Commit 69262fb

Browse files
committed
gh-139482: Add posix.clearenv() function
1 parent 5776d0d commit 69262fb

File tree

8 files changed

+74
-3
lines changed

8 files changed

+74
-3
lines changed

Lib/os.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,12 @@ def __ror__(self, other):
768768
new.update(self)
769769
return new
770770

771+
if _exists("clearenv"):
772+
def clear(self):
773+
clearenv()
774+
self._data.clear()
775+
776+
771777
def _create_environ_mapping():
772778
if name == 'nt':
773779
# Where Env Var Names Must Be UPPERCASE

Lib/test/test_os/test_os.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,14 @@ def test_reload_environ(self):
14941494
self.assertNotIn(b'test_env', os.environb)
14951495
self.assertNotIn('test_env', os.environ)
14961496

1497+
def test_clearenv(self):
1498+
os.environ['REMOVEME'] = '1'
1499+
os.environ.clear()
1500+
self.assertEqual(os.environ, {})
1501+
1502+
self.assertRaises(TypeError, os.environ.clear, None)
1503+
1504+
14971505
class WalkTests(unittest.TestCase):
14981506
"""Tests for os.walk()."""
14991507
is_fwalk = False
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Optimize :data:`os.environ.clear() <os.environ>` by calling ``clearenv()``
2+
when this function is available. Patch by Victor Stinner.

Modules/clinic/posixmodule.c.h

Lines changed: 26 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13201,6 +13201,25 @@ os_unsetenv_impl(PyObject *module, PyObject *name)
1320113201
#endif /* !MS_WINDOWS */
1320213202

1320313203

13204+
#ifdef HAVE_CLEARENV
13205+
/*[clinic input]
13206+
os.clearenv
13207+
[clinic start generated code]*/
13208+
13209+
static PyObject *
13210+
os_clearenv_impl(PyObject *module)
13211+
/*[clinic end generated code: output=417e500890b2b9cf input=04ce6a2cb66ec46e]*/
13212+
{
13213+
errno = 0;
13214+
int err = clearenv();
13215+
if (err) {
13216+
return posix_error();
13217+
}
13218+
Py_RETURN_NONE;
13219+
}
13220+
#endif
13221+
13222+
1320413223
/*[clinic input]
1320513224
os.strerror
1320613225
@@ -17167,6 +17186,7 @@ static PyMethodDef posix_methods[] = {
1716717186
OS_POSIX_FADVISE_METHODDEF
1716817187
OS_PUTENV_METHODDEF
1716917188
OS_UNSETENV_METHODDEF
17189+
OS_CLEARENV_METHODDEF
1717017190
OS_STRERROR_METHODDEF
1717117191
OS_FCHDIR_METHODDEF
1717217192
OS_FSYNC_METHODDEF

configure

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5226,7 +5226,8 @@ fi
52265226

52275227
# checks for library functions
52285228
AC_CHECK_FUNCS([ \
5229-
accept4 alarm bind_textdomain_codeset chmod chown clock closefrom close_range confstr \
5229+
accept4 alarm bind_textdomain_codeset chmod chown clearenv \
5230+
clock closefrom close_range confstr \
52305231
copy_file_range ctermid dladdr dup dup3 execv explicit_bzero explicit_memset \
52315232
faccessat fchmod fchmodat fchown fchownat fdopendir fdwalk fexecve \
52325233
fork fork1 fpathconf fstatat ftime ftruncate futimens futimes futimesat \
@@ -8173,7 +8174,7 @@ PY_STDLIB_MOD([xxlimited_35], [test "$TEST_MODULES" = yes], [test "$ac_cv_func_d
81738174

81748175
# Determine JIT stencils header files based on target platform
81758176
JIT_STENCILS_H=""
8176-
AS_VAR_IF([enable_experimental_jit], [no],
8177+
AS_VAR_IF([enable_experimental_jit], [no],
81778178
[],
81788179
[case "$host" in
81798180
aarch64-apple-darwin*)

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@
141141
/* Define if you have the 'chroot' function. */
142142
#undef HAVE_CHROOT
143143

144+
/* Define to 1 if you have the 'clearenv' function. */
145+
#undef HAVE_CLEARENV
146+
144147
/* Define to 1 if you have the 'clock' function. */
145148
#undef HAVE_CLOCK
146149

0 commit comments

Comments
 (0)