Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5b06681

Browse files
committed
use %R format code; fixes invalid dereferencing #10391
1 parent c8c60c2 commit 5b06681

4 files changed

Lines changed: 43 additions & 58 deletions

File tree

Lib/test/test_ast.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,13 @@ def test_pickling(self):
197197
ast2 = mod.loads(mod.dumps(ast, protocol))
198198
self.assertEquals(to_tuple(ast2), to_tuple(ast))
199199

200+
def test_invalid_sum(self):
201+
pos = dict(lineno=2, col_offset=3)
202+
m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
203+
with self.assertRaises(TypeError) as cm:
204+
compile(m, "<test>", "exec")
205+
self.assertIn("but got <_ast.expr", str(cm.exception))
206+
200207

201208
class ASTHelpers_Test(unittest.TestCase):
202209

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2 Beta 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10391: Don't dereference invalid memory in error messages in the ast
14+
module.
15+
1316
- Issue #9518: Extend the PyModuleDef_HEAD_INIT macro to explicitly
1417
zero-initialize all fields, fixing compiler warnings seen when building
1518
extension modules with gcc with "-Wmissing-field-initializers" (implied

Parser/asdl_c.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,19 +366,19 @@ def funcHeader(self, name):
366366
self.emit("obj2ast_%s(PyObject* obj, %s* out, PyArena* arena)" % (name, ctype), 0)
367367
self.emit("{", 0)
368368
self.emit("PyObject* tmp = NULL;", 1)
369+
# Prevent compiler warnings about unused variable.
370+
self.emit("tmp = tmp;", 1)
369371
self.emit("int isinstance;", 1)
370372
self.emit("", 0)
371373

372-
def sumTrailer(self, name):
374+
def sumTrailer(self, name, add_label=False):
373375
self.emit("", 0)
374-
self.emit("tmp = PyObject_Repr(obj);", 1)
375376
# there's really nothing more we can do if this fails ...
376-
self.emit("if (tmp == NULL) goto failed;", 1)
377-
error = "expected some sort of %s, but got %%.400s" % name
378-
format = "PyErr_Format(PyExc_TypeError, \"%s\", PyBytes_AS_STRING(tmp));"
377+
error = "expected some sort of %s, but got %%R" % name
378+
format = "PyErr_Format(PyExc_TypeError, \"%s\", obj);"
379379
self.emit(format % error, 1, reflow=False)
380-
self.emit("failed:", 0)
381-
self.emit("Py_XDECREF(tmp);", 1)
380+
if add_label:
381+
self.emit("failed:", 1)
382382
self.emit("return 1;", 1)
383383
self.emit("}", 0)
384384
self.emit("", 0)
@@ -430,7 +430,7 @@ def complexSum(self, sum, name):
430430
self.emit("if (*out == NULL) goto failed;", 2)
431431
self.emit("return 0;", 2)
432432
self.emit("}", 1)
433-
self.sumTrailer(name)
433+
self.sumTrailer(name, True)
434434

435435
def visitAttributeDeclaration(self, a, name, sum=sum):
436436
ctype = get_c_type(a.type)

Python/Python-ast.c

Lines changed: 25 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3375,6 +3375,7 @@ int
33753375
obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
33763376
{
33773377
PyObject* tmp = NULL;
3378+
tmp = tmp;
33783379
int isinstance;
33793380

33803381

@@ -3514,18 +3515,16 @@ obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
35143515
return 0;
35153516
}
35163517

3517-
tmp = PyObject_Repr(obj);
3518-
if (tmp == NULL) goto failed;
3519-
PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyBytes_AS_STRING(tmp));
3520-
failed:
3521-
Py_XDECREF(tmp);
3518+
PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3519+
failed:
35223520
return 1;
35233521
}
35243522

35253523
int
35263524
obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
35273525
{
35283526
PyObject* tmp = NULL;
3527+
tmp = tmp;
35293528
int isinstance;
35303529

35313530
int lineno;
@@ -4712,18 +4711,16 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
47124711
return 0;
47134712
}
47144713

4715-
tmp = PyObject_Repr(obj);
4716-
if (tmp == NULL) goto failed;
4717-
PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyBytes_AS_STRING(tmp));
4718-
failed:
4719-
Py_XDECREF(tmp);
4714+
PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4715+
failed:
47204716
return 1;
47214717
}
47224718

47234719
int
47244720
obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
47254721
{
47264722
PyObject* tmp = NULL;
4723+
tmp = tmp;
47274724
int isinstance;
47284725

47294726
int lineno;
@@ -5830,18 +5827,16 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
58305827
return 0;
58315828
}
58325829

