~pythonregexp2.7/python/issue2636-12

« back to all changes in this revision

Viewing changes to Python/ast.c

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-06-09 14:52:42 UTC
  • mfrom: (39033.1.3 Regexp-2.6)
  • Revision ID: darklord@timehorse.com-20080609145242-9m268zc6u87rp1vp
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
113
113
    PyErr_Restore(type, value, tback);
114
114
}
115
115
 
 
116
static int
 
117
ast_warn(struct compiling *c, const node *n, char *msg)
 
118
{
 
119
    if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, LINENO(n),
 
120
                           NULL, NULL) < 0) {
 
121
        /* if -Werr, change it to a SyntaxError */
 
122
        if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_SyntaxWarning))
 
123
            ast_error(n, msg);
 
124
        return 0;
 
125
    }
 
126
    return 1;
 
127
}
 
128
 
 
129
static int
 
130
forbidden_check(struct compiling *c, const node *n, const char *x)
 
131
{
 
132
    if (!strcmp(x, "None"))
 
133
        return ast_error(n, "assignment to None");
 
134
    if (Py_Py3kWarningFlag && !(strcmp(x, "True") && strcmp(x, "False")) &&
 
135
        !ast_warn(c, n, "assignment to True or False is forbidden in 3.x"))
 
136
        return 0;
 
137
    return 1;
 
138
}
 
139
 
116
140
/* num_stmts() returns number of contained statements.
117
141
 
118
142
   Use this routine to determine how big a sequence is needed for
351
375
 
352
376
    switch (e->kind) {
353
377
        case Attribute_kind:
354
 
            if (ctx == Store &&
355
 
                !strcmp(PyString_AS_STRING(e->v.Attribute.attr), "None")) {
356
 
                return ast_error(n, "assignment to None");
357
 
            }
 
378
            if (ctx == Store && !forbidden_check(c, n,
 
379
                                PyBytes_AS_STRING(e->v.Attribute.attr)))
 
380
                    return 0;
358
381
            e->v.Attribute.ctx = ctx;
359
382
            break;
360
383
        case Subscript_kind:
361
384
            e->v.Subscript.ctx = ctx;
362
385
            break;
363
386
        case Name_kind:
364
 
            if (ctx == Store &&
365
 
                !strcmp(PyString_AS_STRING(e->v.Name.id), "None")) {
366
 
                    return ast_error(n, "assignment to None");
367
 
            }
 
387
            if (ctx == Store && !forbidden_check(c, n,
 
388
                                PyBytes_AS_STRING(e->v.Name.id)))
 
389
                    return 0;
368
390
            e->v.Name.ctx = ctx;
369
391
            break;
370
392
        case List_kind:
582
604
        /* fpdef_node is either a NAME or an fplist */
583
605
        child = CHILD(fpdef_node, 0);
584
606
        if (TYPE(child) == NAME) {
585
 
            if (!strcmp(STR(child), "None")) {
586
 
                ast_error(child, "assignment to None");
587
 
                return NULL;
588
 
            }   
 
607
            if (!forbidden_check(c, n, STR(child)))
 
608
                return NULL;  
589
609
            arg = Name(NEW_IDENTIFIER(child), Store, LINENO(child),
590
610
                       child->n_col_offset, c->c_arena);
591
611
        }
681
701
                    /* def foo((x)): is not complex, special case. */
682
702
                    if (NCH(ch) != 1) {
683
703
                        /* We have complex arguments, setup for unpacking. */
 
704
                        if (Py_Py3kWarningFlag && !ast_warn(c, ch,
 
705
                            "tuple parameter unpacking has been removed in 3.x"))
 
706
                            goto error;
684
707
                        asdl_seq_SET(args, k++, compiler_complex_args(c, ch));
685
708
                        if (!asdl_seq_GET(args, k-1))
686
709
                                goto error;
695
718
                }
