Skip to content

Commit

Permalink
Fix for running in pythonw (#2448)
Browse files Browse the repository at this point in the history
Co-authored-by: Adam Hopkins <adam@amhopkins.com>
  • Loading branch information
timmo001 and ahopkins committed Jun 19, 2022
1 parent d1c5e80 commit 6c48c8b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
4 changes: 3 additions & 1 deletion sanic/application/logo.py
Expand Up @@ -3,6 +3,8 @@

from os import environ

from sanic.compat import is_atty


BASE_LOGO = """
Expand Down Expand Up @@ -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.isatty()
if is_atty()
else BASE_LOGO
)

Expand Down
5 changes: 2 additions & 3 deletions sanic/application/motd.py
@@ -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


Expand Down Expand Up @@ -36,7 +35,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 is_atty() else MOTDBasic
motd_class(logo, serve_location, data, extra).display()


Expand Down
9 changes: 6 additions & 3 deletions sanic/compat.py
@@ -1,8 +1,7 @@
import asyncio
import os
import signal

from sys import argv
import sys

from multidict import CIMultiDict # type: ignore

Expand Down Expand Up @@ -47,7 +46,7 @@ def get_all(self, key: str):
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
Expand Down Expand Up @@ -89,3 +88,7 @@ def ctrlc_handler(sig, frame):
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())
4 changes: 3 additions & 1 deletion sanic/log.py
Expand Up @@ -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,
Expand Down Expand Up @@ -98,7 +100,7 @@ def filter(self, record: logging.LogRecord) -> bool:

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)
9 changes: 4 additions & 5 deletions sanic/mixins/runner.py
Expand Up @@ -2,7 +2,6 @@

import os
import platform
import sys

from asyncio import (
AbstractEventLoop,
Expand All @@ -26,7 +25,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
Expand Down Expand Up @@ -424,7 +423,7 @@ def _helper(

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 "
Expand Down Expand Up @@ -615,7 +614,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 is_atty():
message = "".join(
[
Colors.YELLOW,
Expand Down Expand Up @@ -656,7 +655,7 @@ async def _start_servers(
"The encountered error was: "
)
second_message = str(e)
if sys.stdout.isatty():
if is_atty():
message_parts = [
Colors.YELLOW,
first_message,
Expand Down

0 comments on commit 6c48c8b

Please sign in to comment.