Skip to content

Commit f8cf4e3

Browse files
authored
Write CLI output as UTF-8 so piped output on Windows no longer fails on emoji (#1860)
1 parent c702b55 commit f8cf4e3

3 files changed

Lines changed: 25 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- `datacontract test` checks the constraints and quality rules of nested properties (struct fields and array items) on Dremio servers
1515
- `datacontract test` finds tables in Postgres schemas with upper case letters, such as `"Reporting"`
1616
- `datacontract test` checks the other columns of a Postgres table with a column of a type ibis cannot read, such as `regclass`
17+
- The CLI no longer fails with `UnicodeEncodeError` when its output is piped on Windows
1718
- `datacontract import postgres` and `import sql` no longer map bit strings of more than one bit (`bit(8)`, `bit varying`) to `boolean`
1819

1920
## [1.2.11] - 2026-11-17

‎datacontract/cli.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import codecs
12
import logging
23
import os
34
import sys
@@ -387,6 +388,10 @@ def _parse_enum_csv(
387388

388389

389390
def main():
391+
# Before Python 3.15, piped output on Windows uses the ANSI code page, which cannot encode emoji.
392+
for stream in (sys.stdout, sys.stderr):
393+
if hasattr(stream, "reconfigure") and codecs.lookup(stream.encoding).name != "utf-8":
394+
stream.reconfigure(encoding="utf-8")
390395
try:
391396
app()
392397
except Exception as e:

‎tests/test_cli.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import json
22
import os
33
import re
4+
import subprocess
45
import sys
6+
from pathlib import Path
57
from unittest.mock import MagicMock
68

79
import pytest
@@ -233,3 +235,20 @@ def test_changelog_yaml_and_json_output_hold_the_same_changes():
233235
as_json = json.loads(runner.invoke(app, args + ["json"]).stdout)
234236
as_yaml = yaml.safe_load(runner.invoke(app, args + ["yaml"]).stdout)
235237
assert as_yaml == as_json
238+
239+
240+
def test_test_output_is_utf8_when_console_encoding_has_no_emoji():
241+
result = subprocess.run(
242+
[
243+
sys.executable,
244+
"-c",
245+
"from datacontract.cli import main; main()",
246+
"test",
247+
"tests/fixtures/csv/data/datacontract.yaml",
248+
],
249+
cwd=Path(__file__).parent.parent,
250+
env={**os.environ, "PYTHONIOENCODING": "cp1252"},
251+
capture_output=True,
252+
)
253+
assert result.returncode == 0, result.stderr.decode("utf-8", errors="replace")
254+
assert any(emoji in result.stdout.decode("utf-8") for emoji in "🟢🟠")

0 commit comments

Comments
 (0)