﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33150	EmailBackend.write_message crashes when self.steam is BufferedWriter	Bernd Wechner	nobody	"I have no idea what's going on here alas, but I had to fix this fast locally. I have three versions of the one Danjgo site running, dev, sandbox and live respectively. I run near identically on each one, and as I fix things on dev test them on sandbox and publish them on live. They are not picture perfect identical although sandbox and live certainly come very  close.

Fortunately I can debug all three with PyDev (awesome!) and this is the first time I've had to remotely debug the live site, as it was crashing when users attempt to reset their passwords. it crashed here:

https://github.com/django/django/blob/4ffada36097ceccfd6f0d358217ac3c40d2a729c/django/core/mail/backends/console.py#L21

and it did so because {{{self.stream}}} at this point, on the live server was of type {{{BufferedWriter}}} while on the sandbox and dev sites it is of type {{{TextIOWrapper}}}.

The latter take strings, and is fed a string at this line. Alas the former does not, it only takes byte strings.

I have no idea how or why these sites deviate in self.stream here, or whence that stemeth. I would like to know, but that would a research project I lack time for in the urgent need to get this site live. 

So I patched that method and it now reads:

{{{
    def write_message(self, message):
        #import pydevd; pydevd.settrace('192.168.0.11', stdoutToServer=True, stderrToServer=True)
        msg = message.message()
        msg_data = msg.as_bytes()
        charset = msg.get_charset().get_output_charset() if msg.get_charset() else 'utf-8'
        if self.stream.mode.endswith('b'):
            self.stream.write(msg_data)
            self.stream.write(b'\n')
            self.stream.write(b'-' * 79)
            self.stream.write(b'\n')
        else:
            msg_data = msg_data.decode(charset)
            self.stream.write('%s\n' % msg_data)
            self.stream.write('-' * 79)
            self.stream.write('\n')
}}}

(you can see my breakpoint in the pydev debugger commented out there)

There are two distinct issues here:

1. How is it possible that self.stream is set to an incompatible type?
2. This method should really be more robust (as pictured) anyhow ..."	Uncategorized	closed	Core (Mail)	3.2	Normal	invalid			Unreviewed	0	0	0	0	0	0
