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): ...@@ -302,28 +302,24 @@ class Template(object):
raise SyntaxError( raise SyntaxError(
'invalid syntax in expression: %s' % code) 'invalid syntax in expression: %s' % code)
return value return value
except: except Exception as e:
exc_info = sys.exc_info()
e = exc_info[1]
if getattr(e, 'args', None): if getattr(e, 'args', None):
arg0 = e.args[0] arg0 = e.args[0]
else: else:
arg0 = coerce_text(e) arg0 = coerce_text(e)
e.args = (self._add_line_info(arg0, pos),) e.args = (self._add_line_info(arg0, pos),)
raise exc_info[0], e, exc_info[2] raise
def _exec(self, code, ns, pos): def _exec(self, code, ns, pos):
__traceback_hide__ = True __traceback_hide__ = True
try: try:
exec(code, self.default_namespace, ns) exec(code, self.default_namespace, ns)
except: except Exception as e:
exc_info = sys.exc_info()
e = exc_info[1]
if e.args: if e.args:
e.args = (self._add_line_info(e.args[0], pos),) e.args = (self._add_line_info(e.args[0], pos),)
else: else:
e.args = (self._add_line_info(None, pos),) e.args = (self._add_line_info(None, pos),)
raise exc_info[0], e, exc_info[2] raise
def _repr(self, value, pos): def _repr(self, value, pos):
__traceback_hide__ = True __traceback_hide__ = True
...@@ -341,11 +337,9 @@ class Template(object): ...@@ -341,11 +337,9 @@ class Template(object):
if (is_unicode(value) if (is_unicode(value)
and self.default_encoding): and self.default_encoding):
value = value.encode(self.default_encoding) value = value.encode(self.default_encoding)
except: except Exception as e:
exc_info = sys.exc_info()
e = exc_info[1]
e.args = (self._add_line_info(e.args[0], pos),) e.args = (self._add_line_info(e.args[0], pos),)
raise exc_info[0], e, exc_info[2] raise
else: else:
if self._unicode and isinstance(value, bytes): if self._unicode and isinstance(value, bytes):
if not self.default_encoding: if not self.default_encoding:
......
...@@ -217,10 +217,14 @@ def load_module(name, pyxfilename, pyxbuild_dir=None, is_package=False, ...@@ -217,10 +217,14 @@ def load_module(name, pyxfilename, pyxbuild_dir=None, is_package=False,
mod = imp.load_source(name, pyxfilename) mod = imp.load_source(name, pyxfilename)
assert mod.__file__ in (pyxfilename, pyxfilename+'c', pyxfilename+'o'), (mod.__file__, pyxfilename) assert mod.__file__ in (pyxfilename, pyxfilename+'c', pyxfilename+'o'), (mod.__file__, pyxfilename)
else: else:
tb = sys.exc_info()[2]
import traceback import traceback
raise ImportError("Building module %s failed: %s" % exc = ImportError("Building module %s failed: %s" % (
(name, name, traceback.format_exception_only(*sys.exc_info()[:2])))
traceback.format_exception_only(*sys.exc_info()[:2]))), None, 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 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