tests/winbatch: skip, naming the letters, when cmd.exe can't read a path

Review of #2984 (@iurysf): with 8.3 names off, a path with letters outside the
OEM code page made open(..., encoding="oem") raise UnicodeEncodeError mid-test.
cmd.exe couldn't run that batch file in any encoding, so write_python_cmd now
raises unittest.SkipTest that names the letters (Windows' "oem" codec reports no
useful position, so they are found per character) and writes nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
lines9686-rgb
2026-11-23 00:07:12 +03:00
parent 51a6feece1
commit 14acb49902

View File

@ -17,9 +17,28 @@ def batch_path(path):
return buf.value if 0 < n < len(buf) else path
def _encodes(char):
try:
char.encode("oem")
return True
except UnicodeEncodeError:
return False
def write_python_cmd(cmd_path, python, script):
"""Write cmd_path as a batch file that runs `python script` with the batch file's arguments."""
# "oem" for a path that keeps non-ASCII letters (no short name): the code page cmd.exe reads it with.
with open(cmd_path, "w", encoding="oem") as f:
f.write(f'@"{batch_path(python)}" "{batch_path(script)}" %*\n')
"""Write cmd_path as a batch file that runs `python script` with the batch file's arguments.
Raises unittest.SkipTest when a path has no 8.3 name and letters outside the OEM code page (Cyrillic on a
cp850 machine with 8.3 names off, say): cmd.exe couldn't run such a file in any encoding."""
line = f'@"{batch_path(python)}" "{batch_path(script)}" %*\n'
try:
data = line.encode("oem") # the code page cmd.exe reads a batch file with
except UnicodeEncodeError:
import unittest
# Windows' "oem" codec doesn't report a useful position, so name the letters it can't encode here.
letters = "".join(sorted({c for c in line if not _encodes(c)}))
raise unittest.SkipTest(f"{letters!r} in a path has no 8.3 name and is outside the OEM code page,"
" so cmd.exe can't run a batch file that names it; use an ASCII TEMP") from None
with open(cmd_path, "wb") as f:
f.write(data)
return cmd_path