5833-
tmp = PyObject_Repr(obj);
5834-
if (tmp == NULL) goto failed;
5835-
PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyBytes_AS_STRING(tmp));
5836-
failed:
5837-
Py_XDECREF(tmp);
5830+
PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5831+
failed:
58385832
return 1;
58395833
}
58405834

58415835
int
58425836
obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
58435837
{
58445838
PyObject* tmp = NULL;
5839+
tmp = tmp;
58455840
int isinstance;
58465841

58475842
isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
@@ -5893,18 +5888,15 @@ obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
58935888
return 0;
58945889
}
58955890

5896-
tmp = PyObject_Repr(obj);
5897-
if (tmp == NULL) goto failed;
5898-
PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyBytes_AS_STRING(tmp));
5899-
failed:
5900-
Py_XDECREF(tmp);
5891+
PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
59015892
return 1;
59025893
}
59035894

59045895
int
59055896
obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
59065897
{
59075898
PyObject* tmp = NULL;
5899+
tmp = tmp;
59085900
int isinstance;
59095901

59105902

@@ -6018,18 +6010,16 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
60186010
return 0;
60196011
}
60206012

6021-
tmp = PyObject_Repr(obj);
6022-
if (tmp == NULL) goto failed;
6023-
PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyBytes_AS_STRING(tmp));
6024-
failed:
6025-
Py_XDECREF(tmp);
6013+
PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6014+
failed:
60266015
return 1;
60276016
}
60286017

60296018
int
60306019
obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
60316020
{
60326021
PyObject* tmp = NULL;
6022+
tmp = tmp;
60336023
int isinstance;
60346024

60356025
isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
@@ -6049,18 +6039,15 @@ obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
60496039
return 0;
60506040
}
60516041

6052-
tmp = PyObject_Repr(obj);
6053-
if (tmp == NULL) goto failed;
6054-
PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyBytes_AS_STRING(tmp));
6055-
failed:
6056-
Py_XDECREF(tmp);
6042+
PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
60576043
return 1;
60586044
}
60596045

60606046
int
60616047
obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
60626048
{
60636049
PyObject* tmp = NULL;
6050+
tmp = tmp;
60646051
int isinstance;
60656052

60666053
isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
@@ -6160,18 +6147,15 @@ obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
61606147
return 0;
61616148
}
61626149

6163-
tmp = PyObject_Repr(obj);
6164-
if (tmp == NULL) goto failed;
6165-
PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyBytes_AS_STRING(tmp));
6166-
failed:
6167-
Py_XDECREF(tmp);
6150+
PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
61686151
return 1;
61696152
}
61706153

61716154
int
61726155
obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
61736156
{
61746157
PyObject* tmp = NULL;
6158+
tmp = tmp;
61756159
int isinstance;
61766160

61776161
isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
@@ -6207,18 +6191,15 @@ obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
62076191
return 0;
62086192
}
62096193

6210-
tmp = PyObject_Repr(obj);
6211-
if (tmp == NULL) goto failed;
6212-
PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyBytes_AS_STRING(tmp));
6213-
failed:
6214-
Py_XDECREF(tmp);
6194+
PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
62156195
return 1;
62166196
}
62176197

62186198
int
62196199
obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
62206200
{
62216201
PyObject* tmp = NULL;
6202+
tmp = tmp;
62226203
int isinstance;
62236204

62246205
isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
@@ -6302,11 +6283,7 @@ obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
63026283
return 0;
63036284
}
63046285

6305-
tmp = PyObject_Repr(obj);
6306-
if (tmp == NULL) goto failed;
6307-
PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyBytes_AS_STRING(tmp));
6308-
failed:
6309-
Py_XDECREF(tmp);
6286+
PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
63106287
return 1;
63116288
}
63126289

@@ -6378,6 +6355,7 @@ int
63786355
obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
63796356
{
63806357
PyObject* tmp = NULL;
6358+
tmp = tmp;
63816359
int isinstance;
63826360

63836361
int lineno;
@@ -6473,11 +6451,8 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
64736451
return 0;
64746452
}
64756453

6476-
tmp = PyObject_Repr(obj);
6477-
if (tmp == NULL) goto failed;
6478-
PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyBytes_AS_STRING(tmp));
6479-
failed:
6480-
Py_XDECREF(tmp);
6454+
PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6455+
failed:
64816456
return 1;
64826457
}
64836458

0 commit comments

Comments
 (0)