Skip to content

Commit b2fd616

Browse files
committed
Python: address long-path review feedback
Count Windows path limits in UTF-16 code units instead of Python code points, restrict the ENAMETOOLONG rewrite to FileNotFoundError so collisions and permission failures keep their own types, and size the regression test's file name from the temporary directory so the path always crosses MAX_PATH.
1 parent 62d3e48 commit b2fd616

2 files changed

Lines changed: 99 additions & 12 deletions

File tree

‎python/packages/core/agent_framework/_harness/_file_access.py‎

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@
100100
_MAX_PATH_SEGMENTS: Final[int] = 255
101101
_MAX_PATH_LENGTH: Final[int] = 4096
102102

103-
# Windows path length limits. They are consulted only to explain an error the OS already
104-
# raised (see :func:`_raise_if_path_too_long`); they never gate a path up front.
105-
# ``MAX_PATH`` is 260 characters including the drive, the separators and the terminating
106-
# NUL, and ``CreateDirectory`` needs room for the 8.3 name it may have to synthesize,
107-
# which is where the 248-character directory limit comes from.
103+
# Windows path length limits, counted the way Windows counts them: in UTF-16 code
104+
# units, not Python code points (a supplementary character is two units). They are
105+
# consulted only to explain an error the OS already raised (see
106+
# :func:`_raise_if_path_too_long`); they never gate a path up front. ``MAX_PATH`` is
107+
# 260 of those units including the drive, the separators and the terminating NUL, and
108+
# ``CreateDirectory`` needs room for the 8.3 name it may have to synthesize, which is
109+
# where the 248-unit directory limit comes from.
108110
# https://learn.microsoft.com/windows/win32/fileio/maximum-file-path-limitation
109111
_WINDOWS_MAX_PATH_CHARS: Final[int] = 260
110112
_WINDOWS_MAX_DIRECTORY_PATH_CHARS: Final[int] = 248
@@ -1342,23 +1344,41 @@ async def create_directory(self, path: str) -> None:
13421344
self._explicit_dirs[key] = display
13431345

13441346

1347+
def _windows_path_length(path: Path) -> int:
1348+
"""Return the length Windows measures for *path*, in UTF-16 code units.
1349+
1350+
Windows path limits are expressed in UTF-16 code units, while ``len`` on a Python
1351+
string counts Unicode code points. A supplementary character such as an emoji is one
1352+
code point but two UTF-16 units, so counting code points would understate the path
1353+
and could miss a genuine length failure.
1354+
"""
1355+
return len(str(path).encode("utf-16-le", "surrogatepass")) // 2
1356+
1357+
13451358
def _raise_if_path_too_long(exc: OSError, path: Path, *, limit: int) -> None:
13461359
"""Re-raise an over-long Windows path failure as :data:`errno.ENAMETOOLONG`.
13471360
13481361
Without long-path support Windows reports a path past its limit as a missing path,
13491362
which CPython surfaces as :class:`FileNotFoundError`. Callers then go looking for a
13501363
bug in their own path handling instead of shortening the key.
13511364
1365+
Only that missing-path symptom is rewritten. A collision or a permission failure has
1366+
its own exception type and must keep it, even when the path happens to be over the
1367+
limit: with long-path support enabled Windows reports those directly, and relabeling
1368+
them as a length problem would hide the real cause.
1369+
13521370
The length is used only to explain an error that already happened. Nothing here
13531371
rejects a path before it is used, so a host with long-path support behaves exactly as
13541372
before and every in-limit path stays untouched. The rewrite additionally requires a
13551373
prefix inside the limit to exist; without one the path is missing for an ordinary
13561374
reason and the original error is the accurate one.
13571375
"""
1358-
if not _WINDOWS_PATH_LIMITS_APPLY or len(str(path)) < limit:
1376+
if not _WINDOWS_PATH_LIMITS_APPLY or not isinstance(exc, FileNotFoundError):
1377+
return
1378+
if _windows_path_length(path) < limit:
13591379
return
13601380
prefix = path
1361-
while len(str(prefix)) >= limit:
1381+
while _windows_path_length(prefix) >= limit:
13621382
parent = prefix.parent
13631383
if parent == prefix:
13641384
return
@@ -1390,8 +1410,8 @@ class FileSystemAgentFileStore(AgentFileStore):
13901410
single-tenant or co-operating-tenant use; it is not a sandbox against a
13911411
hostile process that shares the root directory.
13921412
1393-
On Windows the length of the resolved path (root plus key) is limited to 260
1394-
characters for a file and 248 for a directory unless long-path support is enabled
1413+
On Windows the length of the resolved path (root plus key) is limited to 260 UTF-16
1414+
code units for a file and 248 for a directory unless long-path support is enabled
13951415
(``LongPathsEnabled``). :meth:`write` reports that limit as :class:`OSError` with
13961416
:data:`errno.ENAMETOOLONG` instead of the ``FileNotFoundError`` the OS raises, so a
13971417
caller can tell "too long" apart from "missing". The limit only renames a failure

