diff options
Diffstat (limited to 'Python/Python-ast.c')
| -rw-r--r-- | Python/Python-ast.c | 270 | 
1 files changed, 167 insertions, 103 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c index e6f1e58..744e640 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -271,6 +271,10 @@ static PyTypeObject *Bytes_type;  static char *Bytes_fields[]={          "s",  }; +static PyTypeObject *NameConstant_type; +static char *NameConstant_fields[]={ +        "value", +};  static PyTypeObject *Ellipsis_type;  static PyTypeObject *Attribute_type;  _Py_IDENTIFIER(attr); @@ -408,24 +412,24 @@ static char *ExceptHandler_fields[]={  static PyTypeObject *arguments_type;  static PyObject* ast2obj_arguments(void*);  _Py_IDENTIFIER(vararg); -_Py_IDENTIFIER(varargannotation);  _Py_IDENTIFIER(kwonlyargs); +_Py_IDENTIFIER(kw_defaults);  _Py_IDENTIFIER(kwarg); -_Py_IDENTIFIER(kwargannotation);  _Py_IDENTIFIER(defaults); -_Py_IDENTIFIER(kw_defaults);  static char *arguments_fields[]={          "args",          "vararg", -        "varargannotation",          "kwonlyargs", +        "kw_defaults",          "kwarg", -        "kwargannotation",          "defaults", -        "kw_defaults",  };  static PyTypeObject *arg_type;  static PyObject* ast2obj_arg(void*); +static char *arg_attributes[] = { +        "lineno", +        "col_offset", +};  _Py_IDENTIFIER(arg);  _Py_IDENTIFIER(annotation);  static char *arg_fields[]={ @@ -673,6 +677,7 @@ static PyObject* ast2obj_object(void *o)      Py_INCREF((PyObject*)o);      return (PyObject*)o;  } +#define ast2obj_singleton ast2obj_object  #define ast2obj_identifier ast2obj_object  #define ast2obj_string ast2obj_object  #define ast2obj_bytes ast2obj_object @@ -684,6 +689,17 @@ static PyObject* ast2obj_int(long b)  /* Conversion Python -> AST */ +static int obj2ast_singleton(PyObject *obj, PyObject** out, PyArena* arena) +{ +    if (obj != Py_None && obj != Py_True && obj != Py_False) { +        PyErr_SetString(PyExc_ValueError, +                        "AST singleton must be True, False, or None"); +        return 1; +    } +    *out = obj; +    return 0; +} +  static int obj2ast_object(PyObject* obj, PyObject** out, PyArena* arena)  {      if (obj == Py_None) @@ -754,6 +770,19 @@ static int add_ast_fields(void)      return 0;  } +static int exists_not_none(PyObject *obj, _Py_Identifier *id) +{ +    int isnone; +    PyObject *attr = _PyObject_GetAttrId(obj, id); +    if (!attr) { +        PyErr_Clear(); +        return 0; +    } +    isnone = attr == Py_None; +    Py_DECREF(attr); +    return !isnone; +} +  static int init_types(void)  { @@ -860,6 +889,9 @@ static int init_types(void)          if (!Str_type) return 0;          Bytes_type = make_type("Bytes", expr_type, Bytes_fields, 1);          if (!Bytes_type) return 0; +        NameConstant_type = make_type("NameConstant", expr_type, +                                      NameConstant_fields, 1); +        if (!NameConstant_type) return 0;          Ellipsis_type = make_type("Ellipsis", expr_type, NULL, 0);          if (!Ellipsis_type) return 0;          Attribute_type = make_type("Attribute", expr_type, Attribute_fields, 3); @@ -1037,6 +1069,7 @@ static int init_types(void)          comprehension_type = make_type("comprehension", &AST_type,                                         comprehension_fields, 3);          if (!comprehension_type) return 0; +        if (!add_attributes(comprehension_type, NULL, 0)) return 0;          excepthandler_type = make_type("excepthandler", &AST_type, NULL, 0);          if (!excepthandler_type) return 0;          if (!add_attributes(excepthandler_type, excepthandler_attributes, 2)) @@ -1044,16 +1077,21 @@ static int init_types(void)          ExceptHandler_type = make_type("ExceptHandler", excepthandler_type,                                         ExceptHandler_fields, 3);          if (!ExceptHandler_type) return 0; -        arguments_type = make_type("arguments", &AST_type, arguments_fields, 8); +        arguments_type = make_type("arguments", &AST_type, arguments_fields, 6);          if (!arguments_type) return 0; +        if (!add_attributes(arguments_type, NULL, 0)) return 0;          arg_type = make_type("arg", &AST_type, arg_fields, 2);          if (!arg_type) return 0; +        if (!add_attributes(arg_type, arg_attributes, 2)) return 0;          keyword_type = make_type("keyword", &AST_type, keyword_fields, 2);          if (!keyword_type) return 0; +        if (!add_attributes(keyword_type, NULL, 0)) return 0;          alias_type = make_type("alias", &AST_type, alias_fields, 2);          if (!alias_type) return 0; +        if (!add_attributes(alias_type, NULL, 0)) return 0;          withitem_type = make_type("withitem", &AST_type, withitem_fields, 2);          if (!withitem_type) return 0; +        if (!add_attributes(withitem_type, NULL, 0)) return 0;          initialized = 1;          return 1;  } @@ -1921,6 +1959,25 @@ Bytes(bytes s, int lineno, int col_offset, PyArena *arena)  }  expr_ty +NameConstant(singleton value, int lineno, int col_offset, PyArena *arena) +{ +        expr_ty p; +        if (!value) { +                PyErr_SetString(PyExc_ValueError, +                                "field value is required for NameConstant"); +                return NULL; +        } +        p = (expr_ty)PyArena_Malloc(arena, sizeof(*p)); +        if (!p) +                return NULL; +        p->kind = NameConstant_kind; +        p->v.NameConstant.value = value; +        p->lineno = lineno; +        p->col_offset = col_offset; +        return p; +} + +expr_ty  Ellipsis(int lineno, int col_offset, PyArena *arena)  {          expr_ty p; @@ -2175,9 +2232,8 @@ ExceptHandler(expr_ty type, identifier name, asdl_seq * body, int lineno, int  }  arguments_ty -arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation, -          asdl_seq * kwonlyargs, identifier kwarg, expr_ty kwargannotation, -          asdl_seq * defaults, asdl_seq * kw_defaults, PyArena *arena) +arguments(asdl_seq * args, arg_ty vararg, asdl_seq * kwonlyargs, asdl_seq * +          kw_defaults, arg_ty kwarg, asdl_seq * defaults, PyArena *arena)  {          arguments_ty p;          p = (arguments_ty)PyArena_Malloc(arena, sizeof(*p)); @@ -2185,12 +2241,10 @@ arguments(asdl_seq * args, identifier vararg, expr_ty varargannotation,                  return NULL;          p->args = args;          p->vararg = vararg; -        p->varargannotation = varargannotation;          p->kwonlyargs = kwonlyargs; +        p->kw_defaults = kw_defaults;          p->kwarg = kwarg; -        p->kwargannotation = kwargannotation;          p->defaults = defaults; -        p->kw_defaults = kw_defaults;          return p;  } @@ -2948,6 +3002,15 @@ ast2obj_expr(void* _o)                          goto failed;                  Py_DECREF(value);                  break; +        case NameConstant_kind: +                result = PyType_GenericNew(NameConstant_type, NULL, NULL); +                if (!result) goto failed; +                value = ast2obj_singleton(o->v.NameConstant.value); +                if (!value) goto failed; +                if (_PyObject_SetAttrId(result, &PyId_value, value) == -1) +                        goto failed; +                Py_DECREF(value); +                break;          case Ellipsis_kind:                  result = PyType_GenericNew(Ellipsis_type, NULL, NULL);                  if (!result) goto failed; @@ -3366,29 +3429,24 @@ ast2obj_arguments(void* _o)          if (_PyObject_SetAttrId(result, &PyId_args, value) == -1)                  goto failed;          Py_DECREF(value); -        value = ast2obj_identifier(o->vararg); +        value = ast2obj_arg(o->vararg);          if (!value) goto failed;          if (_PyObject_SetAttrId(result, &PyId_vararg, value) == -1)                  goto failed;          Py_DECREF(value); -        value = ast2obj_expr(o->varargannotation); -        if (!value) goto failed; -        if (_PyObject_SetAttrId(result, &PyId_varargannotation, value) == -1) -                goto failed; -        Py_DECREF(value);          value = ast2obj_list(o->kwonlyargs, ast2obj_arg);          if (!value) goto failed;          if (_PyObject_SetAttrId(result, &PyId_kwonlyargs, value) == -1)                  goto failed;          Py_DECREF(value); -        value = ast2obj_identifier(o->kwarg); +        value = ast2obj_list(o->kw_defaults, ast2obj_expr);          if (!value) goto failed; -        if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1) +        if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1)                  goto failed;          Py_DECREF(value); -        value = ast2obj_expr(o->kwargannotation); +        value = ast2obj_arg(o->kwarg);          if (!value) goto failed; -        if (_PyObject_SetAttrId(result, &PyId_kwargannotation, value) == -1) +        if (_PyObject_SetAttrId(result, &PyId_kwarg, value) == -1)                  goto failed;          Py_DECREF(value);          value = ast2obj_list(o->defaults, ast2obj_expr); @@ -3396,11 +3454,6 @@ ast2obj_arguments(void* _o)          if (_PyObject_SetAttrId(result, &PyId_defaults, value) == -1)                  goto failed;          Py_DECREF(value); -        value = ast2obj_list(o->kw_defaults, ast2obj_expr); -        if (!value) goto failed; -        if (_PyObject_SetAttrId(result, &PyId_kw_defaults, value) == -1) -                goto failed; -        Py_DECREF(value);          return result;  failed:          Py_XDECREF(value); @@ -3430,6 +3483,16 @@ ast2obj_arg(void* _o)          if (_PyObject_SetAttrId(result, &PyId_annotation, value) == -1)                  goto failed;          Py_DECREF(value); +        value = ast2obj_int(o->lineno); +        if (!value) goto failed; +        if (_PyObject_SetAttrId(result, &PyId_lineno, value) < 0) +                goto failed; +        Py_DECREF(value); +        value = ast2obj_int(o->col_offset); +        if (!value) goto failed; +        if (_PyObject_SetAttrId(result, &PyId_col_offset, value) < 0) +                goto failed; +        Py_DECREF(value);          return result;  failed:          Py_XDECREF(value); @@ -3796,7 +3859,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)                          PyErr_SetString(PyExc_TypeError, "required field \"decorator_list\" missing from FunctionDef");                          return 1;                  } -                if (_PyObject_HasAttrId(obj, &PyId_returns)) { +                if (exists_not_none(obj, &PyId_returns)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_returns);                          if (tmp == NULL) goto failed; @@ -3887,7 +3950,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)                          PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from ClassDef");                          return 1;                  } -                if (_PyObject_HasAttrId(obj, &PyId_starargs)) { +                if (exists_not_none(obj, &PyId_starargs)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_starargs);                          if (tmp == NULL) goto failed; @@ -3898,7 +3961,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)                  } else {                          starargs = NULL;                  } -                if (_PyObject_HasAttrId(obj, &PyId_kwargs)) { +                if (exists_not_none(obj, &PyId_kwargs)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);                          if (tmp == NULL) goto failed; @@ -3971,7 +4034,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)          if (isinstance) {                  expr_ty value; -                if (_PyObject_HasAttrId(obj, &PyId_value)) { +                if (exists_not_none(obj, &PyId_value)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_value);                          if (tmp == NULL) goto failed; @@ -4429,7 +4492,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)                  expr_ty exc;                  expr_ty cause; -                if (_PyObject_HasAttrId(obj, &PyId_exc)) { +                if (exists_not_none(obj, &PyId_exc)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_exc);                          if (tmp == NULL) goto failed; @@ -4440,7 +4503,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)                  } else {                          exc = NULL;                  } -                if (_PyObject_HasAttrId(obj, &PyId_cause)) { +                if (exists_not_none(obj, &PyId_cause)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_cause);                          if (tmp == NULL) goto failed; @@ -4590,7 +4653,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)                          PyErr_SetString(PyExc_TypeError, "required field \"test\" missing from Assert");                          return 1;                  } -                if (_PyObject_HasAttrId(obj, &PyId_msg)) { +                if (exists_not_none(obj, &PyId_msg)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_msg);                          if (tmp == NULL) goto failed; @@ -4650,7 +4713,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)                  asdl_seq* names;                  int level; -                if (_PyObject_HasAttrId(obj, &PyId_module)) { +                if (exists_not_none(obj, &PyId_module)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_module);                          if (tmp == NULL) goto failed; @@ -4686,7 +4749,7 @@ obj2ast_stmt(PyObject* obj, stmt_ty* out, PyArena* arena)                          PyErr_SetString(PyExc_TypeError, "required field \"names\" missing from ImportFrom");                          return 1;                  } -                if (_PyObject_HasAttrId(obj, &PyId_level)) { +                if (exists_not_none(obj, &PyId_level)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_level);                          if (tmp == NULL) goto failed; @@ -5405,7 +5468,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)          if (isinstance) {                  expr_ty value; -                if (_PyObject_HasAttrId(obj, &PyId_value)) { +                if (exists_not_none(obj, &PyId_value)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_value);                          if (tmp == NULL) goto failed; @@ -5592,7 +5655,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)                          PyErr_SetString(PyExc_TypeError, "required field \"keywords\" missing from Call");                          return 1;                  } -                if (_PyObject_HasAttrId(obj, &PyId_starargs)) { +                if (exists_not_none(obj, &PyId_starargs)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_starargs);                          if (tmp == NULL) goto failed; @@ -5603,7 +5666,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)                  } else {                          starargs = NULL;                  } -                if (_PyObject_HasAttrId(obj, &PyId_kwargs)) { +                if (exists_not_none(obj, &PyId_kwargs)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_kwargs);                          if (tmp == NULL) goto failed; @@ -5688,6 +5751,29 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)                  if (*out == NULL) goto failed;                  return 0;          } +        isinstance = PyObject_IsInstance(obj, (PyObject*)NameConstant_type); +        if (isinstance == -1) { +                return 1; +        } +        if (isinstance) { +                singleton value; + +                if (_PyObject_HasAttrId(obj, &PyId_value)) { +                        int res; +                        tmp = _PyObject_GetAttrId(obj, &PyId_value); +                        if (tmp == NULL) goto failed; +                        res = obj2ast_singleton(tmp, &value, arena); +                        if (res != 0) goto failed; +                        Py_XDECREF(tmp); +                        tmp = NULL; +                } else { +                        PyErr_SetString(PyExc_TypeError, "required field \"value\" missing from NameConstant"); +                        return 1; +                } +                *out = NameConstant(value, lineno, col_offset, arena); +                if (*out == NULL) goto failed; +                return 0; +        }          isinstance = PyObject_IsInstance(obj, (PyObject*)Ellipsis_type);          if (isinstance == -1) {                  return 1; @@ -6051,7 +6137,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)                  expr_ty upper;                  expr_ty step; -                if (_PyObject_HasAttrId(obj, &PyId_lower)) { +                if (exists_not_none(obj, &PyId_lower)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_lower);                          if (tmp == NULL) goto failed; @@ -6062,7 +6148,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)                  } else {                          lower = NULL;                  } -                if (_PyObject_HasAttrId(obj, &PyId_upper)) { +                if (exists_not_none(obj, &PyId_upper)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_upper);                          if (tmp == NULL) goto failed; @@ -6073,7 +6159,7 @@ obj2ast_slice(PyObject* obj, slice_ty* out, PyArena* arena)                  } else {                          upper = NULL;                  } -                if (_PyObject_HasAttrId(obj, &PyId_step)) { +                if (exists_not_none(obj, &PyId_step)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_step);                          if (tmp == NULL) goto failed; @@ -6528,7 +6614,7 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)                  identifier name;                  asdl_seq* body; -                if (_PyObject_HasAttrId(obj, &PyId_type)) { +                if (exists_not_none(obj, &PyId_type)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_type);                          if (tmp == NULL) goto failed; @@ -6539,7 +6625,7 @@ obj2ast_excepthandler(PyObject* obj, excepthandler_ty* out, PyArena* arena)                  } else {                          type = NULL;                  } -                if (_PyObject_HasAttrId(obj, &PyId_name)) { +                if (exists_not_none(obj, &PyId_name)) {                          int res;                          tmp = _PyObject_GetAttrId(obj, &PyId_name);                          if (tmp == NULL) goto failed; @@ -6592,13 +6678,11 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)  {          PyObject* tmp = NULL;          asdl_seq* args; -        identifier vararg; -        expr_ty varargannotation; +        arg_ty vararg;          asdl_seq* kwonlyargs; -        identifier kwarg; -        expr_ty kwargannotation; -        asdl_seq* defaults;          asdl_seq* kw_defaults; +        arg_ty kwarg; +        asdl_seq* defaults;          if (_PyObject_HasAttrId(obj, &PyId_args)) {                  int res; @@ -6625,28 +6709,17 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)                  PyErr_SetString(PyExc_TypeError, "required field \"args\" missing from arguments");                  return 1;          } -        if (_PyObject_HasAttrId(obj, &PyId_vararg)) { +        if (exists_not_none(obj, &PyId_vararg)) {                  int res;                  tmp = _PyObject_GetAttrId(obj, &PyId_vararg);                  if (tmp == NULL) goto failed; -                res = obj2ast_identifier(tmp, &vararg, arena); +                res = obj2ast_arg(tmp, &vararg, arena);                  if (res != 0) goto failed;                  Py_XDECREF(tmp);                  tmp = NULL;          } else {                  vararg = NULL;          } -        if (_PyObject_HasAttrId(obj, &PyId_varargannotation)) { -                int res; -                tmp = _PyObject_GetAttrId(obj, &PyId_varargannotation); -                if (tmp == NULL) goto failed; -                res = obj2ast_expr(tmp, &varargannotation, arena); -                if (res != 0) goto failed; -                Py_XDECREF(tmp); -                tmp = NULL; -        } else { -                varargannotation = NULL; -        }          if (_PyObject_HasAttrId(obj, &PyId_kwonlyargs)) {                  int res;                  Py_ssize_t len; @@ -6672,27 +6745,41 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)                  PyErr_SetString(PyExc_TypeError, "required field \"kwonlyargs\" missing from arguments");                  return 1;          } -        if (_PyObject_HasAttrId(obj, &PyId_kwarg)) { +        if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) {                  int res; -                tmp = _PyObject_GetAttrId(obj, &PyId_kwarg); +                Py_ssize_t len; +                Py_ssize_t i; +                tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults);                  if (tmp == NULL) goto failed; -                res = obj2ast_identifier(tmp, &kwarg, arena); -                if (res != 0) goto failed; +                if (!PyList_Check(tmp)) { +                        PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name); +                        goto failed; +                } +                len = PyList_GET_SIZE(tmp); +                kw_defaults = asdl_seq_new(len, arena); +                if (kw_defaults == NULL) goto failed; +                for (i = 0; i < len; i++) { +                        expr_ty value; +                        res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); +                        if (res != 0) goto failed; +                        asdl_seq_SET(kw_defaults, i, value); +                }                  Py_XDECREF(tmp);                  tmp = NULL;          } else { -                kwarg = NULL; +                PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments"); +                return 1;          } -        if (_PyObject_HasAttrId(obj, &PyId_kwargannotation)) { +        if (exists_not_none(obj, &PyId_kwarg)) {                  int res; -                tmp = _PyObject_GetAttrId(obj, &PyId_kwargannotation); +                tmp = _PyObject_GetAttrId(obj, &PyId_kwarg);                  if (tmp == NULL) goto failed; -                res = obj2ast_expr(tmp, &kwargannotation, arena); +                res = obj2ast_arg(tmp, &kwarg, arena);                  if (res != 0) goto failed;                  Py_XDECREF(tmp);                  tmp = NULL;          } else { -                kwargannotation = NULL; +                kwarg = NULL;          }          if (_PyObject_HasAttrId(obj, &PyId_defaults)) {                  int res; @@ -6719,33 +6806,8 @@ obj2ast_arguments(PyObject* obj, arguments_ty* out, PyArena* arena)                  PyErr_SetString(PyExc_TypeError, "required field \"defaults\" missing from arguments");                  return 1;          } -        if (_PyObject_HasAttrId(obj, &PyId_kw_defaults)) { -                int res; -                Py_ssize_t len; -                Py_ssize_t i; -                tmp = _PyObject_GetAttrId(obj, &PyId_kw_defaults); -                if (tmp == NULL) goto failed; -                if (!PyList_Check(tmp)) { -                        PyErr_Format(PyExc_TypeError, "arguments field \"kw_defaults\" must be a list, not a %.200s", tmp->ob_type->tp_name); -                        goto failed; -                } -                len = PyList_GET_SIZE(tmp); -                kw_defaults = asdl_seq_new(len, arena); -                if (kw_defaults == NULL) goto failed; -                for (i = 0; i < len; i++) { -                        expr_ty value; -                        res = obj2ast_expr(PyList_GET_ITEM(tmp, i), &value, arena); -                        if (res != 0) goto failed; -                        asdl_seq_SET(kw_defaults, i, value); -                } -                Py_XDECREF(tmp); -                tmp = NULL; -        } else { -                PyErr_SetString(PyExc_TypeError, "required field \"kw_defaults\" missing from arguments"); -                return 1; -        } -        *out = arguments(args, vararg, varargannotation, kwonlyargs, kwarg, -                         kwargannotation, defaults, kw_defaults, arena); +        *out = arguments(args, vararg, kwonlyargs, kw_defaults, kwarg, +                         defaults, arena);          return 0;  failed:          Py_XDECREF(tmp); @@ -6771,7 +6833,7 @@ obj2ast_arg(PyObject* obj, arg_ty* out, PyArena* arena)                  PyErr_SetString(PyExc_TypeError, "required field \"arg\" missing from arg");                  return 1;          } -        if (_PyObject_HasAttrId(obj, &PyId_annotation)) { +        if (exists_not_none(obj, &PyId_annotation)) {                  int res;                  tmp = _PyObject_GetAttrId(obj, &PyId_annotation);                  if (tmp == NULL) goto failed; @@ -6846,7 +6908,7 @@ obj2ast_alias(PyObject* obj, alias_ty* out, PyArena* arena)                  PyErr_SetString(PyExc_TypeError, "required field \"name\" missing from alias");                  return 1;          } -        if (_PyObject_HasAttrId(obj, &PyId_asname)) { +        if (exists_not_none(obj, &PyId_asname)) {                  int res;                  tmp = _PyObject_GetAttrId(obj, &PyId_asname);                  if (tmp == NULL) goto failed; @@ -6883,7 +6945,7 @@ obj2ast_withitem(PyObject* obj, withitem_ty* out, PyArena* arena)                  PyErr_SetString(PyExc_TypeError, "required field \"context_expr\" missing from withitem");                  return 1;          } -        if (_PyObject_HasAttrId(obj, &PyId_optional_vars)) { +        if (exists_not_none(obj, &PyId_optional_vars)) {                  int res;                  tmp = _PyObject_GetAttrId(obj, &PyId_optional_vars);                  if (tmp == NULL) goto failed; @@ -6915,7 +6977,7 @@ PyInit__ast(void)          d = PyModule_GetDict(m);          if (PyDict_SetItemString(d, "AST", (PyObject*)&AST_type) < 0) return              NULL; -        if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0) +        if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)                  return NULL;          if (PyDict_SetItemString(d, "mod", (PyObject*)mod_type) < 0) return              NULL; @@ -7008,6 +7070,8 @@ PyInit__ast(void)              NULL;          if (PyDict_SetItemString(d, "Bytes", (PyObject*)Bytes_type) < 0) return              NULL; +        if (PyDict_SetItemString(d, "NameConstant", +            (PyObject*)NameConstant_type) < 0) return NULL;          if (PyDict_SetItemString(d, "Ellipsis", (PyObject*)Ellipsis_type) < 0)              return NULL;          if (PyDict_SetItemString(d, "Attribute", (PyObject*)Attribute_type) <  | 
