diff --git a/readability/compat/__init__.py b/readability/compat/__init__.py new file mode 100644 index 0000000..ed4d350 --- /dev/null +++ b/readability/compat/__init__.py @@ -0,0 +1,6 @@ +""" +This module contains compatibility helpers for Python 2/3 interoperability. + +It mainly exists because their are certain incompatibilities in the Python +syntax that can only be solved by conditionally importing different functions. +""" diff --git a/readability/compat/three.py b/readability/compat/three.py new file mode 100644 index 0000000..2635157 --- /dev/null +++ b/readability/compat/three.py @@ -0,0 +1,6 @@ +def raise_with_traceback(exc_type, traceback, *args, **kwargs): + """ + Raise a new exception of type `exc_type` with an existing `traceback`. All + additional (keyword-)arguments are forwarded to `exc_type` + """ + raise exc_type(*args, **kwargs).with_traceback(traceback) diff --git a/readability/compat/two.py b/readability/compat/two.py new file mode 100644 index 0000000..642ecb7 --- /dev/null +++ b/readability/compat/two.py @@ -0,0 +1,6 @@ +def raise_with_traceback(exc_type, traceback, *args, **kwargs): + """ + Raise a new exception of type `exc_type` with an existing `traceback`. All + additional (keyword-)arguments are forwarded to `exc_type` + """ + raise exc_type(*args, **kwargs), None, traceback diff --git a/readability/readability.py b/readability/readability.py index 18ae4b2..820bc62 100755 --- a/readability/readability.py +++ b/readability/readability.py @@ -202,17 +202,10 @@ class Document: except Exception as e: log.exception('error getting summary: ') if sys.version_info[0] == 2: - # This is the only reason why we can't support Python 3.3: - # 3.3s parser fails to accept the old syntax (although this - # code never runs) which would require write this line as: - # write this line as - # Unparseable(str(e)) - # but then we lose the traceback information. 3.4 on the - # other hand accepts the old syntax and would only complain - # at runtime. - raise Unparseable(str(e)), None, sys.exc_info()[2] + from .compat.two import raise_with_traceback else: - raise Unparseable(str(e)).with_traceback(sys.exc_info()[2]) + from .compat.three import raise_with_traceback + raise_with_traceback(Unparseable, sys.exc_info()[2], str(e)) def get_article(self, candidates, best_candidate, html_partial=False): # Now that we have the top candidate, look through its siblings for diff --git a/tox.ini b/tox.ini index f7c6e93..50b4a74 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = py26, py27, py34 +envlist = py26, py27, py33, py34 [testenv] deps=pytest @@ -14,7 +14,7 @@ deps=pytest # requires a Compiler and the build dependencies), you can download # it from http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml and install it via # $PYTHONDIR\Scripts\pip.exe install *.whl -#sitepackages=True +sitepackages=True commands = pip install -r requirements.txt py.test