mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Refactor _get_platform_max_length with clearer variable names
- Extract _PRACTICAL_ARG_MAX constant to replace magic number 2^17 - Use descriptive variable names: arg_max_base, calculated_length, clamped_length - Extract shared sub-expression for headroom and environment size - Add comments explaining each calculation step
This commit is contained in:
parent
d53bcf2caf
commit
e9da01f9f4
1 changed files with 16 additions and 4 deletions
|
|
@ -26,6 +26,11 @@ TRet = TypeVar('TRet')
|
||||||
# _POSIX_ARG_MAX, so we define it here once and reuse it.
|
# _POSIX_ARG_MAX, so we define it here once and reuse it.
|
||||||
_POSIX_ARG_MAX = 4096
|
_POSIX_ARG_MAX = 4096
|
||||||
|
|
||||||
|
# Practical upper bound for command-line argument length (128 KB).
|
||||||
|
# Even though SC_ARG_MAX can be much larger on modern systems, this provides
|
||||||
|
# a conservative limit that works reliably across different shells and tools.
|
||||||
|
_PRACTICAL_ARG_MAX = 2 ** 17
|
||||||
|
|
||||||
|
|
||||||
def cpu_count() -> int:
|
def cpu_count() -> int:
|
||||||
try:
|
try:
|
||||||
|
|
@ -52,15 +57,22 @@ 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':
|
||||||
|
# Get the base ARG_MAX value from sysconf, or fall back to POSIX minimum
|
||||||
try:
|
try:
|
||||||
maximum = os.sysconf('SC_ARG_MAX') - 2048 - _environ_size()
|
arg_max_base = os.sysconf('SC_ARG_MAX')
|
||||||
except OSError:
|
except OSError:
|
||||||
# Fall back to the POSIX minimum when sysconf is unavailable or
|
# Fall back to the POSIX minimum when sysconf is unavailable or
|
||||||
# blocked by a sandbox (for example, Cursor's restricted
|
# blocked by a sandbox (for example, Cursor's restricted
|
||||||
# environment where SC_ARG_MAX raises PermissionError).
|
# environment where SC_ARG_MAX raises PermissionError).
|
||||||
maximum = _POSIX_ARG_MAX - 2048 - _environ_size()
|
arg_max_base = _POSIX_ARG_MAX
|
||||||
maximum = max(min(maximum, 2 ** 17), _POSIX_ARG_MAX)
|
|
||||||
return maximum
|
# Subtract headroom and environment size (shared sub-expression)
|
||||||
|
headroom_and_environ = 2048 + _environ_size()
|
||||||
|
calculated_length = arg_max_base - headroom_and_environ
|
||||||
|
|
||||||
|
# Clamp to reasonable bounds: minimum _POSIX_ARG_MAX, maximum _PRACTICAL_ARG_MAX
|
||||||
|
clamped_length = max(min(calculated_length, _PRACTICAL_ARG_MAX), _POSIX_ARG_MAX)
|
||||||
|
return clamped_length
|
||||||
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:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue