]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #4806: Avoid masking TypeError when *-unpacking a generator
authorMartin Panter <vadmium+py@gmail.com>
Sun, 31 Jan 2016 06:30:56 +0000 (06:30 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Sun, 31 Jan 2016 06:30:56 +0000 (06:30 +0000)
Based on patch by Hagen Fürstenau.

Lib/test/test_extcall.py
Misc/NEWS
Python/ceval.c

index 80e09a0ec2f6ab5ceb076eac5084ff8327b9504e..babcce9f9b7e12b7d5cfa22cae89b779f57da9fd 100644 (file)
@@ -93,7 +93,7 @@ Verify clearing of SF bug #733667
     >>> g(*Nothing())
     Traceback (most recent call last):
       ...
-    TypeError: g() argument after * must be a sequence, not instance
+    TypeError: g() argument after * must be an iterable, not instance
 
     >>> class Nothing:
     ...     def __len__(self): return 5
@@ -102,7 +102,7 @@ Verify clearing of SF bug #733667
     >>> g(*Nothing())
     Traceback (most recent call last):
       ...
-    TypeError: g() argument after * must be a sequence, not instance
+    TypeError: g() argument after * must be an iterable, not instance
 
     >>> class Nothing():
     ...     def __len__(self): return 5
@@ -128,6 +128,17 @@ Verify clearing of SF bug #733667
     >>> g(*Nothing())
     0 (1, 2, 3) {}
 
+Check for issue #4806: Does a TypeError in a generator get propagated with the
+right error message?
+
+    >>> def broken(): raise TypeError("myerror")
+    ...
+
+    >>> g(*(broken() for i in range(1)))
+    Traceback (most recent call last):
+      ...
+    TypeError: myerror
+
 Make sure that the function doesn't stomp the dictionary
 
     >>> d = {'a': 1, 'b': 2, 'c': 3}
@@ -167,17 +178,17 @@ What about willful misconduct?
     >>> h(*h)
     Traceback (most recent call last):
       ...
-    TypeError: h() argument after * must be a sequence, not function
+    TypeError: h() argument after * must be an iterable, not function
 
     >>> dir(*h)
     Traceback (most recent call last):
       ...
-    TypeError: dir() argument after * must be a sequence, not function
+    TypeError: dir() argument after * must be an iterable, not function
 
     >>> None(*h)
     Traceback (most recent call last):
       ...
-    TypeError: NoneType object argument after * must be a sequence, \
+    TypeError: NoneType object argument after * must be an iterable, \
 not function
 
     >>> h(**h)
index 6a80a280a0458d3f41de41ebb8cd6ff5b208e7bb..f02087f9324f3b36500347226f65af3decc24d3c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,10 @@ What's New in Python 2.7.12?
 Core and Builtins
 -----------------
 
+- Issue #4806: Avoid masking the original TypeError exception when using star
+  (*) unpacking and the exception was raised from a generator.  Based on
+  patch by Hagen Fürstenau.
+
 - Issue #26659: Make the builtin slice type support cycle collection.
 
 - Issue #26718: super.__init__ no longer leaks memory if called multiple times.
index 6e5e27280e9cf1e50fca104511d50256b4e6e73c..0b747d8281bb457316dc73bae475ad93a1a87f1e 100644 (file)
@@ -4615,10 +4615,12 @@ ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
             PyObject *t = NULL;
             t = PySequence_Tuple(stararg);
             if (t == NULL) {
-                if (PyErr_ExceptionMatches(PyExc_TypeError)) {
+                if (PyErr_ExceptionMatches(PyExc_TypeError) &&
+                        /* Don't mask TypeError raised from a generator */
+                        !PyGen_Check(stararg)) {
                     PyErr_Format(PyExc_TypeError,
                                  "%.200s%.200s argument after * "
-                                 "must be a sequence, not %200s",
+                                 "must be an iterable, not %200s",
                                  PyEval_GetFuncName(func),
                                  PyEval_GetFuncDesc(func),
                                  stararg->ob_type->tp_name);