Skip to content

Commit 35e9d41

Browse files
authored
gh-139482: Add posix._clearenv() function (#139965)
1 parent 166cdaa commit 35e9d41

File tree

8 files changed

+80
-3
lines changed

8 files changed

+80
-3
lines changed

Lib/os.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ def _get_exports_list(module):
5858
__all__.append('_exit')
5959
except ImportError:
6060
pass
61+
try:
62+
from posix import _clearenv
63+
__all__.append('_clearenv')
64+
except ImportError:
65+
pass
6166
import posixpath as path
6267

6368
try:
@@ -768,6 +773,12 @@ def __ror__(self, other):
768773
new.update(self)
769774
return new
770775

776+
if _exists("_clearenv"):
777+
def clear(self):
778+
_clearenv()
779+
self._data.clear()
780+
781+
771782
def _create_environ_mapping():
772783
if name == 'nt':
773784
# 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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Optimize :data:`os.environ.clear() <os.environ>` by calling
2+
:manpage:`clearenv(3)` when this function is available.
3+
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=2d6705d62c014b51 input=47d2fa7f323c43ca]*/
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)