Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Python/future.c

    r2 r388  
    99
    1010#define UNDEFINED_FUTURE_FEATURE "future feature %.100s is not defined"
     11#define ERR_LATE_FUTURE \
     12"from __future__ imports must occur at the beginning of the file"
    1113
    1214static int
    1315future_check_features(PyFutureFeatures *ff, stmt_ty s, const char *filename)
    1416{
    15         int i;
    16         asdl_seq *names;
     17    int i;
     18    asdl_seq *names;
    1719
    18         assert(s->kind == ImportFrom_kind);
     20    assert(s->kind == ImportFrom_kind);
    1921
    20         names = s->v.ImportFrom.names;
    21         for (i = 0; i < asdl_seq_LEN(names); i++) {
    22                 alias_ty name = (alias_ty)asdl_seq_GET(names, i);
    23                 const char *feature = PyString_AsString(name->name);
    24                 if (!feature)
    25                         return 0;
    26                 if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
    27                         continue;
    28                 } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
    29                         continue;
    30                 } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
    31                         ff->ff_features |= CO_FUTURE_DIVISION;
    32                 } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
    33                         ff->ff_features |= CO_FUTURE_ABSOLUTE_IMPORT;
    34                 } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
    35                         ff->ff_features |= CO_FUTURE_WITH_STATEMENT;
    36                 } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
    37                         ff->ff_features |= CO_FUTURE_PRINT_FUNCTION;
    38                 } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
    39                         ff->ff_features |= CO_FUTURE_UNICODE_LITERALS;
    40                 } else if (strcmp(feature, "braces") == 0) {
    41                         PyErr_SetString(PyExc_SyntaxError,
    42                                         "not a chance");
    43                         PyErr_SyntaxLocation(filename, s->lineno);
    44                         return 0;
    45                 } else {
    46                         PyErr_Format(PyExc_SyntaxError,
    47                                      UNDEFINED_FUTURE_FEATURE, feature);
    48                         PyErr_SyntaxLocation(filename, s->lineno);
    49                         return 0;
    50                 }
    51         }
    52         return 1;
     22    names = s->v.ImportFrom.names;
     23    for (i = 0; i < asdl_seq_LEN(names); i++) {
     24        alias_ty name = (alias_ty)asdl_seq_GET(names, i);
     25        const char *feature = PyString_AsString(name->name);
     26        if (!feature)
     27            return 0;
     28        if (strcmp(feature, FUTURE_NESTED_SCOPES) == 0) {
     29            continue;
     30        } else if (strcmp(feature, FUTURE_GENERATORS) == 0) {
     31            continue;
     32        } else if (strcmp(feature, FUTURE_DIVISION) == 0) {
     33            ff->ff_features |= CO_FUTURE_DIVISION;
     34        } else if (strcmp(feature, FUTURE_ABSOLUTE_IMPORT) == 0) {
     35            ff->ff_features |= CO_FUTURE_ABSOLUTE_IMPORT;
     36        } else if (strcmp(feature, FUTURE_WITH_STATEMENT) == 0) {
     37            ff->ff_features |= CO_FUTURE_WITH_STATEMENT;
     38        } else if (strcmp(feature, FUTURE_PRINT_FUNCTION) == 0) {
     39            ff->ff_features |= CO_FUTURE_PRINT_FUNCTION;
     40        } else if (strcmp(feature, FUTURE_UNICODE_LITERALS) == 0) {
     41            ff->ff_features |= CO_FUTURE_UNICODE_LITERALS;
     42        } else if (strcmp(feature, "braces") == 0) {
     43            PyErr_SetString(PyExc_SyntaxError,
     44                            "not a chance");
     45            PyErr_SyntaxLocation(filename, s->lineno);
     46            return 0;
     47        } else {
     48            PyErr_Format(PyExc_SyntaxError,
     49                         UNDEFINED_FUTURE_FEATURE, feature);
     50            PyErr_SyntaxLocation(filename, s->lineno);
     51            return 0;
     52        }
     53    }
     54    return 1;
    5355}
    5456
     
    5658future_parse(PyFutureFeatures *ff, mod_ty mod, const char *filename)
    5759{
    58         int i, found_docstring = 0, done = 0, prev_line = 0;
     60    int i, found_docstring = 0, done = 0, prev_line = 0;
    5961
    60         static PyObject *future;
    61         if (!future) {
    62                 future = PyString_InternFromString("__future__");
    63                 if (!future)
    64                         return 0;
    65         }
     62    if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
     63        return 1;
    6664
    67         if (!(mod->kind == Module_kind || mod->kind == Interactive_kind))
    68                 return 1;
     65    /* A subsequent pass will detect future imports that don't
     66       appear at the beginning of the file.  There's one case,
     67       however, that is easier to handle here: A series of imports
     68       joined by semi-colons, where the first import is a future
     69       statement but some subsequent import has the future form
     70       but is preceded by a regular import.
     71    */
    6972
    70         /* A subsequent pass will detect future imports that don't
    71            appear at the beginning of the file.  There's one case,
    72            however, that is easier to handle here: A series of imports
    73            joined by semi-colons, where the first import is a future
    74            statement but some subsequent import has the future form
    75            but is preceded by a regular import.
    76         */
    77            
    7873
    79         for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
    80                 stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
     74    for (i = 0; i < asdl_seq_LEN(mod->v.Module.body); i++) {
     75        stmt_ty s = (stmt_ty)asdl_seq_GET(mod->v.Module.body, i);
    8176
    82                 if (done && s->lineno > prev_line)
    83                         return 1;
    84                 prev_line = s->lineno;
     77        if (done && s->lineno > prev_line)
     78            return 1;
     79        prev_line = s->lineno;
    8580
    86                 /* The tests below will return from this function unless it is
    87                    still possible to find a future statement.  The only things
    88                    that can precede a future statement are another future
    89                    statement and a doc string.
    90                 */
     81        /* The tests below will return from this function unless it is
     82           still possible to find a future statement.  The only things
     83           that can precede a future statement are another future
     84           statement and a doc string.
     85        */
    9186
    92                 if (s->kind == ImportFrom_kind) {
    93                         if (s->v.ImportFrom.module == future) {
    94                                 if (done) {
    95                                         PyErr_SetString(PyExc_SyntaxError,
    96                                                         ERR_LATE_FUTURE);
    97                                         PyErr_SyntaxLocation(filename,
    98                                                              s->lineno);
    99                                         return 0;
    100                                 }
    101                                 if (!future_check_features(ff, s, filename))
    102                                         return 0;
    103                                 ff->ff_lineno = s->lineno;
    104                         }
    105                         else
    106                                 done = 1;
    107                 }
    108                 else if (s->kind == Expr_kind && !found_docstring) {
    109                         expr_ty e = s->v.Expr.value;
    110                         if (e->kind != Str_kind)
    111                                 done = 1;
    112                         else
    113                                 found_docstring = 1;
    114                 }
    115                 else
    116                         done = 1;
    117         }
    118         return 1;
     87        if (s->kind == ImportFrom_kind) {
     88            identifier modname = s->v.ImportFrom.module;
     89            if (modname && PyString_GET_SIZE(modname) == 10 &&
     90                !strcmp(PyString_AS_STRING(modname), "__future__")) {
     91                if (done) {
     92                    PyErr_SetString(PyExc_SyntaxError,
     93                                    ERR_LATE_FUTURE);
     94                    PyErr_SyntaxLocation(filename,
     95                                         s->lineno);
     96                    return 0;
     97                }
     98                if (!future_check_features(ff, s, filename))
     99                    return 0;
     100                ff->ff_lineno = s->lineno;
     101            }
     102            else
     103                done = 1;
     104        }
     105        else if (s->kind == Expr_kind && !found_docstring) {
     106            expr_ty e = s->v.Expr.value;
     107            if (e->kind != Str_kind)
     108                done = 1;
     109            else
     110                found_docstring = 1;
     111        }
     112        else
     113            done = 1;
     114    }
     115    return 1;
    119116}
    120117
     
    123120PyFuture_FromAST(mod_ty mod, const char *filename)
    124121{
    125         PyFutureFeatures *ff;
     122    PyFutureFeatures *ff;
    126123
    127         ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
    128         if (ff == NULL) {
    129                 PyErr_NoMemory();
    130                 return NULL;
    131         }
    132         ff->ff_features = 0;
    133         ff->ff_lineno = -1;
     124    ff = (PyFutureFeatures *)PyObject_Malloc(sizeof(PyFutureFeatures));
     125    if (ff == NULL) {
     126        PyErr_NoMemory();
     127        return NULL;
     128    }
     129    ff->ff_features = 0;
     130    ff->ff_lineno = -1;
    134131
    135         if (!future_parse(ff, mod, filename)) {
    136                 PyObject_Free(ff);
    137                 return NULL;
    138         }
    139         return ff;
     132    if (!future_parse(ff, mod, filename)) {
     133        PyObject_Free(ff);
     134        return NULL;
     135    }
     136    return ff;
    140137}
Note: See TracChangeset for help on using the changeset viewer.