From 34d28c03fa90deab9b4978bacaf292052323077f Mon Sep 17 00:00:00 2001 From: Olivier Date: Mon, 19 Oct 2026 12:01:54 -0400 Subject: [PATCH] =?UTF-8?q?fix:=20v0.4.1=20=E2=80=94=20read=20stdin=20as?= =?UTF-8?q?=20UTF-8=20(section=20--content=20-=20got=20mojibake=20from=20t?= =?UTF-8?q?he=20frozen=20exe)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onyx.py | 9 +++++---- tests/test_obsidian_cli.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/onyx.py b/src/onyx.py index ae41e2f..e3b9e9d 100644 --- a/src/onyx.py +++ b/src/onyx.py @@ -1214,7 +1214,7 @@ def _periodic_output_options(command): help="Don't open Obsidian; a newly created note keeps raw Templater syntax " "until Obsidian processes it")(command) -@click.version_option(version='0.4.0', prog_name='onyx') +@click.version_option(version='0.4.1', prog_name='onyx') @click.group(name='onyx') def cli(): """Onyx - Interact with Obsidian vaults from the command line @@ -1559,9 +1559,10 @@ def frontmatter_command(note_name, edit, view, clear, delete, add, remove, key, cli.frontmatter_list_items(note_name, key[0], value, remove=remove, vault=vault) if __name__ == '__main__': - # Scripts read onyx's output (e.g. `--print-path`): emit UTF-8 with LF on every OS. - # Without this, Windows pipes get the ANSI code page (é -> 0xE9; PyInstaller ignores - # PYTHONUTF8) and CRLF, whose trailing \r survives bash's $(...). + # Scripts talk to onyx through pipes (`--content -`, `--print-path`): UTF-8 with LF on + # every OS. Without this, Windows pipes use the ANSI code page (é -> 0xE9 out, UTF-8 in + # read as "é"; PyInstaller ignores PYTHONUTF8) and CRLF, whose \r survives bash's $(...). + sys.stdin.reconfigure(encoding='utf-8') for stream in (sys.stdout, sys.stderr): stream.reconfigure(encoding='utf-8', newline='\n') cli() diff --git a/tests/test_obsidian_cli.py b/tests/test_obsidian_cli.py index a3e755b..1609464 100644 --- a/tests/test_obsidian_cli.py +++ b/tests/test_obsidian_cli.py @@ -1,5 +1,7 @@ import json import os +import subprocess +import sys import unittest import yaml import shutil @@ -10,6 +12,8 @@ import click from click.testing import CliRunner from datetime import datetime +SRC = Path(__file__).resolve().parent.parent / 'src' / 'onyx.py' + class TestOnyxCLI(unittest.TestCase): def setUp(self): # Create temporary test directory and files @@ -594,5 +598,20 @@ Test content""" self.assertIn(str(self.test_dir.resolve()), printed) self.assertNotIn('path_prefix', printed) + def test_pipes_are_utf8_without_utf8_mode(self): + """stdin/stdout are UTF-8 even when Python isn't in UTF-8 mode (frozen exe on Windows)""" + note = self.test_dir / 'd.md' + note.write_text('## Journal\n', encoding='utf-8') + env = {k: v for k, v in os.environ.items() if k not in ('PYTHONUTF8', 'PYTHONIOENCODING')} + home = str(self.temp_config_dir.resolve()) + env.update(PYTHONUTF8='0', USERPROFILE=home, HOME=home) + result = subprocess.run([sys.executable, str(SRC), 'section', 'd', '--heading', 'Journal', + '--content', '-'], input='Réunion « test »'.encode('utf-8'), + capture_output=True, env=env) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(note.read_text(encoding='utf-8'), '## Journal\nRéunion « test »\n') + printed = subprocess.run([sys.executable, str(SRC), 'print-note', 'd'], capture_output=True, env=env) + self.assertEqual(printed.stdout, '## Journal\nRéunion « test »\n\n'.encode('utf-8')) + if __name__ == '__main__': unittest.main()