title: Syntax Error exec-ing python code from database
body: <p>I'm loading some python code from a database (it's doing dynamic mapping of values that I can change at runtime without a code redeploy).</p>
  
  <p>In my code, I'm doing this to execute the database code:</p>
  
  <pre><code>if lMapping:
    print lMapping
    exec lMapping
    lValue = mapping(lValue, lCsvRow)
  </code></pre>
  
  <p>and here's the value of lMapping:</p>
  
  <pre><code>def mapping(pValue, pCsvRow):
    lInterimStatus = pCsvRow[5]
    lOutComeStatus = pCsvRow[6]
  
    if len(lInterimStatus) == 0:
      lStatus = lOutComeStatus
    else:
      lStatus = lInterimStatus
  
    lStatus = lStatus.lower()
  
    PRIMARY_STATUS_MAPPINGS = {
      'completed with lender' : '4828',
      'not taken up' : '4827',
      'declined across all lenders' : '4726',
      'declined broker duplicate' : '4726',
      'pending' : '4725',
      'pending (in progress with broker)' : '4725',
      'pending (in progress with lender)' : '4725',
      'lender accept in principle' : '4827',
      'lender decline duplicate' : '4743',
      'lender decline post code not supported' : '4743',
      'lender decline score fail' : '4743',
      'lender decline policy fail' : '4743',
      'lender decline no client contact' : '4743',
      'lender decline general' : '4743',
      'lender decline bad data' : '4743',
    }
  
    return PRIMARY_STATUS_MAPPINGS[lStatus]
  </code></pre>
  
  <p>Whenever I do this I'm getting a syntax error on the exec line, I can't work out why:</p>
  
  <p><code>(&lt;type 'exceptions.SyntaxError'&gt;:invalid syntax (&lt;string&gt;, line 1)</code></p>
  
  <h3>UPDATE</h3>
  
  <p>It works if I write the code from the database to a file first:</p>
  
  <pre><code>print lMapping
  lFile = open('/mapping.py','w')
  lFile.write(lMapping)
  lFile.close()
  lReadFile = open('/mapping.py')
  exec lReadFile
  lValue = mapping(lValue, lCsvRow)
  </code></pre>