Comments (10)
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.
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.
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.
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.
use:
raise t(*v.args), None, tb
until fix is merged:
#50
from pycodestyle.
Should be fixed in 0.7.
Thank you for the patch.
from pycodestyle.
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.
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.
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.
@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)
- `exclude` is applied to `.`
- Inconsistent handling of indented comments (E116) HOT 3
- Use setuptools.build_meta HOT 1
- How to get in touch regarding a security concern HOT 2
- Latest version doesn't seem to handle self-documenting f-string syntax added back in 3.8 HOT 1
- pyproject.toml is not supported HOT 1
- False-positive E704 with single-line stubs (black 2024 style) HOT 1
- reporting incorrect information in format strings HOT 1
- Doc: StyleChecker(quiet) not documented HOT 1
- docs directory not shipped in pypi sdist HOT 1
- suppress <name> may be undefined, or defined from star imports HOT 3
- flake8 E704 Errors Triggered by Inline Ellipsis Formatting in Newer black Versions #1926 HOT 1
- docs: meta.pycqa.org code-of-conduct hyperlinks are not redirecting as expected.
- E251: keyword `=` vs. expressions with spaces HOT 1
- Spurious E231 for nested format substitutions
- E122: No valid indent for long function call nested in f-string? HOT 2
- f-string varialbes shows wrong E251 and E202 since python 3.12 upgrade HOT 2
- Not all errors get ignored when comment on same line HOT 1
- E226: unable to detect missing whitespace in f-string HOT 1
- E202 complains about meaningful space before parenthesis in f-strings debug expressions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pycodestyle.