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

Commitc01fe76

Browse files
committed
Deprecate compat.is_<platform>, rewriting all uses
Major changes:- Add a comment in the git.compat module above the definitions of is_win, is_posix, and is_darwin stating that they are deprecated and recommending that os.name (or, where applicable, sys.platform) be used directly instead for clarity (and sometimes accuracy).- Remove all uses of is_win and is_posix in git/ and test/, replacing them with `os.name == "nt"` and `os.name == "posix"`, respectively.There were no uses of is_darwin to be replaced. Although it hadbeen used at one time, the last reference to it appears to havebeen removed in4545762 (#1295).This doesn't emit a DeprecationWarning when those attributes areaccessed in the git.compat module. (That might be valuable thing todo in the future, if the git.compat module is to remainnon-deprecated overall.)Two related less consequential changes are also included:- Improve ordering and grouping of imports, in modules where they were already being changed as a result of no longer needing to import is_<platform> (usually is_win) from git.compat.- Make minor revisions to a few comments, for readability. (This is in addition to somewhat more substantial revisions of comments where rewording was related to replacing uses of is_<platform>.)
1 parentd5fc6e5 commitc01fe76

17 files changed

+118
-128
lines changed

‎.github/workflows/cygwin-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ jobs:
7272
python --version
7373
python -c 'import sys; print(sys.platform)'
7474
python -c 'import os; print(os.name)'
75-
python -c 'import git; print(git.compat.is_win)'
75+
python -c 'import git; print(git.compat.is_win)' # NOTE: Deprecated. Use os.name directly.
7676
7777
-name:Test with pytest
7878
run:|

‎.github/workflows/pythonpackage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ jobs:
6363
python --version
6464
python -c 'import sys; print(sys.platform)'
6565
python -c 'import os; print(os.name)'
66-
python -c 'import git; print(git.compat.is_win)'
66+
python -c 'import git; print(git.compat.is_win)' # NOTE: Deprecated. Use os.name directly.
6767
6868
-name:Check types with mypy
6969
run:|

‎git/cmd.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,21 @@
1717
importthreading
1818
fromtextwrapimportdedent
1919

20-
fromgit.compatimport (
21-
defenc,
22-
force_bytes,
23-
safe_decode,
24-
is_posix,
25-
is_win,
20+
fromgit.compatimportdefenc,force_bytes,safe_decode
21+
fromgit.excimport (
22+
CommandError,
23+
GitCommandError,
24+
GitCommandNotFound,
25+
UnsafeOptionError,
26+
UnsafeProtocolError,
2627
)
27-
fromgit.excimportCommandError
28-
fromgit.utilimportis_cygwin_git,cygpath,expand_path,remove_password_if_present,patch_env
29-
30-
from .excimportGitCommandError,GitCommandNotFound,UnsafeOptionError,UnsafeProtocolError
31-
from .utilimport (
28+
fromgit.utilimport (
3229
LazyMixin,
30+
cygpath,
31+
expand_path,
32+
is_cygwin_git,
33+
patch_env,
34+
remove_password_if_present,
3335
stream_copy,
3436
)
3537

@@ -180,14 +182,13 @@ def pump_stream(
180182
t.start()
181183
threads.append(t)
182184

183-
## FIXME: Why Join?? Will block if `stdin` needs feeding...
184-
#
185+
# FIXME: Why join? Will block if stdin needs feeding...
185186
fortinthreads:
186187
t.join(timeout=kill_after_timeout)
187188
ift.is_alive():
188189
ifisinstance(process,Git.AutoInterrupt):
189190
process._terminate()
190-
else:# Don't want to deal with the other case
191+
else:# Don't want to deal with the other case.
191192
raiseRuntimeError(
192193
"Thread join() timed out in cmd.handle_process_output()."
193194
f" kill_after_timeout={kill_after_timeout} seconds"
@@ -197,11 +198,11 @@ def pump_stream(
197198
"error: process killed because it timed out."f" kill_after_timeout={kill_after_timeout} seconds"
198199
)
199200
ifnotdecode_streamsandisinstance(p_stderr,BinaryIO):
200-
# Assume stderr_handler needs binary input
201+
# Assume stderr_handler needs binary input.
201202
error_str=cast(str,error_str)
202203
error_str=error_str.encode()
203204
# We ignore typing on the next line because mypy does not like
204-
# the way we inferred that stderr takes str or bytes
205+
# the way we inferred that stderr takes str or bytes.
205206
stderr_handler(error_str)# type: ignore
206207

207208
iffinalizer:
@@ -228,14 +229,12 @@ def dict_to_slots_and__excluded_are_none(self: object, d: Mapping[str, Any], exc
228229
## -- End Utilities -- @}
229230

230231

231-
# value of Windows process creation flag taken from MSDN
232-
CREATE_NO_WINDOW=0x08000000
233-
234-
## CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards,
235-
# see https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal
236-
PROC_CREATIONFLAGS= (
237-
CREATE_NO_WINDOW|subprocess.CREATE_NEW_PROCESS_GROUPifis_winelse0# type: ignore[attr-defined]
238-
)# mypy error if not Windows.
232+
ifos.name=="nt":
233+
# CREATE_NEW_PROCESS_GROUP is needed to allow killing it afterwards. See:
234+
# https://docs.python.org/3/library/subprocess.html#subprocess.Popen.send_signal
235+
PROC_CREATIONFLAGS=subprocess.CREATE_NO_WINDOW|subprocess.CREATE_NEW_PROCESS_GROUP
236+
else:
237+
PROC_CREATIONFLAGS=0
239238

240239

241240
classGit(LazyMixin):
@@ -551,7 +550,7 @@ def _terminate(self) -> None:
551550
# For some reason, providing None for stdout/stderr still prints something. This is why
552551
# we simply use the shell and redirect to nul. Slower than CreateProcess. The question
553552
# is whether we really want to see all these messages. It's annoying no matter what.
554-
ifis_win:
553+
ifos.name=="nt":
555554
call(
556555
("TASKKILL /F /T /PID %s 2>nul 1>nul"%str(proc.pid)),
557556
shell=True,
@@ -967,7 +966,7 @@ def execute(
967966
ifinline_envisnotNone:
968967
env.update(inline_env)
969968

970-
ifis_win:
969+
ifos.name=="nt":
971970
cmd_not_found_exception=OSError
972971
ifkill_after_timeoutisnotNone:
973972
raiseGitCommandError(
@@ -999,11 +998,11 @@ def execute(
999998
env=env,
1000999
cwd=cwd,
10011000
bufsize=-1,
1002-
stdin=istreamorDEVNULL,
1001+
stdin=(istreamorDEVNULL),
10031002
stderr=PIPE,
10041003
stdout=stdout_sink,
10051004
shell=shell,
1006-
close_fds=is_posix,# Unsupported on Windows.
1005+
close_fds=(os.name=="posix"),# Unsupported on Windows.
10071006
universal_newlines=universal_newlines,
10081007
creationflags=PROC_CREATIONFLAGS,
10091008
**subprocess_kwargs,
@@ -1073,7 +1072,7 @@ def kill_process(pid: int) -> None:
10731072
)
10741073
ifnotuniversal_newlines:
10751074
stderr_value=stderr_value.encode(defenc)
1076-
#strip trailing "\n"
1075+
#Strip trailing "\n".
10771076
ifstdout_value.endswith(newline)andstrip_newline_in_stdout:# type: ignore
10781077
stdout_value=stdout_value[:-1]
10791078
ifstderr_value.endswith(newline):# type: ignore
@@ -1147,11 +1146,11 @@ def update_environment(self, **kwargs: Any) -> Dict[str, Union[str, None]]:
11471146
"""
11481147
old_env= {}
11491148
forkey,valueinkwargs.items():
1150-
#set value if it is None
1149+
#Set value if it is None.
11511150
ifvalueisnotNone:
11521151
old_env[key]=self._environment.get(key)
11531152
self._environment[key]=value
1154-
#remove key from environment if its value is None
1153+
#Remove key from environment if its value is None.
11551154
elifkeyinself._environment:
11561155
old_env[key]=self._environment[key]
11571156
delself._environment[key]
@@ -1330,7 +1329,8 @@ def _parse_object_header(self, header_line: str) -> Tuple[str, str, int]:
13301329
:return: (hex_sha, type_string, size_as_int)
13311330
13321331
:raise ValueError: If the header contains indication for an error due to
1333-
incorrect input sha"""
1332+
incorrect input sha
1333+
"""
13341334
tokens=header_line.split()
13351335
iflen(tokens)!=3:
13361336
ifnottokens:

‎git/compat.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,20 @@
3434
# ---------------------------------------------------------------------------
3535

3636

37+
# DEPRECATED attributes providing shortcuts to operating system checks based on os.name.
38+
#
39+
# - is_win and is_posix are deprecated because it is clearer, and helps avoid bugs, to
40+
# write out the os.name checks explicitly. For example, is_win is False on Cygwin, but
41+
# is often assumed to be True.
42+
#
43+
# - is_darwin is deprecated because it is always False on all systems, as os.name is
44+
# never "darwin". For macOS, you can check for sys.platform == "darwin". (As on other
45+
# Unix-like systems, os.name == "posix" on macOS. This is also the case on Cygwin.)
46+
#
3747
is_win:bool=os.name=="nt"
3848
is_posix=os.name=="posix"
3949
is_darwin=os.name=="darwin"
50+
4051
defenc=sys.getfilesystemencoding()
4152

4253

‎git/config.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,21 @@
77
"""Module containing module parser implementation able to properly read and write
88
configuration files."""
99

10-
importsys
1110
importabc
11+
importconfigparserascp
12+
importfnmatch
1213
fromfunctoolsimportwraps
1314
importinspect
1415
fromioimportBufferedReader,IOBase
1516
importlogging
1617
importos
18+
importos.pathasosp
1719
importre
18-
importfnmatch
19-
20-
fromgit.compatimport (
21-
defenc,
22-
force_text,
23-
is_win,
24-
)
20+
importsys
2521

22+
fromgit.compatimportdefenc,force_text
2623
fromgit.utilimportLockFile
2724

28-
importos.pathasosp
29-
30-
importconfigparserascp
31-
3225
# typing-------------------------------------------------------
3326

3427
fromtypingimport (
@@ -250,7 +243,7 @@ def items_all(self) -> List[Tuple[str, List[_T]]]:
250243
defget_config_path(config_level:Lit_config_levels)->str:
251244
# We do not support an absolute path of the gitconfig on Windows.
252245
# Use the global config instead.
253-
ifis_winandconfig_level=="system":
246+
ifos.name=="nt"andconfig_level=="system":
254247
config_level="global"
255248

256249
ifconfig_level=="system":

‎git/index/fun.py

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
# NOTE: Autodoc hates it if this is a docstring.
33

44
fromioimportBytesIO
5-
frompathlibimportPath
65
importos
6+
importos.pathasosp
7+
frompathlibimportPath
78
fromstatimport (
89
S_IFDIR,
910
S_IFLNK,
@@ -16,26 +17,17 @@
1617
importsubprocess
1718

1819
fromgit.cmdimportPROC_CREATIONFLAGS,handle_process_output
19-
fromgit.compatimport (
20-
defenc,
21-
force_text,
22-
force_bytes,
23-
is_posix,
24-
is_win,
25-
safe_decode,
26-
)
27-
fromgit.excimportUnmergedEntriesError,HookExecutionError
20+
fromgit.compatimportdefenc,force_bytes,force_text,safe_decode
21+
fromgit.excimportHookExecutionError,UnmergedEntriesError
2822
fromgit.objects.funimport (
29-
tree_to_stream,
3023
traverse_tree_recursive,
3124
traverse_trees_recursive,
25+
tree_to_stream,
3226
)
3327
fromgit.utilimportIndexFileSHA1Writer,finalize_process
3428
fromgitdb.baseimportIStream
3529
fromgitdb.typimportstr_tree_type
3630

37-
importos.pathasosp
38-
3931
from .typimportBaseIndexEntry,IndexEntry,CE_NAMEMASK,CE_STAGESHIFT
4032
from .utilimportpack,unpack
4133

@@ -96,7 +88,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
9688
env["GIT_EDITOR"]=":"
9789
cmd= [hp]
9890
try:
99-
ifis_winandnot_has_file_extension(hp):
91+
ifos.name=="nt"andnot_has_file_extension(hp):
10092
# Windows only uses extensions to determine how to open files
10193
# (doesn't understand shebangs). Try using bash to run the hook.
10294
relative_hp=Path(hp).relative_to(index.repo.working_dir).as_posix()
@@ -108,7 +100,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
108100
stdout=subprocess.PIPE,
109101
stderr=subprocess.PIPE,
110102
cwd=index.repo.working_dir,
111-
close_fds=is_posix,
103+
close_fds=(os.name=="posix"),
112104
creationflags=PROC_CREATIONFLAGS,
113105
)
114106
exceptExceptionasex:

‎git/index/util.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
fromfunctoolsimportwraps
44
importos
5+
importos.pathasosp
56
importstruct
67
importtempfile
78
fromtypesimportTracebackType
89

9-
fromgit.compatimportis_win
10-
11-
importos.pathasosp
12-
13-
1410
# typing ----------------------------------------------------------------------
1511

1612
fromtypingimportAny,Callable,TYPE_CHECKING,Optional,Type
@@ -58,7 +54,7 @@ def __exit__(
5854
exc_tb:Optional[TracebackType],
5955
)->bool:
6056
ifosp.isfile(self.tmp_file_path):
61-
ifis_winandosp.exists(self.file_path):
57+
ifos.name=="nt"andosp.exists(self.file_path):
6258
os.remove(self.file_path)
6359
os.rename(self.tmp_file_path,self.file_path)
6460

‎git/objects/submodule/base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
importgit
99
fromgit.cmdimportGit
10-
fromgit.compatimportdefenc,is_win
10+
fromgit.compatimportdefenc
1111
fromgit.configimportGitConfigParser,SectionConstraint,cp
1212
fromgit.excimport (
1313
BadName,
@@ -353,9 +353,8 @@ def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_ab
353353
"""
354354
git_file=osp.join(working_tree_dir,".git")
355355
rela_path=osp.relpath(module_abspath,start=working_tree_dir)
356-
ifis_win:
357-
ifosp.isfile(git_file):
358-
os.remove(git_file)
356+
ifos.name=="nt"andosp.isfile(git_file):
357+
os.remove(git_file)
359358
withopen(git_file,"wb")asfp:
360359
fp.write(("gitdir: %s"%rela_path).encode(defenc))
361360

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp