[Python-checkins] cpython: add a asdl bytes type, so Bytes.s be properly typechecked

benjamin.peterson python-checkins at python.org
Tue Aug 9 23:17:24 CEST 2011


http://hg.python.org/cpython/rev/f578ca44193d
changeset:   71792:f578ca44193d
parent:      71787:69b354a8c90f
user:        Benjamin Peterson <benjamin at python.org>
date:        Tue Aug 09 16:08:39 2011 -0500
summary:
  add a asdl bytes type, so Bytes.s be properly typechecked

files:
  Include/asdl.h      |   1 +
  Parser/Python.asdl  |   3 ++-
  Parser/asdl.py      |   2 +-
  Parser/asdl_c.py    |  10 ++++++++++
  Python/Python-ast.c |  18 ++++++++++++++----
  5 files changed, 28 insertions(+), 6 deletions(-)


diff --git a/Include/asdl.h b/Include/asdl.h
--- 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
--- 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 @@
 			 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
--- a/Parser/asdl.py
+++ b/Parser/asdl.py
@@ -228,7 +228,7 @@
         " 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
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -776,6 +776,7 @@
 }
 #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 @@
     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
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -573,6 +573,7 @@
 }
 #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 @@
     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 @@
 }
 
 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 @@
         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 @@
                 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;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list