@@ -62,16 +62,7 @@ the storage format for strings.
6262
6363The "pass through encoded data" scheme is what template
6464languages like Cheetah and earlier versions of Myghty do by
65- default. Mako as of version 0.2 also supports this mode of
66- operation when using Python 2, using the ``disable_unicode=True ``
67- flag. However, when using Mako in its default mode of
68- unicode-aware, it requires explicitness when dealing with
69- non-ASCII encodings. Additionally, if you ever need to handle
70- unicode strings and other kinds of encoding conversions more
71- intelligently, the usage of raw byte-strings quickly becomes a
72- nightmare, since you are sending the Python interpreter
73- collections of bytes for which it can make no intelligent
74- decisions with regards to encoding. In Python 3 Mako only allows
65+ default. In Python 3 Mako only allows
7566usage of native, unicode strings.
7667
7768In normal Mako operation, all parsed template constructs and
@@ -255,82 +246,5 @@ which cannot handle encoding of non-ASCII ``unicode`` objects
255246buffering. Otherwise, a custom Mako class called
256247``FastEncodingBuffer `` is used, which essentially is a super
257248dumbed-down version of ``StringIO `` that gathers all strings into
258- a list and uses ``u ''.join(elements) `` to produce the final output
249+ a list and uses ``''.join(elements) `` to produce the final output
259250-- it's markedly faster than ``StringIO ``.
260-
261- .. _unicode_disabled :
262-
263- Saying to Heck with It: Disabling the Usage of Unicode Entirely
264- ===============================================================
265-
266- Some segments of Mako's userbase choose to make no usage of
267- Unicode whatsoever, and instead would prefer the "pass through"
268- approach; all string expressions in their templates return
269- encoded byte-strings, and they would like these strings to pass
270- right through. The only advantage to this approach is that
271- templates need not use ``u"" `` for literal strings; there's an
272- arguable speed improvement as well since raw byte-strings
273- generally perform slightly faster than unicode objects in
274- Python. For these users, assuming they're sticking with Python
275- 2, they can hit the ``disable_unicode=True `` flag as so:
276-
277- .. sourcecode :: python
278-
279- # -*- coding:utf-8 - *-
280- from mako.template import Template
281-
282- t = Template("drôle de petite voix m’a réveillé.", disable_unicode=True, input_encoding='utf-8')
283- print(t.code)
284-
285- The ``disable_unicode `` mode is strictly a Python 2 thing. It is
286- not supported at all in Python 3.
287-
288- The generated module source code will contain elements like
289- these:
290-
291- .. sourcecode :: python
292-
293- # -*- coding:utf-8 - *-
294- # ...more generated code ...
295-
296- def render_body(context,**pageargs):
297- context.caller_stack.push_frame()
298- try:
299- __M_locals = dict(pageargs=pageargs)
300- # SOURCE LINE 1
301- context.write('dr\x c3\x b4le de petite voix m\x e2\x 80\x 99a r\x c3\x a9veill\x c3\x a9.')
302- return ''
303- finally:
304- context.caller_stack.pop_frame()
305-
306- Where above that the string literal used within :meth: `.Context.write `
307- is a regular byte-string.
308-
309- When ``disable_unicode=True `` is turned on, the ``default_filters ``
310- argument which normally defaults to ``["unicode"] `` now defaults
311- to ``["str"] `` instead. Setting ``default_filters `` to the empty list
312- ``[] `` can remove the overhead of the ``str `` call. Also, in this
313- mode you **cannot ** safely call :meth: `~.Template.render_unicode ` -- you'll get
314- unicode/decode errors.
315-
316- The ``h `` filter (HTML escape) uses a less performant pure Python
317- escape function in non-unicode mode. This because
318- MarkupSafe only supports Python unicode objects for non-ASCII
319- strings.
320-
321- .. versionchanged :: 0.3.4
322- In prior versions, it used ``cgi.escape() ``, which has been replaced
323- with a function that also escapes single quotes.
324-
325- Rules for using ``disable_unicode=True ``
326- ----------------------------------------
327-
328- * Don't use this mode unless you really, really want to and you
329- absolutely understand what you're doing.
330- * Don't use this option just because you don't want to learn to
331- use Unicode properly; we aren't supporting user issues in this
332- mode of operation. We will however offer generous help for the
333- vast majority of users who stick to the Unicode program.
334- * Python 3 is unicode by default, and the flag is not available
335- when running on Python 3.
336-
0 commit comments