Fix for running in pythonw (#2448)

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
This commit is contained in:
Aidan Timson 2022-06-19 12:48:06 +01:00 committed by GitHub
parent d1c5e8003b
commit 6c48c8b3ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 13 deletions

View File

@ -3,6 +3,8 @@ import sys
from os import environ
from sanic.compat import is_atty
BASE_LOGO = """
@ -44,7 +46,7 @@ ansi_pattern = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
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 is_atty()
else BASE_LOGO
)

View File

@ -1,11 +1,10 @@
import sys
from abc import ABC, abstractmethod
from shutil import get_terminal_size
from textwrap import indent, wrap
from typing import Dict, Optional
from sanic import __version__
from sanic.compat import is_atty
from sanic.log import logger
@ -36,7 +35,7 @@ class MOTD(ABC):
data: Dict[str, str],
extra: Dict[str, str],
) -> None:
motd_class = MOTDTTY if sys.stdout.isatty() else MOTDBasic
motd_class = MOTDTTY if is_atty() else MOTDBasic
motd_class(logo, serve_location, data, extra).display()

View File

@ -1,8 +1,7 @@
import asyncio
import os
import signal
from sys import argv
import sys
from multidict import CIMultiDict # type: ignore
@ -47,7 +46,7 @@ class Header(CIMultiDict):
return self.getall(key, default=[])
use_trio = argv[0].endswith("hypercorn") and "trio" in argv
use_trio = sys.argv[0].endswith("hypercorn") and "trio" in sys.argv
if use_trio: # pragma: no cover
import trio # type: ignore
@ -89,3 +88,7 @@ def ctrlc_workaround_for_windows(app):
die = False
signal.signal(signal.SIGINT, ctrlc_handler)
app.add_task(stay_active)
def is_atty() -> bool:
return bool(sys.stdout and sys.stdout.isatty())

View File

@ -5,6 +5,8 @@ from enum import Enum
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,
@ -98,7 +100,7 @@ Logger used by Sanic for access logging
def deprecation(message: str, version: float): # no cov
version_info = f"[DEPRECATION v{version}] "
if 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)

View File

@ -2,7 +2,6 @@ from __future__ import annotations
import os
import platform
import sys
from asyncio import (
AbstractEventLoop,
@ -26,7 +25,7 @@ from sanic.application.logo import get_logo
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 +423,7 @@ class RunnerMixin(metaclass=SanicMeta):
self.motd(self.serve_location)
if 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 +614,7 @@ class RunnerMixin(metaclass=SanicMeta):
f"{app.state.workers} worker(s), which will be ignored "
"in favor of the primary application."
)
if sys.stdout.isatty():
if is_atty():
message = "".join(
[
Colors.YELLOW,
@ -656,7 +655,7 @@ class RunnerMixin(metaclass=SanicMeta):
"The encountered error was: "
)
second_message = str(e)
if sys.stdout.isatty():
if is_atty():
message_parts = [
Colors.YELLOW,
first_message,