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

Skip to content

Commit e249841

Browse files
committed
add a asdl bytes type, so Bytes.s be properly typechecked
1 parent 18205ba commit e249841

5 files changed

Lines changed: 28 additions & 7 deletions

File tree

Include/asdl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
typedef PyObject * identifier;
55
typedef PyObject * string;
6+
typedef PyObject * bytes;
67
typedef PyObject * object;
78

89
/* It would be nice if the code generated by asdl_c.py was completely

Parser/Python.asdl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- ASDL's four builtin types are identifier, int, string, object
1+
-- ASDL's five builtin types are identifier, int, string, bytes, object
22

33
module Python
44
{
@@ -67,7 +67,7 @@ module Python
6767
expr? starargs, expr? kwargs)
6868
| Num(object n) -- a number as a PyObject.
6969
| Str(string s) -- need to specify raw, unicode, etc?
70-
| Bytes(string s)
70+
| Bytes(bytes s)
7171
| Ellipsis
7272
-- other literals? bools?
7373

Parser/asdl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def p_field_5(self, type_):
228228
" field ::= Id ? "
229229
return Field(type[0], opt=True)
230230

231-
builtin_types = ("identifier", "string", "int", "bool", "object")
231+
builtin_types = ("identifier", "string", "bytes", "int", "bool", "object")
232232

233233
# below is a collection of classes to capture the AST of an AST :-)
234234
# not sure if any of the methods are useful yet, but I'm adding them

Parser/asdl_c.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,7 @@ def visitModule(self, mod):
776776
}
777777
#define ast2obj_identifier ast2obj_object
778778
#define ast2obj_string ast2obj_object
779+
#define ast2obj_bytes ast2obj_object
779780
780781
static PyObject* ast2obj_int(long b)
781782
{
@@ -813,6 +814,15 @@ def visitModule(self, mod):
813814
return obj2ast_object(obj, out, arena);
814815
}
815816
817+
static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
818+
{
819+
if (!PyBytes_CheckExact(obj)) {
820+
PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
821+
return 1;
822+
}
823+
return obj2ast_object(obj, out, arena);
824+
}
825+
816826
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
817827
{
818828
int i;

Python/Python-ast.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ static PyObject* ast2obj_object(void *o)
573573
}
574574
#define ast2obj_identifier ast2obj_object
575575
#define ast2obj_string ast2obj_object
576+
#define ast2obj_bytes ast2obj_object
576577

577578
static PyObject* ast2obj_int(long b)
578579
{
@@ -610,6 +611,15 @@ static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
610611
return obj2ast_object(obj, out, arena);
611612
}
612613

614+
static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
615+
{
616+
if (!PyBytes_CheckExact(obj)) {
617+
PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
618+
return 1;
619+
}
620+
return obj2ast_object(obj, out, arena);
621+
}
622+
613623
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
614624
{
615625
int i;
@@ -1773,7 +1783,7 @@ Str(string s, int lineno, int col_offset, PyArena *arena)
17731783
}
17741784

17751785
expr_ty
1776-
Bytes(string s, int lineno, int col_offset, PyArena *arena)
1786+
Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
17771787
{
17781788
expr_ty p;
17791789
if (!s) {
@@ -2804,7 +2814,7 @@ ast2obj_expr(void* _o)
28042814
case Bytes_kind:
28052815
result = PyType_GenericNew(Bytes_type, NULL, NULL);
28062816
if (!result) goto failed;
2807-
value = ast2obj_string(o->v.Bytes.s);
2817+
value = ast2obj_bytes(o->v.Bytes.s);
28082818
if (!value) goto failed;
28092819
if (PyObject_SetAttrString(result, "s", value) == -1)
28102820
goto failed;
@@ -5509,13 +5519,13 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
55095519
return 1;
55105520
}
55115521
if (isinstance) {
5512-
string s;
5522+
bytes s;
55135523

55145524
if (PyObject_HasAttrString(obj, "s")) {
55155525
int res;
55165526
tmp = PyObject_GetAttrString(obj, "s");
55175527
if (tmp == NULL) goto failed;
5518-
res = obj2ast_string(tmp, &s, arena);
5528+
res = obj2ast_bytes(tmp, &s, arena);
55195529
if (res != 0) goto failed;
55205530
Py_XDECREF(tmp);
55215531
tmp = NULL;

0 commit comments

Comments
 (0)