Skip to content

gh-139482: Add posix._clearenv() function - #139965

Merged
vstinner merged 3 commits into
python:mainfrom
vstinner:clearenv
Oct 11, 2025
Merged

gh-139482: Add posix._clearenv() function#139965
vstinner merged 3 commits into
python:mainfrom
vstinner:clearenv

Conversation

@vstinner

@vstinnervstinner commented Oct 11, 2025

Copy link
Copy Markdown
Member

@vstinnervstinner changed the title gh-139482: Add posix.clearenv() functiongh-139482: Add posix.clearenv() functionOct 11, 2025
@vstinner

Copy link
Copy Markdown
MemberAuthor

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:

create 10,000 variables: 856.5 ms
clear 10,000 variables: 3276.3 ms

With the change (on Linux with posix.clearenv()):

create 10,000 variables: 856.5 ms
clear 10,000 variables: 1.1 ms

It's around 3,000x faster 😄

@picnixz

Copy link
Copy Markdown
Member

Is it enabled by default on your machine or did you need to use some feature macros when compiling the project?

Comment threadMisc/NEWS.d/next/Library/2025-10-11-20-03-13.gh-issue-139482.du2Stg.rst Outdated
@picnixz

Copy link
Copy Markdown
Member

I should note that I've seen many times the Windows failure today (the failure is in test_profiling).

@vstinner

Copy link
Copy Markdown
MemberAuthor

Is it enabled by default on your machine or did you need to use some feature macros when compiling the project?

I just modified the configure script to detect it, it's detected on Linux, and it just works. I didn't have to touch macros.

I should note that I've seen many times the Windows failure today (the failure is in test_profiling).

I created #139970 to track the test_profiling failure on Windows.

Do you mind documenting os.clearenv as well? or do you prefer not to document it? if you don't plan to, I would suggest having os._clearenv so that users don't use os.clearenv and end up with a desynchronized os.environ.

I prefer to not document it. I just rename it to posix._clearenv() to make it really private.

@AA-TurnerAA-Turner changed the title gh-139482: Add posix.clearenv() functiongh-139482: Add posix._clearenv() functionOct 11, 2025
@vstinner

Copy link
Copy Markdown
MemberAuthor

I tested my PR on my FreeBSD VM. clearenv() function is detected by configure and Pythn builds successfully.

main branch:

create 10,000 variables: 1781.4 ms
clear 10,000 variables: 4726.3 ms

With posix._clearenv():

create 10,000 variables: 1402.2 ms
clear 10,000 variables: 23.5 ms

It's 201x faster.

@vstinner
vstinner merged commit 35e9d41 into python:mainOct 11, 2025
49 checks passed
@vstinner
vstinner deleted the clearenv branch October 11, 2025 20:58
Comment threadModules/posixmodule.c
OS_POSIX_FADVISE_METHODDEF
OS_PUTENV_METHODDEF
OS_UNSETENV_METHODDEF
OS__CLEARENV_METHODDEF

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
MemberAuthor

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) */

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@vstinner@picnixz@itamaro