bpo-32911: Revert bpo-29463. (GH-7121)

Remove the docstring attribute of AST types and restore docstring
expression as a first stmt in their body.

Co-authored-by: INADA Naoki <methane@users.noreply.github.com>
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index f7a0568..38b9292 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -10,10 +10,8 @@
 static PyObject* ast2obj_mod(void*);
 static PyTypeObject *Module_type;
 _Py_IDENTIFIER(body);
-_Py_IDENTIFIER(docstring);
 static char *Module_fields[]={
     "body",
-    "docstring",
 };
 static PyTypeObject *Interactive_type;
 static char *Interactive_fields[]={
@@ -46,7 +44,6 @@
     "body",
     "decorator_list",
     "returns",
-    "docstring",
 };
 static PyTypeObject *AsyncFunctionDef_type;
 static char *AsyncFunctionDef_fields[]={
@@ -55,7 +52,6 @@
     "body",
     "decorator_list",
     "returns",
-    "docstring",
 };
 static PyTypeObject *ClassDef_type;
 _Py_IDENTIFIER(bases);
@@ -66,7 +62,6 @@
     "keywords",
     "body",
     "decorator_list",
-    "docstring",
 };
 static PyTypeObject *Return_type;
 _Py_IDENTIFIER(value);
@@ -847,7 +842,7 @@
     mod_type = make_type("mod", &AST_type, NULL, 0);
     if (!mod_type) return 0;
     if (!add_attributes(mod_type, NULL, 0)) return 0;
-    Module_type = make_type("Module", mod_type, Module_fields, 2);
+    Module_type = make_type("Module", mod_type, Module_fields, 1);
     if (!Module_type) return 0;
     Interactive_type = make_type("Interactive", mod_type, Interactive_fields,
                                  1);
@@ -860,12 +855,12 @@
     if (!stmt_type) return 0;
     if (!add_attributes(stmt_type, stmt_attributes, 2)) return 0;
     FunctionDef_type = make_type("FunctionDef", stmt_type, FunctionDef_fields,
-                                 6);
+                                 5);
     if (!FunctionDef_type) return 0;
     AsyncFunctionDef_type = make_type("AsyncFunctionDef", stmt_type,
-                                      AsyncFunctionDef_fields, 6);
+                                      AsyncFunctionDef_fields, 5);
     if (!AsyncFunctionDef_type) return 0;
-    ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 6);
+    ClassDef_type = make_type("ClassDef", stmt_type, ClassDef_fields, 5);
     if (!ClassDef_type) return 0;
     Return_type = make_type("Return", stmt_type, Return_fields, 1);
     if (!Return_type) return 0;
@@ -1192,7 +1187,7 @@
 static int obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena);
 
 mod_ty
-Module(asdl_seq * body, string docstring, PyArena *arena)
+Module(asdl_seq * body, PyArena *arena)
 {
     mod_ty p;
     p = (mod_ty)PyArena_Malloc(arena, sizeof(*p));
@@ -1200,7 +1195,6 @@
         return NULL;
     p->kind = Module_kind;
     p->v.Module.body = body;
-    p->v.Module.docstring = docstring;
     return p;
 }
 
@@ -1247,8 +1241,8 @@
 
 stmt_ty
 FunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq *
