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

Commit88557bc

Browse files
committed
Have git module use sys.platform to check for Windows
This changes the way code throughout the git module checks to seeif it is running on a native Windows system.Some checks using os.name were already changed to use sys.platformindc95a76 and4191f7d. (Seedc95a76 on why the specific questionof whether the system is native Windows can be answered by eitherchecking os.name or sys.platform, even though in general theydiffer in granularity and are not always suited to the same tasks.)The reasons for this change are:- Consistency: Due todc95a76 and4191f7d, 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 parent2decbe4 commit88557bc

File tree

6 files changed

+21
-15
lines changed

6 files changed

+21
-15
lines changed

‎git/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def items_all(self) -> List[Tuple[str, List[_T]]]:
246246
defget_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-
ifos.name=="nt"andconfig_level=="system":
249+
ifsys.platform=="win32"andconfig_level=="system":
250250
config_level="global"
251251

252252
ifconfig_level=="system":

‎git/index/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
importos
1414
fromstatimportS_ISLNK
1515
importsubprocess
16+
importsys
1617
importtempfile
1718

1819
fromgit.compatimport (
@@ -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-
ifos.name=="nt":
111+
ifsys.platform=="win32":
111112
fd,name=tempfile.mkstemp(dir=directory)
112113
os.close(fd)
113114
try:

‎git/index/fun.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
S_IXUSR,
1919
)
2020
importsubprocess
21+
importsys
2122

2223
fromgit.cmdimporthandle_process_output,safer_popen
2324
fromgit.compatimportdefenc,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-
ifos.name=="nt"andnot_has_file_extension(hp):
103+
ifsys.platform=="win32"andnot_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()

‎git/objects/submodule/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
importos
88
importos.pathasosp
99
importstat
10+
importsys
1011
importuuid
1112

1213
importgit
@@ -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-
ifos.name=="nt"andosp.isfile(git_file):
405+
ifsys.platform=="win32"andosp.isfile(git_file):
405406
os.remove(git_file)
406407
withopen(git_file,"wb")asfp:
407408
fp.write(("gitdir: %s"%rela_path).encode(defenc))

‎git/repo/base.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
frompathlibimportPath
1313
importre
1414
importshlex
15+
importsys
1516
importwarnings
1617

1718
importgitdb
@@ -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-
ifos.name=="nt":
340+
ifsys.platform=="win32":
340341
gc.collect()
341342
gitdb.util.mman.collect()
342-
ifos.name=="nt":
343+
ifsys.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-
ifos.name=="nt"andconfig_level=="system":
622+
ifsys.platform=="win32"andconfig_level=="system":
622623
config_level="global"
623624

624625
ifconfig_level=="system":

‎git/util.py

Lines changed: 10 additions & 8 deletions
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-
ifos.name!="nt":
120+
ifsys.platform!="win32":
121121
returnFalse
122122

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

226-
ifos.name!="nt":
226+
ifsys.platform!="win32":
227227
shutil.rmtree(path)
228228
elifsys.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
ifosp.isfile(path):
238-
ifos.name=="nt":
238+
ifsys.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
returnpath
277277

278278

279-
ifos.name=="nt":
279+
ifsys.platform=="win32":
280280

281281
defto_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
ifPATHEXT:
330330
returntuple(p.upper()forpinPATHEXT.split(os.pathsep))
331-
elifos.name=="nt":
331+
elifsys.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
andos.access(fpath,os.X_OK)
357-
and (os.name!="nt"ornotwinprog_extsorany(fpath.upper().endswith(ext)forextinwinprog_exts))
357+
and (
358+
sys.platform!="win32"ornotwinprog_extsorany(fpath.upper().endswith(ext)forextinwinprog_exts)
359+
)
358360
)
359361

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

452454

453455
defis_cygwin_git(git_executable:Union[None,PathLike])->bool:
454-
ifos.name=="nt":
455-
# This is Windows-native Python, since Cygwin hasos.name == "posix".
456+
ifsys.platform=="win32":
457+
# This is Windows-native Python, since Cygwin hassys.platform == "cygwin".
456458
returnFalse
457459

458460
ifgit_executableisNone:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp