From 8a84db971add35895cda0754a0659326f5d749b8 Mon Sep 17 00:00:00 2001 From: oakkitten Date: Wed, 20 Apr 2022 18:30:20 +0100 Subject: [PATCH] Patch Anki 2.1.50 on Windows to have valid stdout Something in Anki is setting stdout to Null. This is a problem because Anki itself is writing to it, particularly when printing warnings about the deprecated methods that we are using. This patches Anki using the same method that the upcoming Anki 2.1.51 is using. --- plugin/__init__.py | 4 ++++ plugin/util.py | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/plugin/__init__.py b/plugin/__init__.py index ed4c008..898f81b 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -27,6 +27,7 @@ import inspect import json import os import os.path +import platform import re import time import unicodedata @@ -1619,6 +1620,9 @@ class AnkiConnect: # when run inside Anki, `__name__` would be either numeric, # or, if installed via `link.sh`, `AnkiConnectDev` if __name__ != "plugin": + if platform.system() == "Windows" and anki_version == (2, 1, 50): + util.patch_anki_2_1_50_having_null_stdout_on_windows() + Edit.register_with_anki() ac = AnkiConnect() diff --git a/plugin/util.py b/plugin/util.py index cc3c157..3ae5eb1 100644 --- a/plugin/util.py +++ b/plugin/util.py @@ -14,6 +14,7 @@ # along with this program. If not, see . import os +import sys import anki import anki.sync @@ -83,3 +84,10 @@ def setting(key): return aqt.mw.addonManager.getConfig(__name__).get(key, DEFAULT_CONFIG[key]) except: raise Exception('setting {} not found'.format(key)) + + +# see https://github.com/FooSoft/anki-connect/issues/308 +# fixed in https://github.com/ankitects/anki/commit/0b2a226d +def patch_anki_2_1_50_having_null_stdout_on_windows(): + if sys.stdout is None: + sys.stdout = open(os.devnull, "w", encoding="utf8")