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.
pull/64/head
Martin Thurau 9 years ago
parent 3ac56329e2
commit ce7ca26835

@ -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.
"""

@ -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)

@ -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

@ -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

@ -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

Loading…
Cancel
Save