Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc7fad20

Browse files
committed
Fix Windows env var upcasing regression
This uses a simple hand-rolled context manager to patch theNoDefaultCurrentDirectoryInExePath variable, instead ofunittest.mock.patch.dict. The latter set unrelated environmentvariables to the original (same) values via os.environ, and as aresult, their names were all converted to upper-case on Windows.Because only environment variables that are actually set throughos.environ have their names upcased, the only variable whose nameshould be upcased now is NoDefaultCurrentDirectoryInExePath, whichshould be fine (it has a single established use/meaning in Windows,where it's treated case-insensitively as environment variables inWindows *usually* are).
1 parent7296e5c commitc7fad20

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

‎git/cmd.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
importsubprocess
1515
importthreading
1616
fromtextwrapimportdedent
17-
importunittest.mock
1817

1918
fromgit.compatimport (
2019
defenc,
@@ -24,7 +23,7 @@
2423
is_win,
2524
)
2625
fromgit.excimportCommandError
27-
fromgit.utilimportis_cygwin_git,cygpath,expand_path,remove_password_if_present
26+
fromgit.utilimportis_cygwin_git,cygpath,expand_path,remove_password_if_present,patch_env
2827

2928
from .excimportGitCommandError,GitCommandNotFound,UnsafeOptionError,UnsafeProtocolError
3029
from .utilimport (
@@ -965,10 +964,10 @@ def execute(
965964
'"kill_after_timeout" feature is not supported on Windows.',
966965
)
967966
# Only search PATH, not CWD. This must be in the *caller* environment. The "1" can be any value.
968-
patch_caller_env=unittest.mock.patch.dict(os.environ, {"NoDefaultCurrentDirectoryInExePath":"1"})
967+
maybe_patch_caller_env=patch_env("NoDefaultCurrentDirectoryInExePath","1")
969968
else:
970969
cmd_not_found_exception=FileNotFoundError# NOQA # exists, flake8 unknown @UndefinedVariable
971-
patch_caller_env=contextlib.nullcontext()
970+
maybe_patch_caller_env=contextlib.nullcontext()
972971
# end handle
973972

974973
stdout_sink=PIPEifwith_stdoutelsegetattr(subprocess,"DEVNULL",None)oropen(os.devnull,"wb")
@@ -984,7 +983,7 @@ def execute(
984983
istream_ok,
985984
)
986985
try:
987-
withpatch_caller_env:
986+
withmaybe_patch_caller_env:
988987
proc=Popen(
989988
command,
990989
env=env,

‎git/util.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ def cwd(new_dir: PathLike) -> Generator[PathLike, None, None]:
158158
os.chdir(old_dir)
159159

160160

161+
@contextlib.contextmanager
162+
defpatch_env(name:str,value:str)->Generator[None,None,None]:
163+
"""Context manager to temporarily patch an environment variable."""
164+
old_value=os.getenv(name)
165+
os.environ[name]=value
166+
try:
167+
yield
168+
finally:
169+
ifold_valueisNone:
170+
delos.environ[name]
171+
else:
172+
os.environ[name]=old_value
173+
174+
161175
defrmtree(path:PathLike)->None:
162176
"""Remove the given recursively.
163177
@@ -935,7 +949,7 @@ def _obtain_lock_or_raise(self) -> None:
935949
)
936950

937951
try:
938-
withopen(lock_file,mode='w'):
952+
withopen(lock_file,mode="w"):
939953
pass
940954
exceptOSErrorase:
941955
raiseIOError(str(e))frome

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp