[3.12] gh-136065: Fix quadratic complexity in os.path.expandvars() (GH-134952) (GH-140847)

(cherry picked from commit f029e8db626ddc6e3a3beea4eff511a71aaceb5c)

Co-authored-by: Serhiy Storchaka <[email protected]>
Co-authored-by: Łukasz Langa <[email protected]>
5 files changed