8000 Adds compatibility `raise_with_traceback` method to support different… · Harry0201/python-readability@ce7ca26 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce7ca26

Browse files
committed
Adds compatibility raise_with_traceback method to support different raise syntax
Unfortunately the Python 2 `raise` syntax is not supported in Python 3.3 and not all 3.4.x versions so we deal with that by using conditional imports and a compatibility layer.
1 parent 3ac5632 commit ce7ca26

File tree

5 files changed

+23
-12
lines changed

5 files changed

+23
-12
lines changed

readability/compat/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"""
2+
This module contains compatibility helpers for Python 2/3 interoperability.
3+
4+
It mainly exists because their are certain incompatibilities in the Python
5+
syntax that can only be solved by conditionally importing different functions.
6+
"""

readability/compat/three.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def raise_with_traceback(exc_type, traceback, *args, **kwargs):
2+
"""
3+
Raise a new exception of type `exc_type` with an existing `traceback`. All
4+
additional (keyword-)arguments are forwarded to `exc_type`
5+
"""
6+
raise exc_type(*args, **kwargs).with_traceback(traceback)

readability/compat/two.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def raise_with_traceback(exc_type, traceback, *args, **kwargs):
2+
"""
3+
Raise a new exception of type `exc_type` with an existing `traceback`. All
4+
additional (keyword-)arguments are forwarded to `exc_type`
5+
"""
6+
raise exc_type(*args, **kwargs), None, traceback

readability/readability.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,10 @@ def summary(self, html_partial=False):
202202
except Exception as e:
203203
log.exception('error getting summary: ')
204204
if sys.version_info[0] == 2:
205-
# This is the only reason why we can't support Python 3.3:
206-
# 3.3s parser fails to accept the old syntax (although this
207-
# code never runs) which would require write this line as:
208-
# write this line as
209-
# Unparseable(str(e))
210-
# but then we lose the traceback information. 3.4 on the
211-
# other hand accepts the old syntax and would only complain
212-
# at runtime.
213-
raise Unparseable(str(e)), None, sys.exc_info()[2]
205+
from .compat.two import raise_with_traceback
214206
else:
215-
raise Unparseable(str(e)).with_traceback(sys.exc_info()[2])
207+
from .compat.three import raise_with_traceback
208+
raise_with_traceback(Unparseable, sys.exc_info()[2], str(e))
216209

217210
def get_article(self, candidates, best_candidate, html_partial=False):
218211
# Now that we have the top candidate, look through its siblings for

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# and then run "tox" from this directory.
55

66
[tox]
7-
envlist = py26, py27, py34
7+
envlist = py26, py27, py33, py34
88

99
[testenv]
1010
deps=pytest
@@ -14,7 +14,7 @@ deps=pytest
1414
# requires a Compiler and the build dependencies), you can download
1515
# it from http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml and install it via
1616
# $PYTHONDIR\Scripts\pip.exe install *.whl
17-
#sitepackages=True
17+
sitepackages=True
1818
commands =
1919
pip install -r requirements.txt
2020
py.test

0 commit comments

Comments
 (0)
0