title: how to get the line number of an error from exec or execfile in Python
body: <p>Let's say I have the following multi-line string:</p>
  
  <pre><code>cmd = """
      a = 1 + 1
      b = [
         2 + 2,
         4 + 4,
      ]
      bork bork bork
  """
  </code></pre>
  
  <p>and I want to execute it in a particular scope:</p>
  
  <pre><code>scope = {}
  exec( cmd, scope )
  print scope[ 'b' ]
  </code></pre>
  
  <p>There's a <code>SyntaxError</code> at line 6 of the command, and I want to be able to report that to the user.  How do I get the line number?   I've tried this:</p>
  
  <pre><code>try:
      exec( cmd, scope )  # &lt;-- let's say this is on line 123 of the source file
  except Exception, err:
      a, b, c = sys.exc_info()
      line_number = c.tb_lineno  # &lt;-- this gets me 123,  not 6
      print "%s at line %d (%s)" % ( a, line_number, b.message )
  </code></pre>
  
  <p>...but I get the line number of the <code>exec</code> statement, not the line number within the multi-line command.</p>
  
  <p>Update: it turns out the handling of the type of exception that I arbitrarily chose for this example, the <code>SyntaxError</code>, is different from the handling of any other type.  To clarify, I'm looking a solution that copes with any kind of exception.</p>