From 7ab0f3d6afbf7d490e6f7b4a6f9c31c5d5b81240 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Sat, 4 Oct 2025 00:32:26 +0200 Subject: [PATCH] Improve performance of os_Environ.clear --- Lib/os.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Lib/os.py b/Lib/os.py index 710d6f8cfcdf74..5af24c9741e812 100644 --- a/Lib/os.py +++ b/Lib/os.py @@ -732,6 +732,14 @@ def __iter__(self): for key in keys: yield self.decodekey(key) + def clear(self): + # linear complexity removal of keys, see gh-139482 + for key in list(self): + try: + del self[key] + except KeyError: + pass + def __len__(self): return len(self._data)