Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
6ca2de34
Commit
6ca2de34
authored
Mar 31, 2011
by
Haoyu Bai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
explicit execption chaining
parent
f7d7598f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
102 additions
and
8 deletions
+102
-8
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+45
-7
Cython/Compiler/Parsing.py
Cython/Compiler/Parsing.py
+6
-1
tests/run/funcexceptraisefrom.pyx
tests/run/funcexceptraisefrom.pyx
+51
-0
No files found.
Cython/Compiler/Nodes.py
View file @
6ca2de34
...
...
@@ -3942,8 +3942,9 @@ class RaiseStatNode(StatNode):
# exc_type ExprNode or None
# exc_value ExprNode or None
# exc_tb ExprNode or None
# cause ExprNode or None
child_attrs
=
[
"exc_type"
,
"exc_value"
,
"exc_tb"
]
child_attrs
=
[
"exc_type"
,
"exc_value"
,
"exc_tb"
,
"cause"
]
def
analyse_expressions
(
self
,
env
):
if
self
.
exc_type
:
...
...
@@ -3955,6 +3956,9 @@ class RaiseStatNode(StatNode):
if
self
.
exc_tb
:
self
.
exc_tb
.
analyse_types
(
env
)
self
.
exc_tb
=
self
.
exc_tb
.
coerce_to_pyobject
(
env
)
if
self
.
cause
:
self
.
cause
.
analyse_types
(
env
)
self
.
cause
=
self
.
cause
.
coerce_to_pyobject
(
env
)
# special cases for builtin exceptions
self
.
builtin_exc_name
=
None
if
self
.
exc_type
and
not
self
.
exc_value
and
not
self
.
exc_tb
:
...
...
@@ -3990,13 +3994,19 @@ class RaiseStatNode(StatNode):
tb_code
=
self
.
exc_tb
.
py_result
()
else
:
tb_code
=
"0"
if
self
.
cause
:
self
.
cause
.
generate_evaluation_code
(
code
)
cause_code
=
self
.
cause
.
py_result
()
else
:
cause_code
=
"0"
code
.
globalstate
.
use_utility_code
(
raise_utility_code
)
code
.
putln
(
"__Pyx_Raise(%s, %s, %s);"
%
(
"__Pyx_Raise(%s, %s, %s
, %s
);"
%
(
type_code
,
value_code
,
tb_code
))
for
obj
in
(
self
.
exc_type
,
self
.
exc_value
,
self
.
exc_tb
):
tb_code
,
cause_code
))
for
obj
in
(
self
.
exc_type
,
self
.
exc_value
,
self
.
exc_tb
,
self
.
cause
):
if
obj
:
obj
.
generate_disposal_code
(
code
)
obj
.
free_temps
(
code
)
...
...
@@ -4010,6 +4020,8 @@ class RaiseStatNode(StatNode):
self
.
exc_value
.
generate_function_definitions
(
env
,
code
)
if
self
.
exc_tb
is
not
None
:
self
.
exc_tb
.
generate_function_definitions
(
env
,
code
)
if
self
.
cause
is
not
None
:
self
.
cause
.
generate_function_definitions
(
env
,
code
)
def
annotate
(
self
,
code
):
if
self
.
exc_type
:
...
...
@@ -4018,6 +4030,8 @@ class RaiseStatNode(StatNode):
self
.
exc_value
.
annotate
(
code
)
if
self
.
exc_tb
:
self
.
exc_tb
.
annotate
(
code
)
if
self
.
cause
:
self
.
cause
.
annotate
(
code
)
class
ReraiseStatNode
(
StatNode
):
...
...
@@ -5652,11 +5666,12 @@ static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyOb
raise_utility_code
=
UtilityCode
(
proto
=
"""
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb); /*proto*/
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb
, PyObject *cause
); /*proto*/
"""
,
impl
=
"""
#if PY_MAJOR_VERSION < 3
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause) {
/* cause is unused */
Py_XINCREF(type);
Py_XINCREF(value);
Py_XINCREF(tb);
...
...
@@ -5723,7 +5738,7 @@ raise_error:
#else /* Python 3+ */
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb
, PyObject *cause
) {
if (tb == Py_None) {
tb = 0;
} else if (tb && !PyTraceBack_Check(tb)) {
...
...
@@ -5748,6 +5763,29 @@ static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb) {
goto bad;
}
if (cause) {
PyObject *fixed_cause;
if (PyExceptionClass_Check(cause)) {
fixed_cause = PyObject_CallObject(cause, NULL);
if (fixed_cause == NULL)
goto bad;
}
else if (PyExceptionInstance_Check(cause)) {
fixed_cause = cause;
Py_INCREF(fixed_cause);
}
else {
PyErr_SetString(PyExc_TypeError,
"exception causes must derive from "
"BaseException");
goto bad;
}
if (!value) {
value = PyObject_CallObject(type, NULL);
}
PyException_SetCause(value, fixed_cause);
}
PyErr_SetObject(type, value);
if (tb) {
...
...
Cython/Compiler/Parsing.py
View file @
6ca2de34
...
...
@@ -1166,6 +1166,7 @@ def p_raise_statement(s):
exc_type
=
None
exc_value
=
None
exc_tb
=
None
cause
=
None
if
s
.
sy
not
in
statement_terminators
:
exc_type
=
p_test
(
s
)
if
s
.
sy
==
','
:
...
...
@@ -1174,11 +1175,15 @@ def p_raise_statement(s):
if
s
.
sy
==
','
:
s
.
next
()
exc_tb
=
p_test
(
s
)
elif
s
.
sy
==
'from'
:
s
.
next
()
cause
=
p_test
(
s
)
if
exc_type
or
exc_value
or
exc_tb
:
return
Nodes
.
RaiseStatNode
(
pos
,
exc_type
=
exc_type
,
exc_value
=
exc_value
,
exc_tb
=
exc_tb
)
exc_tb
=
exc_tb
,
cause
=
cause
)
else
:
return
Nodes
.
ReraiseStatNode
(
pos
)
...
...
tests/run/funcexceptraisefrom.pyx
0 → 100644
View file @
6ca2de34
__doc__
=
u"""
>>> def bar():
... try:
... foo()
... except ValueError:
... if IS_PY3:
... print(isinstance(sys.exc_info()[1].__cause__, TypeError))
... else:
... print(True)
>>> bar()
True
>>> print(sys.exc_info())
(None, None, None)
>>> def bar2():
... try:
... foo2()
... except ValueError:
... if IS_PY3:
... cause = sys.exc_info()[1].__cause__
... print(isinstance(cause, TypeError))
... print(cause.args==('value',))
... pass
... else:
... print(True)
... print(True)
>>> bar2()
True
True
"""
import
sys
IS_PY3
=
sys
.
version_info
[
0
]
>=
3
if
not
IS_PY3
:
sys
.
exc_clear
()
def
foo
():
try
:
raise
TypeError
except
TypeError
:
raise
ValueError
from
TypeError
def
foo2
():
try
:
raise
TypeError
except
TypeError
:
raise
ValueError
()
from
TypeError
(
'value'
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment