django / django/daphne

Daphne disables warnings module

Open
#352 9 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.7k
Forks
292
Avg merge
2d 15h
Merged PRs (30d)
3

Description

Hi. I'm using django-channels in a django project. I wanted to use Python's [`warnings` module](https://docs.python.org/3/library/warnings.html#warnings.warn) 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:

```python
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](https://github.com/django/daphne/blob/aae0870971cff742a09a0491dae330a1f6cc8ea2/daphne/server.py#L101-L104) in daphne code (`server.py`):

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

In `beginLoggingTo`, twisted runs this code:

```python
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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.