title: PyErr_SetString does not raise the exception immediately(Swig)?
body: <p>I am wrapping a C lib to python module using SWIG. But exception does not seem to be raised at the right place, I have a simple demo for this,</p>
  
  <p><strong>except_test.i</strong></p>
  
  <pre><code>%module except_test
  
  
  %{
  #include "except_test.h"
  #include &lt;stdio.h&gt;
  %}
  
  %{
  static int flagged_exception = 0;
  
  void throw_except()
  {
      flagged_exception = 1;
      printf("flag set \n");
  }
  %}
  
  %exception {
      $action
      printf("exception block\n");
      if (flagged_exception) {
          printf("before setstring\n");
          PyErr_SetString(PyExc_RuntimeError, "test except");
          printf("after setstring\n");
          flagged_exception = 0;
      }
  }
  %include "except_test.h"
  </code></pre>
  
  <p><strong>except_test.c</strong></p>
  
  <pre><code>#include "except_test.h"
  
  
  int except_test(int a) {
  
      if (a &lt; 0) {
          throw_except();
          return 0;
      } else{
          return -1;
      }
  }
  </code></pre>
  
  <p><strong>run_except.py</strong></p>
  
  <pre><code>from except_test import *
  import time
  
  def test():
      b = except_test(-1)
      print 'b=', b
  
  try:
      test()
  except:
      print "caught exception"
  
  for i in range(10):
      print i
      time.sleep(1)
  </code></pre>
  
  <p>Now if I run run_except.py</p>
  
  <pre><code>$python run_except.py 
  flag set 
  exception block
  before setstring
  after setstring
  b= 0
  0
  Traceback (most recent call last):
    File "run_except.py", line 15, in &lt;module&gt;
      time.sleep(1)
  RuntimeError: test except
  </code></pre>
  
  <p>as the output shows, the try/catch block didn't catch the exception.
  Why is this? and How to avoid this?</p>
  
  <p>Thanks,</p>