blob: bd4120231a2fc794955d267f6b5db2bf4d3e0224 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002#include "Python-ast.h"
3#include "code.h"
Jeremy Hylton4db62b12001-02-27 19:07:02 +00004#include "compile.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00006#include "structmember.h"
7
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00008/* two error strings used for warnings */
9#define GLOBAL_AFTER_ASSIGN \
10"name '%.400s' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000011
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000012#define GLOBAL_AFTER_USE \
13"name '%.400s' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000014
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015PySTEntryObject *
16PySTEntry_New(struct symtable *st, identifier name, block_ty block,
17 void *key, int lineno)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000018{
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000019 PySTEntryObject *ste = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000020 PyObject *k, *v;
21
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 k = PyLong_FromVoidPtr(key);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000023 if (k == NULL)
24 goto fail;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000025 ste = (PySTEntryObject *)PyObject_New(PySTEntryObject,
26 &PySTEntry_Type);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000027 ste->ste_table = st;
28 ste->ste_id = k;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000029 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000030
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000031 ste->ste_name = name;
32 Py_INCREF(name);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000033
34 v = PyDict_New();
35 if (v == NULL)
36 goto fail;
37 ste->ste_symbols = v;
38
39 v = PyList_New(0);
40 if (v == NULL)
41 goto fail;
42 ste->ste_varnames = v;
43
44 v = PyList_New(0);
45 if (v == NULL)
46 goto fail;
47 ste->ste_children = v;
48
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000049 ste->ste_type = block;
50 ste->ste_unoptimized = 0;
51 ste->ste_nested = 0;
52 ste->ste_free = 0;
53 ste->ste_varargs = 0;
54 ste->ste_varkeywords = 0;
Jeremy Hylton86424e32001-12-04 02:41:46 +000055 ste->ste_opt_lineno = 0;
Jeremy Hylton4d508ad2003-05-21 17:34:50 +000056 ste->ste_tmpname = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000057 ste->ste_lineno = lineno;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000058
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000059 if (st->st_cur != NULL &&
60 (st->st_cur->ste_nested ||
61 st->st_cur->ste_type == FunctionBlock))
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000062 ste->ste_nested = 1;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000063 ste->ste_child_free = 0;
Tim Peters5ca576e2001-06-18 22:08:13 +000064 ste->ste_generator = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000065
66 if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0)
67 goto fail;
Jeremy Hylton74b3bc42001-02-23 17:55:27 +000068
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000069 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070 fail:
71 Py_XDECREF(ste);
72 return NULL;
73}
74
75static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000076ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000077{
78 char buf[256];
79
Barry Warsaw4b4ab202001-11-28 21:36:28 +000080 PyOS_snprintf(buf, sizeof(buf),
81 "<symtable entry %.100s(%ld), line %d>",
82 PyString_AS_STRING(ste->ste_name),
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000083 PyInt_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084 return PyString_FromString(buf);
85}
86
87static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000088ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000089{
90 ste->ste_table = NULL;
91 Py_XDECREF(ste->ste_id);
92 Py_XDECREF(ste->ste_name);
93 Py_XDECREF(ste->ste_symbols);
94 Py_XDECREF(ste->ste_varnames);
95 Py_XDECREF(ste->ste_children);
96 PyObject_Del(ste);
97}
98
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100
Guido van Rossum6f799372001-09-20 20:46:19 +0000101static PyMemberDef ste_memberlist[] = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000102 {"id", T_OBJECT, OFF(ste_id), READONLY},
103 {"name", T_OBJECT, OFF(ste_name), READONLY},
104 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
105 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
106 {"children", T_OBJECT, OFF(ste_children), READONLY},
107 {"type", T_INT, OFF(ste_type), READONLY},
108 {"lineno", T_INT, OFF(ste_lineno), READONLY},
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109 {NULL}
110};
111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000112PyTypeObject PySTEntry_Type = {
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113 PyObject_HEAD_INIT(&PyType_Type)
114 0,
115 "symtable entry",
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000116 sizeof(PySTEntryObject),
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117 0,
118 (destructor)ste_dealloc, /* tp_dealloc */
119 0, /* tp_print */
Guido van Rossum6f799372001-09-20 20:46:19 +0000120 0, /* tp_getattr */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121 0, /* tp_setattr */
122 0, /* tp_compare */
123 (reprfunc)ste_repr, /* tp_repr */
124 0, /* tp_as_number */
125 0, /* tp_as_sequence */
126 0, /* tp_as_mapping */
127 0, /* tp_hash */
128 0, /* tp_call */
129 0, /* tp_str */
Guido van Rossum6f799372001-09-20 20:46:19 +0000130 PyObject_GenericGetAttr, /* tp_getattro */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000131 0, /* tp_setattro */
132 0, /* tp_as_buffer */
133 Py_TPFLAGS_DEFAULT, /* tp_flags */
134 0, /* tp_doc */
Guido van Rossum6f799372001-09-20 20:46:19 +0000135 0, /* tp_traverse */
136 0, /* tp_clear */
137 0, /* tp_richcompare */
138 0, /* tp_weaklistoffset */
139 0, /* tp_iter */
140 0, /* tp_iternext */
141 0, /* tp_methods */
142 ste_memberlist, /* tp_members */
143 0, /* tp_getset */
144 0, /* tp_base */
145 0, /* tp_dict */
146 0, /* tp_descr_get */
147 0, /* tp_descr_set */
148 0, /* tp_dictoffset */
149 0, /* tp_init */
150 0, /* tp_alloc */
151 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000152};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153
154static int symtable_analyze(struct symtable *st);
155static int symtable_warn(struct symtable *st, char *msg);
156static int symtable_enter_block(struct symtable *st, identifier name,
157 block_ty block, void *ast, int lineno);
158static int symtable_exit_block(struct symtable *st, void *ast);
159static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
160static int symtable_visit_expr(struct symtable *st, expr_ty s);
161static int symtable_visit_genexp(struct symtable *st, expr_ty s);
162static int symtable_visit_arguments(struct symtable *st, arguments_ty);
163static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
164static int symtable_visit_alias(struct symtable *st, alias_ty);
165static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
166static int symtable_visit_keyword(struct symtable *st, keyword_ty);
167static int symtable_visit_slice(struct symtable *st, slice_ty);
168static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top);
169static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args);
170static int symtable_implicit_arg(struct symtable *st, int pos);
171
172
173static identifier top = NULL, lambda = NULL;
174
175#define GET_IDENTIFIER(VAR) \
176 ((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR)))
177
178#define DUPLICATE_ARGUMENT \
179"duplicate argument '%s' in function definition"
180
181static struct symtable *
182symtable_new(void)
183{
184 struct symtable *st;
185
186 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
187 if (st == NULL)
188 return NULL;
189
190 st->st_filename = NULL;
191 if ((st->st_stack = PyList_New(0)) == NULL)
192 goto fail;
193 if ((st->st_symbols = PyDict_New()) == NULL)
194 goto fail;
195 st->st_cur = NULL;
196 st->st_tmpname = 0;
197 st->st_private = NULL;
198 return st;
199 fail:
200 PySymtable_Free(st);
201 return NULL;
202}
203
204struct symtable *
205PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future)
206{
207 struct symtable *st = symtable_new();
208 asdl_seq *seq;
209 int i;
210
211 if (st == NULL)
212 return st;
213 st->st_filename = filename;
214 st->st_future = future;
215 symtable_enter_block(st, GET_IDENTIFIER(top), ModuleBlock,
216 (void *)mod, 0);
217 st->st_top = st->st_cur;
218 st->st_cur->ste_unoptimized = OPT_TOPLEVEL;
219 /* Any other top-level initialization? */
220 switch (mod->kind) {
221 case Module_kind:
222 seq = mod->v.Module.body;
223 for (i = 0; i < asdl_seq_LEN(seq); i++)
224 if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
225 goto error;
226 break;
227 case Expression_kind:
228 if (!symtable_visit_expr(st, mod->v.Expression.body))
229 goto error;
230 break;
231 case Interactive_kind:
232 seq = mod->v.Interactive.body;
233 for (i = 0; i < asdl_seq_LEN(seq); i++)
234 if (!symtable_visit_stmt(st, asdl_seq_GET(seq, i)))
235 goto error;
236 break;
237 case Suite_kind:
238 PyErr_SetString(PyExc_RuntimeError,
239 "this compiler does not handle Suites");
240 return NULL;
241 }
242 if (!symtable_exit_block(st, (void *)mod))
243 return NULL;
244 if (symtable_analyze(st))
245 return st;
246 error:
247 PySymtable_Free(st);
248 return NULL;
249}
250
251void
252PySymtable_Free(struct symtable *st)
253{
254 Py_XDECREF(st->st_symbols);
255 Py_XDECREF(st->st_stack);
256 PyMem_Free((void *)st);
257}
258
259PySTEntryObject *
260PySymtable_Lookup(struct symtable *st, void *key)
261{
262 PyObject *k, *v;
263
264 k = PyLong_FromVoidPtr(key);
265 if (k == NULL)
266 return NULL;
267 v = PyDict_GetItem(st->st_symbols, k);
268 if (v) {
269 assert(PySTEntry_Check(v));
270 Py_DECREF(k);
271 Py_INCREF(v);
272 return (PySTEntryObject *)v;
273 }
274 else {
275 PyErr_SetString(PyExc_KeyError,
276 "unknown symbol table entry");
277 return NULL;
278 }
279}
280
281int
282PyST_GetScope(PySTEntryObject *ste, PyObject *name)
283{
284 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
285 if (!v)
286 return 0;
287 assert(PyInt_Check(v));
288 return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK;
289}
290
291
292/* Analyze raw symbol information to determine scope of each name.
293
294 The next several functions are helpers for PySymtable_Analyze(),
295 which determines whether a name is local, global, or free. In addition,
296 it determines which local variables are cell variables; they provide
297 bindings that are used for free variables in enclosed blocks.
298
299 There are also two kinds of free variables, implicit and explicit. An
300 explicit global is declared with the global statement. An implicit
301 global is a free variable for which the compiler has found no binding
302 in an enclosing function scope. The implicit global is either a global
303 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
304 to handle these names to implement slightly odd semantics. In such a
305 block, the name is treated as global until it is assigned to; then it
306 is treated as a local.
307
308 The symbol table requires two passes to determine the scope of each name.
309 The first pass collects raw facts from the AST: the name is a parameter
310 here, the name is used by not defined here, etc. The second pass analyzes
311 these facts during a pass over the PySTEntryObjects created during pass 1.
312
313 When a function is entered during the second pass, the parent passes
314 the set of all name bindings visible to its children. These bindings
315 are used to determine if the variable is free or an implicit global.
316 After doing the local analysis, it analyzes each of its child blocks
317 using an updated set of name bindings.
318
319 The children update the free variable set. If a local variable is free
320 in a child, the variable is marked as a cell. The current function must
321 provide runtime storage for the variable that may outlive the function's
322 frame. Cell variables are removed from the free set before the analyze
323 function returns to its parent.
324
325 The sets of bound and free variables are implemented as dictionaries
326 mapping strings to None.
327*/
328
329#define SET_SCOPE(DICT, NAME, I) { \
330 PyObject *o = PyInt_FromLong(I); \
331 if (!o) \
332 return 0; \
333 if (PyDict_SetItem((DICT), (NAME), o) < 0) \
334 return 0; \
335}
336
337/* Decide on scope of name, given flags.
338
339 The dicts passed in as arguments are modified as necessary.
340 ste is passed so that flags can be updated.
341*/
342
343static int
344analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, int flags,
345 PyObject *bound, PyObject *local, PyObject *free,
346 PyObject *global)
347{
348 if (flags & DEF_GLOBAL) {
349 if (flags & DEF_PARAM) {
350 PyErr_Format(PyExc_SyntaxError,
351 "name '%s' is local and global",
352 PyString_AS_STRING(name));
353 return 0;
354 }
355 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
356 if (PyDict_SetItem(global, name, Py_None) < 0)
357 return 0;
358 if (bound && PyDict_GetItem(bound, name)) {
359 if (PyDict_DelItem(bound, name) < 0)
360 return 0;
361 }
362 return 1;
363 }
364 if (flags & DEF_BOUND) {
365 SET_SCOPE(dict, name, LOCAL);
366 if (PyDict_SetItem(local, name, Py_None) < 0)
367 return 0;
368 if (PyDict_GetItem(global, name)) {
369 if (PyDict_DelItem(global, name) < 0)
370 return 0;
371 }
372 return 1;
373 }
374 /* If an enclosing block has a binding for this name, it
375 is a free variable rather than a global variable.
376 Note that having a non-NULL bound implies that the block
377 is nested.
378 */
379 if (bound && PyDict_GetItem(bound, name)) {
380 SET_SCOPE(dict, name, FREE);
381 ste->ste_free = 1;
382 if (PyDict_SetItem(free, name, Py_None) < 0)
383 return 0;
384 return 1;
385 }
386 /* If a parent has a global statement, then call it global
387 explicit? It could also be global implicit.
388 */
389 else if (global && PyDict_GetItem(global, name)) {
390 SET_SCOPE(dict, name, GLOBAL_EXPLICIT);
391 return 1;
392 }
393 else {
394 if (ste->ste_nested)
395 ste->ste_free = 1;
396 SET_SCOPE(dict, name, GLOBAL_IMPLICIT);
397 return 1;
398 }
399 return 0; /* Can't get here */
400}
401
402#undef SET_SCOPE
403
404/* If a name is defined in free and also in locals, then this block
405 provides the binding for the free variable. The name should be
406 marked CELL in this block and removed from the free list.
407
408 Note that the current block's free variables are included in free.
409 That's safe because no name can be free and local in the same scope.
410*/
411
412static int
413analyze_cells(PyObject *scope, PyObject *free)
414{
415 PyObject *name, *v, *w;
416 int flags, pos = 0, success = 0;
417
418 w = PyInt_FromLong(CELL);
419 if (!w)
420 return 0;
421 while (PyDict_Next(scope, &pos, &name, &v)) {
422 assert(PyInt_Check(v));
423 flags = PyInt_AS_LONG(v);
424 if (flags != LOCAL)
425 continue;
426 if (!PyDict_GetItem(free, name))
427 continue;
428 /* Replace LOCAL with CELL for this name, and remove
429 from free. It is safe to replace the value of name
430 in the dict, because it will not cause a resize.
431 */
432 if (PyDict_SetItem(scope, name, w) < 0)
433 goto error;
434 if (!PyDict_DelItem(free, name) < 0)
435 goto error;
436 }
437 success = 1;
438 error:
439 Py_DECREF(w);
440 return success;
441}
442
443/* Check for illegal statements in unoptimized namespaces */
444static int
445check_unoptimized(const PySTEntryObject* ste) {
446 char buf[300];
447
448 if (ste->ste_type == ModuleBlock || !ste->ste_unoptimized
449 || !(ste->ste_free || ste->ste_child_free))
450 return 1;
451
452 const char* trailer = (ste->ste_child_free ?
453 "contains a nested function with free variables" :
454 "is a nested function");
455
456 switch (ste->ste_unoptimized) {
457 case OPT_TOPLEVEL: /* exec / import * at top-level is fine */
458 case OPT_EXEC: /* qualified exec is fine */
459 return 1;
460 case OPT_IMPORT_STAR:
461 PyOS_snprintf(buf, sizeof(buf),
462 "import * is not allowed in function '%.100s' "
463 "because it is %s",
464 PyString_AS_STRING(ste->ste_name), trailer);
465 break;
466 case OPT_BARE_EXEC:
467 PyOS_snprintf(buf, sizeof(buf),
468 "unqualified exec is not allowed in function "
469 "'%.100s' it %s",
470 PyString_AS_STRING(ste->ste_name), trailer);
471 break;
472 default:
473 PyOS_snprintf(buf, sizeof(buf),
474 "function '%.100s' uses import * and bare exec, "
475 "which are illegal because it %s",
476 PyString_AS_STRING(ste->ste_name), trailer);
477 break;
478 }
479
480 PyErr_SetString(PyExc_SyntaxError, buf);
481 PyErr_SyntaxLocation(ste->ste_table->st_filename,
482 ste->ste_opt_lineno);
483 return 0;
484}
485
486/* Enter the final scope information into the st_symbols dict.
487 *
488 * All arguments are dicts. Modifies symbols, others are read-only.
489*/
490static int
491update_symbols(PyObject *symbols, PyObject *scope,
492 PyObject *bound, PyObject *free, int class)
493{
494 PyObject *name, *v, *u, *w, *free_value = NULL;
495 int i, flags, pos = 0;
496
497 while (PyDict_Next(symbols, &pos, &name, &v)) {
498 assert(PyInt_Check(v));
499 flags = PyInt_AS_LONG(v);
500 w = PyDict_GetItem(scope, name);
501 assert(w && PyInt_Check(w));
502 i = PyInt_AS_LONG(w);
503 flags |= (i << SCOPE_OFF);
504 u = PyInt_FromLong(flags);
505 if (PyDict_SetItem(symbols, name, u) < 0) {
506 Py_DECREF(u);
507 return 0;
508 }
509 Py_DECREF(u);
510 }
511
512 free_value = PyInt_FromLong(FREE << SCOPE_OFF);
513 if (!free_value)
514 return 0;
515
516 /* add a free variable when it's only use is for creating a closure */
517 pos = 0;
518 while (PyDict_Next(free, &pos, &name, &v)) {
519 PyObject *o = PyDict_GetItem(symbols, name);
520
521 if (o) {
522 /* It could be a free variable in a method of
523 the class that has the same name as a local
524 or global in the class scope.
525 */
526 if (class &&
527 PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) {
528 int i = PyInt_AS_LONG(o) | DEF_FREE_CLASS;
529 o = PyInt_FromLong(i);
530 if (!o) {
531 Py_DECREF(free_value);
532 return 0;
533 }
534 if (PyDict_SetItem(symbols, name, o) < 0) {
535 Py_DECREF(o);
536 Py_DECREF(free_value);
537 return 0;
538 }
539 }
540 /* else it's not free, probably a cell */
541 continue;
542 }
543 if (!PyDict_GetItem(bound, name))
544 continue; /* it's a global */
545
546 if (PyDict_SetItem(symbols, name, free_value) < 0) {
547 Py_DECREF(free_value);
548 return 0;
549 }
550 }
551 Py_DECREF(free_value);
552 return 1;
553}
554
555/* Make final symbol table decisions for block of ste.
556 Arguments:
557 ste -- current symtable entry (input/output)
558 bound -- set of variables bound in enclosing scopes (input)
559 free -- set of free variables in enclosed scopes (output)
560 globals -- set of declared global variables in enclosing scopes (input)
561*/
562
563static int
564analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
565 PyObject *global)
566{
567 PyObject *name, *v, *local = NULL, *scope = NULL, *newbound = NULL;
568 PyObject *newglobal = NULL, *newfree = NULL;
569 int i, flags, pos = 0, success = 0;
570
571 local = PyDict_New();
572 if (!local)
573 goto error;
574 scope = PyDict_New();
575 if (!scope)
576 goto error;
577 newglobal = PyDict_New();
578 if (!newglobal)
579 goto error;
580 newfree = PyDict_New();
581 if (!newfree)
582 goto error;
583 newbound = PyDict_New();
584 if (!newbound)
585 goto error;
586
587 if (ste->ste_type == ClassBlock) {
588 /* make a copy of globals before calling analyze_name(),
589 because global statements in the class have no effect
590 on nested functions.
591 */
592 if (PyDict_Update(newglobal, global) < 0)
593 goto error;
594 if (bound)
595 if (PyDict_Update(newbound, bound) < 0)
596 goto error;
597 }
598
599 assert(PySTEntry_Check(ste));
600 assert(PyDict_Check(ste->ste_symbols));
601 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
602 flags = PyInt_AS_LONG(v);
603 if (!analyze_name(ste, scope, name, flags, bound, local, free,
604 global))
605 goto error;
606 }
607
608 if (ste->ste_type != ClassBlock) {
609 if (ste->ste_type == FunctionBlock) {
610 if (PyDict_Update(newbound, local) < 0)
611 goto error;
612 }
613 if (bound) {
614 if (PyDict_Update(newbound, bound) < 0)
615 goto error;
616 }
617 if (PyDict_Update(newglobal, global) < 0)
618 goto error;
619 }
620
621 /* Recursively call analyze_block() on each child block */
622 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
623 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
624 assert(c && PySTEntry_Check(c));
625 PySTEntryObject* entry = (PySTEntryObject*)c;
626 if (!analyze_block(entry, newbound, newfree, newglobal))
627 goto error;
628 if (entry->ste_free || entry->ste_child_free)
629 ste->ste_child_free = 1;
630 }
631
632 if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree))
633 goto error;
634 if (!update_symbols(ste->ste_symbols, scope, bound, newfree,
635 ste->ste_type == ClassBlock))
636 goto error;
637 if (!check_unoptimized(ste))
638 goto error;
639
640 if (PyDict_Update(free, newfree) < 0)
641 goto error;
642 success = 1;
643 error:
644 Py_XDECREF(local);
645 Py_XDECREF(scope);
646 Py_XDECREF(newbound);
647 Py_XDECREF(newglobal);
648 Py_XDECREF(newfree);
649 if (!success)
650 assert(PyErr_Occurred());
651 return success;
652}
653
654static int
655symtable_analyze(struct symtable *st)
656{
657 PyObject *free, *global;
658 int r;
659
660 free = PyDict_New();
661 if (!free)
662 return 0;
663 global = PyDict_New();
664 if (!global) {
665 Py_DECREF(global);
666 return 0;
667 }
668 r = analyze_block(st->st_top, NULL, free, global);
669 Py_DECREF(free);
670 Py_DECREF(global);
671 return r;
672}
673
674
675static int
676symtable_warn(struct symtable *st, char *msg)
677{
678 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename,
679 st->st_cur->ste_lineno, NULL, NULL) < 0) {
680 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
681 PyErr_SetString(PyExc_SyntaxError, msg);
682 PyErr_SyntaxLocation(st->st_filename,
683 st->st_cur->ste_lineno);
684 }
685 return 0;
686 }
687 return 1;
688}
689
690/* symtable_enter_block() gets a reference via PySTEntry_New().
691 This reference is released when the block is exited, via the DECREF
692 in symtable_exit_block().
693*/
694
695static int
696symtable_exit_block(struct symtable *st, void *ast)
697{
698 int end;
699
700 Py_DECREF(st->st_cur);
701 end = PyList_GET_SIZE(st->st_stack) - 1;
702 if (end >= 0) {
703 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack,
704 end);
705 Py_INCREF(st->st_cur);
706 if (PySequence_DelItem(st->st_stack, end) < 0)
707 return 0;
708 }
709 return 1;
710}
711
712static int
713symtable_enter_block(struct symtable *st, identifier name, block_ty block,
714 void *ast, int lineno)
715{
716 PySTEntryObject *prev = NULL;
717
718 if (st->st_cur) {
719 prev = st->st_cur;
720 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
721 Py_DECREF(st->st_cur);
722 return 0;
723 }
724 Py_DECREF(st->st_cur);
725 }
726 st->st_cur = PySTEntry_New(st, name, block, ast, lineno);
727 if (name == GET_IDENTIFIER(top))
728 st->st_global = st->st_cur->ste_symbols;
729 if (prev) {
730 if (PyList_Append(prev->ste_children,
731 (PyObject *)st->st_cur) < 0) {
732 return 0;
733 }
734 }
735 return 1;
736}
737
738static int
739symtable_lookup(struct symtable *st, PyObject *name)
740{
741 PyObject *o;
742
743 o = PyDict_GetItem(st->st_cur->ste_symbols, name);
744 if (!o)
745 return 0;
746 return PyInt_AsLong(o);
747}
748
749static int
750symtable_add_def(struct symtable *st, PyObject *name, int flag)
751{
752 PyObject *o;
753 PyObject *dict;
754 int val;
755
756 dict = st->st_cur->ste_symbols;
757 if ((o = PyDict_GetItem(dict, name))) {
758 val = PyInt_AS_LONG(o);
759 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
760 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
761 PyString_AsString(name));
762 PyErr_SyntaxLocation(st->st_filename,
763 st->st_cur->ste_lineno);
764 return 0;
765 }
766 val |= flag;
767 } else
768 val = flag;
769 o = PyInt_FromLong(val);
770 if (o == NULL)
771 return 0;
772 if (PyDict_SetItem(dict, name, o) < 0) {
773 Py_DECREF(o);
774 return 0;
775 }
776 Py_DECREF(o);
777
778 if (flag & DEF_PARAM) {
779 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
780 return 0;
781 } else if (flag & DEF_GLOBAL) {
782 /* XXX need to update DEF_GLOBAL for other flags too;
783 perhaps only DEF_FREE_GLOBAL */
784 val = flag;
785 if ((o = PyDict_GetItem(st->st_global, name))) {
786 val |= PyInt_AS_LONG(o);
787 }
788 o = PyInt_FromLong(val);
789 if (o == NULL)
790 return 0;
791 if (PyDict_SetItem(st->st_global, name, o) < 0) {
792 Py_DECREF(o);
793 return 0;
794 }
795 Py_DECREF(o);
796 }
797 return 1;
798}
799
800/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
801 They use the ASDL name to synthesize the name of the C type and the visit
802 function.
803
804 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
805 useful if the first node in the sequence requires special treatment.
806*/
807
808#define VISIT(ST, TYPE, V) \
809 if (!symtable_visit_ ## TYPE((ST), (V))) \
810 return 0;
811
812#define VISIT_SEQ(ST, TYPE, SEQ) { \
813 int i; \
814 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
815 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
816 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
817 if (!symtable_visit_ ## TYPE((ST), elt)) \
818 return 0; \
819 } \
820}
821
822#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
823 int i; \
824 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
825 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
826 TYPE ## _ty elt = asdl_seq_GET(seq, i); \
827 if (!symtable_visit_ ## TYPE((ST), elt)) \
828 return 0; \
829 } \
830}
831
832static int
833symtable_visit_stmt(struct symtable *st, stmt_ty s)
834{
835 switch (s->kind) {
836 case FunctionDef_kind:
837 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
838 return 0;
839 if (s->v.FunctionDef.args->defaults)
840 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
841 if (s->v.FunctionDef.decorators)
842 VISIT_SEQ(st, expr, s->v.FunctionDef.decorators);
843 if (!symtable_enter_block(st, s->v.FunctionDef.name,
844 FunctionBlock, (void *)s, s->lineno))
845 return 0;
846 VISIT(st, arguments, s->v.FunctionDef.args);
847 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
848 if (!symtable_exit_block(st, s))
849 return 0;
850 break;
851 case ClassDef_kind:
852 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
853 return 0;
854 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
855 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
856 (void *)s, s->lineno))
857 return 0;
858 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
859 if (!symtable_exit_block(st, s))
860 return 0;
861 break;
862 case Return_kind:
863 if (s->v.Return.value)
864 VISIT(st, expr, s->v.Return.value);
865 break;
866 case Delete_kind:
867 VISIT_SEQ(st, expr, s->v.Delete.targets);
868 break;
869 case Assign_kind:
870 VISIT_SEQ(st, expr, s->v.Assign.targets);
871 VISIT(st, expr, s->v.Assign.value);
872 break;
873 case AugAssign_kind:
874 VISIT(st, expr, s->v.AugAssign.target);
875 VISIT(st, expr, s->v.AugAssign.value);
876 break;
877 case Print_kind:
878 if (s->v.Print.dest)
879 VISIT(st, expr, s->v.Print.dest);
880 VISIT_SEQ(st, expr, s->v.Print.values);
881 break;
882 case For_kind:
883 VISIT(st, expr, s->v.For.target);
884 VISIT(st, expr, s->v.For.iter);
885 VISIT_SEQ(st, stmt, s->v.For.body);
886 if (s->v.For.orelse)
887 VISIT_SEQ(st, stmt, s->v.For.orelse);
888 break;
889 case While_kind:
890 VISIT(st, expr, s->v.While.test);
891 VISIT_SEQ(st, stmt, s->v.While.body);
892 if (s->v.While.orelse)
893 VISIT_SEQ(st, stmt, s->v.While.orelse);
894 break;
895 case If_kind:
896 /* XXX if 0: and lookup_yield() hacks */
897 VISIT(st, expr, s->v.If.test);
898 VISIT_SEQ(st, stmt, s->v.If.body);
899 if (s->v.If.orelse)
900 VISIT_SEQ(st, stmt, s->v.If.orelse);
901 break;
902 case Raise_kind:
903 if (s->v.Raise.type) {
904 VISIT(st, expr, s->v.Raise.type);
905 if (s->v.Raise.inst) {
906 VISIT(st, expr, s->v.Raise.inst);
907 if (s->v.Raise.tback)
908 VISIT(st, expr, s->v.Raise.tback);
909 }
910 }
911 break;
912 case TryExcept_kind:
913 VISIT_SEQ(st, stmt, s->v.TryExcept.body);
914 VISIT_SEQ(st, stmt, s->v.TryExcept.orelse);
915 VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers);
916 break;
917 case TryFinally_kind:
918 VISIT_SEQ(st, stmt, s->v.TryFinally.body);
919 VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody);
920 break;
921 case Assert_kind:
922 VISIT(st, expr, s->v.Assert.test);
923 if (s->v.Assert.msg)
924 VISIT(st, expr, s->v.Assert.msg);
925 break;
926 case Import_kind:
927 VISIT_SEQ(st, alias, s->v.Import.names);
928 /* XXX Don't have the lineno available inside
929 visit_alias */
930 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
931 st->st_cur->ste_opt_lineno = s->lineno;
932 break;
933 case ImportFrom_kind:
934 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
935 /* XXX Don't have the lineno available inside
936 visit_alias */
937 if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno)
938 st->st_cur->ste_opt_lineno = s->lineno;
939 break;
940 case Exec_kind:
941 VISIT(st, expr, s->v.Exec.body);
942 if (!st->st_cur->ste_opt_lineno)
943 st->st_cur->ste_opt_lineno = s->lineno;
944 if (s->v.Exec.globals) {
945 st->st_cur->ste_unoptimized |= OPT_EXEC;
946 VISIT(st, expr, s->v.Exec.globals);
947 if (s->v.Exec.locals)
948 VISIT(st, expr, s->v.Exec.locals);
949 } else {
950 st->st_cur->ste_unoptimized |= OPT_BARE_EXEC;
951 }
952 break;
953 case Global_kind: {
954 int i;
955 asdl_seq *seq = s->v.Global.names;
956 for (i = 0; i < asdl_seq_LEN(seq); i++) {
957 identifier name = asdl_seq_GET(seq, i);
958 char *c_name = PyString_AS_STRING(name);
959 int cur = symtable_lookup(st, name);
960 if (cur < 0)
961 return 0;
962 if (cur & (DEF_LOCAL | USE)) {
963 char buf[1000];
964 if (cur & DEF_LOCAL)
965 PyOS_snprintf(buf, sizeof(buf),
966 GLOBAL_AFTER_ASSIGN,
967 c_name);
968 else
969 PyOS_snprintf(buf, sizeof(buf),
970 GLOBAL_AFTER_USE,
971 c_name);
972 if (!symtable_warn(st, buf))
973 return 0;
974 }
975 if (!symtable_add_def(st, name, DEF_GLOBAL))
976 return 0;
977
978 }
979
980 break;
981 }
982 case Expr_kind:
983 VISIT(st, expr, s->v.Expr.value);
984 break;
985 case Pass_kind:
986 case Break_kind:
987 case Continue_kind:
988 /* nothing to do here */
989 break;
990 }
991 return 1;
992}
993
994static int
995symtable_visit_expr(struct symtable *st, expr_ty e)
996{
997 switch (e->kind) {
998 case BoolOp_kind:
999 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1000 break;
1001 case BinOp_kind:
1002 VISIT(st, expr, e->v.BinOp.left);
1003 VISIT(st, expr, e->v.BinOp.right);
1004 break;
1005 case UnaryOp_kind:
1006 VISIT(st, expr, e->v.UnaryOp.operand);
1007 break;
1008 case Lambda_kind: {
1009 if (!symtable_add_def(st, GET_IDENTIFIER(lambda), DEF_LOCAL))
1010 return 0;
1011 if (e->v.Lambda.args->defaults)
1012 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
1013 /* XXX how to get line numbers for expressions */
1014 if (!symtable_enter_block(st, GET_IDENTIFIER(lambda),
1015 FunctionBlock, (void *)e, 0))
1016 return 0;
1017 VISIT(st, arguments, e->v.Lambda.args);
1018 VISIT(st, expr, e->v.Lambda.body);
1019 if (!symtable_exit_block(st, (void *)e))
1020 return 0;
1021 break;
1022 }
1023 case Dict_kind:
1024 VISIT_SEQ(st, expr, e->v.Dict.keys);
1025 VISIT_SEQ(st, expr, e->v.Dict.values);
1026 break;
1027 case ListComp_kind: {
1028 char tmpname[256];
1029 identifier tmp;
1030
1031 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
1032 ++st->st_cur->ste_tmpname);
1033 tmp = PyString_FromString(tmpname);
1034 if (!symtable_add_def(st, tmp, DEF_LOCAL))
1035 return 0;
1036 VISIT(st, expr, e->v.ListComp.elt);
1037 VISIT_SEQ(st, comprehension, e->v.ListComp.generators);
1038 break;
1039 }
1040 case GeneratorExp_kind: {
1041 if (!symtable_visit_genexp(st, e)) {
1042 return 0;
1043 }
1044 break;
1045 }
1046 case Yield_kind:
1047 if (e->v.Yield.value)
1048 VISIT(st, expr, e->v.Yield.value);
1049 st->st_cur->ste_generator = 1;
1050 break;
1051 case Compare_kind:
1052 VISIT(st, expr, e->v.Compare.left);
1053 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1054 break;
1055 case Call_kind:
1056 VISIT(st, expr, e->v.Call.func);
1057 VISIT_SEQ(st, expr, e->v.Call.args);
1058 VISIT_SEQ(st, keyword, e->v.Call.keywords);
1059 if (e->v.Call.starargs)
1060 VISIT(st, expr, e->v.Call.starargs);
1061 if (e->v.Call.kwargs)
1062 VISIT(st, expr, e->v.Call.kwargs);
1063 break;
1064 case Repr_kind:
1065 VISIT(st, expr, e->v.Repr.value);
1066 break;
1067 case Num_kind:
1068 case Str_kind:
1069 /* Nothing to do here. */
1070 break;
1071 /* The following exprs can be assignment targets. */
1072 case Attribute_kind:
1073 VISIT(st, expr, e->v.Attribute.value);
1074 break;
1075 case Subscript_kind:
1076 VISIT(st, expr, e->v.Subscript.value);
1077 VISIT(st, slice, e->v.Subscript.slice);
1078 break;
1079 case Name_kind:
1080 if (!symtable_add_def(st, e->v.Name.id,
1081 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
1082 return 0;
1083 break;
1084 /* child nodes of List and Tuple will have expr_context set */
1085 case List_kind:
1086 VISIT_SEQ(st, expr, e->v.List.elts);
1087 break;
1088 case Tuple_kind:
1089 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1090 break;
1091 }
1092 return 1;
1093}
1094
1095static int
1096symtable_implicit_arg(struct symtable *st, int pos)
1097{
1098 PyObject *id = PyString_FromFormat(".%d", pos);
1099 if (id == NULL)
1100 return 0;
1101 if (!symtable_add_def(st, id, DEF_PARAM)) {
1102 Py_DECREF(id);
1103 return 0;
1104 }
1105 Py_DECREF(id);
1106 return 1;
1107}
1108
1109static int
1110symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel)
1111{
1112 int i, complex = 0;
1113
1114 /* go through all the toplevel arguments first */
1115 for (i = 0; i < asdl_seq_LEN(args); i++) {
1116 expr_ty arg = asdl_seq_GET(args, i);
1117 if (arg->kind == Name_kind) {
1118 assert(arg->v.Name.ctx == Param ||
1119 (arg->v.Name.ctx == Store && !toplevel));
1120 if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM))
1121 return 0;
1122 }
1123 else if (arg->kind == Tuple_kind) {
1124 assert(arg->v.Tuple.ctx == Store);
1125 complex = 1;
1126 if (toplevel) {
1127 if (!symtable_implicit_arg(st, i))
1128 return 0;
1129 }
1130 }
1131 else {
1132 /* syntax error */
1133 fprintf(stderr, "unexpected expr in parameter list\n");
1134 return 0;
1135 }
1136 }
1137
1138 if (!toplevel) {
1139 if (!symtable_visit_params_nested(st, args))
1140 return 0;
1141 }
1142
1143 return 1;
1144}
1145
1146static int
1147symtable_visit_params_nested(struct symtable *st, asdl_seq *args)
1148{
1149 int i;
1150 for (i = 0; i < asdl_seq_LEN(args); i++) {
1151 expr_ty arg = asdl_seq_GET(args, i);
1152 if (arg->kind == Tuple_kind &&
1153 !symtable_visit_params(st, arg->v.Tuple.elts, 0))
1154 return 0;
1155 }
1156
1157 return 1;
1158}
1159
1160static int
1161symtable_visit_arguments(struct symtable *st, arguments_ty a)
1162{
1163 /* skip default arguments inside function block
1164 XXX should ast be different?
1165 */
1166 if (a->args && !symtable_visit_params(st, a->args, 1))
1167 return 0;
1168 if (a->vararg) {
1169 if (!symtable_add_def(st, a->vararg, DEF_PARAM))
1170 return 0;
1171 st->st_cur->ste_varargs = 1;
1172 }
1173 if (a->kwarg) {
1174 if (!symtable_add_def(st, a->kwarg, DEF_PARAM))
1175 return 0;
1176 st->st_cur->ste_varkeywords = 1;
1177 }
1178 if (a->args && !symtable_visit_params_nested(st, a->args))
1179 return 0;
1180 return 1;
1181}
1182
1183
1184static int
1185symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1186{
1187 if (eh->type)
1188 VISIT(st, expr, eh->type);
1189 if (eh->name)
1190 VISIT(st, expr, eh->name);
1191 VISIT_SEQ(st, stmt, eh->body);
1192 return 1;
1193}
1194
1195
1196static int
1197symtable_visit_alias(struct symtable *st, alias_ty a)
1198{
1199 /* Compute store_name, the name actually bound by the import
1200 operation. It is diferent than a->name when a->name is a
1201 dotted package name (e.g. spam.eggs)
1202 */
1203 PyObject *store_name;
1204 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
1205 const char *base = PyString_AS_STRING(name);
1206 char *dot = strchr(base, '.');
1207 if (dot)
1208 store_name = PyString_FromStringAndSize(base, dot - base);
1209 else {
1210 store_name = name;
1211 Py_INCREF(store_name);
1212 }
1213 if (strcmp(PyString_AS_STRING(name), "*")) {
1214 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1215 Py_DECREF(store_name);
1216 return r;
1217 }
1218 else {
1219 if (st->st_cur->ste_type != ModuleBlock) {
1220 if (!symtable_warn(st,
1221 "import * only allowed at module level"))
1222 return 0;
1223 }
1224 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1225 return 1;
1226 }
1227}
1228
1229
1230static int
1231symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1232{
1233 VISIT(st, expr, lc->target);
1234 VISIT(st, expr, lc->iter);
1235 VISIT_SEQ(st, expr, lc->ifs);
1236 return 1;
1237}
1238
1239
1240static int
1241symtable_visit_keyword(struct symtable *st, keyword_ty k)
1242{
1243 VISIT(st, expr, k->value);
1244 return 1;
1245}
1246
1247
1248static int
1249symtable_visit_slice(struct symtable *st, slice_ty s)
1250{
1251 switch (s->kind) {
1252 case Slice_kind:
1253 if (s->v.Slice.lower)
1254 VISIT(st, expr, s->v.Slice.lower)
1255 if (s->v.Slice.upper)
1256 VISIT(st, expr, s->v.Slice.upper)
1257 if (s->v.Slice.step)
1258 VISIT(st, expr, s->v.Slice.step)
1259 break;
1260 case ExtSlice_kind:
1261 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1262 break;
1263 case Index_kind:
1264 VISIT(st, expr, s->v.Index.value)
1265 break;
1266 case Ellipsis_kind:
1267 break;
1268 }
1269 return 1;
1270}
1271
1272static int
1273symtable_visit_genexp(struct symtable *st, expr_ty e)
1274{
1275 identifier tmp;
1276 comprehension_ty outermost = ((comprehension_ty)
1277 (asdl_seq_GET(e->v.GeneratorExp.generators, 0)));
1278 /* Outermost iterator is evaluated in current scope */
1279 VISIT(st, expr, outermost->iter);
1280 /* Create generator scope for the rest */
1281 tmp = PyString_FromString("<genexpr>");
1282 if (!symtable_enter_block(st, tmp, FunctionBlock, (void *)e, 0)) {
1283 return 0;
1284 }
1285 st->st_cur->ste_generator = 1;
1286 /* Outermost iter is received as an argument */
1287 if (!symtable_implicit_arg(st, 0)) {
1288 return 0;
1289 }
1290 VISIT(st, expr, outermost->target);
1291 VISIT_SEQ(st, expr, outermost->ifs);
1292 VISIT_SEQ_TAIL(st, comprehension, e->v.GeneratorExp.generators, 1);
1293 VISIT(st, expr, e->v.GeneratorExp.elt);
1294 if (!symtable_exit_block(st, (void *)e))
1295 return 0;
1296 return 1;
1297}