diff --git a/qt/aqt/sound.py b/qt/aqt/sound.py index c95fcf2f5..af6559cf9 100644 --- a/qt/aqt/sound.py +++ b/qt/aqt/sound.py @@ -25,7 +25,7 @@ from aqt import gui_hooks from aqt.mpv import MPV, MPVBase from aqt.qt import * from aqt.taskman import TaskManager -from aqt.utils import restoreGeom, saveGeom +from aqt.utils import restoreGeom, saveGeom, startup_info # AV player protocol ########################################################################## @@ -209,6 +209,21 @@ def _packagedCmd(cmd) -> Tuple[Any, Dict[str, str]]: return cmd, env +# Platform hacks +########################################################################## + +# legacy global for add-ons +si = startup_info() + +# osx throws interrupted system call errors frequently +def retryWait(proc) -> Any: + while 1: + try: + return proc.wait() + except OSError: + continue + + # Simple player implementations ########################################################################## @@ -242,7 +257,9 @@ class SimpleProcessPlayer(Player): # pylint: disable=abstract-method def _play(self, tag: AVTag) -> None: assert isinstance(tag, SoundOrVideoTag) - self._process = subprocess.Popen(self.args + [tag.filename], env=self.env) + self._process = subprocess.Popen( + self.args + [tag.filename], env=self.env, startupinfo=startup_info() + ) self._wait_for_termination(tag) def _wait_for_termination(self, tag: AVTag): @@ -301,34 +318,6 @@ class SimpleMplayerPlayer(SimpleProcessPlayer, SoundOrVideoPlayer): args += ["-ao", "win32"] -# Platform hacks -########################################################################## - -# don't show box on windows -si: Optional[Any] -if sys.platform == "win32": - si = subprocess.STARTUPINFO() # pytype: disable=module-attr - try: - si.dwFlags |= subprocess.STARTF_USESHOWWINDOW # pytype: disable=module-attr - except: - # pylint: disable=no-member - # python2.7+ - si.dwFlags |= ( - subprocess._subprocess.STARTF_USESHOWWINDOW - ) # pytype: disable=module-attr -else: - si = None - - -# osx throws interrupted system call errors frequently -def retryWait(proc) -> Any: - while 1: - try: - return proc.wait() - except OSError: - continue - - # MPV ########################################################################## @@ -396,7 +385,10 @@ class SimpleMplayerSlaveModePlayer(SimpleMplayerPlayer): def _play(self, tag: AVTag) -> None: assert isinstance(tag, SoundOrVideoTag) self._process = subprocess.Popen( - self.args + [tag.filename], env=self.env, stdin=subprocess.PIPE + self.args + [tag.filename], + env=self.env, + stdin=subprocess.PIPE, + startupinfo=startup_info(), ) self._wait_for_termination(tag) @@ -441,7 +433,9 @@ class _Recorder: continue try: cmd, env = _packagedCmd(c) - ret = retryWait(subprocess.Popen(cmd, startupinfo=si, env=env)) + ret = retryWait( + subprocess.Popen(cmd, startupinfo=startup_info(), env=env) + ) except: ret = True finally: diff --git a/qt/aqt/utils.py b/qt/aqt/utils.py index eb084e37a..6c316bd5d 100644 --- a/qt/aqt/utils.py +++ b/qt/aqt/utils.py @@ -5,7 +5,7 @@ import os import re import subprocess import sys -from typing import Optional +from typing import Any, Optional import aqt from anki.lang import _ @@ -759,3 +759,15 @@ def opengl_vendor(): def gfxDriverIsBroken(): driver = opengl_vendor() return driver == "nouveau" + + +###################################################################### + + +def startup_info() -> Any: + "Use subprocess.Popen(startupinfo=...) to avoid opening a console window." + if not sys.platform == "win32": + return None + si = subprocess.STARTUPINFO() # pytype: disable=module-attr + si.dwFlags |= subprocess.STARTF_USESHOWWINDOW # pytype: disable=module-attr + return si diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index 4b324d14b..2c0f28d7d 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -328,13 +328,7 @@ body {{ zoom: {}; {} }} {} """.format( - self.title, - self.zoomFactor(), - fontspec, - widgetspec, - head, - body_class, - body, + self.title, self.zoomFactor(), fontspec, widgetspec, head, body_class, body, ) # print(html) self.setHtml(html)