Make PyErr_Occurred return NULL if there is no current thread.  Previously it
would Py_FatalError, which called PyErr_Occurred, resulting in a semi-infinite
recursion.

Fixes issue 3605.
diff --git a/Lib/test/test_capi.py b/Lib/test/test_capi.py
index 29f7a71..7a6870d 100644
--- a/Lib/test/test_capi.py
+++ b/Lib/test/test_capi.py
@@ -2,9 +2,10 @@
 # these are all functions _testcapi exports whose name begins with 'test_'.
 
 from __future__ import with_statement
+import random
+import subprocess
 import sys
 import time
-import random
 import unittest
 from test import support
 try:
@@ -35,6 +36,19 @@
         self.assertEqual(testfunction.attribute, "test")
         self.assertRaises(AttributeError, setattr, inst.testfunction, "attribute", "test")
 
+    def test_no_FatalError_infinite_loop(self):
+        p = subprocess.Popen([sys.executable, "-c",
+                              'import _testcapi;'
+                              '_testcapi.crash_no_current_thread()'],
+                             stdout=subprocess.PIPE,
+                             stderr=subprocess.PIPE)
+        (out, err) = p.communicate()
+        self.assertEqual(out, b'')
+        # This used to cause an infinite loop.
+        self.assertEqual(err,
+                         b'Fatal Python error:'
+                         b' PyThreadState_Get: no current thread\n')
+
 
 @unittest.skipUnless(threading, 'Threading required for this test.')
 class TestPendingCalls(unittest.TestCase):