comparison Python/compile.c @ 16357:9f2ca855fa44 legacy-trunk

Enforce two illegal import statements that were outlawed in the reference manual but not checked: Names bound by import statemants may not occur in global statements in the same scope. The from ... import * form may only occur in a module scope. I guess these changes could break code, but the reference manual warned about them. Several other small changes If a variable is declared global in the nearest enclosing scope of a free variable, then treat it is a global in the nested scope too. Get rid of com_mangle and symtable_mangle functions and call mangle directly. If errors occur during symtable table creation, return -1 from symtable_build(). Do not increment st_errors in assignment to lambda, because exception is not set. Add extra argument to symtable_assign(); the argument, flag, is ORed with DEF_LOCAL for each symtable_add_def() call.
author Jeremy Hylton <jeremy@alum.mit.edu>
date Tue, 30 Jan 2001 01:24:43 +0000
parents ae04d614a3e0
children f700e6b8de1f
comparison
equal deleted inserted replaced
16356:4ff2858d7a85 16357:9f2ca855fa44
56 #define GLOBAL_IMPLICIT 3 56 #define GLOBAL_IMPLICIT 3
57 #define FREE 4 57 #define FREE 4
58 #define CELL 5 58 #define CELL 5
59 59
60 #define DEL_CLOSURE_ERROR \ 60 #define DEL_CLOSURE_ERROR \
61 "can not delete variable '%s' referenced in nested scope" 61 "can not delete variable '%.400s' referenced in nested scope"
62
63 #define DUPLICATE_ARGUMENT \
64 "duplicate argument '%s' in function definition"
65
66 #define ILLEGAL_IMPORT_STAR \
67 "'from ... import *' may only occur in a module scope"
68
69 #define ILLEGAL_IMPORT_GLOBAL \
70 "may not import name '%.400s' because it is declared global"
62 71
63 #define MANGLE_LEN 256 72 #define MANGLE_LEN 256
64 73
65 #define OFF(x) offsetof(PyCodeObject, x) 74 #define OFF(x) offsetof(PyCodeObject, x)
66 75
427 #define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */ 436 #define DEF_DOUBLESTAR 2<<4 /* parameter is star-star arg */
428 #define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */ 437 #define DEF_INTUPLE 2<<5 /* name defined in tuple in parameters */
429 #define DEF_FREE 2<<6 /* name used by not defined in nested scope */ 438 #define DEF_FREE 2<<6 /* name used by not defined in nested scope */
430 #define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */ 439 #define DEF_FREE_GLOBAL 2<<7 /* free variable is actually implicit global */
431 #define DEF_FREE_CLASS 2<<8 /* free variable from class's method */ 440 #define DEF_FREE_CLASS 2<<8 /* free variable from class's method */
441 #define DEF_IMPORT 2<<9 /* assignment occurred via import */
432 442
433 int is_free(int v) 443 int is_free(int v)
434 { 444 {
435 if ((v & (USE | DEF_FREE)) 445 if ((v & (USE | DEF_FREE))
436 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL))) 446 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
561 static void symtable_default_args(struct symtable *, node *); 571 static void symtable_default_args(struct symtable *, node *);
562 static void symtable_params(struct symtable *, node *); 572 static void symtable_params(struct symtable *, node *);
563 static void symtable_params_fplist(struct symtable *, node *n); 573 static void symtable_params_fplist(struct symtable *, node *n);
564 static void symtable_global(struct symtable *, node *); 574 static void symtable_global(struct symtable *, node *);
565 static void symtable_import(struct symtable *, node *); 575 static void symtable_import(struct symtable *, node *);
566 static void symtable_assign(struct symtable *, node *); 576 static void symtable_assign(struct symtable *, node *, int);
567 static void symtable_list_comprehension(struct symtable *, node *); 577 static void symtable_list_comprehension(struct symtable *, node *);
568 578
569 static int symtable_update_free_vars(struct symtable *); 579 static int symtable_update_free_vars(struct symtable *);
570 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *); 580 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
571 static int symtable_check_global(struct symtable *, PyObject *, PyObject *); 581 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
905 strcpy(buffer+1+plen, name); 915 strcpy(buffer+1+plen, name);
906 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */ 916 /* fprintf(stderr, "mangle %s -> %s\n", name, buffer); */
907 return 1; 917 return 1;
908 } 918 }
909 919
910 static int
911 com_mangle(struct compiling *c, char *name, char *buffer, size_t maxlen)
912 {
913 return mangle(c->c_private, name, buffer, maxlen);
914 }
915
916 static void 920 static void
917 com_addop_name(struct compiling *c, int op, char *name) 921 com_addop_name(struct compiling *c, int op, char *name)
918 { 922 {
919 PyObject *v; 923 PyObject *v;
920 int i; 924 int i;
921 char buffer[MANGLE_LEN]; 925 char buffer[MANGLE_LEN];
922 /* fprintf(stderr, "com_addop_name(%s, %d, %s)\n", 926
923 c->c_name, op, name); 927 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
924 */
925 if (com_mangle(c, name, buffer, sizeof(buffer)))
926 name = buffer; 928 name = buffer;
927 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { 929 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
928 c->c_errors++; 930 c->c_errors++;
929 i = 255; 931 i = 255;
930 } 932 }
957 int i, reftype; 959 int i, reftype;
958 int scope = NAME_DEFAULT; 960 int scope = NAME_DEFAULT;
959 int op = STOP_CODE; 961 int op = STOP_CODE;
960 char buffer[MANGLE_LEN]; 962 char buffer[MANGLE_LEN];
961 963
962 if (com_mangle(c, name, buffer, sizeof(buffer))) 964 if (mangle(c->c_private, name, buffer, sizeof(buffer)))
963 name = buffer; 965 name = buffer;
964 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) { 966 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
965 c->c_errors++; 967 c->c_errors++;
966 i = 255; 968 i = 255;
967 goto done; 969 goto done;
1043 break; 1045 break;
1044 case NAME_DEFAULT: 1046 case NAME_DEFAULT:
1045 op = DELETE_NAME; 1047 op = DELETE_NAME;
1046 break; 1048 break;
1047 case NAME_CLOSURE: { 1049 case NAME_CLOSURE: {
1048 char buf[256]; 1050 char buf[500];
1049 sprintf(buf, DEL_CLOSURE_ERROR, name); 1051 sprintf(buf, DEL_CLOSURE_ERROR, name);
1050 com_error(c, PyExc_SyntaxError, buf); 1052 com_error(c, PyExc_SyntaxError, buf);
1051 i = 255; 1053 i = 255;
1052 break; 1054 break;
1053 } 1055 }
4005 if ((c->c_symtable = symtable_init()) == NULL) 4007 if ((c->c_symtable = symtable_init()) == NULL)
4006 return -1; 4008 return -1;
4007 if (symtable_enter_scope(c->c_symtable, TOP, TYPE(n)) < 0) 4009 if (symtable_enter_scope(c->c_symtable, TOP, TYPE(n)) < 0)
4008 return -1; 4010 return -1;
4009 symtable_node(c->c_symtable, n); 4011 symtable_node(c->c_symtable, n);
4012 if (c->c_symtable->st_errors > 0)
4013 return -1;
4010 /* reset for second pass */ 4014 /* reset for second pass */
4011 c->c_symtable->st_scopes = 1; 4015 c->c_symtable->st_scopes = 1;
4012 c->c_symtable->st_pass = 2; 4016 c->c_symtable->st_pass = 2;
4013 return 0; 4017 return 0;
4014 } 4018 }
4107 if ((info & DEF_PARAM) 4111 if ((info & DEF_PARAM)
4108 && (PyString_AS_STRING(name)[0] != '.')){ 4112 && (PyString_AS_STRING(name)[0] != '.')){
4109 char buf[500]; 4113 char buf[500];
4110 sprintf(buf, 4114 sprintf(buf,
4111 "name '%.400s' is local and global", 4115 "name '%.400s' is local and global",
4116 PyString_AS_STRING(name));
4117 com_error(c, PyExc_SyntaxError, buf);
4118 goto fail;
4119 }
4120 if (info & DEF_IMPORT) {
4121 char buf[500];
4122 sprintf(buf, ILLEGAL_IMPORT_GLOBAL,
4112 PyString_AS_STRING(name)); 4123 PyString_AS_STRING(name));
4113 com_error(c, PyExc_SyntaxError, buf); 4124 com_error(c, PyExc_SyntaxError, buf);
4114 goto fail; 4125 goto fail;
4115 } 4126 }
4116 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0) 4127 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
4332 return symtable_undo_free(st, child, name); 4343 return symtable_undo_free(st, child, name);
4333 o = PyDict_GetItem(st->st_cur, name); 4344 o = PyDict_GetItem(st->st_cur, name);
4334 if (o == NULL) 4345 if (o == NULL)
4335 return symtable_undo_free(st, child, name); 4346 return symtable_undo_free(st, child, name);
4336 v = PyInt_AS_LONG(o); 4347 v = PyInt_AS_LONG(o);
4337 if (is_free(v)) 4348 if (is_free(v) || (v & DEF_GLOBAL))
4338 return symtable_undo_free(st, child, name); 4349 return symtable_undo_free(st, child, name);
4339 else 4350 else
4340 return symtable_add_def_o(st, st->st_cur, name, DEF_FREE); 4351 return symtable_add_def_o(st, st->st_cur, name, DEF_FREE);
4341 } 4352 }
4342 4353
4491 st->st_cur_children = d; 4502 st->st_cur_children = d;
4492 return 0; 4503 return 0;
4493 } 4504 }
4494 4505
4495 static int 4506 static int
4496 symtable_mangle(struct symtable *st, char *name, char *buffer, size_t maxlen)
4497 {
4498 return mangle(st->st_private, name, buffer, maxlen);
4499 }
4500
4501 static int
4502 symtable_add_def(struct symtable *st, char *name, int flag) 4507 symtable_add_def(struct symtable *st, char *name, int flag)
4503 { 4508 {
4504 PyObject *s; 4509 PyObject *s;
4505 char buffer[MANGLE_LEN]; 4510 char buffer[MANGLE_LEN];
4506 4511
4507 if (symtable_mangle(st, name, buffer, sizeof(buffer))) 4512 if (mangle(st->st_private, name, buffer, sizeof(buffer)))
4508 name = buffer; 4513 name = buffer;
4509 if ((s = PyString_InternFromString(name)) == NULL) 4514 if ((s = PyString_InternFromString(name)) == NULL)
4510 return -1; 4515 return -1;
4511 return symtable_add_def_o(st, st->st_cur, s, flag); 4516 return symtable_add_def_o(st, st->st_cur, s, flag);
4512 } 4517 }
4518 PyObject *name, int flag) 4523 PyObject *name, int flag)
4519 { 4524 {
4520 PyObject *o; 4525 PyObject *o;
4521 int val; 4526 int val;
4522 4527
4523 /* fprintf(stderr, "def(%s, %d)\n", REPR(name), flag); */
4524 if ((o = PyDict_GetItem(dict, name))) { 4528 if ((o = PyDict_GetItem(dict, name))) {
4525 val = PyInt_AS_LONG(o); 4529 val = PyInt_AS_LONG(o);
4526 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { 4530 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
4527 PyErr_Format(PyExc_SyntaxError, 4531 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
4528 "duplicate argument '%s' in function definition",
4529 name); 4532 name);
4530 return -1; 4533 return -1;
4531 } 4534 }
4532 val |= flag; 4535 val |= flag;
4533 } else 4536 } else
4632 if (NCH(n) > 4) 4635 if (NCH(n) > 4)
4633 symtable_node(st, CHILD(n, 5)); 4636 symtable_node(st, CHILD(n, 5));
4634 break; 4637 break;
4635 case except_clause: 4638 case except_clause:
4636 if (NCH(n) == 4) 4639 if (NCH(n) == 4)
4637 symtable_assign(st, CHILD(n, 3)); 4640 symtable_assign(st, CHILD(n, 3), 0);
4638 if (NCH(n) > 1) { 4641 if (NCH(n) > 1) {
4639 n = CHILD(n, 1); 4642 n = CHILD(n, 1);
4640 goto loop; 4643 goto loop;
4641 } 4644 }
4642 break; 4645 break;
4643 case del_stmt: 4646 case del_stmt:
4644 symtable_assign(st, CHILD(n, 1)); 4647 symtable_assign(st, CHILD(n, 1), 0);
4645 break; 4648 break;
4646 case expr_stmt: 4649 case expr_stmt:
4647 if (NCH(n) == 1) 4650 if (NCH(n) == 1)
4648 n = CHILD(n, 0); 4651 n = CHILD(n, 0);
4649 else { 4652 else {
4650 if (TYPE(CHILD(n, 1)) == augassign) { 4653 if (TYPE(CHILD(n, 1)) == augassign) {
4651 symtable_assign(st, CHILD(n, 0)); 4654 symtable_assign(st, CHILD(n, 0), 0);
4652 symtable_node(st, CHILD(n, 2)); 4655 symtable_node(st, CHILD(n, 2));
4653 break; 4656 break;
4654 } else { 4657 } else {
4655 int i; 4658 int i;
4656 for (i = 0; i < NCH(n) - 2; i += 2) 4659 for (i = 0; i < NCH(n) - 2; i += 2)
4657 symtable_assign(st, CHILD(n, i)); 4660 symtable_assign(st, CHILD(n, i), 0);
4658 n = CHILD(n, NCH(n) - 1); 4661 n = CHILD(n, NCH(n) - 1);
4659 } 4662 }
4660 } 4663 }
4661 goto loop; 4664 goto loop;
4662 /* watchout for fall-through logic below */ 4665 /* watchout for fall-through logic below */
4676 symtable_add_use(st, STR(CHILD(n, 0))); 4679 symtable_add_use(st, STR(CHILD(n, 0)));
4677 break; 4680 break;
4678 } 4681 }
4679 case for_stmt: 4682 case for_stmt:
4680 if (TYPE(n) == for_stmt) { 4683 if (TYPE(n) == for_stmt) {
4681 symtable_assign(st, CHILD(n, 1)); 4684 symtable_assign(st, CHILD(n, 1), 0);
4682 start = 3; 4685 start = 3;
4683 } 4686 }
4684 default: 4687 default:
4685 if (NCH(n) == 1) { 4688 if (NCH(n) == 1) {
4686 n = CHILD(n, 0); 4689 n = CHILD(n, 0);
4832 { 4835 {
4833 char tmpname[12]; 4836 char tmpname[12];
4834 4837
4835 sprintf(tmpname, "[%d]", ++st->st_tmpname); 4838 sprintf(tmpname, "[%d]", ++st->st_tmpname);
4836 symtable_add_def(st, tmpname, DEF_LOCAL); 4839 symtable_add_def(st, tmpname, DEF_LOCAL);
4837 symtable_assign(st, CHILD(n, 1)); 4840 symtable_assign(st, CHILD(n, 1), 0);
4838 symtable_node(st, CHILD(n, 3)); 4841 symtable_node(st, CHILD(n, 3));
4839 if (NCH(n) == 5) 4842 if (NCH(n) == 5)
4840 symtable_node(st, CHILD(n, 4)); 4843 symtable_node(st, CHILD(n, 4));
4841 --st->st_tmpname; 4844 --st->st_tmpname;
4842 } 4845 }
4852 import_as_name: NAME [NAME NAME] 4855 import_as_name: NAME [NAME NAME]
4853 */ 4856 */
4854 4857
4855 if (STR(CHILD(n, 0))[0] == 'f') { /* from */ 4858 if (STR(CHILD(n, 0))[0] == 'f') { /* from */
4856 if (TYPE(CHILD(n, 3)) == STAR) { 4859 if (TYPE(CHILD(n, 3)) == STAR) {
4860 if (st->st_cur_type != TYPE_MODULE) {
4861 PyErr_SetString(PyExc_SyntaxError,
4862 ILLEGAL_IMPORT_STAR);
4863 st->st_errors++;
4864 return;
4865 }
4857 if (PyDict_SetItemString(st->st_cur, NOOPT, 4866 if (PyDict_SetItemString(st->st_cur, NOOPT,
4858 Py_None) < 0) 4867 Py_None) < 0)
4859 st->st_errors++; 4868 st->st_errors++;
4860 } else { 4869 } else {
4861 for (i = 3; i < NCH(n); i += 2) { 4870 for (i = 3; i < NCH(n); i += 2) {
4862 node *c = CHILD(n, i); 4871 node *c = CHILD(n, i);
4863 if (NCH(c) > 1) /* import as */ 4872 if (NCH(c) > 1) /* import as */
4864 symtable_assign(st, CHILD(c, 2)); 4873 symtable_assign(st, CHILD(c, 2),
4874 DEF_IMPORT);
4865 else 4875 else
4866 symtable_assign(st, CHILD(c, 0)); 4876 symtable_assign(st, CHILD(c, 0),
4877 DEF_IMPORT);
4867 } 4878 }
4868 } 4879 }
4869 } else { 4880 } else {
4870 for (i = 1; i < NCH(n); i += 2) { 4881 for (i = 1; i < NCH(n); i += 2) {
4871 symtable_assign(st, CHILD(n, i)); 4882 symtable_assign(st, CHILD(n, i), DEF_IMPORT);
4872 } 4883 }
4873 } 4884 }
4874 } 4885 }
4875 4886
4876 static void 4887 static void
4877 symtable_assign(struct symtable *st, node *n) 4888 symtable_assign(struct symtable *st, node *n, int flag)
4878 { 4889 {
4879 node *tmp; 4890 node *tmp;
4880 int i; 4891 int i;
4881 4892
4882 loop: 4893 loop:
4883 switch (TYPE(n)) { 4894 switch (TYPE(n)) {
4884 case lambdef: 4895 case lambdef:
4885 /* invalid assignment, e.g. lambda x:x=2 */ 4896 /* invalid assignment, e.g. lambda x:x=2. The next
4886 st->st_errors++; 4897 pass will catch this error. */
4887 return; 4898 return;
4888 case power: 4899 case power:
4889 if (NCH(n) > 2) { 4900 if (NCH(n) > 2) {
4890 for (i = 2; i < NCH(n); ++i) 4901 for (i = 2; i < NCH(n); ++i)
4891 if (TYPE(CHILD(n, i)) != DOUBLESTAR) 4902 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
4902 case listmaker: 4913 case listmaker:
4903 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) 4914 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
4904 symtable_list_comprehension(st, CHILD(n, 1)); 4915 symtable_list_comprehension(st, CHILD(n, 1));
4905 else { 4916 else {
4906 for (i = 0; i < NCH(n); i += 2) 4917 for (i = 0; i < NCH(n); i += 2)
4907 symtable_assign(st, CHILD(n, i)); 4918 symtable_assign(st, CHILD(n, i), flag);
4908 } 4919 }
4909 return; 4920 return;
4910 case exprlist: 4921 case exprlist:
4911 case testlist: 4922 case testlist:
4912 if (NCH(n) == 1) { 4923 if (NCH(n) == 1) {
4914 goto loop; 4925 goto loop;
4915 } 4926 }
4916 else { 4927 else {
4917 int i; 4928 int i;
4918 for (i = 0; i < NCH(n); i += 2) 4929 for (i = 0; i < NCH(n); i += 2)
4919 symtable_assign(st, CHILD(n, i)); 4930 symtable_assign(st, CHILD(n, i), flag);
4920 return; 4931 return;
4921 } 4932 }
4922 goto loop; 4933 goto loop;
4923 case atom: 4934 case atom:
4924 tmp = CHILD(n, 0); 4935 tmp = CHILD(n, 0);
4925 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) { 4936 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
4926 n = CHILD(n, 1); 4937 n = CHILD(n, 1);
4927 goto loop; 4938 goto loop;
4928 } else if (TYPE(tmp) == NAME) 4939 } else if (TYPE(tmp) == NAME)
4929 symtable_add_def(st, STR(tmp), DEF_LOCAL); 4940 symtable_add_def(st, STR(tmp), DEF_LOCAL | flag);
4930 return; 4941 return;
4931 case dotted_as_name: 4942 case dotted_as_name:
4932 if (NCH(n) == 3) 4943 if (NCH(n) == 3)
4933 symtable_add_def(st, STR(CHILD(n, 2)), 4944 symtable_add_def(st, STR(CHILD(n, 2)),
4934 DEF_LOCAL); 4945 DEF_LOCAL | flag);
4935 else 4946 else
4936 symtable_add_def(st, 4947 symtable_add_def(st,
4937 STR(CHILD(CHILD(n, 4948 STR(CHILD(CHILD(n,
4938 0), 0)), 4949 0), 0)),
4939 DEF_LOCAL); 4950 DEF_LOCAL | flag);
4940 return; 4951 return;
4941 case dotted_name: 4952 case dotted_name:
4942 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL); 4953 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | flag);
4943 return; 4954 return;
4944 case NAME: 4955 case NAME:
4945 symtable_add_def(st, STR(n), DEF_LOCAL); 4956 symtable_add_def(st, STR(n), DEF_LOCAL | flag);
4946 return; 4957 return;
4947 default: 4958 default:
4948 if (NCH(n) == 0) 4959 if (NCH(n) == 0)
4949 return; 4960 return;
4950 if (NCH(n) != 1) { 4961 if (NCH(n) != 1) {