Skip to content

Commit 804bc83

Browse files
author
guido.van.rossum
committed
- Issue #2371: Add a Py3k warning when catching an exception that
doesn't derive from BaseException. git-svn-id: http://svn.python.org/projects/python/trunk@61475 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 452a50c commit 804bc83

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ Magnus Kessler
360360
Lawrence Kesteloot
361361
Vivek Khera
362362
Mads Kiilerich
363+
Taek Joo Kim
363364
Steve Kirsch
364365
Ron Klatchko
365366
Bastian Kleineidam

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.6 alpha 2?
1212
Core and builtins
1313
-----------------
1414

15+
- Issue #2371: Add a Py3k warning when catching an exception that
16+
doesn't derive from BaseException.
17+
1518
- Issue #2321: use pymalloc for unicode object string data to reduce
1619
memory usage in some circumstances.
1720

Python/ceval.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4042,6 +4042,13 @@ assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
40424042
}
40434043
}
40444044

4045+
#define Py3kExceptionClass_Check(x) \
4046+
(PyType_Check((x)) && \
4047+
PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4048+
4049+
#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
4050+
"BaseException is not allowed in 3.x."
4051+
40454052
static PyObject *
40464053
cmp_outcome(int op, register PyObject *v, register PyObject *w)
40474054
{
@@ -4079,6 +4086,16 @@ cmp_outcome(int op, register PyObject *v, register PyObject *w)
40794086
if (ret_val == -1)
40804087
return NULL;
40814088
}
4089+
if (Py_Py3kWarningFlag &&
4090+
!Py3kExceptionClass_Check(exc))
4091+
{
4092+
int ret_val;
4093+
ret_val = PyErr_WarnEx(
4094+
PyExc_DeprecationWarning,
4095+
CANNOT_CATCH_MSG, 1);
4096+
if (ret_val == -1)
4097+
return NULL;
4098+
}
40824099
}
40834100
}
40844101
else {
@@ -4091,6 +4108,16 @@ cmp_outcome(int op, register PyObject *v, register PyObject *w)
40914108
if (ret_val == -1)
40924109
return NULL;
40934110
}
4111+
if (Py_Py3kWarningFlag &&
4112+
!Py3kExceptionClass_Check(w))
4113+
{
4114+
int ret_val;
4115+
ret_val = PyErr_WarnEx(
4116+
PyExc_DeprecationWarning,
4117+
CANNOT_CATCH_MSG, 1);
4118+
if (ret_val == -1)
4119+
return NULL;
4120+
}
40944121
}
40954122
res = PyErr_GivenExceptionMatches(v, w);
40964123
break;

0 commit comments

Comments
 (0)