merge -r59315:59316 from py3k: Fix issue #1553: An errornous __length_hint__ can make list() raise a SystemError
diff --git a/Misc/NEWS b/Misc/NEWS
index 735b073..24a43f0 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@
 Core and builtins
 -----------------
 
+- Issue #1553: An errornous __length_hint__ can make list() raise a 
+  SystemError.
+
 - Issue #1521: On 64bit platforms, using PyArgs_ParseTuple with the t# of w#
   format code incorrectly truncated the length to an int, even when
   PY_SSIZE_T_CLEAN is set.  The str.decode method used to return incorrect
diff --git a/Objects/abstract.c b/Objects/abstract.c
index f7a3bfe..fb123db 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1507,8 +1507,9 @@
 	/* Guess result size and allocate space. */
 	n = _PyObject_LengthHint(v);
 	if (n < 0) {
-		if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
-		    !PyErr_ExceptionMatches(PyExc_AttributeError)) {
+		if (PyErr_Occurred()
+		    && !PyErr_ExceptionMatches(PyExc_TypeError)
+		    && !PyErr_ExceptionMatches(PyExc_AttributeError)) {
 			Py_DECREF(it);
 			return NULL;
 		}
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 75ba6d0..c0d0e09 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -784,8 +784,9 @@
 	/* Guess a result list size. */
 	n = _PyObject_LengthHint(b);
 	if (n < 0) {
-		if (!PyErr_ExceptionMatches(PyExc_TypeError)  &&
-		    !PyErr_ExceptionMatches(PyExc_AttributeError)) {
+		if (PyErr_Occurred()
+		    && !PyErr_ExceptionMatches(PyExc_TypeError)
+		    && !PyErr_ExceptionMatches(PyExc_AttributeError)) {
 			Py_DECREF(it);
 			return NULL;
 		}