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

Skip to content

Commit aab9585

Browse files
committed
Merged revisions 86538 via svnmerge from
svn+ssh://[email protected]/python/branches/py3k ........ r86538 | benjamin.peterson | 2010-11-19 19:38:49 -0600 (Fri, 19 Nov 2010) | 1 line use %R format code; fixes invalid dereferencing #10391 ........
1 parent c9a9417 commit aab9585

4 files changed

Lines changed: 46 additions & 58 deletions

File tree

Lib/test/test_ast.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,16 @@ def test_pickling(self):
183183
ast2 = mod.loads(mod.dumps(ast, protocol))
184184
self.assertEquals(to_tuple(ast2), to_tuple(ast))
185185

186+
def test_invalid_sum(self):
187+
pos = dict(lineno=2, col_offset=3)
188+
m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
189+
try:
190+
compile(m, "<test>", "exec")
191+
except TypeError as exc:
192+
self.assertIn("but got <_ast.expr", str(exc))
193+
else:
194+
self.fail("needed TypeError")
195+
186196

187197
class ASTHelpers_Test(unittest.TestCase):
188198

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.1.3?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #10391: Don't dereference invalid memory in error messages in the ast
14+
module.
15+
1316
Library
1417
-------
1518

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
@@ -3379,6 +3379,7 @@ int
33793379
obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
33803380
{
33813381
PyObject* tmp = NULL;
3382+
tmp = tmp;
33823383
int isinstance;
33833384

33843385

@@ -3518,18 +3519,16 @@ obj2ast_mod(PyObject* obj, mod_ty* out, PyArena* arena)
35183519
return 0;
35193520
}
35203521

3521-
tmp = PyObject_Repr(obj);
3522-
if (tmp == NULL) goto failed;
3523-
PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %.400s", PyBytes_AS_STRING(tmp));
3524-
failed:
3525-
Py_XDECREF(tmp);
3522+
PyErr_Format(PyExc_TypeError, "expected some sort of mod, but got %R", obj);
3523+
failed:
35263524
return 1;
35273525
}
35283526

35293527
int
35303528
obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
35313529
{
35323530
PyObject* tmp = NULL;
3531+
tmp = tmp;
35333532
int isinstance;
35343533

35353534
int lineno;
@@ -4717,18 +4716,16 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)
47174716
return 0;
47184717
}
47194718

4720-
tmp = PyObject_Repr(obj);
4721-
if (tmp == NULL) goto failed;
4722-
PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %.400s", PyBytes_AS_STRING(tmp));
4723-
failed:
4724-
Py_XDECREF(tmp);
4719+
PyErr_Format(PyExc_TypeError, "expected some sort of stmt, but got %R", obj);
4720+
failed:
47254721
return 1;
47264722
}
47274723

47284724
int
47294725
obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
47304726
{
47314727
PyObject* tmp = NULL;
4728+
tmp = tmp;
47324729
int isinstance;
47334730

47344731
int lineno;
@@ -5835,18 +5832,16 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
58355832
return 0;
58365833
}
58375834

5838-
tmp = PyObject_Repr(obj);
5839-
if (tmp == NULL) goto failed;
5840-
PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %.400s", PyBytes_AS_STRING(tmp));
5841-
failed:
5842-
Py_XDECREF(tmp);
5835+
PyErr_Format(PyExc_TypeError, "expected some sort of expr, but got %R", obj);
5836+
failed:
58435837
return 1;
58445838
}
58455839

58465840
int
58475841
obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
58485842
{
58495843
PyObject* tmp = NULL;
5844+
tmp = tmp;
58505845
int isinstance;
58515846

58525847
isinstance = PyObject_IsInstance(obj, (PyObject *)Load_type);
@@ -5898,18 +5893,15 @@ obj2ast_expr_context(PyObject* obj, expr_context_ty* out, PyArena* arena)
58985893
return 0;
58995894
}
59005895

5901-
tmp = PyObject_Repr(obj);
5902-
if (tmp == NULL) goto failed;
5903-
PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %.400s", PyBytes_AS_STRING(tmp));
5904-
failed:
5905-
Py_XDECREF(tmp);
5896+
PyErr_Format(PyExc_TypeError, "expected some sort of expr_context, but got %R", obj);
59065897
return 1;
59075898
}
59085899

59095900
int
59105901
obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
59115902
{
59125903
PyObject* tmp = NULL;
5904+
tmp = tmp;
59135905
int isinstance;
59145906

59155907

@@ -6023,18 +6015,16 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)
60236015
return 0;
60246016
}
60256017

6026-
tmp = PyObject_Repr(obj);
6027-
if (tmp == NULL) goto failed;
6028-
PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %.400s", PyBytes_AS_STRING(tmp));
6029-
failed:
6030-
Py_XDECREF(tmp);
6018+
PyErr_Format(PyExc_TypeError, "expected some sort of slice, but got %R", obj);
6019+
failed:
60316020
return 1;
60326021
}
60336022

60346023
int
60356024
obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
60366025
{
60376026
PyObject* tmp = NULL;
6027+
tmp = tmp;
60386028
int isinstance;
60396029

60406030
isinstance = PyObject_IsInstance(obj, (PyObject *)And_type);
@@ -6054,18 +6044,15 @@ obj2ast_boolop(PyObject* obj, boolop_ty* out, PyArena* arena)
60546044
return 0;
60556045
}
60566046

6057-
tmp = PyObject_Repr(obj);
6058-
if (tmp == NULL) goto failed;
6059-
PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %.400s", PyBytes_AS_STRING(tmp));
6060-
failed:
6061-
Py_XDECREF(tmp);
6047+
PyErr_Format(PyExc_TypeError, "expected some sort of boolop, but got %R", obj);
60626048
return 1;
60636049
}
60646050

60656051
int
60666052
obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
60676053
{
60686054
PyObject* tmp = NULL;
6055+
tmp = tmp;
60696056
int isinstance;
60706057

60716058
isinstance = PyObject_IsInstance(obj, (PyObject *)Add_type);
@@ -6165,18 +6152,15 @@ obj2ast_operator(PyObject* obj, operator_ty* out, PyArena* arena)
61656152
return 0;
61666153
}
61676154

6168-
tmp = PyObject_Repr(obj);
6169-
if (tmp == NULL) goto failed;
6170-
PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %.400s", PyBytes_AS_STRING(tmp));
6171-
failed:
6172-
Py_XDECREF(tmp);
6155+
PyErr_Format(PyExc_TypeError, "expected some sort of operator, but got %R", obj);
61736156
return 1;
61746157
}
61756158

61766159
int
61776160
obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
61786161
{
61796162
PyObject* tmp = NULL;
6163+
tmp = tmp;
61806164
int isinstance;
61816165

61826166
isinstance = PyObject_IsInstance(obj, (PyObject *)Invert_type);
@@ -6212,18 +6196,15 @@ obj2ast_unaryop(PyObject* obj, unaryop_ty* out, PyArena* arena)
62126196
return 0;
62136197
}
62146198

6215-
tmp = PyObject_Repr(obj);
6216-
if (tmp == NULL) goto failed;
6217-
PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %.400s", PyBytes_AS_STRING(tmp));
6218-
failed:
6219-
Py_XDECREF(tmp);
6199+
PyErr_Format(PyExc_TypeError, "expected some sort of unaryop, but got %R", obj);
62206200
return 1;
62216201
}
62226202

62236203
int
62246204
obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
62256205
{
62266206
PyObject* tmp = NULL;
6207+
tmp = tmp;
62276208
int isinstance;
62286209

62296210
isinstance = PyObject_IsInstance(obj, (PyObject *)Eq_type);
@@ -6307,11 +6288,7 @@ obj2ast_cmpop(PyObject* obj, cmpop_ty* out, PyArena* arena)
63076288
return 0;
63086289
}
63096290

6310-
tmp = PyObject_Repr(obj);
6311-
if (tmp == NULL) goto failed;
6312-
PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %.400s", PyBytes_AS_STRING(tmp));
6313-
failed:
6314-
Py_XDECREF(tmp);
6291+
PyErr_Format(PyExc_TypeError, "expected some sort of cmpop, but got %R", obj);
63156292
return 1;
63166293
}
63176294

@@ -6383,6 +6360,7 @@ int
63836360
obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
63846361
{
63856362
PyObject* tmp = NULL;
6363+
tmp = tmp;
63866364
int isinstance;
63876365

63886366
int lineno;
@@ -6478,11 +6456,8 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)
64786456
return 0;
64796457
}
64806458

6481-
tmp = PyObject_Repr(obj);
6482-
if (tmp == NULL) goto failed;
6483-
PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %.400s", PyBytes_AS_STRING(tmp));
6484-
failed:
6485-
Py_XDECREF(tmp);
6459+
PyErr_Format(PyExc_TypeError, "expected some sort of excepthandler, but got %R", obj);
6460+
failed:
64866461
return 1;
64876462
}
64886463

0 commit comments

Comments
 (0)