-            decorator_list, expr_ty returns, string docstring, int lineno, int
-            col_offset, PyArena *arena)
+            decorator_list, expr_ty returns, int lineno, int col_offset,
+            PyArena *arena)
 {
     stmt_ty p;
     if (!name) {
@@ -1270,7 +1264,6 @@
     p->v.FunctionDef.body = body;
     p->v.FunctionDef.decorator_list = decorator_list;
     p->v.FunctionDef.returns = returns;
-    p->v.FunctionDef.docstring = docstring;
     p->lineno = lineno;
     p->col_offset = col_offset;
     return p;
@@ -1278,8 +1271,8 @@
 
 stmt_ty
 AsyncFunctionDef(identifier name, arguments_ty args, asdl_seq * body, asdl_seq
-                 * decorator_list, expr_ty returns, string docstring, int
-                 lineno, int col_offset, PyArena *arena)
+                 * decorator_list, expr_ty returns, int lineno, int col_offset,
+                 PyArena *arena)
 {
     stmt_ty p;
     if (!name) {
@@ -1301,7 +1294,6 @@
     p->v.AsyncFunctionDef.body = body;
     p->v.AsyncFunctionDef.decorator_list = decorator_list;
     p->v.AsyncFunctionDef.returns = returns;
-    p->v.AsyncFunctionDef.docstring = docstring;
     p->lineno = lineno;
     p->col_offset = col_offset;
     return p;
@@ -1309,8 +1301,8 @@
 
 stmt_ty
 ClassDef(identifier name, asdl_seq * bases, asdl_seq * keywords, asdl_seq *
-         body, asdl_seq * decorator_list, string docstring, int lineno, int
-         col_offset, PyArena *arena)
+         body, asdl_seq * decorator_list, int lineno, int col_offset, PyArena
+         *arena)
 {
     stmt_ty p;
     if (!name) {
@@ -1327,7 +1319,6 @@
     p->v.ClassDef.keywords = keywords;
     p->v.ClassDef.body = body;
     p->v.ClassDef.decorator_list = decorator_list;
-    p->v.ClassDef.docstring = docstring;
     p->lineno = lineno;
     p->col_offset = col_offset;
     return p;
@@ -2591,11 +2582,6 @@
         if (_PyObject_SetAttrId(result, &PyId_body, value) == -1)
             goto failed;
         Py_DECREF(value);
-        value = ast2obj_string(o->v.Module.docstring);
-        if (!value) goto failed;
-        if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
-            goto failed;
-        Py_DECREF(value);
         break;
     case Interactive_kind:
         result = PyType_GenericNew(Interactive_type, NULL, NULL);
@@ -2670,11 +2656,6 @@
         if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
             goto failed;
         Py_DECREF(value);
-        value = ast2obj_string(o->v.FunctionDef.docstring);
-        if (!value) goto failed;
-        if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
-            goto failed;
-        Py_DECREF(value);
         break;
     case AsyncFunctionDef_kind:
         result = PyType_GenericNew(AsyncFunctionDef_type, NULL, NULL);
@@ -2705,11 +2686,6 @@
         if (_PyObject_SetAttrId(result, &PyId_returns, value) == -1)
             goto failed;
         Py_DECREF(value);
-        value = ast2obj_string(o->v.AsyncFunctionDef.docstring);
-        if (!value) goto failed;
-        if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
-            goto failed;
-        Py_DECREF(value);
         break;
     case ClassDef_kind:
         result = PyType_GenericNew(ClassDef_type, NULL, NULL);
@@ -2739,11 +2715,6 @@
         if (_PyObject_SetAttrId(result, &PyId_decorator_list, value) == -1)
             goto failed;
         Py_DECREF(value);
-        value = ast2obj_string(o->v.ClassDef.docstring);
-        if (!value) goto failed;
-        if (_PyObject_SetAttrId(result, &PyId_docstring, value) == -1)
-            goto failed;
-        Py_DECREF(value);
         break;
     case Return_kind:
         result = PyType_GenericNew(Return_type, NULL, NULL);
@@ -3984,7 +3955,6 @@
     }
     if (isinstance) {
         asdl_seq* body;
-        string docstring;
 
         if (_PyObject_LookupAttrId(obj, &PyId_body, &tmp) < 0) {
             return 1;
@@ -4016,20 +3986,7 @@
             }
             Py_CLEAR(tmp);
         }
-        if (_PyObject_LookupAttrId(obj, &PyId_docstring, &tmp) < 0) {
-            return 1;
-        }
-        if (tmp == NULL || tmp == Py_None) {
-            Py_CLEAR(tmp);
-            docstring = NULL;
-        }
-        else {
-            int res;
-            res = obj2ast_string(tmp, &docstring, arena);
-            if (res != 0) goto failed;
-            Py_CLEAR(tmp);
-        }
-        *out = Module(body, docstring, arena);
+        *out = Module(body, arena);
         if (*out == NULL) goto failed;
         return 0;
     }
@@ -4195,7 +4152,6 @@
         asdl_seq* body;
         asdl_seq* decorator_list;
         expr_ty returns;
-        string docstring;
 
         if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
             return 1;
@@ -4296,21 +4252,8 @@
             if (res != 0) goto failed;
             Py_CLEAR(tmp);
         }
-        if (_PyObject_LookupAttrId(obj, &PyId_docstring, &tmp) < 0) {
-            return 1;
-        }
-        if (tmp == NULL || tmp == Py_None) {
-            Py_CLEAR(tmp);
-            docstring = NULL;
-        }
-        else {
-            int res;
-            res = obj2ast_string(tmp, &docstring, arena);
-            if (res != 0) goto failed;
-            Py_CLEAR(tmp);
-        }
-        *out = FunctionDef(name, args, body, decorator_list, returns,
-                           docstring, lineno, col_offset, arena);
+        *out = FunctionDef(name, args, body, decorator_list, returns, lineno,
+                           col_offset, arena);
         if (*out == NULL) goto failed;
         return 0;
     }
@@ -4324,7 +4267,6 @@
         asdl_seq* body;
         asdl_seq* decorator_list;
         expr_ty returns;
-        string docstring;
 
         if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
             return 1;
@@ -4425,21 +4367,8 @@
             if (res != 0) goto failed;
             Py_CLEAR(tmp);
         }
-        if (_PyObject_LookupAttrId(obj, &PyId_docstring, &tmp) < 0) {
-            return 1;
-        }
-        if (tmp == NULL || tmp == Py_None) {
-            Py_CLEAR(tmp);
-            docstring = NULL;
-        }
-        else {
-            int res;
-            res = obj2ast_string(tmp, &docstring, arena);
-            if (res != 0) goto failed;
-            Py_CLEAR(tmp);
-        }
         *out = AsyncFunctionDef(name, args, body, decorator_list, returns,
-                                docstring, lineno, col_offset, arena);
+                                lineno, col_offset, arena);
         if (*out == NULL) goto failed;
         return 0;
     }
@@ -4453,7 +4382,6 @@
         asdl_seq* keywords;
         asdl_seq* body;
         asdl_seq* decorator_list;
-        string docstring;
 
         if (_PyObject_LookupAttrId(obj, &PyId_name, &tmp) < 0) {
             return 1;
@@ -4588,21 +4516,8 @@
             }
             Py_CLEAR(tmp);
         }
-        if (_PyObject_LookupAttrId(obj, &PyId_docstring, &tmp) < 0) {
-            return 1;
-        }
-        if (tmp == NULL || tmp == Py_None) {
-            Py_CLEAR(tmp);
-            docstring = NULL;
-        }
-        else {
-            int res;
-            res = obj2ast_string(tmp, &docstring, arena);
-            if (res != 0) goto failed;
-            Py_CLEAR(tmp);
-        }
-        *out = ClassDef(name, bases, keywords, body, decorator_list, docstring,
-                        lineno, col_offset, arena);
+        *out = ClassDef(name, bases, keywords, body, decorator_list, lineno,
+                        col_offset, arena);
         if (*out == NULL) goto failed;
         return 0;
     }