Coder Social home page Coder Social logo

Comments (10)

paulegan avatar paulegan commented on August 28, 2024

I came across the same issue recently.

I think the preferred form for your example would be:

try:
    foo()
except Exception, error:
    try:
        bar()
    except:
        pass
    raise error

However if you want to raise a different exception, while preserving the original traceback, then I think you gave to break PEP8 and use the "old" form:

raise MyError, sys.exc_value, sys.exc_traceback

from pycodestyle.

jerdfelt avatar jerdfelt commented on August 28, 2024

Unfortunately the proposed "preferred form" changes the behavior. While it raises the original exception, it changes the traceback, which pretty much makes the traceback useless.

The three-arg form of raise looks to be the only way to raise the original exception and preserve the traceback.

from pycodestyle.

rconradharris avatar rconradharris commented on August 28, 2024

I've been searching around and can't find any confirmation that the 3-arg raise is deprecated for Python 2.x. Anybody able to turn up an official statement regarding this?

(Python 3 can preserve the traceback using the with_exception method; given that this is not an option in Python 2.x, might not make sense to deprecate the only way of re-raising an exception with the original traceback.)

from pycodestyle.

fdr avatar fdr commented on August 28, 2024

I also have code that attempts to preserve the original traceback and thusly annoys pep8, which I feel it shouldn't since nobody has produced any other option that has the same semantics.

from pycodestyle.

clayg avatar clayg commented on August 28, 2024

use:
raise t(*v.args), None, tb

until fix is merged:
#50

from pycodestyle.

florentx avatar florentx commented on August 28, 2024

Should be fixed in 0.7.
Thank you for the patch.

from pycodestyle.

ajdavis avatar ajdavis commented on August 28, 2024

I think we have to reopen this. pep8 version 1.4.6 on this file:

import sys


def f():
    try:
        raise Exception('foo')
    except:
        e, v, t = sys.exc_info()
        raise Exception, Exception("f"), t

causes:

foo.py:9:24: W602 deprecated form of raising exception

from pycodestyle.

xsleonard avatar xsleonard commented on August 28, 2024

This is getting W602, should it not be?

pep 1.5.7

import sys


def f():
    try:
        raise ValueError
    except ValueError as e:
        raise e, None, sys.exc_info()[2]

from pycodestyle.

florentx avatar florentx commented on August 28, 2024

well, the check for W602 is regex-based, and it's not obvious to avoid such false positive.

btw, you could probably rewrite the expression with a more readable syntax

import sys


def f():
    try:
        raise ValueError
    except ValueError:
        (exc, exc_type, tb) = sys.exc_info()
        raise exc, None, tb

from pycodestyle.

mforbes avatar mforbes commented on August 28, 2024

@florentx You might like to check the order of your arguments in your suggestion: I think it best be (exc_type, exc, tb) = sys.exc_info().

from pycodestyle.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.