From 037b286eb81dba0b6604dc06f70c07670cc3156f Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Wed, 4 May 2022 12:40:50 +0100 Subject: [PATCH 1/8] Fix for running in pythonw --- sanic/mixins/runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/mixins/runner.py b/sanic/mixins/runner.py index 1df77e551e..30645b2b1d 100644 --- a/sanic/mixins/runner.py +++ b/sanic/mixins/runner.py @@ -424,7 +424,7 @@ def _helper( self.motd(self.serve_location) - if sys.stdout.isatty() and not self.state.is_debug: + if sys.stdout and sys.stdout.isatty() and not self.state.is_debug: error_logger.warning( f"{Colors.YELLOW}Sanic is running in PRODUCTION mode. " "Consider using '--debug' or '--dev' while actively " From 87b0de9c3348279fed31cffa2955e378d6bd71d1 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Wed, 4 May 2022 12:40:50 +0100 Subject: [PATCH 2/8] Fix for running in pythonw --- sanic/mixins/runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanic/mixins/runner.py b/sanic/mixins/runner.py index 1df77e551e..30645b2b1d 100644 --- a/sanic/mixins/runner.py +++ b/sanic/mixins/runner.py @@ -424,7 +424,7 @@ def _helper( self.motd(self.serve_location) - if sys.stdout.isatty() and not self.state.is_debug: + if sys.stdout and sys.stdout.isatty() and not self.state.is_debug: error_logger.warning( f"{Colors.YELLOW}Sanic is running in PRODUCTION mode. " "Consider using '--debug' or '--dev' while actively " From 2a328f3a6413a8432a192fc7de9f947af8134186 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Thu, 12 May 2022 18:51:12 +0100 Subject: [PATCH 3/8] Additional checks --- sanic/application/logo.py | 2 +- sanic/application/motd.py | 2 +- sanic/log.py | 2 +- sanic/mixins/runner.py | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/sanic/application/logo.py b/sanic/application/logo.py index 56b8c0b107..5a87ecc16d 100644 --- a/sanic/application/logo.py +++ b/sanic/application/logo.py @@ -44,7 +44,7 @@ def get_logo(full=False, coffee=False): logo = ( (FULL_COLOR_LOGO if full else (COFFEE_LOGO if coffee else COLOR_LOGO)) - if sys.stdout.isatty() + if sys.stdout and sys.stdout.isatty() else BASE_LOGO ) diff --git a/sanic/application/motd.py b/sanic/application/motd.py index 4de046a5d8..a00abcb271 100644 --- a/sanic/application/motd.py +++ b/sanic/application/motd.py @@ -36,7 +36,7 @@ def output( data: Dict[str, str], extra: Dict[str, str], ) -> None: - motd_class = MOTDTTY if sys.stdout.isatty() else MOTDBasic + motd_class = MOTDTTY if sys.stdout and sys.stdout.isatty() else MOTDBasic motd_class(logo, serve_location, data, extra).display() diff --git a/sanic/log.py b/sanic/log.py index 4b3b960c4d..f0814b5b45 100644 --- a/sanic/log.py +++ b/sanic/log.py @@ -83,7 +83,7 @@ class Colors(str, Enum): # no cov def deprecation(message: str, version: float): # no cov version_info = f"[DEPRECATION v{version}] " - if sys.stdout.isatty(): + if sys.stdout and sys.stdout.isatty(): version_info = f"{Colors.RED}{version_info}" message = f"{Colors.YELLOW}{message}{Colors.END}" warn(version_info + message, DeprecationWarning) diff --git a/sanic/mixins/runner.py b/sanic/mixins/runner.py index 30645b2b1d..519e7c2928 100644 --- a/sanic/mixins/runner.py +++ b/sanic/mixins/runner.py @@ -615,7 +615,7 @@ async def _start_servers( f"{app.state.workers} worker(s), which will be ignored " "in favor of the primary application." ) - if sys.stdout.isatty(): + if sys.stdout and sys.stdout.isatty(): message = "".join( [ Colors.YELLOW, @@ -656,7 +656,7 @@ async def _start_servers( "The encountered error was: " ) second_message = str(e) - if sys.stdout.isatty(): + if sys.stdout and sys.stdout.isatty(): message_parts = [ Colors.YELLOW, first_message, From 89f56ac14186c1b850c07d69381aa7faa9822a8c Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Thu, 12 May 2022 19:03:01 +0100 Subject: [PATCH 4/8] Use helper function --- sanic/application/logo.py | 4 +++- sanic/application/motd.py | 3 ++- sanic/compat.py | 6 +++++- sanic/log.py | 4 +++- sanic/mixins/runner.py | 8 ++++---- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/sanic/application/logo.py b/sanic/application/logo.py index 5a87ecc16d..3c16a4421d 100644 --- a/sanic/application/logo.py +++ b/sanic/application/logo.py @@ -3,6 +3,8 @@ from os import environ +from sanic.compat import is_atty + BASE_LOGO = """ @@ -44,7 +46,7 @@ def get_logo(full=False, coffee=False): logo = ( (FULL_COLOR_LOGO if full else (COFFEE_LOGO if coffee else COLOR_LOGO)) - if sys.stdout and sys.stdout.isatty() + if is_atty() else BASE_LOGO ) diff --git a/sanic/application/motd.py b/sanic/application/motd.py index a00abcb271..4963d6408f 100644 --- a/sanic/application/motd.py +++ b/sanic/application/motd.py @@ -6,6 +6,7 @@ from typing import Dict, Optional from sanic import __version__ +from sanic.compat import is_atty from sanic.log import logger @@ -36,7 +37,7 @@ def output( data: Dict[str, str], extra: Dict[str, str], ) -> None: - motd_class = MOTDTTY if sys.stdout and sys.stdout.isatty() else MOTDBasic + motd_class = MOTDTTY if is_atty() else MOTDBasic motd_class(logo, serve_location, data, extra).display() diff --git a/sanic/compat.py b/sanic/compat.py index d8e0bea11f..27dfe5bef1 100644 --- a/sanic/compat.py +++ b/sanic/compat.py @@ -2,7 +2,7 @@ import os import signal -from sys import argv +from sys import argv, stdout from multidict import CIMultiDict # type: ignore @@ -25,6 +25,10 @@ def enable_windows_color_support(): kernel.SetConsoleMode(kernel.GetStdHandle(-11), 7) +def is_atty(): + return stdout and stdout.isatty() + + class Header(CIMultiDict): """ Container used for both request and response headers. It is a subclass of diff --git a/sanic/log.py b/sanic/log.py index f0814b5b45..9273b278d2 100644 --- a/sanic/log.py +++ b/sanic/log.py @@ -5,6 +5,8 @@ from typing import Any, Dict from warnings import warn +from sanic.compat import is_atty + LOGGING_CONFIG_DEFAULTS: Dict[str, Any] = dict( # no cov version=1, @@ -83,7 +85,7 @@ class Colors(str, Enum): # no cov def deprecation(message: str, version: float): # no cov version_info = f"[DEPRECATION v{version}] " - if sys.stdout and sys.stdout.isatty(): + if is_atty(): version_info = f"{Colors.RED}{version_info}" message = f"{Colors.YELLOW}{message}{Colors.END}" warn(version_info + message, DeprecationWarning) diff --git a/sanic/mixins/runner.py b/sanic/mixins/runner.py index 519e7c2928..4b6238a474 100644 --- a/sanic/mixins/runner.py +++ b/sanic/mixins/runner.py @@ -26,7 +26,7 @@ from sanic.application.motd import MOTD from sanic.application.state import ApplicationServerInfo, Mode, ServerStage from sanic.base.meta import SanicMeta -from sanic.compat import OS_IS_WINDOWS +from sanic.compat import OS_IS_WINDOWS, is_atty from sanic.helpers import _default from sanic.log import Colors, error_logger, logger from sanic.models.handler_types import ListenerType @@ -424,7 +424,7 @@ def _helper( self.motd(self.serve_location) - if sys.stdout and sys.stdout.isatty() and not self.state.is_debug: + if is_atty() and not self.state.is_debug: error_logger.warning( f"{Colors.YELLOW}Sanic is running in PRODUCTION mode. " "Consider using '--debug' or '--dev' while actively " @@ -615,7 +615,7 @@ async def _start_servers( f"{app.state.workers} worker(s), which will be ignored " "in favor of the primary application." ) - if sys.stdout and sys.stdout.isatty(): + if is_atty(): message = "".join( [ Colors.YELLOW, @@ -656,7 +656,7 @@ async def _start_servers( "The encountered error was: " ) second_message = str(e) - if sys.stdout and sys.stdout.isatty(): + if is_atty(): message_parts = [ Colors.YELLOW, first_message, From b3351cf1c9177616e91c0c88252aa70c15e5c1d7 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Thu, 12 May 2022 19:04:19 +0100 Subject: [PATCH 5/8] Cleanup import --- sanic/mixins/runner.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sanic/mixins/runner.py b/sanic/mixins/runner.py index 4b6238a474..d2d1e66ceb 100644 --- a/sanic/mixins/runner.py +++ b/sanic/mixins/runner.py @@ -2,7 +2,6 @@ import os import platform -import sys from asyncio import ( AbstractEventLoop, From 9f6ccb723b5aea7df1367a04116a8515324af375 Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Thu, 12 May 2022 19:06:27 +0100 Subject: [PATCH 6/8] Move to bottom --- sanic/compat.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/sanic/compat.py b/sanic/compat.py index 27dfe5bef1..ae4e0f214f 100644 --- a/sanic/compat.py +++ b/sanic/compat.py @@ -25,10 +25,6 @@ def enable_windows_color_support(): kernel.SetConsoleMode(kernel.GetStdHandle(-11), 7) -def is_atty(): - return stdout and stdout.isatty() - - class Header(CIMultiDict): """ Container used for both request and response headers. It is a subclass of @@ -93,3 +89,7 @@ def ctrlc_handler(sig, frame): die = False signal.signal(signal.SIGINT, ctrlc_handler) app.add_task(stay_active) + + +def is_atty(): + return stdout and stdout.isatty() From a675d05afb8034e27c4ea1aa687edb5171b4669b Mon Sep 17 00:00:00 2001 From: Aidan Timson Date: Tue, 14 Jun 2022 22:56:47 +0100 Subject: [PATCH 7/8] Remove unused import --- sanic/application/motd.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sanic/application/motd.py b/sanic/application/motd.py index 4963d6408f..df1f1338bd 100644 --- a/sanic/application/motd.py +++ b/sanic/application/motd.py @@ -1,5 +1,3 @@ -import sys - from abc import ABC, abstractmethod from shutil import get_terminal_size from textwrap import indent, wrap From db3238fa92ebe1465d697820c4b991cf409dad7b Mon Sep 17 00:00:00 2001 From: Adam Hopkins Date: Sun, 19 Jun 2022 12:48:03 +0300 Subject: [PATCH 8/8] Force boolean --- sanic/compat.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sanic/compat.py b/sanic/compat.py index 196fd04450..880ca68199 100644 --- a/sanic/compat.py +++ b/sanic/compat.py @@ -90,5 +90,5 @@ def ctrlc_handler(sig, frame): app.add_task(stay_active) -def is_atty(): - return sys.stdout and sys.stdout.isatty() +def is_atty() -> bool: + return bool(sys.stdout and sys.stdout.isatty())