Skip to content

Commit a23bb4f

Browse files
committed
Fix Windows Unicode encoding issue [skip ci]
1 parent 9abcf13 commit a23bb4f

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

.github/workflows/ci.yml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,21 @@ on:
55
branches: [main]
66
pull_request:
77
branches: [main]
8+
workflow_dispatch:
9+
inputs:
10+
run_windows:
11+
description: 'Run Windows tests'
12+
required: false
13+
default: false
14+
type: boolean
815

916
jobs:
1017
test:
1118
runs-on: ${{ matrix.os }}
1219
strategy:
1320
fail-fast: false
1421
matrix:
15-
os: [ubuntu-latest, macos-latest, windows-latest]
22+
os: [ubuntu-latest, macos-latest]
1623
python-version: ["3.10", "3.11", "3.12"]
1724

1825
steps:
@@ -39,6 +46,31 @@ jobs:
3946
files: ./coverage.xml
4047
fail_ci_if_error: false
4148

49+
test-windows:
50+
if: github.event_name == 'workflow_dispatch' && inputs.run_windows
51+
runs-on: windows-latest
52+
strategy:
53+
fail-fast: false
54+
matrix:
55+
python-version: ["3.10", "3.11", "3.12"]
56+
57+
steps:
58+
- uses: actions/checkout@v4
59+
60+
- name: Set up Python ${{ matrix.python-version }}
61+
uses: actions/setup-python@v5
62+
with:
63+
python-version: ${{ matrix.python-version }}
64+
65+
- name: Install dependencies
66+
run: |
67+
python -m pip install --upgrade pip
68+
pip install -e ".[dev]"
69+
70+
- name: Run tests
71+
run: |
72+
pytest -v
73+
4274
lint:
4375
runs-on: ubuntu-latest
4476
steps:

src/shredguard/output.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ def supports_unicode() -> bool:
4949
if os.environ.get("FORCE_ASCII"):
5050
return False
5151

52-
if sys.platform == "win32":
53-
# Check if the console is using UTF-8 (code page 65001)
54-
try:
55-
import ctypes
56-
57-
return ctypes.windll.kernel32.GetConsoleOutputCP() == 65001
58-
except Exception:
59-
return False
60-
return True
52+
if os.environ.get("FORCE_UNICODE"):
53+
return True
54+
55+
# Check stdout encoding - this is the most reliable way to determine
56+
# if Unicode characters can be written without encoding errors
57+
encoding = getattr(sys.stdout, "encoding", None) or ""
58+
encoding = encoding.lower().replace("-", "").replace("_", "")
59+
60+
# Only allow Unicode if we have a Unicode-capable encoding
61+
return encoding in ("utf8", "utf16", "utf32")
6162

6263

6364
class Formatter:

0 commit comments

Comments
 (0)