unicode.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  3. <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
  4. <head>
  5. <title>
  6. The Unicode Chapter
  7. &mdash;
  8. Mako 1.0.7 Documentation
  9. </title>
  10. <!-- begin iterate through sphinx environment css_files -->
  11. <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
  12. <link rel="stylesheet" href="_static/docs.css" type="text/css" />
  13. <link rel="stylesheet" href="_static/site.css" type="text/css" />
  14. <link rel="stylesheet" href="_static/changelog.css" type="text/css" />
  15. <link rel="stylesheet" href="_static/sphinx_paramlinks.css" type="text/css" />
  16. <!-- end iterate through sphinx environment css_files -->
  17. <script type="text/javascript">
  18. var DOCUMENTATION_OPTIONS = {
  19. URL_ROOT: './',
  20. VERSION: '1.0.7',
  21. COLLAPSE_MODINDEX: false,
  22. FILE_SUFFIX: '.html'
  23. };
  24. </script>
  25. <script type="text/javascript" src="_static/jquery.js"></script>
  26. <script type="text/javascript" src="_static/underscore.js"></script>
  27. <script type="text/javascript" src="_static/doctools.js"></script>
  28. <link rel="index" title="Index" href="genindex.html" />
  29. <link rel="search" title="Search" href="search.html" />
  30. <link rel="top" title="Mako 1.0.7 Documentation" href="index.html" />
  31. <link rel="next" title="Caching" href="caching.html" />
  32. <link rel="prev" title="Filtering and Buffering" href="filtering.html" />
  33. </head>
  34. <body>
  35. <div id="wrap">
  36. <div class="rightbar">
  37. <div class="slogan">
  38. Hyperfast and lightweight templating for the Python platform.
  39. </div>
  40. </div>
  41. <a href="http://www.makotemplates.org/"><img src="_static/makoLogo.png" /></a>
  42. <hr/>
  43. <div id="docs-container">
  44. <div id="docs-header">
  45. <h1>Mako 1.0.7 Documentation</h1>
  46. <div id="docs-search">
  47. Search:
  48. <form class="search" action="search.html" method="get">
  49. <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
  50. <input type="hidden" name="check_keywords" value="yes" />
  51. <input type="hidden" name="area" value="default" />
  52. </form>
  53. </div>
  54. <div id="docs-version-header">
  55. Release: <span class="version-num">1.0.7</span>
  56. </div>
  57. </div>
  58. <div id="docs-top-navigation">
  59. <div id="docs-top-page-control" class="docs-navigation-links">
  60. <ul>
  61. <li>Prev:
  62. <a href="filtering.html" title="previous chapter">Filtering and Buffering</a>
  63. </li>
  64. <li>Next:
  65. <a href="caching.html" title="next chapter">Caching</a>
  66. </li>
  67. <li>
  68. <a href="index.html">Table of Contents</a> |
  69. <a href="genindex.html">Index</a>
  70. | <a href="_sources/unicode.rst.txt">view source
  71. </li>
  72. </ul>
  73. </div>
  74. <div id="docs-navigation-banner">
  75. <a href="index.html">Mako 1.0.7 Documentation</a>
  76. »
  77. The Unicode Chapter
  78. <h2>
  79. The Unicode Chapter
  80. </h2>
  81. </div>
  82. </div>
  83. <div id="docs-body-container">
  84. <div id="docs-sidebar">
  85. <h3><a href="index.html">Table of Contents</a></h3>
  86. <ul>
  87. <li><a class="reference internal" href="#">The Unicode Chapter</a><ul>
  88. <li><a class="reference internal" href="#specifying-the-encoding-of-a-template-file">Specifying the Encoding of a Template File</a></li>
  89. <li><a class="reference internal" href="#handling-expressions">Handling Expressions</a></li>
  90. <li><a class="reference internal" href="#defining-output-encoding">Defining Output Encoding</a><ul>
  91. <li><a class="reference internal" href="#buffer-selection">Buffer Selection</a></li>
  92. </ul>
  93. </li>
  94. <li><a class="reference internal" href="#saying-to-heck-with-it-disabling-the-usage-of-unicode-entirely">Saying to Heck with It: Disabling the Usage of Unicode Entirely</a><ul>
  95. <li><a class="reference internal" href="#rules-for-using-disable-unicode-true">Rules for using <code class="docutils literal"><span class="pre">disable_unicode=True</span></code></a></li>
  96. </ul>
  97. </li>
  98. </ul>
  99. </li>
  100. </ul>
  101. <h4>Previous Topic</h4>
  102. <p>
  103. <a href="filtering.html" title="previous chapter">Filtering and Buffering</a>
  104. </p>
  105. <h4>Next Topic</h4>
  106. <p>
  107. <a href="caching.html" title="next chapter">Caching</a>
  108. </p>
  109. <h4>Quick Search</h4>
  110. <p>
  111. <form class="search" action="search.html" method="get">
  112. <input type="text" name="q" size="18" /> <input type="submit" value="Search" />
  113. <input type="hidden" name="check_keywords" value="yes" />
  114. <input type="hidden" name="area" value="default" />
  115. </form>
  116. </p>
  117. </div>
  118. <div id="docs-body" class="withsidebar" >
  119. <div class="section" id="the-unicode-chapter">
  120. <span id="unicode-toplevel"></span><h1>The Unicode Chapter<a class="headerlink" href="#the-unicode-chapter" title="Permalink to this headline">¶</a></h1>
  121. <p>The Python language supports two ways of representing what we
  122. know as &#8220;strings&#8221;, i.e. series of characters. In Python 2, the
  123. two types are <code class="docutils literal"><span class="pre">string</span></code> and <code class="docutils literal"><span class="pre">unicode</span></code>, and in Python 3 they are
  124. <code class="docutils literal"><span class="pre">bytes</span></code> and <code class="docutils literal"><span class="pre">string</span></code>. A key aspect of the Python 2 <code class="docutils literal"><span class="pre">string</span></code> and
  125. Python 3 <code class="docutils literal"><span class="pre">bytes</span></code> types are that they contain no information
  126. regarding what <strong>encoding</strong> the data is stored in. For this
  127. reason they were commonly referred to as <strong>byte strings</strong> on
  128. Python 2, and Python 3 makes this name more explicit. The
  129. origins of this come from Python&#8217;s background of being developed
  130. before the Unicode standard was even available, back when
  131. strings were C-style strings and were just that, a series of
  132. bytes. Strings that had only values below 128 just happened to
  133. be <strong>ASCII</strong> strings and were printable on the console, whereas
  134. strings with values above 128 would produce all kinds of
  135. graphical characters and bells.</p>
  136. <p>Contrast the &#8220;byte-string&#8221; type with the &#8220;unicode/string&#8221; type.
  137. Objects of this latter type are created whenever you say something like
  138. <code class="docutils literal"><span class="pre">u&quot;hello</span> <span class="pre">world&quot;</span></code> (or in Python 3, just <code class="docutils literal"><span class="pre">&quot;hello</span> <span class="pre">world&quot;</span></code>). In this
  139. case, Python represents each character in the string internally
  140. using multiple bytes per character (something similar to
  141. UTF-16). What&#8217;s important is that when using the
  142. <code class="docutils literal"><span class="pre">unicode</span></code>/<code class="docutils literal"><span class="pre">string</span></code> type to store strings, Python knows the
  143. data&#8217;s encoding; it&#8217;s in its own internal format. Whereas when
  144. using the <code class="docutils literal"><span class="pre">string</span></code>/<code class="docutils literal"><span class="pre">bytes</span></code> type, it does not.</p>
  145. <p>When Python 2 attempts to treat a byte-string as a string, which
  146. means it&#8217;s attempting to compare/parse its characters, to coerce
  147. it into another encoding, or to decode it to a unicode object,
  148. it has to guess what the encoding is. In this case, it will
  149. pretty much always guess the encoding as <code class="docutils literal"><span class="pre">ascii</span></code>... and if the
  150. byte-string contains bytes above value 128, you&#8217;ll get an error.
  151. Python 3 eliminates much of this confusion by just raising an
  152. error unconditionally if a byte-string is used in a
  153. character-aware context.</p>
  154. <p>There is one operation that Python <em>can</em> do with a non-ASCII
  155. byte-string, and it&#8217;s a great source of confusion: it can dump the
  156. byte-string straight out to a stream or a file, with nary a care
  157. what the encoding is. To Python, this is pretty much like
  158. dumping any other kind of binary data (like an image) to a
  159. stream somewhere. In Python 2, it is common to see programs that
  160. embed all kinds of international characters and encodings into
  161. plain byte-strings (i.e. using <code class="docutils literal"><span class="pre">&quot;hello</span> <span class="pre">world&quot;</span></code> style literals)
  162. can fly right through their run, sending reams of strings out to
  163. wherever they are going, and the programmer, seeing the same
  164. output as was expressed in the input, is now under the illusion
  165. that his or her program is Unicode-compliant. In fact, their
  166. program has no unicode awareness whatsoever, and similarly has
  167. no ability to interact with libraries that <em>are</em> unicode aware.
  168. Python 3 makes this much less likely by defaulting to unicode as
  169. the storage format for strings.</p>
  170. <p>The &#8220;pass through encoded data&#8221; scheme is what template
  171. languages like Cheetah and earlier versions of Myghty do by
  172. default. Mako as of version 0.2 also supports this mode of
  173. operation when using Python 2, using the <code class="docutils literal"><span class="pre">disable_unicode=True</span></code>
  174. flag. However, when using Mako in its default mode of
  175. unicode-aware, it requires explicitness when dealing with
  176. non-ASCII encodings. Additionally, if you ever need to handle
  177. unicode strings and other kinds of encoding conversions more
  178. intelligently, the usage of raw byte-strings quickly becomes a
  179. nightmare, since you are sending the Python interpreter
  180. collections of bytes for which it can make no intelligent
  181. decisions with regards to encoding. In Python 3 Mako only allows
  182. usage of native, unicode strings.</p>
  183. <p>In normal Mako operation, all parsed template constructs and
  184. output streams are handled internally as Python <code class="docutils literal"><span class="pre">unicode</span></code>
  185. objects. It&#8217;s only at the point of <a class="reference internal" href="usage.html#mako.template.Template.render" title="mako.template.Template.render"><code class="xref py py-meth docutils literal"><span class="pre">render()</span></code></a> that this unicode
  186. stream may be rendered into whatever the desired output encoding
  187. is. The implication here is that the template developer must
  188. :ensure that <a class="reference internal" href="#set-template-file-encoding"><span class="std std-ref">the encoding of all non-ASCII templates is explicit</span></a> (still required in Python 3),
  189. that <a class="reference internal" href="#handling-non-ascii-expressions"><span class="std std-ref">all non-ASCII-encoded expressions are in one way or another
  190. converted to unicode</span></a>
  191. (not much of a burden in Python 3), and that <a class="reference internal" href="#defining-output-encoding"><span class="std std-ref">the output stream of the
  192. template is handled as a unicode stream being encoded to some
  193. encoding</span></a> (still required in Python 3).</p>
  194. <div class="section" id="specifying-the-encoding-of-a-template-file">
  195. <span id="set-template-file-encoding"></span><h2>Specifying the Encoding of a Template File<a class="headerlink" href="#specifying-the-encoding-of-a-template-file" title="Permalink to this headline">¶</a></h2>
  196. <p>This is the most basic encoding-related setting, and it is
  197. equivalent to Python&#8217;s &#8220;magic encoding comment&#8221;, as described in
  198. <a class="reference external" href="http://www.python.org/dev/peps/pep-0263/">pep-0263</a>. Any
  199. template that contains non-ASCII characters requires that this
  200. comment be present so that Mako can decode to unicode (and also
  201. make usage of Python&#8217;s AST parsing services). Mako&#8217;s lexer will
  202. use this encoding in order to convert the template source into a
  203. <code class="docutils literal"><span class="pre">unicode</span></code> object before continuing its parsing:</p>
  204. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">## -*- coding: utf-8 -*-</span><span class="x"></span>
  205. <span class="x">Alors vous imaginez ma surprise, au lever du jour, quand</span>
  206. <span class="x">une drôle de petite voix m’a réveillé. Elle disait:</span>
  207. <span class="x"> « S’il vous plaît… dessine-moi un mouton! »</span>
  208. </pre></div>
  209. </div>
  210. <p>For the picky, the regular expression used is derived from that
  211. of the above mentioned pep:</p>
  212. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1">#.*coding[:=]\s*([-\w.]+).*\n</span>
  213. </pre></div>
  214. </div>
  215. <p>The lexer will convert to unicode in all cases, so that if any
  216. characters exist in the template that are outside of the
  217. specified encoding (or the default of <code class="docutils literal"><span class="pre">ascii</span></code>), the error will
  218. be immediate.</p>
  219. <p>As an alternative, the template encoding can be specified
  220. programmatically to either <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> or <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a> via
  221. the <code class="docutils literal"><span class="pre">input_encoding</span></code> parameter:</p>
  222. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">t</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;./&#39;</span><span class="p">],</span> <span class="n">input_encoding</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
  223. </pre></div>
  224. </div>
  225. <p>The above will assume all located templates specify <code class="docutils literal"><span class="pre">utf-8</span></code>
  226. encoding, unless the template itself contains its own magic
  227. encoding comment, which takes precedence.</p>
  228. </div>
  229. <div class="section" id="handling-expressions">
  230. <span id="handling-non-ascii-expressions"></span><h2>Handling Expressions<a class="headerlink" href="#handling-expressions" title="Permalink to this headline">¶</a></h2>
  231. <p>The next area that encoding comes into play is in expression
  232. constructs. By default, Mako&#8217;s treatment of an expression like
  233. this:</p>
  234. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">${</span><span class="s2">&quot;hello world&quot;</span><span class="cp">}</span><span class="x"></span>
  235. </pre></div>
  236. </div>
  237. <p>looks something like this:</p>
  238. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="nb">unicode</span><span class="p">(</span><span class="s2">&quot;hello world&quot;</span><span class="p">))</span>
  239. </pre></div>
  240. </div>
  241. <p>In Python 3, it&#8217;s just:</p>
  242. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="s2">&quot;hello world&quot;</span><span class="p">))</span>
  243. </pre></div>
  244. </div>
  245. <p>That is, <strong>the output of all expressions is run through the
  246. ``unicode`` built-in</strong>. This is the default setting, and can be
  247. modified to expect various encodings. The <code class="docutils literal"><span class="pre">unicode</span></code> step serves
  248. both the purpose of rendering non-string expressions into
  249. strings (such as integers or objects which contain <code class="docutils literal"><span class="pre">__str()__</span></code>
  250. methods), and to ensure that the final output stream is
  251. constructed as a unicode object. The main implication of this is
  252. that <strong>any raw byte-strings that contain an encoding other than
  253. ASCII must first be decoded to a Python unicode object</strong>. It
  254. means you can&#8217;t say this in Python 2:</p>
  255. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">${</span><span class="s2">&quot;voix m’a réveillé.&quot;</span><span class="cp">}</span> <span class="cp">## error in Python 2!</span><span class="x"></span>
  256. </pre></div>
  257. </div>
  258. <p>You must instead say this:</p>
  259. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">${</span><span class="sa">u</span><span class="s2">&quot;voix m’a réveillé.&quot;</span><span class="cp">}</span> <span class="cp">## OK !</span><span class="x"></span>
  260. </pre></div>
  261. </div>
  262. <p>Similarly, if you are reading data from a file that is streaming
  263. bytes, or returning data from some object that is returning a
  264. Python byte-string containing a non-ASCII encoding, you have to
  265. explicitly decode to unicode first, such as:</p>
  266. <div class="highlight-mako"><div class="highlight"><pre><span></span><span class="cp">${</span><span class="n">call_my_object</span><span class="p">()</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span><span class="cp">}</span><span class="x"></span>
  267. </pre></div>
  268. </div>
  269. <p>Note that filehandles acquired by <code class="docutils literal"><span class="pre">open()</span></code> in Python 3 default
  270. to returning &#8220;text&#8221;, that is the decoding is done for you. See
  271. Python 3&#8217;s documentation for the <code class="docutils literal"><span class="pre">open()</span></code> built-in for details on
  272. this.</p>
  273. <p>If you want a certain encoding applied to <em>all</em> expressions,
  274. override the <code class="docutils literal"><span class="pre">unicode</span></code> builtin with the <code class="docutils literal"><span class="pre">decode</span></code> built-in at the
  275. <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> or <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a> level:</p>
  276. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="n">templatetext</span><span class="p">,</span> <span class="n">default_filters</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;decode.utf8&#39;</span><span class="p">])</span>
  277. </pre></div>
  278. </div>
  279. <p>Note that the built-in <code class="docutils literal"><span class="pre">decode</span></code> object is slower than the
  280. <code class="docutils literal"><span class="pre">unicode</span></code> function, since unlike <code class="docutils literal"><span class="pre">unicode</span></code> it&#8217;s not a Python
  281. built-in, and it also checks the type of the incoming data to
  282. determine if string conversion is needed first.</p>
  283. <p>The <code class="docutils literal"><span class="pre">default_filters</span></code> argument can be used to entirely customize
  284. the filtering process of expressions. This argument is described
  285. in <a class="reference internal" href="filtering.html#filtering-default-filters"><span class="std std-ref">The default_filters Argument</span></a>.</p>
  286. </div>
  287. <div class="section" id="defining-output-encoding">
  288. <span id="id1"></span><h2>Defining Output Encoding<a class="headerlink" href="#defining-output-encoding" title="Permalink to this headline">¶</a></h2>
  289. <p>Now that we have a template which produces a pure unicode output
  290. stream, all the hard work is done. We can take the output and do
  291. anything with it.</p>
  292. <p>As stated in the <a class="reference internal" href="usage.html"><span class="doc">&#8220;Usage&#8221; chapter</span></a>, both <a class="reference internal" href="usage.html#mako.template.Template" title="mako.template.Template"><code class="xref py py-class docutils literal"><span class="pre">Template</span></code></a> and
  293. <a class="reference internal" href="usage.html#mako.lookup.TemplateLookup" title="mako.lookup.TemplateLookup"><code class="xref py py-class docutils literal"><span class="pre">TemplateLookup</span></code></a> accept <code class="docutils literal"><span class="pre">output_encoding</span></code> and <code class="docutils literal"><span class="pre">encoding_errors</span></code>
  294. parameters which can be used to encode the output in any Python
  295. supported codec:</p>
  296. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  297. <span class="kn">from</span> <span class="nn">mako.lookup</span> <span class="kn">import</span> <span class="n">TemplateLookup</span>
  298. <span class="n">mylookup</span> <span class="o">=</span> <span class="n">TemplateLookup</span><span class="p">(</span><span class="n">directories</span><span class="o">=</span><span class="p">[</span><span class="s1">&#39;/docs&#39;</span><span class="p">],</span> <span class="n">output_encoding</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">,</span> <span class="n">encoding_errors</span><span class="o">=</span><span class="s1">&#39;replace&#39;</span><span class="p">)</span>
  299. <span class="n">mytemplate</span> <span class="o">=</span> <span class="n">mylookup</span><span class="o">.</span><span class="n">get_template</span><span class="p">(</span><span class="s2">&quot;foo.txt&quot;</span><span class="p">)</span>
  300. <span class="k">print</span><span class="p">(</span><span class="n">mytemplate</span><span class="o">.</span><span class="n">render</span><span class="p">())</span>
  301. </pre></div>
  302. </div>
  303. <p><a class="reference internal" href="usage.html#mako.template.Template.render" title="mako.template.Template.render"><code class="xref py py-meth docutils literal"><span class="pre">render()</span></code></a> will return a <code class="docutils literal"><span class="pre">bytes</span></code> object in Python 3 if an output
  304. encoding is specified. By default it performs no encoding and
  305. returns a native string.</p>
  306. <p><a class="reference internal" href="usage.html#mako.template.Template.render_unicode" title="mako.template.Template.render_unicode"><code class="xref py py-meth docutils literal"><span class="pre">render_unicode()</span></code></a> will return the template output as a Python
  307. <code class="docutils literal"><span class="pre">unicode</span></code> object (or <code class="docutils literal"><span class="pre">string</span></code> in Python 3):</p>
  308. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="n">mytemplate</span><span class="o">.</span><span class="n">render_unicode</span><span class="p">())</span>
  309. </pre></div>
  310. </div>
  311. <p>The above method disgards the output encoding keyword argument;
  312. you can encode yourself by saying:</p>
  313. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">print</span><span class="p">(</span><span class="n">mytemplate</span><span class="o">.</span><span class="n">render_unicode</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s1">&#39;utf-8&#39;</span><span class="p">,</span> <span class="s1">&#39;replace&#39;</span><span class="p">))</span>
  314. </pre></div>
  315. </div>
  316. <div class="section" id="buffer-selection">
  317. <h3>Buffer Selection<a class="headerlink" href="#buffer-selection" title="Permalink to this headline">¶</a></h3>
  318. <p>Mako does play some games with the style of buffering used
  319. internally, to maximize performance. Since the buffer is by far
  320. the most heavily used object in a render operation, it&#8217;s
  321. important!</p>
  322. <p>When calling <a class="reference internal" href="usage.html#mako.template.Template.render" title="mako.template.Template.render"><code class="xref py py-meth docutils literal"><span class="pre">render()</span></code></a> on a template that does not specify any
  323. output encoding (i.e. it&#8217;s <code class="docutils literal"><span class="pre">ascii</span></code>), Python&#8217;s <code class="docutils literal"><span class="pre">cStringIO</span></code> module,
  324. which cannot handle encoding of non-ASCII <code class="docutils literal"><span class="pre">unicode</span></code> objects
  325. (even though it can send raw byte-strings through), is used for
  326. buffering. Otherwise, a custom Mako class called
  327. <code class="docutils literal"><span class="pre">FastEncodingBuffer</span></code> is used, which essentially is a super
  328. dumbed-down version of <code class="docutils literal"><span class="pre">StringIO</span></code> that gathers all strings into
  329. a list and uses <code class="docutils literal"><span class="pre">u''.join(elements)</span></code> to produce the final output
  330. &#8211; it&#8217;s markedly faster than <code class="docutils literal"><span class="pre">StringIO</span></code>.</p>
  331. </div>
  332. </div>
  333. <div class="section" id="saying-to-heck-with-it-disabling-the-usage-of-unicode-entirely">
  334. <span id="unicode-disabled"></span><h2>Saying to Heck with It: Disabling the Usage of Unicode Entirely<a class="headerlink" href="#saying-to-heck-with-it-disabling-the-usage-of-unicode-entirely" title="Permalink to this headline">¶</a></h2>
  335. <p>Some segments of Mako&#8217;s userbase choose to make no usage of
  336. Unicode whatsoever, and instead would prefer the &#8220;pass through&#8221;
  337. approach; all string expressions in their templates return
  338. encoded byte-strings, and they would like these strings to pass
  339. right through. The only advantage to this approach is that
  340. templates need not use <code class="docutils literal"><span class="pre">u&quot;&quot;</span></code> for literal strings; there&#8217;s an
  341. arguable speed improvement as well since raw byte-strings
  342. generally perform slightly faster than unicode objects in
  343. Python. For these users, assuming they&#8217;re sticking with Python
  344. 2, they can hit the <code class="docutils literal"><span class="pre">disable_unicode=True</span></code> flag as so:</p>
  345. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># -*- coding:utf-8 -*-</span>
  346. <span class="kn">from</span> <span class="nn">mako.template</span> <span class="kn">import</span> <span class="n">Template</span>
  347. <span class="n">t</span> <span class="o">=</span> <span class="n">Template</span><span class="p">(</span><span class="s2">&quot;drôle de petite voix m’a réveillé.&quot;</span><span class="p">,</span> <span class="n">disable_unicode</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">input_encoding</span><span class="o">=</span><span class="s1">&#39;utf-8&#39;</span><span class="p">)</span>
  348. <span class="k">print</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">code</span><span class="p">)</span>
  349. </pre></div>
  350. </div>
  351. <p>The <code class="docutils literal"><span class="pre">disable_unicode</span></code> mode is strictly a Python 2 thing. It is
  352. not supported at all in Python 3.</p>
  353. <p>The generated module source code will contain elements like
  354. these:</p>
  355. <div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># -*- coding:utf-8 -*-</span>
  356. <span class="c1"># ...more generated code ...</span>
  357. <span class="k">def</span> <span class="nf">render_body</span><span class="p">(</span><span class="n">context</span><span class="p">,</span><span class="o">**</span><span class="n">pageargs</span><span class="p">):</span>
  358. <span class="n">context</span><span class="o">.</span><span class="n">caller_stack</span><span class="o">.</span><span class="n">push_frame</span><span class="p">()</span>
  359. <span class="k">try</span><span class="p">:</span>
  360. <span class="n">__M_locals</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">pageargs</span><span class="o">=</span><span class="n">pageargs</span><span class="p">)</span>
  361. <span class="c1"># SOURCE LINE 1</span>
  362. <span class="n">context</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s1">&#39;dr</span><span class="se">\xc3\xb4</span><span class="s1">le de petite voix m</span><span class="se">\xe2\x80\x99</span><span class="s1">a r</span><span class="se">\xc3\xa9</span><span class="s1">veill</span><span class="se">\xc3\xa9</span><span class="s1">.&#39;</span><span class="p">)</span>
  363. <span class="k">return</span> <span class="s1">&#39;&#39;</span>
  364. <span class="k">finally</span><span class="p">:</span>
  365. <span class="n">context</span><span class="o">.</span><span class="n">caller_stack</span><span class="o">.</span><span class="n">pop_frame</span><span class="p">()</span>
  366. </pre></div>
  367. </div>
  368. <p>Where above that the string literal used within <a class="reference internal" href="runtime.html#mako.runtime.Context.write" title="mako.runtime.Context.write"><code class="xref py py-meth docutils literal"><span class="pre">Context.write()</span></code></a>
  369. is a regular byte-string.</p>
  370. <p>When <code class="docutils literal"><span class="pre">disable_unicode=True</span></code> is turned on, the <code class="docutils literal"><span class="pre">default_filters</span></code>
  371. argument which normally defaults to <code class="docutils literal"><span class="pre">[&quot;unicode&quot;]</span></code> now defaults
  372. to <code class="docutils literal"><span class="pre">[&quot;str&quot;]</span></code> instead. Setting <code class="docutils literal"><span class="pre">default_filters</span></code> to the empty list
  373. <code class="docutils literal"><span class="pre">[]</span></code> can remove the overhead of the <code class="docutils literal"><span class="pre">str</span></code> call. Also, in this
  374. mode you <strong>cannot</strong> safely call <a class="reference internal" href="usage.html#mako.template.Template.render_unicode" title="mako.template.Template.render_unicode"><code class="xref py py-meth docutils literal"><span class="pre">render_unicode()</span></code></a> &#8211; you&#8217;ll get
  375. unicode/decode errors.</p>
  376. <p>The <code class="docutils literal"><span class="pre">h</span></code> filter (HTML escape) uses a less performant pure Python
  377. escape function in non-unicode mode. This because
  378. MarkupSafe only supports Python unicode objects for non-ASCII
  379. strings.</p>
  380. <div class="versionchanged">
  381. <p><span class="versionmodified">Changed in version 0.3.4: </span>In prior versions, it used <code class="docutils literal"><span class="pre">cgi.escape()</span></code>, which has been replaced
  382. with a function that also escapes single quotes.</p>
  383. </div>
  384. <div class="section" id="rules-for-using-disable-unicode-true">
  385. <h3>Rules for using <code class="docutils literal"><span class="pre">disable_unicode=True</span></code><a class="headerlink" href="#rules-for-using-disable-unicode-true" title="Permalink to this headline">¶</a></h3>
  386. <ul class="simple">
  387. <li>Don&#8217;t use this mode unless you really, really want to and you
  388. absolutely understand what you&#8217;re doing.</li>
  389. <li>Don&#8217;t use this option just because you don&#8217;t want to learn to
  390. use Unicode properly; we aren&#8217;t supporting user issues in this
  391. mode of operation. We will however offer generous help for the
  392. vast majority of users who stick to the Unicode program.</li>
  393. <li>Python 3 is unicode by default, and the flag is not available
  394. when running on Python 3.</li>
  395. </ul>
  396. </div>
  397. </div>
  398. </div>
  399. </div>
  400. </div>
  401. <div id="docs-bottom-navigation" class="docs-navigation-links">
  402. Previous:
  403. <a href="filtering.html" title="previous chapter">Filtering and Buffering</a>
  404. Next:
  405. <a href="caching.html" title="next chapter">Caching</a>
  406. <div id="docs-copyright">
  407. &copy; Copyright the Mako authors and contributors.
  408. Documentation generated using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.5.3
  409. with Mako templates.
  410. </div>
  411. </div>
  412. </div>
  413. <div class="clearfix">
  414. <hr/>
  415. <div class="copyright">Website content copyright &copy; by Michael Bayer.
  416. All rights reserved. Mako and its documentation are licensed
  417. under the MIT license. mike(&)zzzcomputing.com</div>
  418. </div>
  419. </div>
  420. </body>
  421. </html>