Using bitwise or in mako expression
<p>We're converting a bunch of hand-rolled code-generation stuff over to using mako templates, and some of the code we're converting from used the bitwise or (<code>|</code>) operator to compute some of it's values.</p>

<p>E.g., the original was something like:</p>

<pre><code>for my_foo in foo_constants():
    output += "#define %s_THING %d\n" % (my_foo.name, my_foo.id | 0x8000)
</code></pre>

<p>Now in mako, our first attempt was:</p>

<pre><code>% for my_foo in foo_constants():
#define ${my_foo.name}_THING ${my_foo.id | 0x10000}
% endfor
</code></pre>

<p>But this threw the error <code>TypeError: 'int' object is not callable</code>, because mako compiled that line to:</p>

<pre><code>__M_writer(65536(unicode(my_foo.id )))
</code></pre>

<p>Since mako uses the <code>|</code> character to mean "pipe the left to the function on the right". Surrounding the expression with an extra layer of parentheses, like <code>${(my_foo.id | 0x10000)}</code>, didn't help. It produced</p>

<pre><code>mako.exceptions.SyntaxException: (SyntaxError) invalid syntax (&lt;unknown&gt;, line 1) (u'0x10000),') in file '/tmp/test_template.mako' at line: 2 char: 29
</code></pre>

<p>At the moment, we're going to just add a <code>bit_or</code> function to our template's context and call that, but as the mako documentation doesn't seem to mention this issue at all I was wondering if there's an obvious solution we're missing.</p>

<hr>

<p><strong>Update</strong>: As of Mako version 1.0.4, the solution is an extra layer of parentheses:</p>

<pre><code>% for my_foo in foo_constants():
#define ${my_foo.name}_THING ${(my_foo.id | 0x10000)}
% endfor
</code></pre>

<p>(That won't work in version 1.0.3, which was the current version when this question was posted)</p>