Commit fecf254c authored by Benjamin Peterson's avatar Benjamin Peterson

merge heads

parents 0d51e5d7 83d783e6
...@@ -745,6 +745,11 @@ type objects) *must* have the :attr:`ob_size` field. ...@@ -745,6 +745,11 @@ type objects) *must* have the :attr:`ob_size` field.
This field is not inherited by subtypes (though the attributes defined in here This field is not inherited by subtypes (though the attributes defined in here
are inherited through a different mechanism). are inherited through a different mechanism).
.. warning::
It is not safe to use :c:func:`PyDict_SetItem` on or otherwise modify
:attr:`tp_dict` with the dictionary C-API.
.. c:member:: descrgetfunc PyTypeObject.tp_descr_get .. c:member:: descrgetfunc PyTypeObject.tp_descr_get
......
...@@ -271,7 +271,7 @@ struct _expr { ...@@ -271,7 +271,7 @@ struct _expr {
} Str; } Str;
struct { struct {
string s; bytes s;
} Bytes; } Bytes;
struct { struct {
...@@ -501,7 +501,7 @@ expr_ty _Py_Num(object n, int lineno, int col_offset, PyArena *arena); ...@@ -501,7 +501,7 @@ expr_ty _Py_Num(object n, int lineno, int col_offset, PyArena *arena);
#define Str(a0, a1, a2, a3) _Py_Str(a0, a1, a2, a3) #define Str(a0, a1, a2, a3) _Py_Str(a0, a1, a2, a3)
expr_ty _Py_Str(string s, int lineno, int col_offset, PyArena *arena); expr_ty _Py_Str(string s, int lineno, int col_offset, PyArena *arena);
#define Bytes(a0, a1, a2, a3) _Py_Bytes(a0, a1, a2, a3) #define Bytes(a0, a1, a2, a3) _Py_Bytes(a0, a1, a2, a3)
expr_ty _Py_Bytes(string s, int lineno, int col_offset, PyArena *arena); expr_ty _Py_Bytes(bytes s, int lineno, int col_offset, PyArena *arena);
#define Ellipsis(a0, a1, a2) _Py_Ellipsis(a0, a1, a2) #define Ellipsis(a0, a1, a2) _Py_Ellipsis(a0, a1, a2)
expr_ty _Py_Ellipsis(int lineno, int col_offset, PyArena *arena); expr_ty _Py_Ellipsis(int lineno, int col_offset, PyArena *arena);
#define Attribute(a0, a1, a2, a3, a4, a5) _Py_Attribute(a0, a1, a2, a3, a4, a5) #define Attribute(a0, a1, a2, a3, a4, a5) _Py_Attribute(a0, a1, a2, a3, a4, a5)
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
typedef PyObject * identifier; typedef PyObject * identifier;
typedef PyObject * string; typedef PyObject * string;
typedef PyObject * bytes;
typedef PyObject * object; typedef PyObject * object;
/* It would be nice if the code generated by asdl_c.py was completely /* It would be nice if the code generated by asdl_c.py was completely
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
extern "C" { extern "C" {
#endif #endif
PyAPI_FUNC(int) PyAST_Validate(mod_ty);
PyAPI_FUNC(mod_ty) PyAST_FromNode( PyAPI_FUNC(mod_ty) PyAST_FromNode(
const node *n, const node *n,
PyCompilerFlags *flags, PyCompilerFlags *flags,
......
This diff is collapsed.
...@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1? ...@@ -10,6 +10,8 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #12575: Validate user-generated AST before it is compiled.
- Make type(None), type(Ellipsis), and type(NotImplemented) callable. They - Make type(None), type(Ellipsis), and type(NotImplemented) callable. They
return the respective singleton instances. return the respective singleton instances.
......
-- ASDL's four builtin types are identifier, int, string, object -- ASDL's five builtin types are identifier, int, string, bytes, object
module Python module Python
{ {
...@@ -67,7 +67,7 @@ module Python ...@@ -67,7 +67,7 @@ module Python
expr? starargs, expr? kwargs) expr? starargs, expr? kwargs)
| Num(object n) -- a number as a PyObject. | Num(object n) -- a number as a PyObject.
| Str(string s) -- need to specify raw, unicode, etc? | Str(string s) -- need to specify raw, unicode, etc?
| Bytes(string s) | Bytes(bytes s)
| Ellipsis | Ellipsis
-- other literals? bools? -- other literals? bools?
......
...@@ -228,7 +228,7 @@ class ASDLParser(spark.GenericParser, object): ...@@ -228,7 +228,7 @@ class ASDLParser(spark.GenericParser, object):
" field ::= Id ? " " field ::= Id ? "
return Field(type[0], opt=True) return Field(type[0], opt=True)
builtin_types = ("identifier", "string", "int", "bool", "object") builtin_types = ("identifier", "string", "bytes", "int", "object")
# below is a collection of classes to capture the AST of an AST :-) # below is a collection of classes to capture the AST of an AST :-)
# not sure if any of the methods are useful yet, but I'm adding them # not sure if any of the methods are useful yet, but I'm adding them
......
...@@ -776,6 +776,7 @@ static PyObject* ast2obj_object(void *o) ...@@ -776,6 +776,7 @@ static PyObject* ast2obj_object(void *o)
} }
#define ast2obj_identifier ast2obj_object #define ast2obj_identifier ast2obj_object
#define ast2obj_string ast2obj_object #define ast2obj_string ast2obj_object
#define ast2obj_bytes ast2obj_object
static PyObject* ast2obj_int(long b) static PyObject* ast2obj_int(long b)
{ {
...@@ -813,6 +814,15 @@ static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena) ...@@ -813,6 +814,15 @@ static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
return obj2ast_object(obj, out, arena); return obj2ast_object(obj, out, arena);
} }
static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
{
if (!PyBytes_CheckExact(obj)) {
PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
return 1;
}
return obj2ast_object(obj, out, arena);
}
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
{ {
int i; int i;
......
...@@ -573,6 +573,7 @@ static PyObject* ast2obj_object(void *o) ...@@ -573,6 +573,7 @@ static PyObject* ast2obj_object(void *o)
} }
#define ast2obj_identifier ast2obj_object #define ast2obj_identifier ast2obj_object
#define ast2obj_string ast2obj_object #define ast2obj_string ast2obj_object
#define ast2obj_bytes ast2obj_object
static PyObject* ast2obj_int(long b) static PyObject* ast2obj_int(long b)
{ {
...@@ -610,6 +611,15 @@ static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena) ...@@ -610,6 +611,15 @@ static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* arena)
return obj2ast_object(obj, out, arena); return obj2ast_object(obj, out, arena);
} }
static int obj2ast_bytes(PyObject* obj, PyObject** out, PyArena* arena)
{
if (!PyBytes_CheckExact(obj)) {
PyErr_SetString(PyExc_TypeError, "AST bytes must be of type bytes");
return 1;
}
return obj2ast_object(obj, out, arena);
}
static int obj2ast_int(PyObject* obj, int* out, PyArena* arena) static int obj2ast_int(PyObject* obj, int* out, PyArena* arena)
{ {
int i; int i;
...@@ -1773,7 +1783,7 @@ Str(string s, int lineno, int col_offset, PyArena *arena) ...@@ -1773,7 +1783,7 @@ Str(string s, int lineno, int col_offset, PyArena *arena)
} }
expr_ty expr_ty
Bytes(string s, int lineno, int col_offset, PyArena *arena) Bytes(bytes s, int lineno, int col_offset, PyArena *arena)
{ {
expr_ty p; expr_ty p;
if (!s) { if (!s) {
...@@ -2804,7 +2814,7 @@ ast2obj_expr(void* _o) ...@@ -2804,7 +2814,7 @@ ast2obj_expr(void* _o)
case Bytes_kind: case Bytes_kind:
result = PyType_GenericNew(Bytes_type, NULL, NULL); result = PyType_GenericNew(Bytes_type, NULL, NULL);
if (!result) goto failed; if (!result) goto failed;
value = ast2obj_string(o->v.Bytes.s); value = ast2obj_bytes(o->v.Bytes.s);
if (!value) goto failed; if (!value) goto failed;
if (PyObject_SetAttrString(result, "s", value) == -1) if (PyObject_SetAttrString(result, "s", value) == -1)
goto failed; goto failed;
...@@ -5509,13 +5519,13 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena) ...@@ -5509,13 +5519,13 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
return 1; return 1;
} }
if (isinstance) { if (isinstance) {
string s; bytes s;
if (PyObject_HasAttrString(obj, "s")) { if (PyObject_HasAttrString(obj, "s")) {
int res; int res;
tmp = PyObject_GetAttrString(obj, "s"); tmp = PyObject_GetAttrString(obj, "s");
if (tmp == NULL) goto failed; if (tmp == NULL) goto failed;
res = obj2ast_string(tmp, &s, arena); res = obj2ast_bytes(tmp, &s, arena);
if (res != 0) goto failed; if (res != 0) goto failed;
Py_XDECREF(tmp); Py_XDECREF(tmp);
tmp = NULL; tmp = NULL;
......
This diff is collapsed.
...@@ -604,6 +604,10 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds) ...@@ -604,6 +604,10 @@ builtin_compile(PyObject *self, PyObject *args, PyObject *kwds)
PyArena_Free(arena); PyArena_Free(arena);
goto error; goto error;
} }
if (!PyAST_Validate(mod)) {
PyArena_Free(arena);
goto error;
}
result = (PyObject*)PyAST_CompileEx(mod, filename, result = (PyObject*)PyAST_CompileEx(mod, filename,
&cf, optimize, arena); &cf, optimize, arena);
PyArena_Free(arena); PyArena_Free(arena);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment