Issue 3514: Fixed segfault dues to infinite loop in __getattr__.
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 0f5b06b..52fa156 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -3834,8 +3834,11 @@
     inst = self->stack->data[self->stack->length - 1];
 
     setstate = PyObject_GetAttrString(inst, "__setstate__");
-    if (setstate == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
-        PyErr_Clear();
+    if (setstate == NULL) {
+        if (PyErr_ExceptionMatches(PyExc_AttributeError))
+            PyErr_Clear();
+        else
+            return -1;
     }
     else {
         PyObject *result;