They're not my objects chap. It's for a shell, so the user will be able to print any stuff they like. If they print a list of 3 Foos, in a browser based client, they should not get a list of three broken [invisible] HTML elements. I'm after a way to tweak Python so that all objects that are represented after the tweaks are made, are rendered differently to the default.
In that case, you could change sys.displayhook()
:
import sys
from cgi import escape
try:
import __builtin__ as builtins
except ImportError: # Python 3
import builtins
def html_displayhook(value):
if value is None:
return
text = escape(repr(value)) # <-- make html-safe text
sys.stdout.write(text)
sys.stdout.write("\n")
builtins._ = value
sys.displayhook = html_displayhook
It prints html-safe object representations by default and it doesn't prevent users from printing a raw html if they like it.