tornado.template encoding handling

16 views
Skip to first unread message

Raphael Krupinski

unread,
Nov 5, 2023, 5:42:03 PM11/5/23
to Tornado Web Server
Hi,

I'm looking into using tornado.template in my project, and I really like how simple in principle it is.

One thing I find confusing is the encoding handling. Wouldn't it be simpler to treat everything as str and only encode just before sending it over the wire?

The change in template.py would be small, ca. 10 LOC.

Is it a legacy from python 2 or am I missing a bigger picture?

Best Regards

Ben Darnell

unread,
Nov 5, 2023, 7:57:39 PM11/5/23
to python-...@googlegroups.com
It's mainly a legacy of the python 2 days, and fear of breaking backward compatibility.

But also, we need to avoid mixing str and bytes objects internally to the template functions. So if we kept everything as str, we'd need to decode any bytes objects that came in. But we know that ultimately anything we produce in a template is going out on the wire as bytes, so we'd potentially do a redundant decode/encode pair. So "encode everything to bytes" seems like a better default policy than "decode everything to str". 

If everything is str already, that's no big deal, but given Tornado's historical bias towards bytes, it seems likely that we'd have to make more far-reaching changes than 10 LOC in template.py to make a change like this without performance regressions.

-Ben

--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python-tornado/0f289589-7c47-43ab-b0d4-74b6f5f840ffn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages