gh-139482: Add posix._clearenv() function#139965
Conversation
posix.clearenv() function
|
Benchmark: import os, time
VARIABLES = 10_000
env = os.environ
t1 = time.perf_counter()
for i in range(VARIABLES):
env[f"k{i}"] = "1"
t2 = time.perf_counter()
print(f"create {VARIABLES:,} variables: {(t2-t1)*1e3:.1f} ms")
t1 = time.perf_counter()
env.clear()
t2 = time.perf_counter()
print(f"clear {VARIABLES:,} variables: {(t2-t1)*1e3:.1f} ms")Without the change: With the change (on Linux with It's around 3,000x faster 😄 |
|
Is it enabled by default on your machine or did you need to use some feature macros when compiling the project? |
Misc/NEWS.d/next/Library/2025-10-11-20-03-13.gh-issue-139482.du2Stg.rst
Outdated
Show resolved
Hide resolved
|
I should note that I've seen many times the Windows failure today (the failure is in |
I just modified the
I created #139970 to track the test_profiling failure on Windows.
I prefer to not document it. I just rename it to |
posix.clearenv() functionposix._clearenv() function
|
I tested my PR on my FreeBSD VM. main branch: With It's 201x faster. |
| OS_POSIX_FADVISE_METHODDEF | ||
| OS_PUTENV_METHODDEF | ||
| OS_UNSETENV_METHODDEF | ||
| OS__CLEARENV_METHODDEF |
There was a problem hiding this comment.
Shouldn't this also be inside ifdef guard?
There was a problem hiding this comment.
AC handles this by creating a no-op macro
There was a problem hiding this comment.
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.environ.clear()has a quadratic time complexity #139482