Skip to content

Commit 88557bc

Browse files
committed
Have git module use sys.platform to check for Windows
This changes the way code throughout the git module checks to see if it is running on a native Windows system. Some checks using os.name were already changed to use sys.platform in dc95a76 and 4191f7d. (See dc95a76 on why the specific question of whether the system is native Windows can be answered by either checking os.name or sys.platform, even though in general they differ in granularity and are not always suited to the same tasks.) The reasons for this change are: - Consistency: Due to dc95a76 and 4191f7d, some of these checks use sys.platform, so in the absence of a reason to do otherwise, it is best that they all do. Otherwise, it creates an appearance that the technical reasons behind the difference are stronger than they really are, or even that differnt things are being checked. - Better static typing: mypy treats sys.platform as a constant (and also allows checking on platform on another via --platform). Likewise, typeshed conditions platform-dependent declarations on sys.platform checks, rather than os.name or other checks. Really this is the original reason for those earlier, more selective changes, but here the goal is more general, since this is not needed to address any specific preexisting mypy errors. This is incomplete, for two reasons: - I'm deliberately not including changes in the tests in this commit. Arguably it should be kept as os.name in the tests, on the grounds that the test suite is not currently statically typed anyway, plus having them differ more compellingly shows that the behavior is the same whether an os.name or sys.platform check is used. However, it would be confusing to keep them different, and also somewhat unnatural; one approach would probably end up leaking through. Furthermore, because some tests have to check for cygwin specifically, which cannot be done with os.name, it may be clearer to use sys.platform for all platform checking in the test suite. But to verify and demonstrate that the change really is safe, I'm waiting to make the change in the tests until after making them in the code under test. - Some forms of checks against constants produce unreachable code errors from mypy (python/mypy#10773). This can be worked around, but I have not done that in this commit. Furthermore, the new mypy errors this produces--one on Windows, and one on non-Windows systems--could be fixed in a way that makes type annotations richer, by allowing the return type to be a literal on one platform or the other.
1 parent 2decbe4 commit 88557bc

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

Diff for: git/config.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def items_all(self) -> List[Tuple[str, List[_T]]]:
246246
def get_config_path(config_level: Lit_config_levels) -> str:
247247
# We do not support an absolute path of the gitconfig on Windows.
248248
# Use the global config instead.
249-
if os.name == "nt" and config_level == "system":
249+
if sys.platform == "win32" and config_level == "system":
250250
config_level = "global"
251251

252252
if config_level == "system":

Diff for: git/index/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414
from stat import S_ISLNK
1515
import subprocess
16+
import sys
1617
import tempfile
1718

1819
from git.compat import (
@@ -107,7 +108,7 @@ def _named_temporary_file_for_subprocess(directory: PathLike) -> Generator[str,
107108
A context manager object that creates the file and provides its name on entry,
108109
and deletes it on exit.
109110
"""
110-
if os.name == "nt":
111+
if sys.platform == "win32":
111112
fd, name = tempfile.mkstemp(dir=directory)
112113
os.close(fd)
113114
try:

Diff for: git/index/fun.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
S_IXUSR,
1919
)
2020
import subprocess
21+
import sys
2122

2223
from git.cmd import handle_process_output, safer_popen
2324
from git.compat import defenc, force_bytes, force_text, safe_decode
@@ -99,7 +100,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
99100
env["GIT_EDITOR"] = ":"
100101
cmd = [hp]
101102
try:
102-
if os.name == "nt" and not _has_file_extension(hp):
103+
if sys.platform == "win32" and not _has_file_extension(hp):
103104
# Windows only uses extensions to determine how to open files
104105
# (doesn't understand shebangs). Try using bash to run the hook.
105106
relative_hp = Path(hp).relative_to(index.repo.working_dir).as_posix()

Diff for: git/objects/submodule/base.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import os.path as osp
99
import stat
10+
import sys
1011
import uuid
1112

1213
import git
@@ -401,7 +402,7 @@ def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_ab
401402
"""
402403
git_file = osp.join(working_tree_dir, ".git")
403404
rela_path = osp.relpath(module_abspath, start=working_tree_dir)
404-
if os.name == "nt" and osp.isfile(git_file):
405+
if sys.platform == "win32" and osp.isfile(git_file):
405406
os.remove(git_file)
406407
with open(git_file, "wb") as fp:
407408
fp.write(("gitdir: %s" % rela_path).encode(defenc))

Diff for: git/repo/base.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from pathlib import Path
1313
import re
1414
import shlex
15+
import sys
1516
import warnings
1617

1718
import gitdb
@@ -336,10 +337,10 @@ def close(self) -> None:
336337
# they are collected by the garbage collector, thus preventing deletion.
337338
# TODO: Find these references and ensure they are closed and deleted
338339
# synchronously rather than forcing a gc collection.
339-
if os.name == "nt":
340+
if sys.platform == "win32":
340341
gc.collect()
341342
gitdb.util.mman.collect()
342-
if os.name == "nt":
343+
if sys.platform == "win32":
343344
gc.collect()
344345

345346
def __eq__(self, rhs: object) -> bool:
@@ -618,7 +619,7 @@ def _get_config_path(self, config_level: Lit_config_levels, git_dir: Optional[Pa
618619
git_dir = self.git_dir
619620
# We do not support an absolute path of the gitconfig on Windows.
620621
# Use the global config instead.
621-
if os.name == "nt" and config_level == "system":
622+
if sys.platform == "win32" and config_level == "system":
622623
config_level = "global"
623624

624625
if config_level == "system":

Diff for: git/util.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _read_win_env_flag(name: str, default: bool) -> bool:
117117
:note:
118118
This only accesses the environment on Windows.
119119
"""
120-
if os.name != "nt":
120+
if sys.platform != "win32":
121121
return False
122122

123123
try:
@@ -223,7 +223,7 @@ def handler(function: Callable, path: PathLike, _excinfo: Any) -> None:
223223
raise SkipTest(f"FIXME: fails with: PermissionError\n {ex}") from ex
224224
raise
225225

226-
if os.name != "nt":
226+
if sys.platform != "win32":
227227
shutil.rmtree(path)
228228
elif sys.version_info >= (3, 12):
229229
shutil.rmtree(path, onexc=handler)
@@ -235,7 +235,7 @@ def rmfile(path: PathLike) -> None:
235235
"""Ensure file deleted also on *Windows* where read-only files need special
236236
treatment."""
237237
if osp.isfile(path):
238-
if os.name == "nt":
238+
if sys.platform == "win32":
239239
os.chmod(path, 0o777)
240240
os.remove(path)
241241

@@ -276,7 +276,7 @@ def join_path(a: PathLike, *p: PathLike) -> PathLike:
276276
return path
277277

278278

279-
if os.name == "nt":
279+
if sys.platform == "win32":
280280

281281
def to_native_path_windows(path: PathLike) -> PathLike:
282282
path = str(path)
@@ -328,7 +328,7 @@ def _get_exe_extensions() -> Sequence[str]:
328328
PATHEXT = os.environ.get("PATHEXT", None)
329329
if PATHEXT:
330330
return tuple(p.upper() for p in PATHEXT.split(os.pathsep))
331-
elif os.name == "nt":
331+
elif sys.platform == "win32":
332332
return (".BAT", "COM", ".EXE")
333333
else:
334334
return ()
@@ -354,7 +354,9 @@ def is_exec(fpath: str) -> bool:
354354
return (
355355
osp.isfile(fpath)
356356
and os.access(fpath, os.X_OK)
357-
and (os.name != "nt" or not winprog_exts or any(fpath.upper().endswith(ext) for ext in winprog_exts))
357+
and (
358+
sys.platform != "win32" or not winprog_exts or any(fpath.upper().endswith(ext) for ext in winprog_exts)
359+
)
358360
)
359361

360362
progs = []
@@ -451,8 +453,8 @@ def is_cygwin_git(git_executable: PathLike) -> bool:
451453

452454

453455
def is_cygwin_git(git_executable: Union[None, PathLike]) -> bool:
454-
if os.name == "nt":
455-
# This is Windows-native Python, since Cygwin has os.name == "posix".
456+
if sys.platform == "win32":
457+
# This is Windows-native Python, since Cygwin has sys.platform == "cygwin".
456458
return False
457459

458460
if git_executable is None:

0 commit comments

Comments
 (0)