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

Commitd5d897c

Browse files
authored
Merge pull request#1732 from EliahKagan/compat
Deprecate compat.is_<platform>, rewriting all uses
2 parentsc594433 +562bdee commitd5d897c

17 files changed

+117
-130
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
@@ -16,19 +16,21 @@
1616
importthreading
1717
fromtextwrapimportdedent
1818

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

@@ -179,14 +181,13 @@ def pump_stream(
179181
t.start()
180182
threads.append(t)
181183

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

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

229230

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

239238

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

969-
ifis_win:
968+
ifos.name=="nt":
970969
cmd_not_found_exception=OSError
971970
ifkill_after_timeoutisnotNone:
972971
raiseGitCommandError(
@@ -998,11 +997,11 @@ def execute(
998997
env=env,
999998
cwd=cwd,
1000999
bufsize=-1,
1001-
stdin=istreamorDEVNULL,
1000+
stdin=(istreamorDEVNULL),
10021001
stderr=PIPE,
10031002
stdout=stdout_sink,
10041003
shell=shell,
1005-
close_fds=is_posix,# Unsupported on Windows.
1004+
close_fds=(os.name=="posix"),# Unsupported on Windows.
10061005
universal_newlines=universal_newlines,
10071006
creationflags=PROC_CREATIONFLAGS,
10081007
**subprocess_kwargs,
@@ -1072,7 +1071,7 @@ def kill_process(pid: int) -> None:
10721071
)
10731072
ifnotuniversal_newlines:
10741073
stderr_value=stderr_value.encode(defenc)
1075-
#strip trailing "\n"
1074+
#Strip trailing "\n".
10761075
ifstdout_value.endswith(newline)andstrip_newline_in_stdout:# type: ignore
10771076
stdout_value=stdout_value[:-1]
10781077
ifstderr_value.endswith(newline):# type: ignore
@@ -1146,11 +1145,11 @@ def update_environment(self, **kwargs: Any) -> Dict[str, Union[str, None]]:
11461145
"""
11471146
old_env= {}
11481147
forkey,valueinkwargs.items():
1149-
#set value if it is None
1148+
#Set value if it is None.
11501149
ifvalueisnotNone:
11511150
old_env[key]=self._environment.get(key)
11521151
self._environment[key]=value
1153-
#remove key from environment if its value is None
1152+
#Remove key from environment if its value is None.
11541153
elifkeyinself._environment:
11551154
old_env[key]=self._environment[key]
11561155
delself._environment[key]
@@ -1329,7 +1328,8 @@ def _parse_object_header(self, header_line: str) -> Tuple[str, str, int]:
13291328
:return: (hex_sha, type_string, size_as_int)
13301329
13311330
:raise ValueError: If the header contains indication for an error due to
1332-
incorrect input sha"""
1331+
incorrect input sha
1332+
"""
13331333
tokens=header_line.split()
13341334
iflen(tokens)!=3:
13351335
ifnottokens:

‎git/compat.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,17 @@
3333
# ---------------------------------------------------------------------------
3434

3535

36-
is_win:bool=os.name=="nt"
36+
# DEPRECATED attributes providing shortcuts to operating system checks based on os.name.
37+
#
38+
# These are deprecated because it is clearer, and helps avoid bugs, to write out the
39+
# os.name or sys.platform checks explicitly, especially in cases where it matters which
40+
# is used. For example, is_win is False on Cygwin, but is often assumed True. To detect
41+
# Cygwin, use sys.platform == "cygwin". (Also, in the past, is_darwin was unreliable.)
42+
#
43+
is_win=os.name=="nt"
3744
is_posix=os.name=="posix"
38-
is_darwin=os.name=="darwin"
45+
is_darwin=sys.platform=="darwin"
46+
3947
defenc=sys.getfilesystemencoding()
4048

4149

‎git/config.py

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

9-
importsys
109
importabc
10+
importconfigparserascp
11+
importfnmatch
1112
fromfunctoolsimportwraps
1213
importinspect
1314
fromioimportBufferedReader,IOBase
1415
importlogging
1516
importos
17+
importos.pathasosp
1618
importre
17-
importfnmatch
18-
19-
fromgit.compatimport (
20-
defenc,
21-
force_text,
22-
is_win,
23-
)
19+
importsys
2420

21+
fromgit.compatimportdefenc,force_text
2522
fromgit.utilimportLockFile
2623

27-
importos.pathasosp
28-
29-
importconfigparserascp
30-
3124
# typing-------------------------------------------------------
3225

3326
fromtypingimport (
@@ -249,7 +242,7 @@ def items_all(self) -> List[Tuple[str, List[_T]]]:
249242
defget_config_path(config_level:Lit_config_levels)->str:
250243
# We do not support an absolute path of the gitconfig on Windows.
251244
# Use the global config instead.
252-
ifis_winandconfig_level=="system":
245+
ifos.name=="nt"andconfig_level=="system":
253246
config_level="global"
254247

255248
ifconfig_level=="system":

‎git/index/fun.py

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

77
fromioimportBytesIO
8-
frompathlibimportPath
98
importos
9+
importos.pathasosp
10+
frompathlibimportPath
1011
fromstatimport (
1112
S_IFDIR,
1213
S_IFLNK,
@@ -19,26 +20,17 @@
1920
importsubprocess
2021

2122
fromgit.cmdimportPROC_CREATIONFLAGS,handle_process_output
22-
fromgit.compatimport (
23-
defenc,
24-
force_text,
25-
force_bytes,
26-
is_posix,
27-
is_win,
28-
safe_decode,
29-
)
30-
fromgit.excimportUnmergedEntriesError,HookExecutionError
23+
fromgit.compatimportdefenc,force_bytes,force_text,safe_decode
24+
fromgit.excimportHookExecutionError,UnmergedEntriesError
3125
fromgit.objects.funimport (
32-
tree_to_stream,
3326
traverse_tree_recursive,
3427
traverse_trees_recursive,
28+
tree_to_stream,
3529
)
3630
fromgit.utilimportIndexFileSHA1Writer,finalize_process
3731
fromgitdb.baseimportIStream
3832
fromgitdb.typimportstr_tree_type
3933

40-
importos.pathasosp
41-
4234
from .typimportBaseIndexEntry,IndexEntry,CE_NAMEMASK,CE_STAGESHIFT
4335
from .utilimportpack,unpack
4436

@@ -99,7 +91,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
9991
env["GIT_EDITOR"]=":"
10092
cmd= [hp]
10193
try:
102-
ifis_winandnot_has_file_extension(hp):
94+
ifos.name=="nt"andnot_has_file_extension(hp):
10395
# Windows only uses extensions to determine how to open files
10496
# (doesn't understand shebangs). Try using bash to run the hook.
10597
relative_hp=Path(hp).relative_to(index.repo.working_dir).as_posix()
@@ -111,7 +103,7 @@ def run_commit_hook(name: str, index: "IndexFile", *args: str) -> None:
111103
stdout=subprocess.PIPE,
112104
stderr=subprocess.PIPE,
113105
cwd=index.repo.working_dir,
114-
close_fds=is_posix,
106+
close_fds=(os.name=="posix"),
115107
creationflags=PROC_CREATIONFLAGS,
116108
)
117109
exceptExceptionasex:

‎git/index/util.py

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

66
fromfunctoolsimportwraps
77
importos
8+
importos.pathasosp
89
importstruct
910
importtempfile
1011
fromtypesimportTracebackType
1112

12-
fromgit.compatimportis_win
13-
14-
importos.pathasosp
15-
16-
1713
# typing ----------------------------------------------------------------------
1814

1915
fromtypingimportAny,Callable,TYPE_CHECKING,Optional,Type
@@ -61,7 +57,7 @@ def __exit__(
6157
exc_tb:Optional[TracebackType],
6258
)->bool:
6359
ifosp.isfile(self.tmp_file_path):
64-
ifis_winandosp.exists(self.file_path):
60+
ifos.name=="nt"andosp.exists(self.file_path):
6561
os.remove(self.file_path)
6662
os.rename(self.tmp_file_path,self.file_path)
6763

‎git/objects/submodule/base.py

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

1111
importgit
1212
fromgit.cmdimportGit
13-
fromgit.compatimportdefenc,is_win
13+
fromgit.compatimportdefenc
1414
fromgit.configimportGitConfigParser,SectionConstraint,cp
1515
fromgit.excimport (
1616
BadName,
@@ -356,9 +356,8 @@ def _write_git_file_and_module_config(cls, working_tree_dir: PathLike, module_ab
356356
"""
357357
git_file=osp.join(working_tree_dir,".git")
358358
rela_path=osp.relpath(module_abspath,start=working_tree_dir)
359-
ifis_win:
360-
ifosp.isfile(git_file):
361-
os.remove(git_file)
359+
ifos.name=="nt"andosp.isfile(git_file):
360+
os.remove(git_file)
362361
withopen(git_file,"wb")asfp:
363362
fp.write(("gitdir: %s"%rela_path).encode(defenc))
364363

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp