Bug 264237
| Summary: | [meta] Python CGI should use binary mode for stdout for Windows Python | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | Fujii Hironori <fujii.hironori> |
| Component: | Tools / Tests | Assignee: | Nobody <webkit-unassigned> |
| Status: | NEW | ||
| Severity: | Normal | CC: | kohei.asano, webkit-bug-importer |
| Priority: | P2 | Keywords: | InRadar |
| Version: | WebKit Nightly Build | ||
| Hardware: | Unspecified | ||
| OS: | Unspecified | ||
| Bug Depends on: | 263425, 263701, 264238 | ||
| Bug Blocks: | |||
Fujii Hironori
[meta] Python CGI should use binary mode for stdout for Windows Python
Windows Python automatically converts '\n' to '\r\n' for text mode.
| Attachments | ||
|---|---|---|
| Add attachment proposed patch, testcase, etc. |
Radar WebKit Bug Importer
<rdar://problem/118316460>
Kohei Asano
Although this is just cleaning for pre-existing file only, but we can fix this by inserting sys.stdout.reconfigure(newline='');
```bash
C:\Users\0300093582\tmp>python -c "import sys;sys.stdout.reconfigure(newline='');sys.stdout.write('Hello\nWorld\n')" | xxd
00000000: 4865 6c6c 6f0a 576f 726c 640a Hello.World.
```
also on Linux(Ubuntu) I confirmed no effect appears
```bash
[12:22] JPC00234727:tmp | python3 -c "import sys;sys.stdout.reconfigure(newline='');sys.stdout.write('Hello\nWorld\n')" | xxd
00000000: 4865 6c6c 6f0a 576f 726c 640a Hello.World.
[12:24] JPC00234727:tmp | cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.4 LTS"
```
Kohei Asano
Pull request: https://github.com/WebKit/WebKit/pull/39971
Kohei Asano
I tried to fix by the following shell script, but its lines are much more than I expected, I'm not so sure it's worth to apply all, but I leave this for notes
```
I inserted by the following shell scripts, and it wasÂ
#!/bin/bash
TARGET_DIR="LayoutTests"
grep -rl 'sys.stdout.write' "$TARGET_DIR" --include="*.py" | while read -r file; do
tmpfile=$(mktemp)
inserted=0
while IFS= read -r line || [[ -n "$line" ]]; do
echo "$line" >> "$tmpfile"
if [[ $line =~ ^[[:space:]]*import\ sys$ ]]; then
indent=$(echo "$line" | grep -o '^[[:space:]]*')
echo "${indent}sys.stdout.reconfigure(newline=\"\") # prevent windows \n -> \n\r conversion" >> "$tmpfile"
fi
done < "$file"
mv "$tmpfile" "$file"
done
```
Kohei Asano
> Pull request: https://github.com/WebKit/WebKit/pull/39971
I'm sorry, I think PR itself is a little bothering because of too much noises. Also I learned meta issue meaning. Thanks!