Skip to content

Commit 7e96d87

Browse files
exception warnings
1 parent 8deb7c5 commit 7e96d87

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Lib/test/test_py3kwarn.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ def test_sys_exc_clear(self):
189189
with check_py3k_warnings() as w:
190190
self.assertWarning(sys.exc_clear(), w, expected)
191191

192+
def test_sys_exc_info(self):
193+
expected = 'sys.exc_info() not supported in 3.x; use except clauses'
194+
with check_py3k_warnings() as w:
195+
self.assertWarning(sys.exc_info(), w, expected)
196+
192197
def test_methods_members(self):
193198
expected = '__members__ and __methods__ not supported in 3.x'
194199
class C:

Python/ast.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,11 @@ ast_for_flow_stmt(struct compiling *c, const node *n)
24092409
n->n_col_offset, c->c_arena);
24102410
}
24112411
else if (NCH(ch) == 4) {
2412+
if (Py_Py3kWarningFlag &&
2413+
!ast_3x_warn(c, n, "the raise clause with three components is not supported in 3.x",
2414+
"use 'raise' with a single object")) {
2415+
return NULL;
2416+
}
24122417
expr_ty expr1, expr2;
24132418

24142419
expr1 = ast_for_expr(c, CHILD(ch, 1));
@@ -3059,6 +3064,20 @@ ast_for_except_clause(struct compiling *c, const node *exc, node *body)
30593064
asdl_seq *suite_seq;
30603065
expr_ty expression;
30613066
expr_ty e = ast_for_expr(c, CHILD(exc, 3));
3067+
expr_ty as_or_comma = ast_for_expr(c, CHILD(exc, 2));
3068+
3069+
if (TYPE(CHILD(exc, 2)) == COMMA)
3070+
if (Py_Py3kWarningFlag &&
3071+
!ast_3x_warn(c, exc, "the commas syntax for the except clause is not supported in 3.x",
3072+
"use the 'as' syntax instead")) {
3073+
return NULL;
3074+
}
3075+
if (e == Tuple_kind)
3076+
if (Py_Py3kWarningFlag &&
3077+
!ast_3x_warn(c, exc, "Iterable exceptions are not supported in 3.x",
3078+
"access the arguments through the 'args' attribute instead")) {
3079+
return NULL;
3080+
}
30623081
if (!e)
30633082
return NULL;
30643083
if (!set_context(c, e, Store, CHILD(exc, 3)))

Python/sysmodule.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ PyDoc_STRVAR(excepthook_doc,
149149
static PyObject *
150150
sys_exc_info(PyObject *self, PyObject *noargs)
151151
{
152+
if (Py_Py3kWarningFlag) {
153+
if (PyErr_WarnExplicit_WithFix(PyExc_Py3xWarning,
154+
"sys.exc_info() not supported in 3.x",
155+
"use except clauses", NULL, NULL,
156+
NULL, NULL)) {
157+
return NULL;
158+
}
159+
}
152160
PyThreadState *tstate;
153161
tstate = PyThreadState_GET();
154162
return Py_BuildValue(

0 commit comments

Comments
 (0)