title: How can I check whether a thread currently holds the GIL?
body: <p>I tried to find a function that tells me whether the current thread has the global interpreter lock or not.</p>
  
  <p>The Python/C-API documentation does not seem to contain such a function.</p>
  
  <p>My current solution is to just acquire the lock using <code>PyGILState_Ensure()</code> before releasing it using <code>PyEval_SaveThread</code> to not try releasing a lock that wasn't acquired by the current thread.</p>
  
  <p>(btw. what does "issues a fatal error" mean?)</p>
  
  <p>Background of this question: I have a multithreaded application which embeds Python. If a thread is closed without releasing the lock (which might occur due to crashes), other threads are not able to run any more. Thus, when cleaning up/closing the thread, I would like to check whether the lock is held by this thread and release it in this case.</p>
  
  <p>Thanks in advance for answers!</p>