‎python/packages/core/tests/core/test_harness_file_access.py‎

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3081,14 +3081,15 @@ def length_aware_mkdir(self: Path, mode: int = 0o777, parents: bool = False, exi
30813081

30823082
monkeypatch.setattr(Path, "mkdir", length_aware_mkdir)
30833083
else:
3084-
# Grow the parent until it is just inside the directory limit, then add a file name
3085-
# that pushes the absolute path past the file limit, so only the open fails.
3084+
# Grow the parent until it is just inside the directory limit, then size the file
3085+
# name from the current length so the absolute path is guaranteed to reach the
3086+
# file limit wherever ``tmp_path`` happens to live.
30863087
segments: list[str] = []
30873088
current = len(str(tmp_path))
30883089
while current + 1 + 34 <= 247:
30893090
segments.append("d" * 34)
30903091
current += 1 + 34
3091-
key = "/".join([*segments, "f" * 20])
3092+
key = "/".join([*segments, "f" * max(1, 260 - current - 1)])
30923093
monkeypatch.setattr(os, "open", raise_too_long)
30933094

30943095
with pytest.raises(OSError) as exc_info:
@@ -3132,3 +3133,69 @@ def raise_missing(*_args: object, **_kwargs: object) -> None:
31323133
await store.write("nested/file.txt", "x")
31333134

31343135
assert exc_info.value is missing
3136+
3137+
3138+
async def test_filesystem_store_measures_long_paths_in_utf16_code_units(
3139+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
3140+
) -> None:
3141+
"""The Windows limit is compared in UTF-16 code units, not Python code points.
3142+
3143+
A supplementary character such as an emoji is one code point but two UTF-16 units.
3144+
A path one code point short of 260 can therefore already have reached the Windows
3145+
limit, and must still be reported as too long.
3146+
"""
3147+
import errno
3148+
3149+
monkeypatch.setattr("agent_framework._harness._file_access._WINDOWS_PATH_LIMITS_APPLY", True)
3150+
store = FileSystemAgentFileStore(str(tmp_path))
3151+
root = str(store.root_path)
3152+
key = "f" * (257 - len(root)) + "\U0001f600"
3153+
target = store.root_path / key
3154+
# 259 code points, 260 UTF-16 units: only the Windows length has reached the limit.
3155+
assert len(str(target)) == 259
3156+
assert len(str(target).encode("utf-16-le")) // 2 == 260
3157+
3158+
too_long = FileNotFoundError(errno.ENOENT, "The system cannot find the path specified")
3159+
3160+
def raise_too_long(*_args: object, **_kwargs: object) -> None:
3161+
raise too_long
3162+
3163+
monkeypatch.setattr(os, "open", raise_too_long)
3164+
3165+
with pytest.raises(OSError) as exc_info:
3166+
await store.write(key, "x")
3167+
3168+
assert not isinstance(exc_info.value, FileNotFoundError)
3169+
assert exc_info.value.errno == errno.ENAMETOOLONG
3170+
assert exc_info.value.__cause__ is too_long
3171+
3172+
3173+
@pytest.mark.parametrize("error_type", [FileExistsError, PermissionError])
3174+
async def test_filesystem_store_preserves_non_missing_errors_on_long_paths(
3175+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, error_type: type[OSError]
3176+
) -> None:
3177+
"""Collisions and permission failures on an over-long path keep their own type.
3178+
3179+
Only the legacy "missing path" symptom is rewritten. With long-path support enabled
3180+
Windows reports an existing file as ``FileExistsError`` and a refusal as
3181+
``PermissionError``; relabeling either as a length problem would hide the real cause.
3182+
"""
3183+
import errno
3184+
3185+
monkeypatch.setattr("agent_framework._harness._file_access._WINDOWS_PATH_LIMITS_APPLY", True)
3186+
store = FileSystemAgentFileStore(str(tmp_path))
3187+
key = "f" * (260 - len(str(store.root_path)) - 1)
3188+
error_number = errno.EEXIST if error_type is FileExistsError else errno.EACCES
3189+
error = error_type(error_number, "original failure")
3190+
assert len(str(store.root_path / key)) == 260
3191+
3192+
def raise_error(*_args: object, **_kwargs: object) -> None:
3193+
raise error
3194+
3195+
monkeypatch.setattr(os, "open", raise_error)
3196+
3197+
with pytest.raises(OSError) as exc_info:
3198+
await store.write(key, "x")
3199+
3200+
assert exc_info.value is error
3201+
assert exc_info.value.errno == error_number

0 commit comments

Comments
 (0)