Skip to content

Commit 2fc5c84

Browse files
Fix Windows path handling in child process tests
Windows file paths contain backslashes that were being interpreted as escape sequences when embedded in Python code strings, causing syntax errors like "truncated \UXXXXXXXX escape". The fix: 1. Added escape_path_for_python() helper that converts backslashes to forward slashes (which work on all platforms) 2. Updated all file path interpolations to use this helper 3. Fixed indentation in the grandchild script This ensures Windows paths like C:\Users\... are properly handled when embedded in dynamically generated Python scripts. Reported-by: fweinberger
1 parent 2990d6b commit 2fc5c84

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

tests/client/test_stdio.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
python: str = shutil.which("python") # type: ignore
2323

2424

25+
def escape_path_for_python(path: str) -> str:
26+
"""Escape a file path for use in Python code strings."""
27+
# Use forward slashes which work on all platforms and don't need escaping
28+
return repr(path.replace('\\', '/'))
29+
30+
2531
class WindowsProcessWrapper:
2632
"""Minimal wrapper for subprocess.Popen to work with anyio-style process interface."""
2733

@@ -381,13 +387,13 @@ async def test_stdio_client_child_process_cleanup():
381387
import os
382388
383389
# Mark that parent started
384-
with open({repr(parent_marker)}, 'w') as f:
390+
with open({escape_path_for_python(parent_marker)}, 'w') as f:
385391
f.write('parent started\\n')
386392
387393
# Child script that writes continuously
388394
child_script = f'''
389395
import time
390-
with open({repr(marker_file)}, 'a') as f:
396+
with open({escape_path_for_python(marker_file)}, 'a') as f:
391397
while True:
392398
f.write(f"{time.time()}")
393399
f.flush()
@@ -491,17 +497,17 @@ async def test_stdio_client_nested_process_tree():
491497
492498
# Grandchild just writes to file
493499
grandchild_script = \"\"\"import time
494-
with open({repr(grandchild_file)}, 'a') as f:
495-
while True:
496-
f.write(f"gc {{time.time()}}")
497-
f.flush()
498-
time.sleep(0.1)\"\"\"
500+
with open({escape_path_for_python(grandchild_file)}, 'a') as f:
501+
while True:
502+
f.write(f"gc {{time.time()}}")
503+
f.flush()
504+
time.sleep(0.1)\"\"\"
499505
500506
# Spawn grandchild
501507
subprocess.Popen([sys.executable, '-c', grandchild_script])
502508
503509
# Child writes to its file
504-
with open({repr(child_file)}, 'a') as f:
510+
with open({escape_path_for_python(child_file)}, 'a') as f:
505511
while True:
506512
f.write(f"c {time.time()}")
507513
f.flush()
@@ -511,7 +517,7 @@ async def test_stdio_client_nested_process_tree():
511517
subprocess.Popen([sys.executable, '-c', child_script])
512518
513519
# Parent writes to its file
514-
with open({repr(parent_file)}, 'a') as f:
520+
with open({escape_path_for_python(parent_file)}, 'a') as f:
515521
while True:
516522
f.write(f"p {time.time()}")
517523
f.flush()
@@ -589,7 +595,7 @@ async def test_stdio_client_early_parent_exit():
589595
590596
# Child that continues running
591597
child_script = f'''import time
592-
with open({repr(marker_file)}, 'a') as f:
598+
with open({escape_path_for_python(marker_file)}, 'a') as f:
593599
while True:
594600
f.write(f"child {time.time()}")
595601
f.flush()

0 commit comments

Comments
 (0)