[Cython] Bug: comprehensions clear current exception

Stefan Behnel stefan_ml at behnel.de
Tue Jun 16 17:53:43 CEST 2015


Andrew Svetlov schrieb am 13.06.2015 um 08:30:
> I have an issue in aiohttp library:
> https://github.com/KeepSafe/aiohttp/issues/410
> 
> The source of problem is: when I execute the following code
> 
> body = ', '.join("'{}': {!r}".format(k, v) for k, v in self.items())
> 
> in except block Cython clears exception.
> 
> Changing from comprehension to regular iteration works well:
> 
> lst = []
> for k, v in self._items:
>     lst.append("'{}': {!r}".format(k, v))
> body = ', '.join(lst)
> 
> In pyre Python both versions don't clear current exception.

Thanks for the report, I can reproduce it with this code:

'''
def genexpr_in_except(x, result):
    """
    >>> result = []
    >>> try: genexpr_in_except([1, 2, 3], result)
    ... except ValueError: print("OK")
    ... else: print("NOT RAISED!")
    OK
    >>> result
    [2, 4, 6]

    >>> result = []
    >>> genexpr_in_except([], result)
    >>> result
    []
    """
    try:
        if x:
            raise ValueError()
    except ValueError:
        assert sys.exc_info()[0] is ValueError, 1
        result[:] = list(i*2 for i in x)
        assert sys.exc_info()[0] is ValueError, 2
        raise
'''

The "raise" at the end actually works, but the second "assert" makes it fail.

Stefan



More information about the cython-devel mailing list