Skip to content

Daphne disables warnings module #352

Open
django/channels
#1620
@mjnaderi

Description

Hi. I'm using django-channels in a django project. I wanted to use Python's warnings module to display a warning message. But it didn't work. Nothing was printed. I started the server with python -Wa manage.py runserver, but the warning didn't show up.

My code:

import warnings

def myview(request):
    warnings.warn("you've been warned!")
    ...

I removed channels from INSTALLED_APPS and ran runserver again. Issue was gone and the message was printed.

I tracked down the issue to this line in daphne code (server.py):

# Redirect the Twisted log to nowhere
globalLogBeginner.beginLoggingTo(
    [lambda _: None], redirectStandardIO=False, discardBuffer=True
)

In beginLoggingTo, twisted runs this code:

self._warningsModule.showwarning = self.showwarning

which replaces warnings.showwarning with a custom function. (When I comment this line in twisted, issue is fixed.)

Would you please show me how can I use Python's warnings module properly?

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    • Status

      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions