summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2011-08-09 16:08:39 -0500
committerBenjamin Peterson <benjamin@python.org>2011-08-09 16:08:39 -0500
commit7bf0e0ea86f0b88e6cf0c6c6bd8a7113761769df (patch)
tree1488878b5f358e8d615a71000b719ba42110ef3e
parent97177225907715f8b4064716cb854d24a34ccff0 (diff)
downloadcpython-7bf0e0ea86f0b88e6cf0c6c6bd8a7113761769df.tar.gz
add a asdl bytes type, so Bytes.s be properly typechecked
-rw-r--r--Include/asdl.h1
-rw-r--r--Parser/Python.asdl4
-rw-r--r--Parser/asdl.py2
-rwxr-xr-xParser/asdl_c.py10
-rw-r--r--Python/Python-ast.c18
5 files changed, 28 insertions, 7 deletions
diff --git a/Include/asdl.h b/Include/asdl.h
index 9bb06978da..42bbbf8b02 100644
--- a/Include/asdl.h
+++ b/Include/asdl.h
@@ -3,6 +3,7 @@
typedef PyObject * identifier;
typedef PyObject * string;
+typedef PyObject * bytes;
typedef PyObject * object;
/* It would be nice if the code generated by asdl_c.py was completely
diff --git a/Parser/Python.asdl b/Parser/Python.asdl
index dc322dc9f3..6955199280 100644
--- a/Parser/Python.asdl
+++ b/Parser/Python.asdl
@@ -1,4 +1,4 @@
--- ASDL's four builtin types are identifier, int, string, object
+-- ASDL's five builtin types are identifier, int, string, bytes, object
module Python
{
@@ -67,7 +67,7 @@ module Python
expr? starargs, expr? kwargs)
| Num(object n) -- a number as a PyObject.
| Str(string s) -- need to specify raw, unicode, etc?
- | Bytes(string s)
+ | Bytes(bytes s)
| Ellipsis
-- other literals? bools?
diff --git a/Parser/asdl.py b/Parser/asdl.py
index c63dfa7fb6..c90d2e26c3 100644
--- a/Parser/asdl.py
+++ b/Parser/asdl.py
@@ -228,7 +228,7 @@ class ASDLParser(spark.GenericParser, object):
" field ::= Id ? "
return Field(type[0], opt=True)
-builtin_types = ("identifier", "string", "int", "bool", "object")
+builtin_types = ("identifier", "string", "bytes", "int", "bool", "object")
# 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
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index 432d7b7c25..0b95aaa1e8 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -776,6 +776,7 @@ static PyObject* ast2obj_object(void *o)
}
#define ast2obj_identifier ast2obj_object
#define ast2obj_string ast2obj_object
+#define ast2obj_bytes ast2obj_object
static PyObject* ast2obj_int(long b)
{
@@ -813,6 +814,15 @@ static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* 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)
{
int i;
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index 68b109722b..f6e345c916 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -573,6 +573,7 @@ static PyObject* ast2obj_object(void *o)
}
#define ast2obj_identifier ast2obj_object
#define ast2obj_string ast2obj_object
+#define ast2obj_bytes ast2obj_object
static PyObject* ast2obj_int(long b)
{
@@ -610,6 +611,15 @@ static int obj2ast_string(PyObject* obj, PyObject** out, PyArena* 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)
{
int i;
@@ -1773,7 +1783,7 @@ Str(string s, int lineno, int col_offset, PyArena *arena)
}
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;
if (!s) {
@@ -2804,7 +2814,7 @@ ast2obj_expr(void* _o)
case Bytes_kind:
result = PyType_GenericNew(Bytes_type, NULL, NULL);
if (!result) goto failed;
- value = ast2obj_string(o->v.Bytes.s);
+ value = ast2obj_bytes(o->v.Bytes.s);
if (!value) goto failed;
if (PyObject_SetAttrString(result, "s", value) == -1)
goto failed;
@@ -5509,13 +5519,13 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
return 1;
}
if (isinstance) {
- string s;
+ bytes s;
if (PyObject_HasAttrString(obj, "s")) {
int res;
tmp = PyObject_GetAttrString(obj, "s");
if (tmp == NULL) goto failed;
- res = obj2ast_string(tmp, &s, arena);
+ res = obj2ast_bytes(tmp, &s, arena);
if (res != 0) goto failed;
Py_XDECREF(tmp);
tmp = NULL;