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

Skip to content

Commit 86f088e

Browse files
committed
merge 3.2
2 parents db57e8d + 2193d2b commit 86f088e

4 files changed

Lines changed: 55 additions & 4 deletions

File tree

Lib/test/test_ast.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,20 @@ def test_invalid_sum(self):
367367
compile(m, "<test>", "exec")
368368
self.assertIn("but got <_ast.expr", str(cm.exception))
369369

370+
def test_invalid_identitifer(self):
371+
m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
372+
ast.fix_missing_locations(m)
373+
with self.assertRaises(TypeError) as cm:
374+
compile(m, "<test>", "exec")
375+
self.assertIn("identifier must be of type str", str(cm.exception))
376+
377+
def test_invalid_string(self):
378+
m = ast.Module([ast.Expr(ast.Str(42))])
379+
ast.fix_missing_locations(m)
380+
with self.assertRaises(TypeError) as cm:
381+
compile(m, "<test>", "exec")
382+
self.assertIn("string must be of type str", str(cm.exception))
383+
370384

371385
class ASTHelpers_Test(unittest.TestCase):
372386

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

13+
- Verify the types of AST strings and identifiers provided by the user before
14+
compiling them.
15+
1316
- Issue #12579: str.format_map() now raises a ValueError if used on a
1417
format string that contains positional fields. Initial patch by
1518
Julian Berman.

Parser/asdl_c.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,8 +795,25 @@ def visitModule(self, mod):
795795
return 0;
796796
}
797797
798-
#define obj2ast_identifier obj2ast_object
799-
#define obj2ast_string obj2ast_object
798+
static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena,
799+
const char *name)
800+
{
801+
if (!PyUnicode_CheckExact(name)) {
802+
PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name);
803+
return 1;
804+
}
805+
return obj2ast_object(obj, out, arena);
806+
}
807+
808+
static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
809+
{
810+
return obj2ast_stringlike(obj, out, arena, "identifier");
811+
}
812+
813+
static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
814+
{
815+
return obj2ast_stringlike(obj, out, arena, "string");
816+
}
800817
801818
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
802819
{

Python/Python-ast.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,25 @@ static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
592592
return 0;
593593
}
594594

595-
#define obj2ast_identifier obj2ast_object
596-
#define obj2ast_string obj2ast_object
595+
static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena,
596+
const char *name)
597+
{
598+
if (!PyUnicode_CheckExact(name)) {
599+
PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name);
600+
return 1;
601+
}
602+
return obj2ast_object(obj, out, arena);
603+
}
604+
605+
static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
606+
{
607+
return obj2ast_stringlike(obj, out, arena, "identifier");
608+
}
609+
610+
static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
611+
{
612+
return obj2ast_stringlike(obj, out, arena, "string");
613+
}
597614

598615
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
599616
{

0 commit comments

Comments
 (0)