diff options
Diffstat (limited to 'Python/ast.c')
| -rw-r--r-- | Python/ast.c | 159 | 
1 files changed, 89 insertions, 70 deletions
| diff --git a/Python/ast.c b/Python/ast.c index 9a0b064..073d59f 100644 --- a/Python/ast.c +++ b/Python/ast.c @@ -109,22 +109,14 @@ validate_arguments(arguments_ty args)  {      if (!validate_args(args->args))          return 0; -    if (args->varargannotation) { -        if (!args->vararg) { -            PyErr_SetString(PyExc_ValueError, "varargannotation but no vararg on arguments"); -            return 0; -        } -        if (!validate_expr(args->varargannotation, Load)) +    if (args->vararg && args->vararg->annotation +        && !validate_expr(args->vararg->annotation, Load)) {              return 0;      }      if (!validate_args(args->kwonlyargs))          return 0; -    if (args->kwargannotation) { -        if (!args->kwarg) { -            PyErr_SetString(PyExc_ValueError, "kwargannotation but no kwarg on arguments"); -            return 0; -        } -        if (!validate_expr(args->kwargannotation, Load)) +    if (args->kwarg && args->kwarg->annotation  +        && !validate_expr(args->kwarg->annotation, Load)) {              return 0;      }      if (asdl_seq_LEN(args->defaults) > asdl_seq_LEN(args->args)) { @@ -282,6 +274,7 @@ validate_expr(expr_ty exp, expr_context_ty ctx)          return validate_exprs(exp->v.Tuple.elts, ctx, 0);      /* These last cases don't have any checking. */      case Name_kind: +    case NameConstant_kind:      case Ellipsis_kind:          return 1;      default: @@ -498,7 +491,7 @@ PyAST_Validate(mod_ty mod)  struct compiling {      char *c_encoding; /* source encoding */      PyArena *c_arena; /* arena for allocating memeory */ -    const char *c_filename; /* filename */ +    PyObject *c_filename; /* filename */      PyObject *c_normalize; /* Normalization function from unicodedata. */      PyObject *c_normalize_args; /* Normalization argument tuple. */  }; @@ -567,7 +560,10 @@ new_identifier(const char *n, struct compiling *c)          id = id2;      }      PyUnicode_InternInPlace(&id); -    PyArena_AddPyObject(c->c_arena, id); +    if (PyArena_AddPyObject(c->c_arena, id) < 0) { +        Py_DECREF(id); +        return NULL; +    }      return id;  } @@ -577,24 +573,13 @@ static int  ast_error(struct compiling *c, const node *n, const char *errmsg)  {      PyObject *value, *errstr, *loc, *tmp; -    PyObject *filename_obj; -    loc = PyErr_ProgramText(c->c_filename, LINENO(n)); +    loc = PyErr_ProgramTextObject(c->c_filename, LINENO(n));      if (!loc) {          Py_INCREF(Py_None);          loc = Py_None;      } -    if (c->c_filename) { -        filename_obj = PyUnicode_DecodeFSDefault(c->c_filename); -        if (!filename_obj) { -            Py_DECREF(loc); -            return 0; -        } -    } else { -        Py_INCREF(Py_None); -        filename_obj = Py_None; -    } -    tmp = Py_BuildValue("(NiiN)", filename_obj, LINENO(n), n->n_col_offset, loc); +    tmp = Py_BuildValue("(OiiN)", c->c_filename, LINENO(n), n->n_col_offset, loc);      if (!tmp)          return 0;      errstr = PyUnicode_FromString(errmsg); @@ -677,8 +662,8 @@ num_stmts(const node *n)  */  mod_ty -PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename, -               PyArena *arena) +PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags, +                     PyObject *filename, PyArena *arena)  {      int i, j, k, num;      asdl_seq *stmts = NULL; @@ -688,6 +673,7 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,      mod_ty res = NULL;      c.c_arena = arena; +    /* borrowed reference */      c.c_filename = filename;      c.c_normalize = c.c_normalize_args = NULL;      if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) { @@ -801,6 +787,21 @@ PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename,      return res;  } +mod_ty +PyAST_FromNode(const node *n, PyCompilerFlags *flags, const char *filename_str, +               PyArena *arena) +{ +    mod_ty mod; +    PyObject *filename; +    filename = PyUnicode_DecodeFSDefault(filename_str); +    if (filename == NULL) +        return NULL; +    mod = PyAST_FromNodeObject(n, flags, filename, arena); +    Py_DECREF(filename); +    return mod; + +} +  /* Return the AST repr. of the operator represented as syntax (|, ^, etc.)  */ @@ -903,7 +904,7 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)              break;          case Name_kind:              if (ctx == Store) { -                if (forbidden_name(c, e->v.Name.id, n, 1)) +                if (forbidden_name(c, e->v.Name.id, n, 0))                      return 0; /* forbidden_name() calls ast_error() */              }              e->v.Name.ctx = ctx; @@ -955,6 +956,9 @@ set_context(struct compiling *c, expr_ty e, expr_context_ty ctx, const node *n)          case Bytes_kind:              expr_name = "literal";              break; +        case NameConstant_kind: +            expr_name = "keyword"; +            break;          case Ellipsis_kind:              expr_name = "Ellipsis";              break; @@ -1119,6 +1123,7 @@ ast_for_arg(struct compiling *c, const node *n)      identifier name;      expr_ty annotation = NULL;      node *ch; +    arg_ty tmp;      assert(TYPE(n) == tfpdef || TYPE(n) == vfpdef);      ch = CHILD(n, 0); @@ -1134,7 +1139,13 @@ ast_for_arg(struct compiling *c, const node *n)              return NULL;      } -    return arg(name, annotation, c->c_arena); +    tmp = arg(name, annotation, c->c_arena); +    if (!tmp) +        return NULL; + +    tmp->lineno = LINENO(n); +    tmp->col_offset = n->n_col_offset; +    return tmp;  }  /* returns -1 if failed to handle keyword only arguments @@ -1230,15 +1241,13 @@ ast_for_arguments(struct compiling *c, const node *n)      int i, j, k, nposargs = 0, nkwonlyargs = 0;      int nposdefaults = 0, found_default = 0;      asdl_seq *posargs, *posdefaults, *kwonlyargs, *kwdefaults; -    identifier vararg = NULL, kwarg = NULL; +    arg_ty vararg = NULL, kwarg = NULL;      arg_ty arg; -    expr_ty varargannotation = NULL, kwargannotation = NULL;      node *ch;      if (TYPE(n) == parameters) {          if (NCH(n) == 2) /* () as argument list */ -            return arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, -                             NULL, c->c_arena); +            return arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);          n = CHILD(n, 1);      }      assert(TYPE(n) == typedargslist || TYPE(n) == varargslist); @@ -1344,17 +1353,10 @@ ast_for_arguments(struct compiling *c, const node *n)                      i = res; /* res has new position to process */                  }                  else { -                    vararg = NEW_IDENTIFIER(CHILD(ch, 0)); +                    vararg = ast_for_arg(c, ch);                      if (!vararg)                          return NULL; -                    if (forbidden_name(c, vararg, CHILD(ch, 0), 0)) -                        return NULL; -                    if (NCH(ch) > 1) { -                        /* there is an annotation on the vararg */ -                        varargannotation = ast_for_expr(c, CHILD(ch, 2)); -                        if (!varargannotation) -                            return NULL; -                    } +                      i += 3;                      if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef                                      || TYPE(CHILD(n, i)) == vfpdef)) { @@ -1369,17 +1371,9 @@ ast_for_arguments(struct compiling *c, const node *n)              case DOUBLESTAR:                  ch = CHILD(n, i+1);  /* tfpdef */                  assert(TYPE(ch) == tfpdef || TYPE(ch) == vfpdef); -                kwarg = NEW_IDENTIFIER(CHILD(ch, 0)); +                kwarg = ast_for_arg(c, ch);                  if (!kwarg)                      return NULL; -                if (NCH(ch) > 1) { -                    /* there is an annotation on the kwarg */ -                    kwargannotation = ast_for_expr(c, CHILD(ch, 2)); -                    if (!kwargannotation) -                        return NULL; -                } -                if (forbidden_name(c, kwarg, CHILD(ch, 0), 0)) -                    return NULL;                  i += 3;                  break;              default: @@ -1389,8 +1383,7 @@ ast_for_arguments(struct compiling *c, const node *n)                  return NULL;          }      } -    return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg, -                    kwargannotation, posdefaults, kwdefaults, c->c_arena); +    return arguments(posargs, vararg, kwonlyargs, kwdefaults, kwarg, posdefaults, c->c_arena);  }  static expr_ty @@ -1555,8 +1548,7 @@ ast_for_lambdef(struct compiling *c, const node *n)      expr_ty expression;      if (NCH(n) == 3) { -        args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, NULL, -                         NULL, c->c_arena); +        args = arguments(NULL, NULL, NULL, NULL, NULL, NULL, c->c_arena);          if (!args)              return NULL;          expression = ast_for_expr(c, CHILD(n, 2)); @@ -1819,11 +1811,21 @@ ast_for_atom(struct compiling *c, const node *n)      switch (TYPE(ch)) {      case NAME: { -        /* All names start in Load context, but may later be -           changed. */ -        PyObject *name = NEW_IDENTIFIER(ch); +        PyObject *name; +        const char *s = STR(ch); +        size_t len = strlen(s); +        if (len >= 4 && len <= 5) { +            if (!strcmp(s, "None")) +                return NameConstant(Py_None, LINENO(n), n->n_col_offset, c->c_arena); +            if (!strcmp(s, "True")) +                return NameConstant(Py_True, LINENO(n), n->n_col_offset, c->c_arena); +            if (!strcmp(s, "False")) +                return NameConstant(Py_False, LINENO(n), n->n_col_offset, c->c_arena); +        } +        name = new_identifier(s, c);          if (!name)              return NULL; +        /* All names start in Load context, but may later be changed. */          return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);      }      case STRING: { @@ -1848,12 +1850,15 @@ ast_for_atom(struct compiling *c, const node *n)                  }                  ast_error(c, n, buf);                  Py_DECREF(type); -                Py_DECREF(value); +                Py_XDECREF(value);                  Py_XDECREF(tback);              }              return NULL;          } -        PyArena_AddPyObject(c->c_arena, str); +        if (PyArena_AddPyObject(c->c_arena, str) < 0) { +            Py_DECREF(str); +            return NULL; +        }          if (bytesmode)              return Bytes(str, LINENO(n), n->n_col_offset, c->c_arena);          else @@ -1864,7 +1869,10 @@ ast_for_atom(struct compiling *c, const node *n)          if (!pynum)              return NULL; -        PyArena_AddPyObject(c->c_arena, pynum); +        if (PyArena_AddPyObject(c->c_arena, pynum) < 0) { +            Py_DECREF(pynum); +            return NULL; +        }          return Num(pynum, LINENO(n), n->n_col_offset, c->c_arena);      }      case ELLIPSIS: /* Ellipsis */ @@ -2093,15 +2101,22 @@ ast_for_trailer(struct compiling *c, const node *n, expr_ty left_expr)          if (NCH(n) == 2)              return Call(left_expr, NULL, NULL, NULL, NULL, LINENO(n),                          n->n_col_offset, c->c_arena); -        else -            return ast_for_call(c, CHILD(n, 1), left_expr); +        else { +            expr_ty tmp = ast_for_call(c, CHILD(n, 1), left_expr); +            if (!tmp) +                return NULL; + +            tmp->lineno = LINENO(n); +            tmp->col_offset = n->n_col_offset; +            return tmp; +        }      }      else if (TYPE(CHILD(n, 0)) == DOT ) {          PyObject *attr_id = NEW_IDENTIFIER(CHILD(n, 1));          if (!attr_id)              return NULL;          return Attribute(left_expr, attr_id, Load, -                         LINENO(n), n->n_col_offset, c->c_arena); +                         LINENO(CHILD(n, 1)), CHILD(n, 1)->n_col_offset, c->c_arena);      }      else {          REQ(CHILD(n, 0), LSQB); @@ -2202,8 +2217,6 @@ ast_for_power(struct compiling *c, const node *n)          tmp = ast_for_trailer(c, ch, e);          if (!tmp)              return NULL; -        tmp->lineno = e->lineno; -        tmp->col_offset = e->col_offset;          e = tmp;      }      if (TYPE(CHILD(n, NCH(n) - 1)) == factor) { @@ -2846,13 +2859,19 @@ alias_for_import_name(struct compiling *c, const node *n, int store)                      return NULL;                  str = uni;                  PyUnicode_InternInPlace(&str); -                PyArena_AddPyObject(c->c_arena, str); +                if (PyArena_AddPyObject(c->c_arena, str) < 0) { +                    Py_DECREF(str); +                    return NULL; +                }                  return alias(str, NULL, c->c_arena);              }              break;          case STAR:              str = PyUnicode_InternFromString("*"); -            PyArena_AddPyObject(c->c_arena, str); +            if (PyArena_AddPyObject(c->c_arena, str) < 0) { +                Py_DECREF(str); +                return NULL; +            }              return alias(str, NULL, c->c_arena);          default:              PyErr_Format(PyExc_SystemError, | 
