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

Skip to content

Commit 2193d2b

Browse files
committed
type check AST strings and identifiers
This is related to a21829180423 as well as #12609 and #12610.
1 parent 996f606 commit 2193d2b

4 files changed

Lines changed: 57 additions & 6 deletions

File tree

Lib/test/test_ast.py

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

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

368382
class ASTHelpers_Test(unittest.TestCase):
369383

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.2?
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
@@ -794,8 +794,25 @@ def visitModule(self, mod):
794794
return 0;
795795
}
796796
797-
#define obj2ast_identifier obj2ast_object
798-
#define obj2ast_string obj2ast_object
797+
static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena,
798+
const char *name)
799+
{
800+
if (!PyUnicode_CheckExact(name)) {
801+
PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name);
802+
return 1;
803+
}
804+
return obj2ast_object(obj, out, arena);
805+
}
806+
807+
static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
808+
{
809+
return obj2ast_stringlike(obj, out, arena, "identifier");
810+
}
811+
812+
static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
813+
{
814+
return obj2ast_stringlike(obj, out, arena, "string");
815+
}
799816
800817
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
801818
{

Python/Python-ast.c

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
/*
5-
__version__ 82163.
5+
__version__ .
66
77
This module must be committed separately after each AST grammar change;
88
The __version__ number is set to the revision number of the commit
@@ -600,8 +600,25 @@ static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)
600600
return 0;
601601
}
602602

603-
#define obj2ast_identifier obj2ast_object
604-
#define obj2ast_string obj2ast_object
603+
static int obj2ast_stringlike(PyObject* obj, PyObject** out, PyArena* arena,
604+
const char *name)
605+
{
606+
if (!PyUnicode_CheckExact(name)) {
607+
PyErr_Format(PyExc_TypeError, "AST %s must be of type str", name);
608+
return 1;
609+
}
610+
return obj2ast_object(obj, out, arena);
611+
}
612+
613+
static int obj2ast_identifier(PyObject* obj, PyObject** out, PyArena* arena)
614+
{
615+
return obj2ast_stringlike(obj, out, arena, "identifier");
616+
}
617+
618+
static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
619+
{
620+
return obj2ast_stringlike(obj, out, arena, "string");
621+
}
605622

606623
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
607624
{
@@ -6739,7 +6756,7 @@ PyInit__ast(void)
67396756
NULL;
67406757
if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)
67416758
return NULL;
6742-
if (PyModule_AddStringConstant(m, "__version__", "82163") < 0)
6759+
if (PyModule_AddStringConstant(m, "__version__", "") < 0)
67436760
return NULL;
67446761
if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return
67456762
NULL;

0 commit comments

Comments
 (0)