Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ def _get_exports_list(module):
__all__.append('_exit')
except ImportError:
pass
try:
from posix import _clearenv
__all__.append('_clearenv')
except ImportError:
pass
import posixpath as path

try:
Expand Down Expand Up @@ -768,6 +773,12 @@ def __ror__(self, other):
new.update(self)
return new

if _exists("_clearenv"):
def clear(self):
_clearenv()
self._data.clear()


def _create_environ_mapping():
if name == 'nt':
# Where Env Var Names Must Be UPPERCASE
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_os/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,14 @@ def test_reload_environ(self):
self.assertNotIn(b'test_env', os.environb)
self.assertNotIn('test_env', os.environ)

def test_clearenv(self):
os.environ['REMOVEME'] = '1'
os.environ.clear()
self.assertEqual(os.environ, {})

self.assertRaises(TypeError, os.environ.clear, None)


class WalkTests(unittest.TestCase):
"""Tests for os.walk()."""
is_fwalk = False
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Optimize :data:`os.environ.clear() <os.environ>` by calling
:manpage:`clearenv(3)` when this function is available.
Patch by Victor Stinner.
27 changes: 26 additions & 1 deletion Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -13201,6 +13201,25 @@ os_unsetenv_impl(PyObject *module, PyObject *name)
#endif /* !MS_WINDOWS */


#ifdef HAVE_CLEARENV
/*[clinic input]
os._clearenv
[clinic start generated code]*/

static PyObject *
os__clearenv_impl(PyObject *module)
/*[clinic end generated code: output=2d6705d62c014b51 input=47d2fa7f323c43ca]*/
{
errno = 0;
int err = clearenv();
if (err) {
return posix_error();
}
Py_RETURN_NONE;
}
#endif


/*[clinic input]
os.strerror

Expand Down Expand Up @@ -17167,6 +17186,7 @@ static PyMethodDef posix_methods[] = {
OS_POSIX_FADVISE_METHODDEF
OS_PUTENV_METHODDEF
OS_UNSETENV_METHODDEF
OS__CLEARENV_METHODDEF
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also be inside ifdef guard?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AC handles this by creating a no-op macro

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, AC is magic :-) It just works. Look at Modules/clinic/posixmodule.c.h:

#if defined(HAVE_CLEARENV)
...
#define OS__CLEARENV_METHODDEF    \
    {"_clearenv", (PyCFunction)os__clearenv, METH_NOARGS, os__clearenv__doc__}, 
... 
#endif /* defined(HAVE_CLEARENV) */ 

...

#ifndef OS__CLEARENV_METHODDEF
    #define OS__CLEARENV_METHODDEF
#endif /* !defined(OS__CLEARENV_METHODDEF) */

OS_STRERROR_METHODDEF
OS_FCHDIR_METHODDEF
OS_FSYNC_METHODDEF
Expand Down
6 changes: 6 additions & 0 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -5226,7 +5226,8 @@ fi

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

# Determine JIT stencils header files based on target platform
JIT_STENCILS_H=""
AS_VAR_IF([enable_experimental_jit], [no],
AS_VAR_IF([enable_experimental_jit], [no],
[],
[case "$host" in
aarch64-apple-darwin*)
Expand Down
3 changes: 3 additions & 0 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@
/* Define if you have the 'chroot' function. */
#undef HAVE_CHROOT

/* Define to 1 if you have the 'clearenv' function. */
#undef HAVE_CLEARENV

/* Define to 1 if you have the 'clock' function. */
#undef HAVE_CLOCK

Expand Down
Loading