Uh oh!
There was an error while loading. Please reload this page.
gh-139482: Add posix._clearenv() function - #139965
Conversation
posix.clearenv() functionvstinner
commented
Oct 11, 2025
Benchmark: importos, timeVARIABLES=10_000env=os.environt1=time.perf_counter()
foriinrange(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 😄 |
picnixz
commented
Oct 11, 2025
Is it enabled by default on your machine or did you need to use some feature macros when compiling the project? |
Uh oh!
There was an error while loading. Please reload this page.
picnixz
commented
Oct 11, 2025
I should note that I've seen many times the Windows failure today (the failure is in |
vstinner
commented
Oct 11, 2025
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() functionvstinner
commented
Oct 11, 2025
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