696
719
                if (TYPE(CHILD(ch, 0)) == NAME) {
697
720
                    expr_ty name;
698
 
                    if (!strcmp(STR(CHILD(ch, 0)), "None")) {
699
 
                        ast_error(CHILD(ch, 0), "assignment to None");
 
721
                    if (!forbidden_check(c, n, STR(CHILD(ch, 0))))
700
722
                        goto error;
701
 
                    }
702
723
                    name = Name(NEW_IDENTIFIER(CHILD(ch, 0)),
703
724
                                Param, LINENO(ch), ch->n_col_offset,
704
725
                                c->c_arena);
710
731
                i += 2; /* the name and the comma */
711
732
                break;
712
733
            case STAR:
713
 
                if (!strcmp(STR(CHILD(n, i+1)), "None")) {
714
 
                    ast_error(CHILD(n, i+1), "assignment to None");
 
734
                if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
715
735
                    goto error;
716
 
                }
717
736
                vararg = NEW_IDENTIFIER(CHILD(n, i+1));
718
737
                i += 3;
719
738
                break;
720
739
            case DOUBLESTAR:
721
 
                if (!strcmp(STR(CHILD(n, i+1)), "None")) {
722
 
                    ast_error(CHILD(n, i+1), "assignment to None");
 
740
                if (!forbidden_check(c, CHILD(n, i+1), STR(CHILD(n, i+1))))
723
741
                    goto error;
724
 
                }
725
742
                kwarg = NEW_IDENTIFIER(CHILD(n, i+1));
726
743
                i += 3;
727
744
                break;
844
861
    name = NEW_IDENTIFIER(CHILD(n, name_i));
845
862
    if (!name)
846
863
        return NULL;
847
 
    else if (!strcmp(STR(CHILD(n, name_i)), "None")) {
848
 
        ast_error(CHILD(n, name_i), "assignment to None");
 
864
    else if (!forbidden_check(c, CHILD(n, name_i), STR(CHILD(n, name_i))))
849
865
        return NULL;
850
 
    }
851
866
    args = ast_for_arguments(c, CHILD(n, name_i + 1));
852
867
    if (!args)
853
868
        return NULL;
1363
1378
    }
1364
1379
    case BACKQUOTE: { /* repr */
1365
1380
        expr_ty expression;
1366
 
        if (Py_Py3kWarningFlag) {
1367
 
            if (PyErr_WarnExplicit(PyExc_DeprecationWarning,
1368
 
                                   "backquote not supported in 3.x; use repr()",
1369
 
                                   c->c_filename, LINENO(n),
1370
 
                                   NULL, NULL)) {
 
1381
        if (Py_Py3kWarningFlag &&
 
1382
            !ast_warn(c, n, "backquote not supported in 3.x; use repr()"))
1371
1383
            return NULL;
1372
 
            }
1373
 
        }
1374
1384
        expression = ast_for_testlist(c, CHILD(n, 1));
1375
1385
        if (!expression)
1376
1386
            return NULL;
1921
1931
                    return NULL;
1922
1932
                }
1923
1933
                key = e->v.Name.id;
1924
 
                if (!strcmp(PyString_AS_STRING(key), "None")) {
1925
 
                    ast_error(CHILD(ch, 0), "assignment to None");
 
1934
                if (!forbidden_check(c, CHILD(ch, 0), PyBytes_AS_STRING(key)))
1926
1935
                    return NULL;
1927
 
                }
1928
1936
                e = ast_for_expr(c, CHILD(ch, 2));
1929
1937
                if (!e)
1930
1938
                    return NULL;
2050
2058
                          "expression not possible");
2051
2059
                return NULL;
2052
2060
            case Name_kind: {
2053
 
                const char *var_name = PyString_AS_STRING(expr1->v.Name.id);
2054
 
                if (var_name[0] == 'N' && !strcmp(var_name, "None")) {
2055
 
                    ast_error(ch, "assignment to None");
 
2061
                const char *var_name = PyBytes_AS_STRING(expr1->v.Name.id);
 
2062
                if ((var_name[0] == 'N' || var_name[0] == 'T' || var_name[0] == 'F') &&
 
2063
                    !forbidden_check(c, ch, var_name))
2056
2064
                    return NULL;
2057
 
                }
2058
2065
                break;
2059
2066
            }
2060
2067
            case Attribute_kind:
2997
3004
    
2998
3005
    REQ(n, classdef);
2999
3006
 
3000
 
    if (!strcmp(STR(CHILD(n, 1)), "None")) {
3001
 
            ast_error(n, "assignment to None");
 
3007
    if (!forbidden_check(c, n, STR(CHILD(n, 1))))
3002
3008
            return NULL;
3003
 
    }
3004
3009
 
3005
3010
    if (NCH(n) == 4) {
3006
3011
        s = ast_for_suite(c, CHILD(n, 3));