mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-04-15 18:11:48 +04:00
Fix xargs import failure when os.sysconf is blocked
- Lazy-evaluate _max_length to avoid import-time os.sysconf call - Handle OSError in _get_platform_max_length() with POSIX fallback - Use consistent calculation pattern in fallback path
This commit is contained in:
parent
6cd92aa910
commit
d53bcf2caf
1 changed files with 17 additions and 4 deletions
|
|
@ -22,6 +22,10 @@ from pre_commit.util import cmd_output_p
|
||||||
TArg = TypeVar('TArg')
|
TArg = TypeVar('TArg')
|
||||||
TRet = TypeVar('TRet')
|
TRet = TypeVar('TRet')
|
||||||
|
|
||||||
|
# POSIX minimum for ARG_MAX in bytes (see POSIX.1-2008); Python does not expose
|
||||||
|
# _POSIX_ARG_MAX, so we define it here once and reuse it.
|
||||||
|
_POSIX_ARG_MAX = 4096
|
||||||
|
|
||||||
|
|
||||||
def cpu_count() -> int:
|
def cpu_count() -> int:
|
||||||
try:
|
try:
|
||||||
|
|
@ -48,14 +52,20 @@ def _environ_size(_env: MutableMapping[str, str] | None = None) -> int:
|
||||||
|
|
||||||
def _get_platform_max_length() -> int: # pragma: no cover (platform specific)
|
def _get_platform_max_length() -> int: # pragma: no cover (platform specific)
|
||||||
if os.name == 'posix':
|
if os.name == 'posix':
|
||||||
|
try:
|
||||||
maximum = os.sysconf('SC_ARG_MAX') - 2048 - _environ_size()
|
maximum = os.sysconf('SC_ARG_MAX') - 2048 - _environ_size()
|
||||||
maximum = max(min(maximum, 2 ** 17), 2 ** 12)
|
except OSError:
|
||||||
|
# Fall back to the POSIX minimum when sysconf is unavailable or
|
||||||
|
# blocked by a sandbox (for example, Cursor's restricted
|
||||||
|
# environment where SC_ARG_MAX raises PermissionError).
|
||||||
|
maximum = _POSIX_ARG_MAX - 2048 - _environ_size()
|
||||||
|
maximum = max(min(maximum, 2 ** 17), _POSIX_ARG_MAX)
|
||||||
return maximum
|
return maximum
|
||||||
elif os.name == 'nt':
|
elif os.name == 'nt':
|
||||||
return 2 ** 15 - 2048 # UNICODE_STRING max - headroom
|
return 2 ** 15 - 2048 # UNICODE_STRING max - headroom
|
||||||
else:
|
else:
|
||||||
# posix minimum
|
# posix minimum
|
||||||
return 2 ** 12
|
return _POSIX_ARG_MAX
|
||||||
|
|
||||||
|
|
||||||
def _command_length(*cmd: str) -> int:
|
def _command_length(*cmd: str) -> int:
|
||||||
|
|
@ -134,7 +144,7 @@ def xargs(
|
||||||
*,
|
*,
|
||||||
color: bool = False,
|
color: bool = False,
|
||||||
target_concurrency: int = 1,
|
target_concurrency: int = 1,
|
||||||
_max_length: int = _get_platform_max_length(),
|
_max_length: int | None = None,
|
||||||
**kwargs: Any,
|
**kwargs: Any,
|
||||||
) -> tuple[int, bytes]:
|
) -> tuple[int, bytes]:
|
||||||
"""A simplified implementation of xargs.
|
"""A simplified implementation of xargs.
|
||||||
|
|
@ -142,6 +152,9 @@ def xargs(
|
||||||
color: Make a pty if on a platform that supports it
|
color: Make a pty if on a platform that supports it
|
||||||
target_concurrency: Target number of partitions to run concurrently
|
target_concurrency: Target number of partitions to run concurrently
|
||||||
"""
|
"""
|
||||||
|
if _max_length is None:
|
||||||
|
_max_length = _get_platform_max_length()
|
||||||
|
|
||||||
cmd_fn = cmd_output_p if color else cmd_output_b
|
cmd_fn = cmd_output_p if color else cmd_output_b
|
||||||
retcode = 0
|
retcode = 0
|
||||||
stdout = b''
|
stdout = b''
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue