Commit ba7b7577 authored by Michael R. Crusoe's avatar Michael R. Crusoe 🏳️‍🌈
Browse files

New upstream version 1.1.1

parent 378a3e92
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
Metadata-Version: 1.1
Name: typed-ast
Version: 1.1.0
Version: 1.1.1
Summary: a fork of Python 2 and 3 ast modules with type comment support
Home-page: https://github.com/python/typed_ast
Author: David Fisher

README.md

0 → 100644
+56 −0
Original line number Diff line number Diff line
# Typed AST

[![Build Status](https://travis-ci.org/python/typed_ast.svg?branch=master)](https://travis-ci.org/python/typed_ast)
[![Chat at https://gitter.im/python/typed_ast](https://badges.gitter.im/python/typed_ast.svg)](https://gitter.im/python/typed_ast)

`typed_ast` is a Python 3 package that provides a Python 2.7 and Python 3
parser similar to the standard `ast` library.  Unlike `ast`, the parsers in
`typed_ast` include [PEP 484](https://www.python.org/dev/peps/pep-0484/) type
comments and are independent of the version of Python under which they are run.
The `typed_ast` parsers produce the standard Python AST (plus type comments),
and are both fast and correct, as they are based on the CPython 2.7 and 3.6
parsers.  `typed_ast` runs on Python 3.3-3.6 on Linux, OS X and Windows.

## Development Philosophy

This project is a drop-in replacement for the builtin `ast` module.  It is
intended to be bug-for-bug compatible and behave identically, except for the
presence of a few additional fields on the returned classes and a few
additional optional arguments to the `parse` call.  Therefore, `typed_ast` will
not accept any bugfixes for bugs in `ast` -- they should be fixed upstream
instead.  To avoid feature bloat, any new features for `typed_ast` should have
the potential to be broadly useful and not be built just for one niche usecase
or in a manner such that only one project can use them.

## Submodules
### ast3
The `ast3` parser produces the AST from the latest version of Python 3
(currently Python 3.6).  When new versions of Python 3 are released, it will be
updated to match any changes in their AST.  (For rationale and technical
details, see [here](update_process.md).)  The AST it currently produces is described in
[ast3/Parser/Python.asdl](ast3/Parser/Python.asdl).  If you wish to limit
parsing to older versions of Python 3, `ast3` can be configured to to give a
SyntaxError for new syntax features introduced beyond a given Python version.
For more information, see the module docstring in
[typed\_ast/ast3.py](typed_ast/ast3.py).

### ast27
The `ast27` parser tracks the standard Python 2.7 AST, which is expected to
never receive further updates. The AST it produces is described in
[ast27/Parser/Python.asdl](ast27/Parser/Python.asdl).  For more information,
see the module docstring in [typed\_ast/ast27.py](typed_ast/ast27.py).

### conversions
`typed_ast` also provides a `conversions` module which converts `ast27` ASTs
into `ast3` ASTs.  This functionality is somewhat experimental, however.  For
more information, see the `py2to3` docstring in
[typed\_ast/conversions](typed_ast/conversions.py).


Note: as these parsers consider type comments part of the grammar, incorrectly
placed type comments are considered syntax errors.

## Updates and Releases
To update `typed_ast` for new major Python releases, see [`update_process.md`](update_process.md).

To make a new `typed_ast` release, see [`release_process.md`](release_process.md).
+2 −2
Original line number Diff line number Diff line
@@ -286,7 +286,7 @@ struct _expr {
                
                struct {
                        string s;
                        int has_b;
                        string kind;
                } Str;
                
                struct {
@@ -505,7 +505,7 @@ expr_ty _Ta27_Repr(expr_ty value, int lineno, int col_offset, PyArena *arena);
#define Num(a0, a1, a2, a3) _Ta27_Num(a0, a1, a2, a3)
expr_ty _Ta27_Num(object n, int lineno, int col_offset, PyArena *arena);
#define Str(a0, a1, a2, a3, a4) _Ta27_Str(a0, a1, a2, a3, a4)
expr_ty _Ta27_Str(string s, int has_b, int lineno, int col_offset, PyArena *arena);
expr_ty _Ta27_Str(string s, string kind, int lineno, int col_offset, PyArena *arena);
#define Attribute(a0, a1, a2, a3, a4, a5) _Ta27_Attribute(a0, a1, a2, a3, a4, a5)
expr_ty _Ta27_Attribute(expr_ty value, identifier attr, expr_context_ty ctx, int lineno, int
                        col_offset, PyArena *arena);
+17 −11
Original line number Diff line number Diff line
@@ -252,7 +252,7 @@ static char *Num_fields[]={
static PyTypeObject *Str_type;
static char *Str_fields[]={
        "s",
        "has_b",
        "kind",
};
static PyTypeObject *Attribute_type;
static char *Attribute_fields[]={
@@ -1850,7 +1850,7 @@ Num(object n, int lineno, int col_offset, PyArena *arena)
}

expr_ty
Str(string s, int has_b, int lineno, int col_offset, PyArena *arena)
Str(string s, string kind, int lineno, int col_offset, PyArena *arena)
{
        expr_ty p;
        if (!s) {
@@ -1858,12 +1858,17 @@ Str(string s, int has_b, int lineno, int col_offset, PyArena *arena)
                                "field s is required for Str");
                return NULL;
        }
        if (!kind) {
                PyErr_SetString(PyExc_ValueError,
                                "field kind is required for Str");
                return NULL;
        }
        p = (expr_ty)PyArena_Malloc(arena, sizeof(*p));
        if (!p)
                return NULL;
        p->kind = Str_kind;
        p->v.Str.s = s;
        p->v.Str.has_b = has_b;
        p->v.Str.kind = kind;
        p->lineno = lineno;
        p->col_offset = col_offset;
        return p;
@@ -2889,9 +2894,9 @@ ast2obj_expr(void* _o)
                if (PyObject_SetAttrString(result, "s", value) == -1)
                        goto failed;
                Py_DECREF(value);
                value = ast2obj_int(o->v.Str.has_b);
                value = ast2obj_string(o->v.Str.kind);
                if (!value) goto failed;
                if (PyObject_SetAttrString(result, "has_b", value) == -1)
                if (PyObject_SetAttrString(result, "kind", value) == -1)
                        goto failed;
                Py_DECREF(value);
                break;
@@ -5714,7 +5719,7 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
        }
        if (isinstance) {
                string s;
                int has_b;
                string kind;

                if (PyObject_HasAttrString(obj, "s")) {
                        int res;
@@ -5728,18 +5733,19 @@ obj2ast_expr(PyObject* obj, expr_ty* out, PyArena* arena)
                        PyErr_SetString(PyExc_TypeError, "required field \"s\" missing from Str");
                        return 1;
                }
                if (PyObject_HasAttrString(obj, "has_b")) {
                if (PyObject_HasAttrString(obj, "kind")) {
                        int res;
                        tmp = PyObject_GetAttrString(obj, "has_b");
                        tmp = PyObject_GetAttrString(obj, "kind");
                        if (tmp == NULL) goto failed;
                        res = obj2ast_int(tmp, &has_b, arena);
                        res = obj2ast_string(tmp, &kind, arena);
                        if (res != 0) goto failed;
                        Py_XDECREF(tmp);
                        tmp = NULL;
                } else {
                        has_b = 0;
                        PyErr_SetString(PyExc_TypeError, "required field \"kind\" missing from Str");
                        return 1;
                }
                *out = Str(s, has_b, lineno, col_offset, arena);
                *out = Str(s, kind, lineno, col_offset, arena);
                if (*out == NULL) goto failed;
                return 0;
        }
+14 −7
Original line number Diff line number Diff line
@@ -1498,10 +1498,20 @@ ast_for_atom(struct compiling *c, const node *n)
        return Name(name, Load, LINENO(n), n->n_col_offset, c->c_arena);
    }
    case STRING: {
        PyObject *str = parsestrplus(c, n);
        const char *s = STR(CHILD(n, 0));
        PyObject *kind, *str = parsestrplus(c, n);
        const char *raw, *s = STR(CHILD(n, 0));
        int quote = Py_CHARMASK(*s);
        int has_b = 0;
        /* currently Python allows up to 2 string modifiers */
        char *ch, s_kind[3] = {0, 0, 0};
        ch = s_kind;
        raw = s;
        while (*raw && *raw != '\'' && *raw != '"') {
            *ch++ = *raw++;
        }
        kind = PyUnicode_FromString(s_kind);
        if (!kind) {
            return NULL;
        }
        if (!str) {
#ifdef Py_USING_UNICODE
            if (PyErr_ExceptionMatches(PyExc_UnicodeError)){
@@ -1526,10 +1536,7 @@ ast_for_atom(struct compiling *c, const node *n)
            return NULL;
        }
        PyArena_AddPyObject(c->c_arena, str);
        if (quote == 'b' || quote == 'B') {
            has_b = 1;
        }
        return Str(str, has_b, LINENO(n), n->n_col_offset, c->c_arena);
        return Str(str, kind, LINENO(n), n->n_col_offset, c->c_arena);
    }
    case NUMBER: {
        PyObject *pynum = parsenumber(c, STR(ch));
Loading