Commit 9c7d4b13 authored by Stefan Behnel's avatar Stefan Behnel

adapt Py2-only 'raise' statements to Py2/Py3

parent fd4ba3c5
......@@ -302,28 +302,24 @@ class Template(object):
raise SyntaxError(
'invalid syntax in expression: %s' % code)
return value
except:
exc_info = sys.exc_info()
e = exc_info[1]
except Exception as e:
if getattr(e, 'args', None):
arg0 = e.args[0]
else:
arg0 = coerce_text(e)
e.args = (self._add_line_info(arg0, pos),)
raise exc_info[0], e, exc_info[2]
raise
def _exec(self, code, ns, pos):
__traceback_hide__ = True
try:
exec(code, self.default_namespace, ns)
except:
exc_info = sys.exc_info()
e = exc_info[1]
except Exception as e:
if e.args:
e.args = (self._add_line_info(e.args[0], pos),)
else:
e.args = (self._add_line_info(None, pos),)
raise exc_info[0], e, exc_info[2]
raise
def _repr(self, value, pos):
__traceback_hide__ = True
......@@ -341,11 +337,9 @@ class Template(object):
if (is_unicode(value)
and self.default_encoding):
value = value.encode(self.default_encoding)
except:
exc_info = sys.exc_info()
e = exc_info[1]
except Exception as e:
e.args = (self._add_line_info(e.args[0], pos),)
raise exc_info[0], e, exc_info[2]
raise
else:
if self._unicode and isinstance(value, bytes):
if not self.default_encoding:
......
......@@ -217,10 +217,14 @@ def load_module(name, pyxfilename, pyxbuild_dir=None, is_package=False,
mod = imp.load_source(name, pyxfilename)
assert mod.__file__ in (pyxfilename, pyxfilename+'c', pyxfilename+'o'), (mod.__file__, pyxfilename)
else:
tb = sys.exc_info()[2]
import traceback
raise ImportError("Building module %s failed: %s" %
(name,
traceback.format_exception_only(*sys.exc_info()[:2]))), None, sys.exc_info()[2]
exc = ImportError("Building module %s failed: %s" % (
name, traceback.format_exception_only(*sys.exc_info()[:2])))
if sys.version_info[0] >= 3:
raise exc.with_traceback(tb)
else:
exec("raise exc, None, tb", {'exc': exc, 'tb': tb})
return mod
......
PYTHON -c "import pyximport_test; pyximport_test.test()"
######## pyximport_test.py ########
import os.path
from contextlib import contextmanager
import pyximport
pyximport.install(build_dir=os.path.join(os.path.dirname(__file__), "BUILD"))
@contextmanager
def fails(exc=ImportError):
try:
yield
except exc:
pass
else:
raise RuntimeError("NOT RAISED!")
def test():
with fails():
import compiler_error
with fails():
import syntax_error
with fails():
import runtime_error
######## compiler_error.pyx ########
from __future__ import braces
######## syntax_error.pyx ########
def test {
BRACES!
}
######## runtime_error.pyx ########
raise ValueError()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment