Why does this get a syntax error when exec() is called?
<p>I have a database of questions, and a few of those questions have need of special validation.  So, I added a field to my model called py_validation and when those questions are answered, I validate them by running the code that is stored in py_validation.  When I paste the code into the interpereter, it works correctly, but when I pass it into exec, it fails with the error:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;string&gt;", line 2, in &lt;fragment&gt;
invalid syntax: &lt;string&gt;, line 2, pos 31
</code></pre>

<p>The code looks like:</p>

<pre><code># This code relies on the field type being a char field
if len(value.split('\n')) &lt; 5:
    raise ValidationError(_("You must specify at least 5 widgets, one per line"))
super(CharField, self).validate(value)
</code></pre>

<p>I'm calling it using:</p>

<pre><code>def do_py_validate(field, value):
    exec field.py_validation
</code></pre>

<p>so that variable value is in local scope while executing.  Doesn't quite make sense that the code that works just fine, bails when executed like this.</p>
