annotate Python/compile.c @ 34622:516bb8fd75b4 legacy-trunk

Teach the peepholer to fold unary operations on constants. Afterwards, -0.5 loads in a single step and no longer requires a runtime UNARY_NEGATIVE operation.
author Raymond Hettinger <python@rcn.com>
date Sun, 20 Feb 2005 12:41:32 +0000
parents 369e8e71ea33
children fcacf9d4b8f0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* Compile an expression node to intermediate code */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3 /* XXX TO DO:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4 XXX add __doc__ attribute == co_doc to code object attributes?
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
5 XXX (it's currently the first item of the co_const tuple)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
6 XXX Generate simple jump for break/return outside 'try...finally'
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
7 XXX Allow 'continue' inside finally clause of try-finally
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
8 XXX New opcode for loading the initial index for a for loop
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
9 XXX other JAR tricks?
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
10 */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
12 #include "Python.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
13
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #include "node.h"
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #include "token.h"
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #include "graminit.h"
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #include "compile.h"
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
18 #include "symtable.h"
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 #include "opcode.h"
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
20 #include "structmember.h"
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
21
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
22 #include <ctype.h>
176
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
23
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
24 /* Three symbols from graminit.h are also defined in Python.h, with
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
25 Py_ prefixes to their names. Python.h can't include graminit.h
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
26 (which defines too many confusing symbols), but we can check here
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
27 that they haven't changed (which is very unlikely, but possible). */
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
28 #if Py_single_input != single_input
5566
19d4a2874645 Indent the #error directives so a classic K&R cpp doesn't see them.
Guido van Rossum <guido@python.org>
parents: 5453
diff changeset
29 #error "single_input has changed -- update Py_single_input in Python.h"
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
30 #endif
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
31 #if Py_file_input != file_input
5566
19d4a2874645 Indent the #error directives so a classic K&R cpp doesn't see them.
Guido van Rossum <guido@python.org>
parents: 5453
diff changeset
32 #error "file_input has changed -- update Py_file_input in Python.h"
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
33 #endif
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
34 #if Py_eval_input != eval_input
5566
19d4a2874645 Indent the #error directives so a classic K&R cpp doesn't see them.
Guido van Rossum <guido@python.org>
parents: 5453
diff changeset
35 #error "eval_input has changed -- update Py_eval_input in Python.h"
5438
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
36 #endif
e926c331ebbf Instead of importing graminit.h whenever one of the three grammar 'root'
Guido van Rossum <guido@python.org>
parents: 5373
diff changeset
37
5164
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
38 int Py_OptimizeFlag = 0;
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
39
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
40 #define OP_DELETE 0
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
41 #define OP_ASSIGN 1
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
42 #define OP_APPLY 2
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
43
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
44 #define VAR_LOAD 0
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
45 #define VAR_STORE 1
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
46 #define VAR_DELETE 2
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
47
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
48 #define DEL_CLOSURE_ERROR \
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
49 "can not delete variable '%.400s' referenced in nested scope"
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
50
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
51 #define DUPLICATE_ARGUMENT \
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
52 "duplicate argument '%s' in function definition"
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
53
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
54 #define GLOBAL_AFTER_ASSIGN \
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
55 "name '%.400s' is assigned to before global declaration"
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
56
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
57 #define GLOBAL_AFTER_USE \
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
58 "name '%.400s' is used prior to global declaration"
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
59
30713
9c7e0227c835 Patch #792869: Clarify error message for parameters declared global,
Martin v. Löwis <martin@v.loewis.de>
parents: 30683
diff changeset
60 #define PARAM_GLOBAL \
21833
070d392042e3 Fix spelling mistakes. Bugfix candidates.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21725
diff changeset
61 "name '%.400s' is a function parameter and declared global"
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
62
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
63 #define LATE_FUTURE \
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
64 "from __future__ imports must occur at the beginning of the file"
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
65
17185
f9a30015e8b0 Make it illegal to assign to __debug__ as per Guido's request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17132
diff changeset
66 #define ASSIGN_DEBUG \
f9a30015e8b0 Make it illegal to assign to __debug__ as per Guido's request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17132
diff changeset
67 "can not assign to __debug__"
f9a30015e8b0 Make it illegal to assign to __debug__ as per Guido's request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17132
diff changeset
68
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
69 #define MANGLE_LEN 256
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
70
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
71 #define OFF(x) offsetof(PyCodeObject, x)
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
72
20012
3faabf54b8b8 Add optional docstrings to member descriptors. For backwards
Guido van Rossum <guido@python.org>
parents: 19915
diff changeset
73 static PyMemberDef code_memberlist[] = {
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
74 {"co_argcount", T_INT, OFF(co_argcount), READONLY},
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
75 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
76 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
77 {"co_flags", T_INT, OFF(co_flags), READONLY},
568
92509d304e47 add READONLY to member list.
Guido van Rossum <guido@python.org>
parents: 544
diff changeset
78 {"co_code", T_OBJECT, OFF(co_code), READONLY},
92509d304e47 add READONLY to member list.
Guido van Rossum <guido@python.org>
parents: 544
diff changeset
79 {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
92509d304e47 add READONLY to member list.
Guido van Rossum <guido@python.org>
parents: 544
diff changeset
80 {"co_names", T_OBJECT, OFF(co_names), READONLY},
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
81 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
82 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
83 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
568
92509d304e47 add READONLY to member list.
Guido van Rossum <guido@python.org>
parents: 544
diff changeset
84 {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
85 {"co_name", T_OBJECT, OFF(co_name), READONLY},
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
86 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
87 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
88 {NULL} /* Sentinel */
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
89 };
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
90
33100
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
91 /* Helper for code_new: return a shallow copy of a tuple that is
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
92 guaranteed to contain exact strings, by converting string subclasses
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
93 to exact strings and complaining if a non-string is found. */
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
94 static PyObject*
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
95 validate_and_copy_tuple(PyObject *tup)
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
96 {
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
97 PyObject *newtuple;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
98 PyObject *item;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
99 int i, len;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
100
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
101 len = PyTuple_GET_SIZE(tup);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
102 newtuple = PyTuple_New(len);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
103 if (newtuple == NULL)
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
104 return NULL;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
105
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
106 for (i = 0; i < len; i++) {
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
107 item = PyTuple_GET_ITEM(tup, i);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
108 if (PyString_CheckExact(item)) {
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
109 Py_INCREF(item);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
110 }
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
111 else if (!PyString_Check(item)) {
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
112 PyErr_Format(
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
113 PyExc_TypeError,
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
114 "name tuples must contain only "
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
115 "strings, not '%.500s'",
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
116 item->ob_type->tp_name);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
117 Py_DECREF(newtuple);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
118 return NULL;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
119 }
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
120 else {
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
121 item = PyString_FromStringAndSize(
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
122 PyString_AS_STRING(item),
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
123 PyString_GET_SIZE(item));
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
124 if (item == NULL) {
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
125 Py_DECREF(newtuple);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
126 return NULL;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
127 }
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
128 }
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
129 PyTuple_SET_ITEM(newtuple, i, item);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
130 }
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
131
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
132 return newtuple;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
133 }
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
134
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
135 PyDoc_STRVAR(code_doc,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
136 "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
137 varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
138 \n\
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
139 Create a code object. Not for the faint of heart.");
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
140
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
141 static PyObject *
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
142 code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
143 {
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
144 int argcount;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
145 int nlocals;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
146 int stacksize;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
147 int flags;
33103
6b8e499687d6 code_new(): Wouldn't compile on Windows, because of gcc'ism.
Tim Peters <tim.peters@gmail.com>
parents: 33100
diff changeset
148 PyObject *co = NULL;
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
149 PyObject *code;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
150 PyObject *consts;
33100
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
151 PyObject *names, *ournames = NULL;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
152 PyObject *varnames, *ourvarnames = NULL;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
153 PyObject *freevars = NULL, *ourfreevars = NULL;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
154 PyObject *cellvars = NULL, *ourcellvars = NULL;
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
155 PyObject *filename;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
156 PyObject *name;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
157 int firstlineno;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
158 PyObject *lnotab;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
159
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
160 if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
161 &argcount, &nlocals, &stacksize, &flags,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
162 &code,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
163 &PyTuple_Type, &consts,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
164 &PyTuple_Type, &names,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
165 &PyTuple_Type, &varnames,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
166 &filename, &name,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
167 &firstlineno, &lnotab,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
168 &PyTuple_Type, &freevars,
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
169 &PyTuple_Type, &cellvars))
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
170 return NULL;
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
171
33100
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
172 if (argcount < 0) {
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
173 PyErr_SetString(
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
174 PyExc_ValueError,
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
175 "code: argcount must not be negative");
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
176 goto cleanup;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
177 }
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
178
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
179 if (nlocals < 0) {
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
180 PyErr_SetString(
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
181 PyExc_ValueError,
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
182 "code: nlocals must not be negative");
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
183 goto cleanup;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
184 }
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
185
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
186 ournames = validate_and_copy_tuple(names);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
187 if (ournames == NULL)
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
188 goto cleanup;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
189 ourvarnames = validate_and_copy_tuple(varnames);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
190 if (ourvarnames == NULL)
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
191 goto cleanup;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
192 if (freevars)
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
193 ourfreevars = validate_and_copy_tuple(freevars);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
194 else
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
195 ourfreevars = PyTuple_New(0);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
196 if (ourfreevars == NULL)
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
197 goto cleanup;
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
198 if (cellvars)
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
199 ourcellvars = validate_and_copy_tuple(cellvars);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
200 else
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
201 ourcellvars = PyTuple_New(0);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
202 if (ourcellvars == NULL)
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
203 goto cleanup;
30428
c2c10f1eb8e2 Fix leak discovered in test_new by Michael Hudson.
Raymond Hettinger <python@rcn.com>
parents: 30226
diff changeset
204
c2c10f1eb8e2 Fix leak discovered in test_new by Michael Hudson.
Raymond Hettinger <python@rcn.com>
parents: 30226
diff changeset
205 co = (PyObject *) PyCode_New(argcount, nlocals, stacksize, flags,
33100
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
206 code, consts, ournames, ourvarnames,
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
207 ourfreevars, ourcellvars, filename,
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
208 name, firstlineno, lnotab);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
209 cleanup:
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
210 Py_XDECREF(ournames);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
211 Py_XDECREF(ourvarnames);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
212 Py_XDECREF(ourfreevars);
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
213 Py_XDECREF(ourcellvars);
30428
c2c10f1eb8e2 Fix leak discovered in test_new by Michael Hudson.
Raymond Hettinger <python@rcn.com>
parents: 30226
diff changeset
214 return co;
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
215 }
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
216
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
218 code_dealloc(PyCodeObject *co)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
220 Py_XDECREF(co->co_code);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
221 Py_XDECREF(co->co_consts);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
222 Py_XDECREF(co->co_names);
5901
ba49a6ef7214 Plug a leak in code_dealloc() (and reordered the deallocs to match the
Guido van Rossum <guido@python.org>
parents: 5754
diff changeset
223 Py_XDECREF(co->co_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
224 Py_XDECREF(co->co_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
225 Py_XDECREF(co->co_cellvars);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
226 Py_XDECREF(co->co_filename);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
227 Py_XDECREF(co->co_name);
5901
ba49a6ef7214 Plug a leak in code_dealloc() (and reordered the deallocs to match the
Guido van Rossum <guido@python.org>
parents: 5754
diff changeset
228 Py_XDECREF(co->co_lnotab);
12576
1c7c2dd1beb1 Vladimir Marangozov's long-awaited malloc restructuring.
Guido van Rossum <guido@python.org>
parents: 12538
diff changeset
229 PyObject_DEL(co);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
230 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
232 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
233 code_repr(PyCodeObject *co)
1029
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
234 {
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
235 char buf[500];
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
236 int lineno = -1;
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
237 char *filename = "???";
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
238 char *name = "???";
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
239
12278
647bf71912f0 Vladimir Marangozov: This fixes the line number in the string
Guido van Rossum <guido@python.org>
parents: 12086
diff changeset
240 if (co->co_firstlineno != 0)
647bf71912f0 Vladimir Marangozov: This fixes the line number in the string
Guido van Rossum <guido@python.org>
parents: 12086
diff changeset
241 lineno = co->co_firstlineno;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
242 if (co->co_filename && PyString_Check(co->co_filename))
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
243 filename = PyString_AS_STRING(co->co_filename);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
244 if (co->co_name && PyString_Check(co->co_name))
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
245 name = PyString_AS_STRING(co->co_name);
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
246 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
247 "<code object %.100s at %p, file \"%.300s\", line %d>",
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
248 name, co, filename, lineno);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
249 return PyString_FromString(buf);
1029
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
250 }
2f9805c963e8 Give code objects a more useful representation.
Guido van Rossum <guido@python.org>
parents: 792
diff changeset
251
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
252 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
253 code_compare(PyCodeObject *co, PyCodeObject *cp)
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
254 {
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
255 int cmp;
12333
525b2358721e Patch by Vladimir Marangozov to include the function name when
Guido van Rossum <guido@python.org>
parents: 12278
diff changeset
256 cmp = PyObject_Compare(co->co_name, cp->co_name);
525b2358721e Patch by Vladimir Marangozov to include the function name when
Guido van Rossum <guido@python.org>
parents: 12278
diff changeset
257 if (cmp) return cmp;
6186
2e755d1ff661 Cprrect stuoid tyops -- was comparing variabes with themselves because
Guido van Rossum <guido@python.org>
parents: 5901
diff changeset
258 cmp = co->co_argcount - cp->co_argcount;
25538
2c0e0f0dca6f Clamp code objects' tp_compare result to [-1, 1].
Michael W. Hudson <mwh@python.net>
parents: 25078
diff changeset
259 if (cmp) return (cmp<0)?-1:1;
6186
2e755d1ff661 Cprrect stuoid tyops -- was comparing variabes with themselves because
Guido van Rossum <guido@python.org>
parents: 5901
diff changeset
260 cmp = co->co_nlocals - cp->co_nlocals;
25538
2c0e0f0dca6f Clamp code objects' tp_compare result to [-1, 1].
Michael W. Hudson <mwh@python.net>
parents: 25078
diff changeset
261 if (cmp) return (cmp<0)?-1:1;
6186
2e755d1ff661 Cprrect stuoid tyops -- was comparing variabes with themselves because
Guido van Rossum <guido@python.org>
parents: 5901
diff changeset
262 cmp = co->co_flags - cp->co_flags;
25538
2c0e0f0dca6f Clamp code objects' tp_compare result to [-1, 1].
Michael W. Hudson <mwh@python.net>
parents: 25078
diff changeset
263 if (cmp) return (cmp<0)?-1:1;
33822
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33627
diff changeset
264 cmp = co->co_firstlineno - cp->co_firstlineno;
bf18caca0730 SF bug #1048870: call arg of lambda not updating
Raymond Hettinger <python@rcn.com>
parents: 33627
diff changeset
265 if (cmp) return (cmp<0)?-1:1;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
266 cmp = PyObject_Compare(co->co_code, cp->co_code);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
267 if (cmp) return cmp;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
268 cmp = PyObject_Compare(co->co_consts, cp->co_consts);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
269 if (cmp) return cmp;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
270 cmp = PyObject_Compare(co->co_names, cp->co_names);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
271 if (cmp) return cmp;
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
272 cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
273 if (cmp) return cmp;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
274 cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
275 if (cmp) return cmp;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
276 cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
277 return cmp;
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
278 }
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
279
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
280 static long
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
281 code_hash(PyCodeObject *co)
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
282 {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
283 long h, h0, h1, h2, h3, h4, h5, h6;
12333
525b2358721e Patch by Vladimir Marangozov to include the function name when
Guido van Rossum <guido@python.org>
parents: 12278
diff changeset
284 h0 = PyObject_Hash(co->co_name);
525b2358721e Patch by Vladimir Marangozov to include the function name when
Guido van Rossum <guido@python.org>
parents: 12278
diff changeset
285 if (h0 == -1) return -1;
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
286 h1 = PyObject_Hash(co->co_code);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
287 if (h1 == -1) return -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
288 h2 = PyObject_Hash(co->co_consts);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
289 if (h2 == -1) return -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
290 h3 = PyObject_Hash(co->co_names);
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
291 if (h3 == -1) return -1;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
292 h4 = PyObject_Hash(co->co_varnames);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
293 if (h4 == -1) return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
294 h5 = PyObject_Hash(co->co_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
295 if (h5 == -1) return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
296 h6 = PyObject_Hash(co->co_cellvars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
297 if (h6 == -1) return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
298 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
299 co->co_argcount ^ co->co_nlocals ^ co->co_flags;
1399
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
300 if (h == -1) h = -2;
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
301 return h;
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
302 }
cc172ce05561 Added compare operations for functions and code objects.
Guido van Rossum <guido@python.org>
parents: 1369
diff changeset
303
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
304 /* XXX code objects need to participate in GC? */
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
305
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
306 PyTypeObject PyCode_Type = {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
307 PyObject_HEAD_INIT(&PyType_Type)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 0,
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 "code",
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
310 sizeof(PyCodeObject),
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 0,
19915
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
312 (destructor)code_dealloc, /* tp_dealloc */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
313 0, /* tp_print */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
314 0, /* tp_getattr */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
315 0, /* tp_setattr */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
316 (cmpfunc)code_compare, /* tp_compare */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
317 (reprfunc)code_repr, /* tp_repr */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
318 0, /* tp_as_number */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
319 0, /* tp_as_sequence */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
320 0, /* tp_as_mapping */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
321 (hashfunc)code_hash, /* tp_hash */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
322 0, /* tp_call */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
323 0, /* tp_str */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
324 PyObject_GenericGetAttr, /* tp_getattro */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
325 0, /* tp_setattro */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
326 0, /* tp_as_buffer */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
327 Py_TPFLAGS_DEFAULT, /* tp_flags */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
328 code_doc, /* tp_doc */
19915
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
329 0, /* tp_traverse */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
330 0, /* tp_clear */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
331 0, /* tp_richcompare */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
332 0, /* tp_weaklistoffset */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
333 0, /* tp_iter */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
334 0, /* tp_iternext */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
335 0, /* tp_methods */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
336 code_memberlist, /* tp_members */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
337 0, /* tp_getset */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
338 0, /* tp_base */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
339 0, /* tp_dict */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
340 0, /* tp_descr_get */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
341 0, /* tp_descr_set */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
342 0, /* tp_dictoffset */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
343 0, /* tp_init */
2263cb43eaab Supply code objects a new-style tp_members slot and tp_getattr impl.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19793
diff changeset
344 0, /* tp_alloc */
23868
8e6760bf1884 SF patch 568629 by Oren Tirosh: types made callable.
Guido van Rossum <guido@python.org>
parents: 23589
diff changeset
345 code_new, /* tp_new */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 };
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347
5287
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 5264
diff changeset
348 #define NAME_CHARS \
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 5264
diff changeset
349 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
5af766a1b531 Tweaks to keep the Microsoft compiler quier.
Guido van Rossum <guido@python.org>
parents: 5264
diff changeset
350
16006
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
351 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
352
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
353 static int
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
354 all_name_chars(unsigned char *s)
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
355 {
16523
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
356 static char ok_name_char[256];
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
357 static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
358
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
359 if (ok_name_char[*name_chars] == 0) {
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
360 unsigned char *p;
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
361 for (p = name_chars; *p; p++)
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
362 ok_name_char[*p] = 1;
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
363 }
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
364 while (*s) {
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
365 if (ok_name_char[*s++] == 0)
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
366 return 0;
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
367 }
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
368 return 1;
16006
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
369 }
51811d78a9da SF Patch #103250, by pj99: Optimize a strspn() out of startup.
Guido van Rossum <guido@python.org>
parents: 15438
diff changeset
370
33100
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
371 static void
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
372 intern_strings(PyObject *tuple)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
373 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
374 int i;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
375
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
376 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
377 PyObject *v = PyTuple_GET_ITEM(tuple, i);
33100
356887410bde Fix bug
Michael W. Hudson <mwh@python.net>
parents: 32977
diff changeset
378 if (v == NULL || !PyString_CheckExact(v)) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
379 Py_FatalError("non-string found in code slot");
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
380 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
381 PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
382 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
383 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
384
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
385 /* Begin: Peephole optimizations ----------------------------------------- */
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
386
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
387 #define GETARG(arr, i) ((int)((arr[i+2]<<8) + arr[i+1]))
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
388 #define UNCONDITIONAL_JUMP(op) (op==JUMP_ABSOLUTE || op==JUMP_FORWARD)
28417
1a2fd109f6b6 Factored out test for absolute jumps.
Raymond Hettinger <python@rcn.com>
parents: 28406
diff changeset
389 #define ABSOLUTE_JUMP(op) (op==JUMP_ABSOLUTE || op==CONTINUE_LOOP)
1a2fd109f6b6 Factored out test for absolute jumps.
Raymond Hettinger <python@rcn.com>
parents: 28406
diff changeset
390 #define GETJUMPTGT(arr, i) (GETARG(arr,i) + (ABSOLUTE_JUMP(arr[i]) ? 0 : i+3))
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
391 #define SETARG(arr, i, val) arr[i+2] = val>>8; arr[i+1] = val & 255
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
392 #define CODESIZE(op) (HAS_ARG(op) ? 3 : 1)
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
393 #define ISBASICBLOCK(blocks, start, bytes) (blocks[start]==blocks[start+bytes-1])
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
394
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
395 /* Replace LOAD_CONST c1. LOAD_CONST c2 ... LOAD_CONST cn BUILD_TUPLE n
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
396 with LOAD_CONST (c1, c2, ... cn).
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
397 The consts table must still be in list form so that the
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
398 new constant (c1, c2, ... cn) can be appended.
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
399 Called with codestr pointing to the first LOAD_CONST.
34553
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
400 Bails out with no change if one or more of the LOAD_CONSTs is missing.
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
401 Also works for BUILD_LIST when followed by an "in" or "not in" test.
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
402 */
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
403 static int
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
404 tuple_of_constants(unsigned char *codestr, int n, PyObject *consts)
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
405 {
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
406 PyObject *newconst, *constant;
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
407 int i, arg, len_consts;
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
408
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
409 /* Pre-conditions */
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
410 assert(PyList_CheckExact(consts));
34553
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
411 assert(codestr[n*3] == BUILD_TUPLE || codestr[n*3] == BUILD_LIST);
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
412 assert(GETARG(codestr, (n*3)) == n);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
413 for (i=0 ; i<n ; i++)
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
414 assert(codestr[i*3] == LOAD_CONST);
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
415
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
416 /* Buildup new tuple of constants */
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
417 newconst = PyTuple_New(n);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
418 if (newconst == NULL)
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
419 return 0;
33838
fdce3d39bec1 SF bug #1053819: Segfault in tuple_of_constants
Raymond Hettinger <python@rcn.com>
parents: 33822
diff changeset
420 len_consts = PyList_GET_SIZE(consts);
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
421 for (i=0 ; i<n ; i++) {
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
422 arg = GETARG(codestr, (i*3));
33838
fdce3d39bec1 SF bug #1053819: Segfault in tuple_of_constants
Raymond Hettinger <python@rcn.com>
parents: 33822
diff changeset
423 assert(arg < len_consts);
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
424 constant = PyList_GET_ITEM(consts, arg);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
425 Py_INCREF(constant);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
426 PyTuple_SET_ITEM(newconst, i, constant);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
427 }
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
428
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
429 /* Append folded constant onto consts */
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
430 if (PyList_Append(consts, newconst)) {
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
431 Py_DECREF(newconst);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
432 return 0;
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
433 }
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
434 Py_DECREF(newconst);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
435
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
436 /* Write NOPs over old LOAD_CONSTS and
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
437 add a new LOAD_CONST newconst on top of the BUILD_TUPLE n */
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
438 memset(codestr, NOP, n*3);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
439 codestr[n*3] = LOAD_CONST;
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
440 SETARG(codestr, (n*3), len_consts);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
441 return 1;
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
442 }
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
443
34305
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
444 /* Replace LOAD_CONST c1. LOAD_CONST c2 BINOP
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
445 with LOAD_CONST binop(c1,c2)
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
446 The consts table must still be in list form so that the
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
447 new constant can be appended.
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
448 Called with codestr pointing to the first LOAD_CONST.
34506
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
449 Abandons the transformation if the folding fails (i.e. 1+'a').
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
450 If the new constant is a sequence, only folds when the size
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
451 is below a threshold value. That keeps pyc files from
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
452 becoming large in the presence of code like: (None,)*1000.
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
453 */
34305
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
454 static int
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
455 fold_binops_on_constants(unsigned char *codestr, PyObject *consts)
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
456 {
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
457 PyObject *newconst, *v, *w;
34506
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
458 int len_consts, opcode, size;
34305
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
459
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
460 /* Pre-conditions */
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
461 assert(PyList_CheckExact(consts));
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
462 assert(codestr[0] == LOAD_CONST);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
463 assert(codestr[3] == LOAD_CONST);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
464
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
465 /* Create new constant */
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
466 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
467 w = PyList_GET_ITEM(consts, GETARG(codestr, 3));
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
468 opcode = codestr[6];
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
469 switch (opcode) {
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
470 case BINARY_POWER:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
471 newconst = PyNumber_Power(v, w, Py_None);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
472 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
473 case BINARY_MULTIPLY:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
474 newconst = PyNumber_Multiply(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
475 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
476 case BINARY_DIVIDE:
34506
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
477 /* Cannot fold this operation statically since
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
478 the result can depend on the run-time presence of the -Qnew flag */
34359
d81b6173f050 Re-running python with/without the -Qnew flag uses incorrectly optimized
Armin Rigo <arigo@tunes.org>
parents: 34305
diff changeset
479 return 0;
34305
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
480 case BINARY_TRUE_DIVIDE:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
481 newconst = PyNumber_TrueDivide(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
482 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
483 case BINARY_FLOOR_DIVIDE:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
484 newconst = PyNumber_FloorDivide(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
485 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
486 case BINARY_MODULO:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
487 newconst = PyNumber_Remainder(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
488 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
489 case BINARY_ADD:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
490 newconst = PyNumber_Add(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
491 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
492 case BINARY_SUBTRACT:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
493 newconst = PyNumber_Subtract(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
494 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
495 case BINARY_SUBSCR:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
496 newconst = PyObject_GetItem(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
497 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
498 case BINARY_LSHIFT:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
499 newconst = PyNumber_Lshift(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
500 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
501 case BINARY_RSHIFT:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
502 newconst = PyNumber_Rshift(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
503 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
504 case BINARY_AND:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
505 newconst = PyNumber_And(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
506 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
507 case BINARY_XOR:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
508 newconst = PyNumber_Xor(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
509 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
510 case BINARY_OR:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
511 newconst = PyNumber_Or(v, w);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
512 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
513 default:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
514 /* Called with an unknown opcode */
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
515 assert(0);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
516 return 0;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
517 }
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
518 if (newconst == NULL) {
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
519 PyErr_Clear();
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
520 return 0;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
521 }
34506
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
522 size = PyObject_Size(newconst);
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
523 if (size == -1)
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
524 PyErr_Clear();
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
525 else if (size > 20) {
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
526 Py_DECREF(newconst);
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
527 return 0;
bda933873758 Do not fold a constant if a large sequence will result.
Raymond Hettinger <python@rcn.com>
parents: 34359
diff changeset
528 }
34305
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
529
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
530 /* Append folded constant into consts table */
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
531 len_consts = PyList_GET_SIZE(consts);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
532 if (PyList_Append(consts, newconst)) {
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
533 Py_DECREF(newconst);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
534 return 0;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
535 }
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
536 Py_DECREF(newconst);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
537
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
538 /* Write NOP NOP NOP NOP LOAD_CONST newconst */
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
539 memset(codestr, NOP, 4);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
540 codestr[4] = LOAD_CONST;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
541 SETARG(codestr, 4, len_consts);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
542 return 1;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
543 }
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
544
34622
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
545 static int
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
546 fold_unaryops_on_constants(unsigned char *codestr, PyObject *consts)
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
547 {
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
548 PyObject *newconst, *v;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
549 int len_consts, opcode;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
550
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
551 /* Pre-conditions */
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
552 assert(PyList_CheckExact(consts));
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
553 assert(codestr[0] == LOAD_CONST);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
554
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
555 /* Create new constant */
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
556 v = PyList_GET_ITEM(consts, GETARG(codestr, 0));
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
557 opcode = codestr[3];
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
558 switch (opcode) {
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
559 case UNARY_NEGATIVE:
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
560 newconst = PyNumber_Negative(v);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
561 break;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
562 case UNARY_CONVERT:
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
563 newconst = PyObject_Repr(v);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
564 break;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
565 case UNARY_INVERT:
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
566 newconst = PyNumber_Invert(v);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
567 break;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
568 default:
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
569 /* Called with an unknown opcode */
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
570 assert(0);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
571 return 0;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
572 }
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
573 if (newconst == NULL) {
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
574 PyErr_Clear();
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
575 return 0;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
576 }
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
577
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
578 /* Append folded constant into consts table */
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
579 len_consts = PyList_GET_SIZE(consts);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
580 if (PyList_Append(consts, newconst)) {
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
581 Py_DECREF(newconst);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
582 return 0;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
583 }
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
584 Py_DECREF(newconst);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
585
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
586 /* Write NOP LOAD_CONST newconst */
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
587 codestr[0] = NOP;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
588 codestr[1] = LOAD_CONST;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
589 SETARG(codestr, 1, len_consts);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
590 return 1;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
591 }
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
592
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
593 static unsigned int *
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
594 markblocks(unsigned char *code, int len)
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
595 {
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
596 unsigned int *blocks = PyMem_Malloc(len*sizeof(int));
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
597 int i,j, opcode, blockcnt = 0;
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
598
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
599 if (blocks == NULL)
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
600 return NULL;
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
601 memset(blocks, 0, len*sizeof(int));
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
602
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
603 /* Mark labels in the first pass */
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
604 for (i=0 ; i<len ; i+=CODESIZE(opcode)) {
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
605 opcode = code[i];
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
606 switch (opcode) {
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
607 case FOR_ITER:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
608 case JUMP_FORWARD:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
609 case JUMP_IF_FALSE:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
610 case JUMP_IF_TRUE:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
611 case JUMP_ABSOLUTE:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
612 case CONTINUE_LOOP:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
613 case SETUP_LOOP:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
614 case SETUP_EXCEPT:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
615 case SETUP_FINALLY:
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
616 j = GETJUMPTGT(code, i);
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
617 blocks[j] = 1;
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
618 break;
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
619 }
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
620 }
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
621 /* Build block numbers in the second pass */
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
622 for (i=0 ; i<len ; i++) {
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
623 blockcnt += blocks[i]; /* increment blockcnt over labels */
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
624 blocks[i] = blockcnt;
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
625 }
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
626 return blocks;
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
627 }
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
628
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
629 /* Perform basic peephole optimizations to components of a code object.
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
630 The consts object should still be in list form to allow new constants
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
631 to be appended.
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
632
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
633 To keep the optimizer simple, it bails out (does nothing) for code
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
634 containing extended arguments or that has a length over 32,700. That
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
635 allows us to avoid overflow and sign issues. Likewise, it bails when
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
636 the lineno table has complex encoding for gaps >= 255.
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
637
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
638 Optimizations are restricted to simple transformations occuring within a
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
639 single basic block. All transformations keep the code size the same or
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
640 smaller. For those that reduce size, the gaps are initially filled with
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
641 NOPs. Later those NOPs are removed and the jump addresses retargeted in
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
642 a single pass. Line numbering is adjusted accordingly. */
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
643
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
644 static PyObject *
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
645 optimize_code(PyObject *code, PyObject* consts, PyObject *names, PyObject *lineno_obj)
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
646 {
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
647 int i, j, codelen, nops, h, adj;
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
648 int tgt, tgttgt, opcode;
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
649 unsigned char *codestr = NULL;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
650 unsigned char *lineno;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
651 int *addrmap = NULL;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
652 int new_line, cum_orig_line, last_line, tabsiz;
33838
fdce3d39bec1 SF bug #1053819: Segfault in tuple_of_constants
Raymond Hettinger <python@rcn.com>
parents: 33822
diff changeset
653 int cumlc=0, lastlc=0; /* Count runs of consecutive LOAD_CONST codes */
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
654 unsigned int *blocks = NULL;
32711
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
655 char *name;
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
656
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
657 /* Bail out if an exception is set */
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
658 if (PyErr_Occurred())
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
659 goto exitUnchanged;
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
660
33272
bac997b79b19 Bypass peepholing of code with lineno tables having intervals >= 255.
Raymond Hettinger <python@rcn.com>
parents: 33271
diff changeset
661 /* Bypass optimization when the lineno table is too complex */
bac997b79b19 Bypass peepholing of code with lineno tables having intervals >= 255.
Raymond Hettinger <python@rcn.com>
parents: 33271
diff changeset
662 assert(PyString_Check(lineno_obj));
bac997b79b19 Bypass peepholing of code with lineno tables having intervals >= 255.
Raymond Hettinger <python@rcn.com>
parents: 33271
diff changeset
663 lineno = PyString_AS_STRING(lineno_obj);
bac997b79b19 Bypass peepholing of code with lineno tables having intervals >= 255.
Raymond Hettinger <python@rcn.com>
parents: 33271
diff changeset
664 tabsiz = PyString_GET_SIZE(lineno_obj);
bac997b79b19 Bypass peepholing of code with lineno tables having intervals >= 255.
Raymond Hettinger <python@rcn.com>
parents: 33271
diff changeset
665 if (memchr(lineno, 255, tabsiz) != NULL)
bac997b79b19 Bypass peepholing of code with lineno tables having intervals >= 255.
Raymond Hettinger <python@rcn.com>
parents: 33271
diff changeset
666 goto exitUnchanged;
bac997b79b19 Bypass peepholing of code with lineno tables having intervals >= 255.
Raymond Hettinger <python@rcn.com>
parents: 33271
diff changeset
667
33249
c654334dc961 Incorporate review comments courtesy of Neal Norwitz:
Raymond Hettinger <python@rcn.com>
parents: 33248
diff changeset
668 /* Avoid situations where jump retargeting could overflow */
33620
27aa0e119330 Replaced a test with an assertion.
Raymond Hettinger <python@rcn.com>
parents: 33539
diff changeset
669 assert(PyString_Check(code));
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
670 codelen = PyString_Size(code);
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
671 if (codelen > 32700)
33249
c654334dc961 Incorporate review comments courtesy of Neal Norwitz:
Raymond Hettinger <python@rcn.com>
parents: 33248
diff changeset
672 goto exitUnchanged;
c654334dc961 Incorporate review comments courtesy of Neal Norwitz:
Raymond Hettinger <python@rcn.com>
parents: 33248
diff changeset
673
c654334dc961 Incorporate review comments courtesy of Neal Norwitz:
Raymond Hettinger <python@rcn.com>
parents: 33248
diff changeset
674 /* Make a modifiable copy of the code string */
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
675 codestr = PyMem_Malloc(codelen);
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
676 if (codestr == NULL)
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
677 goto exitUnchanged;
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
678 codestr = memcpy(codestr, PyString_AS_STRING(code), codelen);
32977
40c3a51cb71c SF bug #1004088: big code objects (>64K) may be optimized incorrectly
Raymond Hettinger <python@rcn.com>
parents: 32956
diff changeset
679
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
680 /* Mapping to new jump targets after NOPs are removed */
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
681 addrmap = PyMem_Malloc(codelen * sizeof(int));
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
682 if (addrmap == NULL)
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
683 goto exitUnchanged;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
684
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
685 blocks = markblocks(codestr, codelen);
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
686 if (blocks == NULL)
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
687 goto exitUnchanged;
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
688 assert(PyList_Check(consts));
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
689
33875
24fc3eaa19e0 Simplify and future proof NOP counting in the peepholer.
Raymond Hettinger <python@rcn.com>
parents: 33861
diff changeset
690 for (i=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
691 opcode = codestr[i];
33838
fdce3d39bec1 SF bug #1053819: Segfault in tuple_of_constants
Raymond Hettinger <python@rcn.com>
parents: 33822
diff changeset
692
fdce3d39bec1 SF bug #1053819: Segfault in tuple_of_constants
Raymond Hettinger <python@rcn.com>
parents: 33822
diff changeset
693 lastlc = cumlc;
fdce3d39bec1 SF bug #1053819: Segfault in tuple_of_constants
Raymond Hettinger <python@rcn.com>
parents: 33822
diff changeset
694 cumlc = 0;
fdce3d39bec1 SF bug #1053819: Segfault in tuple_of_constants
Raymond Hettinger <python@rcn.com>
parents: 33822
diff changeset
695
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
696 switch (opcode) {
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
697
32402
1c2baae14828 Move NOP to end of code transformation.
Raymond Hettinger <python@rcn.com>
parents: 32395
diff changeset
698 /* Replace UNARY_NOT JUMP_IF_FALSE POP_TOP with
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
699 with JUMP_IF_TRUE POP_TOP */
32395
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
700 case UNARY_NOT:
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
701 if (codestr[i+1] != JUMP_IF_FALSE ||
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
702 codestr[i+4] != POP_TOP ||
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
703 !ISBASICBLOCK(blocks,i,5))
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
704 continue;
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
705 tgt = GETJUMPTGT(codestr, (i+1));
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
706 if (codestr[tgt] != POP_TOP)
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
707 continue;
32402
1c2baae14828 Move NOP to end of code transformation.
Raymond Hettinger <python@rcn.com>
parents: 32395
diff changeset
708 j = GETARG(codestr, i+1) + 1;
1c2baae14828 Move NOP to end of code transformation.
Raymond Hettinger <python@rcn.com>
parents: 32395
diff changeset
709 codestr[i] = JUMP_IF_TRUE;
1c2baae14828 Move NOP to end of code transformation.
Raymond Hettinger <python@rcn.com>
parents: 32395
diff changeset
710 SETARG(codestr, i, j);
1c2baae14828 Move NOP to end of code transformation.
Raymond Hettinger <python@rcn.com>
parents: 32395
diff changeset
711 codestr[i+3] = POP_TOP;
1c2baae14828 Move NOP to end of code transformation.
Raymond Hettinger <python@rcn.com>
parents: 32395
diff changeset
712 codestr[i+4] = NOP;
32395
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
713 break;
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
714
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
715 /* not a is b --> a is not b
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
716 not a in b --> a not in b
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
717 not a is not b --> a is b
34574
369e8e71ea33 Remove the set conversion which didn't work with: [] in (0,)
Raymond Hettinger <python@rcn.com>
parents: 34571
diff changeset
718 not a not in b --> a in b
34547
835b645a168f Transform "x in (1,2,3)" to "x in frozenset([1,2,3])".
Raymond Hettinger <python@rcn.com>
parents: 34506
diff changeset
719 */
32395
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
720 case COMPARE_OP:
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
721 j = GETARG(codestr, i);
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
722 if (j < 6 || j > 9 ||
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
723 codestr[i+3] != UNARY_NOT ||
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
724 !ISBASICBLOCK(blocks,i,4))
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
725 continue;
32395
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
726 SETARG(codestr, i, (j^1));
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
727 codestr[i+3] = NOP;
32717
0e890ac1c0f2 optimize_code(): Repaired gross error in new special-casing for None.
Tim Peters <tim.peters@gmail.com>
parents: 32711
diff changeset
728 break;
0e890ac1c0f2 optimize_code(): Repaired gross error in new special-casing for None.
Tim Peters <tim.peters@gmail.com>
parents: 32711
diff changeset
729
32711
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
730 /* Replace LOAD_GLOBAL/LOAD_NAME None with LOAD_CONST None */
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
731 case LOAD_NAME:
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
732 case LOAD_GLOBAL:
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
733 j = GETARG(codestr, i);
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
734 name = PyString_AsString(PyTuple_GET_ITEM(names, j));
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
735 if (name == NULL || strcmp(name, "None") != 0)
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
736 continue;
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
737 for (j=0 ; j < PyList_GET_SIZE(consts) ; j++) {
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
738 if (PyList_GET_ITEM(consts, j) == Py_None) {
32711
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
739 codestr[i] = LOAD_CONST;
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
740 SETARG(codestr, i, j);
33880
8a09c82e4261 Maintain peepholer's cumlc invariant by updating the running total
Raymond Hettinger <python@rcn.com>
parents: 33875
diff changeset
741 cumlc = lastlc + 1;
32711
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
742 break;
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
743 }
c7c9b68ddda2 Treat None as a constant.
Raymond Hettinger <python@rcn.com>
parents: 32531
diff changeset
744 }
32395
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
745 break;
6bc6abba8659 Install two code generation optimizations that depend on NOP.
Raymond Hettinger <python@rcn.com>
parents: 32305
diff changeset
746
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
747 /* Skip over LOAD_CONST trueconst JUMP_IF_FALSE xx POP_TOP */
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
748 case LOAD_CONST:
33838
fdce3d39bec1 SF bug #1053819: Segfault in tuple_of_constants
Raymond Hettinger <python@rcn.com>
parents: 33822
diff changeset
749 cumlc = lastlc + 1;
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
750 j = GETARG(codestr, i);
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
751 if (codestr[i+3] != JUMP_IF_FALSE ||
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
752 codestr[i+6] != POP_TOP ||
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
753 !ISBASICBLOCK(blocks,i,7) ||
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
754 !PyObject_IsTrue(PyList_GET_ITEM(consts, j)))
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
755 continue;
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
756 memset(codestr+i, NOP, 7);
33880
8a09c82e4261 Maintain peepholer's cumlc invariant by updating the running total
Raymond Hettinger <python@rcn.com>
parents: 33875
diff changeset
757 cumlc = 0;
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
758 break;
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
759
34553
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
760 /* Try to fold tuples of constants (includes a case for lists
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
761 which are only used for "in" and "not in" tests).
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
762 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
763 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
764 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
765 case BUILD_TUPLE:
34553
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
766 case BUILD_LIST:
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
767 j = GETARG(codestr, i);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
768 h = i - 3 * j;
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
769 if (h >= 0 &&
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
770 j <= lastlc &&
34553
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
771 (opcode == BUILD_TUPLE &&
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
772 ISBASICBLOCK(blocks, h, 3*(j+1)) ||
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
773 opcode == BUILD_LIST &&
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
774 codestr[i+3]==COMPARE_OP &&
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
775 ISBASICBLOCK(blocks, h, 3*(j+2)) &&
cd04f2d18a6f Adopt Skip's idea to optimize lists of constants in the context
Raymond Hettinger <python@rcn.com>
parents: 34547
diff changeset
776 (GETARG(codestr,i+3)==6 || GETARG(codestr,i+3)==7)) &&
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
777 tuple_of_constants(&codestr[h], j, consts)) {
33880
8a09c82e4261 Maintain peepholer's cumlc invariant by updating the running total
Raymond Hettinger <python@rcn.com>
parents: 33875
diff changeset
778 assert(codestr[i] == LOAD_CONST);
8a09c82e4261 Maintain peepholer's cumlc invariant by updating the running total
Raymond Hettinger <python@rcn.com>
parents: 33875
diff changeset
779 cumlc = 1;
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
780 break;
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
781 }
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
782 if (codestr[i+3] != UNPACK_SEQUENCE ||
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
783 !ISBASICBLOCK(blocks,i,6) ||
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
784 j != GETARG(codestr, i+3))
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
785 continue;
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
786 if (j == 1) {
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
787 memset(codestr+i, NOP, 6);
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
788 } else if (j == 2) {
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
789 codestr[i] = ROT_TWO;
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
790 memset(codestr+i+1, NOP, 5);
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
791 } else if (j == 3) {
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
792 codestr[i] = ROT_THREE;
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
793 codestr[i+1] = ROT_TWO;
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
794 memset(codestr+i+2, NOP, 4);
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
795 }
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
796 break;
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
797
34305
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
798 /* Fold binary ops on constants.
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
799 LOAD_CONST c1 LOAD_CONST c2 BINOP --> LOAD_CONST binop(c1,c2) */
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
800 case BINARY_POWER:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
801 case BINARY_MULTIPLY:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
802 case BINARY_TRUE_DIVIDE:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
803 case BINARY_FLOOR_DIVIDE:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
804 case BINARY_MODULO:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
805 case BINARY_ADD:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
806 case BINARY_SUBTRACT:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
807 case BINARY_SUBSCR:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
808 case BINARY_LSHIFT:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
809 case BINARY_RSHIFT:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
810 case BINARY_AND:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
811 case BINARY_XOR:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
812 case BINARY_OR:
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
813 if (lastlc >= 2 &&
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
814 ISBASICBLOCK(blocks, i-6, 7) &&
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
815 fold_binops_on_constants(&codestr[i-6], consts)) {
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
816 i -= 2;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
817 assert(codestr[i] == LOAD_CONST);
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
818 cumlc = 1;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
819 }
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
820 break;
0c326327e722 Teach the peephole optimizer to fold simple constant expressions.
Raymond Hettinger <python@rcn.com>
parents: 33925
diff changeset
821
34622
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
822 /* Fold unary ops on constants.
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
823 LOAD_CONST c1 UNARY_OP --> LOAD_CONST unary_op(c) */
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
824 case UNARY_NEGATIVE:
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
825 case UNARY_CONVERT:
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
826 case UNARY_INVERT:
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
827 if (lastlc >= 1 &&
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
828 ISBASICBLOCK(blocks, i-3, 4) &&
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
829 fold_unaryops_on_constants(&codestr[i-3], consts)) {
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
830 i -= 2;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
831 assert(codestr[i] == LOAD_CONST);
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
832 cumlc = 1;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
833 }
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
834 break;
516bb8fd75b4 Teach the peepholer to fold unary operations on constants.
Raymond Hettinger <python@rcn.com>
parents: 34574
diff changeset
835
33259
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
836 /* Simplify conditional jump to conditional jump where the
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
837 result of the first test implies the success of a similar
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
838 test or the failure of the opposite test.
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
839 Arises in code like:
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
840 "if a and b:"
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
841 "if a or b:"
33259
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
842 "a and b or c"
33861
4a3eaa1aa3af Fixed a comment and added another one.
Armin Rigo <arigo@tunes.org>
parents: 33860
diff changeset
843 "(a and b) and c"
33259
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
844 x:JUMP_IF_FALSE y y:JUMP_IF_FALSE z --> x:JUMP_IF_FALSE z
33271
3dbdbd572d94 Fix typo in comment and add clarification.
Raymond Hettinger <python@rcn.com>
parents: 33259
diff changeset
845 x:JUMP_IF_FALSE y y:JUMP_IF_TRUE z --> x:JUMP_IF_FALSE y+3
3dbdbd572d94 Fix typo in comment and add clarification.
Raymond Hettinger <python@rcn.com>
parents: 33259
diff changeset
846 where y+3 is the instruction following the second test.
33259
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
847 */
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
848 case JUMP_IF_FALSE:
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
849 case JUMP_IF_TRUE:
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
850 tgt = GETJUMPTGT(codestr, i);
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
851 j = codestr[tgt];
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
852 if (j == JUMP_IF_FALSE || j == JUMP_IF_TRUE) {
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
853 if (j == opcode) {
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
854 tgttgt = GETJUMPTGT(codestr, tgt) - i - 3;
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
855 SETARG(codestr, i, tgttgt);
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
856 } else {
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
857 tgt -= i;
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
858 SETARG(codestr, i, tgt);
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
859 }
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
860 break;
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
861 }
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
862 /* Intentional fallthrough */
b63b30821ef4 Simplify chains of conditional jumps.
Raymond Hettinger <python@rcn.com>
parents: 33249
diff changeset
863
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
864 /* Replace jumps to unconditional jumps */
28610
67d36f4dfea1 Extend SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28417
diff changeset
865 case FOR_ITER:
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
866 case JUMP_FORWARD:
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
867 case JUMP_ABSOLUTE:
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
868 case CONTINUE_LOOP:
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
869 case SETUP_LOOP:
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
870 case SETUP_EXCEPT:
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
871 case SETUP_FINALLY:
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
872 tgt = GETJUMPTGT(codestr, i);
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
873 if (!UNCONDITIONAL_JUMP(codestr[tgt]))
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
874 continue;
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
875 tgttgt = GETJUMPTGT(codestr, tgt);
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
876 if (opcode == JUMP_FORWARD) /* JMP_ABS can go backwards */
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
877 opcode = JUMP_ABSOLUTE;
28417
1a2fd109f6b6 Factored out test for absolute jumps.
Raymond Hettinger <python@rcn.com>
parents: 28406
diff changeset
878 if (!ABSOLUTE_JUMP(opcode))
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
879 tgttgt -= i + 3; /* Calc relative jump addr */
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
880 if (tgttgt < 0) /* No backward relative jumps */
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
881 continue;
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
882 codestr[i] = opcode;
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
883 SETARG(codestr, i, tgttgt);
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
884 break;
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
885
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
886 case EXTENDED_ARG:
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
887 goto exitUnchanged;
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
888
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
889 /* Replace RETURN LOAD_CONST None RETURN with just RETURN */
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
890 case RETURN_VALUE:
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
891 if (i+4 >= codelen ||
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
892 codestr[i+4] != RETURN_VALUE ||
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
893 !ISBASICBLOCK(blocks,i,5))
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
894 continue;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
895 memset(codestr+i+1, NOP, 4);
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
896 break;
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
897 }
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
898 }
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
899
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
900 /* Fixup linenotab */
33875
24fc3eaa19e0 Simplify and future proof NOP counting in the peepholer.
Raymond Hettinger <python@rcn.com>
parents: 33861
diff changeset
901 for (i=0, nops=0 ; i<codelen ; i += CODESIZE(codestr[i])) {
24fc3eaa19e0 Simplify and future proof NOP counting in the peepholer.
Raymond Hettinger <python@rcn.com>
parents: 33861
diff changeset
902 addrmap[i] = i - nops;
24fc3eaa19e0 Simplify and future proof NOP counting in the peepholer.
Raymond Hettinger <python@rcn.com>
parents: 33861
diff changeset
903 if (codestr[i] == NOP)
24fc3eaa19e0 Simplify and future proof NOP counting in the peepholer.
Raymond Hettinger <python@rcn.com>
parents: 33861
diff changeset
904 nops++;
24fc3eaa19e0 Simplify and future proof NOP counting in the peepholer.
Raymond Hettinger <python@rcn.com>
parents: 33861
diff changeset
905 }
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
906 cum_orig_line = 0;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
907 last_line = 0;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
908 for (i=0 ; i < tabsiz ; i+=2) {
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
909 cum_orig_line += lineno[i];
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
910 new_line = addrmap[cum_orig_line];
33272
bac997b79b19 Bypass peepholing of code with lineno tables having intervals >= 255.
Raymond Hettinger <python@rcn.com>
parents: 33271
diff changeset
911 assert (new_line - last_line < 255);
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
912 lineno[i] =((unsigned char)(new_line - last_line));
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
913 last_line = new_line;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
914 }
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
915
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
916 /* Remove NOPs and fixup jump targets */
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
917 for (i=0, h=0 ; i<codelen ; ) {
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
918 opcode = codestr[i];
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
919 switch (opcode) {
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
920 case NOP:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
921 i++;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
922 continue;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
923
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
924 case JUMP_ABSOLUTE:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
925 case CONTINUE_LOOP:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
926 j = addrmap[GETARG(codestr, i)];
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
927 SETARG(codestr, i, j);
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
928 break;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
929
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
930 case FOR_ITER:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
931 case JUMP_FORWARD:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
932 case JUMP_IF_FALSE:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
933 case JUMP_IF_TRUE:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
934 case SETUP_LOOP:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
935 case SETUP_EXCEPT:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
936 case SETUP_FINALLY:
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
937 j = addrmap[GETARG(codestr, i) + i + 3] - addrmap[i] - 3;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
938 SETARG(codestr, i, j);
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
939 break;
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
940 }
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
941 adj = CODESIZE(opcode);
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
942 while (adj--)
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
943 codestr[h++] = codestr[i++];
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
944 }
33249
c654334dc961 Incorporate review comments courtesy of Neal Norwitz:
Raymond Hettinger <python@rcn.com>
parents: 33248
diff changeset
945 assert(h + nops == codelen);
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
946
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
947 code = PyString_FromStringAndSize((char *)codestr, h);
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
948 PyMem_Free(addrmap);
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
949 PyMem_Free(codestr);
31728
6509354e7876 Improve byte coding for multiple assignments.
Raymond Hettinger <python@rcn.com>
parents: 31618
diff changeset
950 PyMem_Free(blocks);
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
951 return code;
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
952
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
953 exitUnchanged:
33860
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
954 if (blocks != NULL)
3467288089a1 Adopt some peepholer suggestions from Armin Rigo:
Raymond Hettinger <python@rcn.com>
parents: 33838
diff changeset
955 PyMem_Free(blocks);
33248
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
956 if (addrmap != NULL)
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
957 PyMem_Free(addrmap);
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
958 if (codestr != NULL)
1f9b9c226b43 SF Patch #1013667: Cleanup Peepholer Output
Raymond Hettinger <python@rcn.com>
parents: 33169
diff changeset
959 PyMem_Free(codestr);
28406
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
960 Py_INCREF(code);
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
961 return code;
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
962 }
733729261c41 SF patch #707257: Improve code generation
Raymond Hettinger <python@rcn.com>
parents: 28402
diff changeset
963
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
964 /* End: Peephole optimizations ----------------------------------------- */
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
965
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
966 PyCodeObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
967 PyCode_New(int argcount, int nlocals, int stacksize, int flags,
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
968 PyObject *code, PyObject *consts, PyObject *names,
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
969 PyObject *varnames, PyObject *freevars, PyObject *cellvars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
970 PyObject *filename, PyObject *name, int firstlineno,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
971 PyObject *lnotab)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
973 PyCodeObject *co;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 /* Check argument types */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
976 if (argcount < 0 || nlocals < 0 ||
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
977 code == NULL ||
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
978 consts == NULL || !PyTuple_Check(consts) ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
979 names == NULL || !PyTuple_Check(names) ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
980 varnames == NULL || !PyTuple_Check(varnames) ||
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
981 freevars == NULL || !PyTuple_Check(freevars) ||
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
982 cellvars == NULL || !PyTuple_Check(cellvars) ||
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
983 name == NULL || !PyString_Check(name) ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
984 filename == NULL || !PyString_Check(filename) ||
20947
c555f1e7c4be Use PyObject_CheckReadBuffer().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20939
diff changeset
985 lnotab == NULL || !PyString_Check(lnotab) ||
c555f1e7c4be Use PyObject_CheckReadBuffer().
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20939
diff changeset
986 !PyObject_CheckReadBuffer(code)) {
9293
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
987 PyErr_BadInternalCall();
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
988 return NULL;
0a67db47e1d0 Changes to support other object types besides strings
Guido van Rossum <guido@python.org>
parents: 9215
diff changeset
989 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
990 intern_strings(names);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
991 intern_strings(varnames);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
992 intern_strings(freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
993 intern_strings(cellvars);
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
994 /* Intern selected string constants */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
995 for (i = PyTuple_Size(consts); --i >= 0; ) {
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
996 PyObject *v = PyTuple_GetItem(consts, i);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
997 if (!PyString_Check(v))
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
998 continue;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
999 if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
1000 continue;
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
1001 PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1003 co = PyObject_NEW(PyCodeObject, &PyCode_Type);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 if (co != NULL) {
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1005 co->co_argcount = argcount;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1006 co->co_nlocals = nlocals;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1007 co->co_stacksize = stacksize;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1008 co->co_flags = flags;
33169
5778f6331334 Move the bytecode optimizer upstream so that its results are saved in pyc
Raymond Hettinger <python@rcn.com>
parents: 33166
diff changeset
1009 Py_INCREF(code);
5778f6331334 Move the bytecode optimizer upstream so that its results are saved in pyc
Raymond Hettinger <python@rcn.com>
parents: 33166
diff changeset
1010 co->co_code = code;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1011 Py_INCREF(consts);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 co->co_consts = consts;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1013 Py_INCREF(names);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 co->co_names = names;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1015 Py_INCREF(varnames);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1016 co->co_varnames = varnames;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1017 Py_INCREF(freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1018 co->co_freevars = freevars;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1019 Py_INCREF(cellvars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1020 co->co_cellvars = cellvars;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1021 Py_INCREF(filename);
266
b5826bf29677 Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents: 238
diff changeset
1022 co->co_filename = filename;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1023 Py_INCREF(name);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
1024 co->co_name = name;
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1025 co->co_firstlineno = firstlineno;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1026 Py_INCREF(lnotab);
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1027 co->co_lnotab = lnotab;
27712
16856c9514e0 Small function call optimization and special build option for call stats.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27270
diff changeset
1028 if (PyTuple_GET_SIZE(freevars) == 0 &&
16856c9514e0 Small function call optimization and special build option for call stats.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27270
diff changeset
1029 PyTuple_GET_SIZE(cellvars) == 0)
16856c9514e0 Small function call optimization and special build option for call stats.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 27270
diff changeset
1030 co->co_flags |= CO_NOFREE;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032 return co;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1035
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 /* Data structure used internally */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1037
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1038 /* The compiler uses two passes to generate bytecodes. The first pass
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1039 builds the symbol table. The second pass generates the bytecode.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1040
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1041 The first pass uses a single symtable struct. The second pass uses
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1042 a compiling struct for each code block. The compiling structs
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1043 share a reference to the symtable.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1044
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1045 The two passes communicate via symtable_load_symbols() and via
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1046 is_local() and is_global(). The former initializes several slots
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1047 in the compiling struct: c_varnames, c_locals, c_nlocals,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1048 c_argcount, c_globals, and c_flags.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1049 */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1050
18065
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1051 /* All about c_lnotab.
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1052
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
1053 c_lnotab is an array of unsigned bytes disguised as a Python string. Since
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
1054 version 2.3, SET_LINENO opcodes are never generated and bytecode offsets are
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
1055 mapped to source code line #s via c_lnotab instead.
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
1056
18065
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1057 The array is conceptually a list of
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1058 (bytecode offset increment, line number increment)
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1059 pairs. The details are important and delicate, best illustrated by example:
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1060
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1061 byte code offset source code line number
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1062 0 1
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1063 6 2
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1064 50 7
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1065 350 307
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1066 361 308
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1067
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1068 The first trick is that these numbers aren't stored, only the increments
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1069 from one row to the next (this doesn't really work, but it's a start):
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1070
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1071 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1072
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1073 The second trick is that an unsigned byte can't hold negative values, or
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1074 values larger than 255, so (a) there's a deep assumption that byte code
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1075 offsets and their corresponding line #s both increase monotonically, and (b)
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1076 if at least one column jumps by more than 255 from one row to the next, more
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1077 than one pair is written to the table. In case #b, there's no way to know
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1078 from looking at the table later how many were written. That's the delicate
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1079 part. A user of c_lnotab desiring to find the source line number
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1080 corresponding to a bytecode address A should do something like this
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1081
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1082 lineno = addr = 0
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1083 for addr_incr, line_incr in c_lnotab:
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1084 addr += addr_incr
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1085 if addr > A:
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1086 return lineno
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1087 lineno += line_incr
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1088
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1089 In order for this to work, when the addr field increments by more than 255,
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1090 the line # increment in each pair generated must be 0 until the remaining addr
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1091 increment is < 256. So, in the example above, com_set_lineno should not (as
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1092 was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1093 255, 0, 45, 255, 0, 45.
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1094 */
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1095
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1096 struct compiling {
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
1097 PyObject *c_code; /* string */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1098 PyObject *c_consts; /* list of objects */
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1099 PyObject *c_const_dict; /* inverse of c_consts */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1100 PyObject *c_names; /* list of strings (names) */
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1101 PyObject *c_name_dict; /* inverse of c_names */
26701
45ab31127bf9 SF # 654960, remove unnecessary static variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 26670
diff changeset
1102 PyObject *c_globals; /* dictionary (value=None or True) */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1103 PyObject *c_locals; /* dictionary (value=localID) */
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1104 PyObject *c_varnames; /* list (inverse of c_locals) */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1105 PyObject *c_freevars; /* dictionary (value=None) */
33132
e4992e930cb2 Fix incorrect comment for (struct compiling)->c_cellvars
Brett Cannon <bcannon@gmail.com>
parents: 33103
diff changeset
1106 PyObject *c_cellvars; /* dictionary */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1107 int c_nlocals; /* index of next local */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1108 int c_argcount; /* number of top-level arguments */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1109 int c_flags; /* same as co_flags */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110 int c_nexti; /* index into c_code */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 int c_errors; /* counts errors occurred */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
1112 int c_infunction; /* set when compiling a function */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1113 int c_interactive; /* generating code for interactive command */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
1114 int c_loops; /* counts nested loops */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1115 int c_begin; /* begin of current loop, for 'continue' */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1116 int c_block[CO_MAXBLOCKS]; /* stack of block types */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1117 int c_nblocks; /* current block stack level */
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
1118 const char *c_filename; /* filename of current node */
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
1119 char *c_name; /* name of object (e.g. function) */
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1120 int c_lineno; /* Current line number */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1121 int c_stacklevel; /* Current stack level */
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1122 int c_maxstacklevel; /* Maximum stack level */
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1123 int c_firstlineno;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1124 PyObject *c_lnotab; /* Table mapping address to line number */
31764
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1125 int c_last_addr; /* last op addr seen and recorded in lnotab */
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1126 int c_last_line; /* last line seen and recorded in lnotab */
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1127 int c_lnotab_next; /* current length of lnotab */
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1128 int c_lnotab_last; /* start of last lnotab record added */
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1129 char *c_private; /* for private name mangling */
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1130 int c_tmpname; /* temporary local name counter */
16523
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
1131 int c_nested; /* Is block nested funcdef or lamdef? */
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
1132 int c_closure; /* Is nested w/freevars? */
9d625303f914 Reindent a function that was somehow indented by 7 spaces. Also did a
Guido van Rossum <guido@python.org>
parents: 16428
diff changeset
1133 struct symtable *c_symtable; /* pointer to module symbol table */
16799
08d937107fe6 Improved __future__ parser; still more to do
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16783
diff changeset
1134 PyFutureFeatures *c_future; /* pointer to module's __future__ */
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1135 char *c_encoding; /* source encoding (a borrowed reference) */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1136 };
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137
17518
f939ce52a734 Make some private symbols static.
Guido van Rossum <guido@python.org>
parents: 17355
diff changeset
1138 static int
f939ce52a734 Make some private symbols static.
Guido van Rossum <guido@python.org>
parents: 17355
diff changeset
1139 is_free(int v)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1140 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1141 if ((v & (USE | DEF_FREE))
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1142 && !(v & (DEF_LOCAL | DEF_PARAM | DEF_GLOBAL)))
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1143 return 1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1144 if (v & DEF_FREE_CLASS)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1145 return 1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1146 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1147 }
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1148
16428
6496844c4a42 Fix symbol table pass to generation SyntaxError exceptions that
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16421
diff changeset
1149 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1150 com_error(struct compiling *c, PyObject *exc, char *msg)
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1151 {
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1152 PyObject *t = NULL, *v = NULL, *w = NULL, *line = NULL;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1153
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1154 if (c == NULL) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1155 /* Error occurred via symtable call to
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1156 is_constant_false */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1157 PyErr_SetString(exc, msg);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1158 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1159 }
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
1160 c->c_errors++;
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1161 if (c->c_lineno < 1 || c->c_interactive) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1162 /* Unknown line number or interactive input */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1163 PyErr_SetString(exc, msg);
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1164 return;
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1165 }
13993
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
1166 v = PyString_FromString(msg);
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1167 if (v == NULL)
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1168 return; /* MemoryError, too bad */
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1169
16838
09f6c1372056 Improve SyntaxErrors for bad future statements. Set file and location
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16831
diff changeset
1170 line = PyErr_ProgramText(c->c_filename, c->c_lineno);
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1171 if (line == NULL) {
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1172 Py_INCREF(Py_None);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1173 line = Py_None;
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1174 }
22120
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1175 if (exc == PyExc_SyntaxError) {
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1176 t = Py_BuildValue("(ziOO)", c->c_filename, c->c_lineno,
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1177 Py_None, line);
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1178 if (t == NULL)
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1179 goto exit;
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30488
diff changeset
1180 w = PyTuple_Pack(2, v, t);
22120
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1181 if (w == NULL)
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1182 goto exit;
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1183 PyErr_SetObject(exc, w);
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1184 } else {
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1185 /* Make sure additional exceptions are printed with
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1186 file and line, also. */
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1187 PyErr_SetObject(exc, v);
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1188 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1189 }
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1190 exit:
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1191 Py_XDECREF(t);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1192 Py_XDECREF(v);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1193 Py_XDECREF(w);
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
1194 Py_XDECREF(line);
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1195 }
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1196
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1197 /* Interface to the block stack */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1198
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1199 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1200 block_push(struct compiling *c, int type)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1201 {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1202 if (c->c_nblocks >= CO_MAXBLOCKS) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1203 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1204 "too many statically nested blocks");
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1205 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1206 else {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1207 c->c_block[c->c_nblocks++] = type;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1208 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1209 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1210
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1211 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1212 block_pop(struct compiling *c, int type)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1213 {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1214 if (c->c_nblocks > 0)
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1215 c->c_nblocks--;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1216 if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1217 com_error(c, PyExc_SystemError, "bad block pop");
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1218 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1219 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1220
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1221 /* Prototype forward declarations */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1222
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
1223 static int issue_warning(const char *, const char *, int);
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
1224 static int com_init(struct compiling *, const char *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1225 static void com_free(struct compiling *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1226 static void com_push(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1227 static void com_pop(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1228 static void com_done(struct compiling *);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1229 static void com_node(struct compiling *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1230 static void com_factor(struct compiling *, node *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1231 static void com_addbyte(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1232 static void com_addint(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1233 static void com_addoparg(struct compiling *, int, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1234 static void com_addfwref(struct compiling *, int, int *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1235 static void com_backpatch(struct compiling *, int);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1236 static int com_add(struct compiling *, PyObject *, PyObject *, PyObject *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1237 static int com_addconst(struct compiling *, PyObject *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1238 static int com_addname(struct compiling *, PyObject *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1239 static void com_addopname(struct compiling *, int, node *);
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
1240 static void com_test(struct compiling *c, node *n);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1241 static void com_list(struct compiling *, node *, int);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1242 static void com_list_iter(struct compiling *, node *, node *, char *);
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
1243 static void com_gen_iter(struct compiling *, node *, node *);
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
1244 static int com_argdefs(struct compiling *, node *);
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1245 static void com_assign(struct compiling *, node *, int, node *);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1246 static void com_assign_name(struct compiling *, node *, int);
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
1247 static int com_make_closure(struct compiling *c, PyCodeObject *co);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
1248
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1249 static PyCodeObject *icompile(node *, struct compiling *);
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
1250 static PyCodeObject *jcompile(node *, const char *, struct compiling *,
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
1251 PyCompilerFlags *);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
1252 static PyObject *parsestrplus(struct compiling*, node *);
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
1253 static PyObject *parsestr(struct compiling *, char *);
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
1254 static node *get_rawdocstring(node *);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1255
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1256 static int get_ref_type(struct compiling *, char *);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1257
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1258 /* symtable operations */
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
1259 static int symtable_lookup(struct symtable *st, char *name);
29869
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
1260 static struct symtable *symtable_build(node *, PyFutureFeatures *,
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
1261 const char *filename);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1262 static int symtable_load_symbols(struct compiling *);
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
1263 static struct symtable *symtable_init(void);
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
1264 static void symtable_enter_scope(struct symtable *, char *, int, int);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1265 static int symtable_exit_scope(struct symtable *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1266 static int symtable_add_def(struct symtable *, char *, int);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1267 static int symtable_add_def_o(struct symtable *, PyObject *, PyObject *, int);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1268
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1269 static void symtable_node(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1270 static void symtable_funcdef(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1271 static void symtable_default_args(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1272 static void symtable_params(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1273 static void symtable_params_fplist(struct symtable *, node *n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1274 static void symtable_global(struct symtable *, node *);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1275 static void symtable_import(struct symtable *, node *);
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
1276 static void symtable_assign(struct symtable *, node *, int);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1277 static void symtable_list_comprehension(struct symtable *, node *);
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
1278 static void symtable_generator_expression(struct symtable *, node *);
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
1279 static void symtable_list_for(struct symtable *, node *);
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
1280 static void symtable_gen_for(struct symtable *, node *, int);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
1281 static void symtable_gen_iter(struct symtable *, node *);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1282
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1283 static int symtable_update_free_vars(struct symtable *);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1284 static int symtable_undo_free(struct symtable *, PyObject *, PyObject *);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1285 static int symtable_check_global(struct symtable *, PyObject *, PyObject *);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1286
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1287 /* helper */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1288 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1289 do_pad(int pad)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1290 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1291 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1292 for (i = 0; i < pad; ++i)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1293 fprintf(stderr, " ");
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1294 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1295
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1296 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1297 dump(node *n, int pad, int depth)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1298 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1299 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1300 if (depth == 0)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1301 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1302 do_pad(pad);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1303 fprintf(stderr, "%d: %s\n", TYPE(n), STR(n));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1304 if (depth > 0)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1305 depth--;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1306 for (i = 0; i < NCH(n); ++i)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1307 dump(CHILD(n, i), pad + 1, depth);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1308 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1309
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1310 static int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
1311 com_init(struct compiling *c, const char *filename)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1312 {
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1313 memset((void *)c, '\0', sizeof(struct compiling));
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1314 if ((c->c_code = PyString_FromStringAndSize((char *)NULL,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1315 1000)) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1316 goto fail;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1317 if ((c->c_consts = PyList_New(0)) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1318 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1319 if ((c->c_const_dict = PyDict_New()) == NULL)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1320 goto fail;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1321 if ((c->c_names = PyList_New(0)) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1322 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1323 if ((c->c_name_dict = PyDict_New()) == NULL)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1324 goto fail;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1325 if ((c->c_locals = PyDict_New()) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1326 goto fail;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1327 if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1328 1000)) == NULL)
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1329 goto fail;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1330 c->c_globals = NULL;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1331 c->c_varnames = NULL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1332 c->c_freevars = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1333 c->c_cellvars = NULL;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1334 c->c_nlocals = 0;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1335 c->c_argcount = 0;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1336 c->c_flags = 0;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 c->c_nexti = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1338 c->c_errors = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
1339 c->c_infunction = 0;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1340 c->c_interactive = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
1341 c->c_loops = 0;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1342 c->c_begin = 0;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
1343 c->c_nblocks = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
1344 c->c_filename = filename;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
1345 c->c_name = "?";
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1346 c->c_lineno = 0;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1347 c->c_stacklevel = 0;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1348 c->c_maxstacklevel = 0;
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1349 c->c_firstlineno = 0;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1350 c->c_last_addr = 0;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1351 c->c_last_line = 0;
16096
fb67122e5c1e com_init(): My entry into the smallest patch possible category.
Barry Warsaw <barry@python.org>
parents: 16060
diff changeset
1352 c->c_lnotab_next = 0;
31764
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1353 c->c_lnotab_last = 0;
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
1354 c->c_tmpname = 0;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1355 c->c_nested = 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1356 c->c_closure = 0;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1357 c->c_symtable = NULL;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1358 return 1;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1359
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1360 fail:
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1361 com_free(c);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 return 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1363 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1364
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1365 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1366 com_free(struct compiling *c)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1367 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1368 Py_XDECREF(c->c_code);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1369 Py_XDECREF(c->c_consts);
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1370 Py_XDECREF(c->c_const_dict);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1371 Py_XDECREF(c->c_names);
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1372 Py_XDECREF(c->c_name_dict);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1373 Py_XDECREF(c->c_globals);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1374 Py_XDECREF(c->c_locals);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1375 Py_XDECREF(c->c_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1376 Py_XDECREF(c->c_freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1377 Py_XDECREF(c->c_cellvars);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1378 Py_XDECREF(c->c_lnotab);
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
1379 if (c->c_future)
24529
3444005b6527 SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 24373
diff changeset
1380 PyObject_FREE((void *)c->c_future);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1382
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1383 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1384 com_push(struct compiling *c, int n)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1385 {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1386 c->c_stacklevel += n;
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1387 if (c->c_stacklevel > c->c_maxstacklevel) {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1388 c->c_maxstacklevel = c->c_stacklevel;
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1389 /*
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1390 fprintf(stderr, "%s:%s:%d max stack nexti=%d level=%d n=%d\n",
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1391 c->c_filename, c->c_name, c->c_lineno,
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1392 c->c_nexti, c->c_stacklevel, n);
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1393 */
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1394 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1395 }
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1396
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1397 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1398 com_pop(struct compiling *c, int n)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1399 {
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1400 if (c->c_stacklevel < n)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1401 c->c_stacklevel = 0;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1402 else
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1403 c->c_stacklevel -= n;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1404 }
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1405
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
1406 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1407 com_done(struct compiling *c)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409 if (c->c_code != NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1410 _PyString_Resize(&c->c_code, c->c_nexti);
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1411 if (c->c_lnotab != NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1412 _PyString_Resize(&c->c_lnotab, c->c_lnotab_next);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1413 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1414
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1415 static int
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1416 com_check_size(PyObject **s, int offset)
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1417 {
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1418 int len = PyString_GET_SIZE(*s);
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1419 if (offset >= len)
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1420 return _PyString_Resize(s, len * 2);
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1421 return 0;
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1422 }
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1423
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1424 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1425 com_addbyte(struct compiling *c, int byte)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1426 {
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
1427 /*fprintf(stderr, "%3d: %3d\n", c->c_nexti, byte);*/
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1428 assert(byte >= 0 && byte <= 255);
21609
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
1429 assert(c->c_code != 0);
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1430 if (com_check_size(&c->c_code, c->c_nexti)) {
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1431 c->c_errors++;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1432 return;
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1433 }
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1434 PyString_AS_STRING(c->c_code)[c->c_nexti++] = byte;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1435 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1436
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1437 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1438 com_addint(struct compiling *c, int x)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1439 {
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
1440 com_addbyte(c, x & 0xff);
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
1441 com_addbyte(c, x >> 8); /* XXX x should be positive */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1442 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1443
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1444 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1445 com_add_lnotab(struct compiling *c, int addr, int line)
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1446 {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1447 char *p;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1448 if (c->c_lnotab == NULL)
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1449 return;
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1450 if (com_check_size(&c->c_lnotab, c->c_lnotab_next + 2)) {
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1451 c->c_errors++;
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1452 return;
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1453 }
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1454 p = PyString_AS_STRING(c->c_lnotab) + c->c_lnotab_next;
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1455 *p++ = addr;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1456 *p++ = line;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1457 c->c_lnotab_next += 2;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1458 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1459
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1460 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1461 com_set_lineno(struct compiling *c, int lineno)
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1462 {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1463 c->c_lineno = lineno;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1464 if (c->c_firstlineno == 0) {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1465 c->c_firstlineno = c->c_last_line = lineno;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1466 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1467 else {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1468 int incr_addr = c->c_nexti - c->c_last_addr;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1469 int incr_line = lineno - c->c_last_line;
31764
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1470 c->c_lnotab_last = c->c_lnotab_next;
18065
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1471 while (incr_addr > 255) {
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1472 com_add_lnotab(c, 255, 0);
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1473 incr_addr -= 255;
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1474 }
18065
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1475 while (incr_line > 255) {
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1476 com_add_lnotab(c, incr_addr, 255);
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1477 incr_line -=255;
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1478 incr_addr = 0;
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1479 }
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1480 if (incr_addr > 0 || incr_line > 0)
889db6d55506 SF bug 430991: wrong co_lnotab
Tim Peters <tim.peters@gmail.com>
parents: 17736
diff changeset
1481 com_add_lnotab(c, incr_addr, incr_line);
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1482 c->c_last_addr = c->c_nexti;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1483 c->c_last_line = lineno;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1484 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1485 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1486
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
1487 static void
31764
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1488 com_strip_lnotab(struct compiling *c)
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1489 {
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1490 /* strip the last lnotab entry if no opcode were emitted.
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1491 * This prevents a line number to be generated on a final
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1492 * pass, like in the following example:
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1493 *
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1494 * if a:
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1495 * print 5
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1496 * else:
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1497 * pass
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1498 *
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1499 * Without the fix, a line trace event would be generated
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1500 * on the pass even if a is true (because of the implicit
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1501 * return).
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1502 */
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1503 if (c->c_nexti == c->c_last_addr && c->c_lnotab_last > 0) {
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1504 c->c_lnotab_next = c->c_lnotab_last;
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1505 }
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1506 }
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1507
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
1508 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1509 com_addoparg(struct compiling *c, int op, int arg)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1510 {
14147
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1511 int extended_arg = arg >> 16;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1512 if (extended_arg){
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1513 com_addbyte(c, EXTENDED_ARG);
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1514 com_addint(c, extended_arg);
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1515 arg &= 0xffff;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1516 }
5164
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
1517 com_addbyte(c, op);
8c277f29b535 Add global Py_OptimizeFlag. SET_LINENO is omitted again unless this is
Guido van Rossum <guido@python.org>
parents: 5119
diff changeset
1518 com_addint(c, arg);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1519 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1520
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1521 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1522 com_addfwref(struct compiling *c, int op, int *p_anchor)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1523 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1524 /* Compile a forward reference for backpatching */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1525 int here;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1526 int anchor;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1527 com_addbyte(c, op);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1528 here = c->c_nexti;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1529 anchor = *p_anchor;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1530 *p_anchor = here;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1531 com_addint(c, anchor == 0 ? 0 : here - anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1532 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1533
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1534 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1535 com_backpatch(struct compiling *c, int anchor)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1536 {
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
1537 unsigned char *code = (unsigned char *) PyString_AS_STRING(c->c_code);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1538 int target = c->c_nexti;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1539 int dist;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1540 int prev;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1541 for (;;) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1542 /* Make the JUMP instruction at anchor point to target */
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
1543 prev = code[anchor] + (code[anchor+1] << 8);
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
1544 dist = target - (anchor+2);
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
1545 code[anchor] = dist & 0xff;
14147
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1546 dist >>= 8;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1547 code[anchor+1] = dist;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1548 dist >>= 8;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1549 if (dist) {
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1550 com_error(c, PyExc_SystemError,
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1551 "com_backpatch: offset too large");
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1552 break;
21ec69a56228 Charles G. Waldman <cgw@fnal.gov>:
Fred Drake <fdrake@acm.org>
parents: 14117
diff changeset
1553 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1554 if (!prev)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1555 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1556 anchor -= prev;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1557 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1558 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1559
544
d0baa8300005 Minor changes.
Guido van Rossum <guido@python.org>
parents: 493
diff changeset
1560 /* Handle literals and names uniformly */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1561
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1562 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1563 com_add(struct compiling *c, PyObject *list, PyObject *dict, PyObject *v)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1564 {
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1565 PyObject *w, *t, *np=NULL;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1566 long n;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1567
30683
3b59fa5d25b1 Simplify and speedup uses of Py_BuildValue():
Raymond Hettinger <python@rcn.com>
parents: 30488
diff changeset
1568 t = PyTuple_Pack(2, v, v->ob_type);
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1569 if (t == NULL)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1570 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1571 w = PyDict_GetItem(dict, t);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1572 if (w != NULL) {
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1573 n = PyInt_AsLong(w);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1574 } else {
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1575 n = PyList_Size(list);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1576 np = PyInt_FromLong(n);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1577 if (np == NULL)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1578 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1579 if (PyList_Append(list, v) != 0)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1580 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1581 if (PyDict_SetItem(dict, t, np) != 0)
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1582 goto fail;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1583 Py_DECREF(np);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
1584 }
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1585 Py_DECREF(t);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
1586 return n;
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1587 fail:
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1588 Py_XDECREF(np);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1589 Py_XDECREF(t);
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1590 c->c_errors++;
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1591 return 0;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1592 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1593
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1594 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1595 com_addconst(struct compiling *c, PyObject *v)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1596 {
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1597 return com_add(c, c->c_consts, c->c_const_dict, v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1598 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1599
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1600 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1601 com_addname(struct compiling *c, PyObject *v)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1602 {
12522
0c28c9cabbda Charles G Waldman:
Guido van Rossum <guido@python.org>
parents: 12388
diff changeset
1603 return com_add(c, c->c_names, c->c_name_dict, v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1604 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1605
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23868
diff changeset
1606 int
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23868
diff changeset
1607 _Py_Mangle(char *p, char *name, char *buffer, size_t maxlen)
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1608 {
4131
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
1609 /* Name mangling: __private becomes _classname__private.
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1610 This is independent from how the name is used. */
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1611 size_t nlen, plen;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1612 if (p == NULL || name == NULL || name[0] != '_' || name[1] != '_')
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1613 return 0;
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1614 nlen = strlen(name);
4131
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
1615 if (nlen+2 >= maxlen)
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1616 return 0; /* Don't mangle __extremely_long_names */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1617 if (name[nlen-1] == '_' && name[nlen-2] == '_')
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1618 return 0; /* Don't mangle __whatever__ */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1619 /* Strip leading underscores from class name */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1620 while (*p == '_')
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1621 p++;
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1622 if (*p == '\0')
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1623 return 0; /* Don't mangle if class is just underscores */
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1624 plen = strlen(p);
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1625 if (plen + nlen >= maxlen)
4131
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
1626 plen = maxlen-nlen-2; /* Truncate class name if too long */
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
1627 /* buffer = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1628 buffer[0] = '_';
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1629 strncpy(buffer+1, p, plen);
4131
6b589334e0e9 Afterthough: leave both leading underscores in,
Guido van Rossum <guido@python.org>
parents: 4130
diff changeset
1630 strcpy(buffer+1+plen, name);
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1631 return 1;
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1632 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1633
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1634 static void
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1635 com_addop_name(struct compiling *c, int op, char *name)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1636 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1637 PyObject *v;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1638 int i;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1639 char buffer[MANGLE_LEN];
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
1640
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23868
diff changeset
1641 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
1642 name = buffer;
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
1643 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1644 c->c_errors++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1645 i = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1646 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1647 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1648 i = com_addname(c, v);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1649 Py_DECREF(v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1650 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1651 com_addoparg(c, op, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1652 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1653
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1654 #define NAME_LOCAL 0
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1655 #define NAME_GLOBAL 1
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1656 #define NAME_DEFAULT 2
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1657 #define NAME_CLOSURE 3
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1658
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1659 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1660 com_lookup_arg(PyObject *dict, PyObject *name)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1661 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1662 PyObject *v = PyDict_GetItem(dict, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1663 if (v == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1664 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1665 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1666 return PyInt_AS_LONG(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1667 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1668
24855
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1669 static int
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1670 none_assignment_check(struct compiling *c, char *name, int assigning)
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1671 {
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1672 if (name[0] == 'N' && strcmp(name, "None") == 0) {
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1673 char *msg;
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1674 if (assigning)
27270
392feecd2f1c A. Lloyd Flanagan pointed out a spelling error on c.l.py.
Michael W. Hudson <mwh@python.net>
parents: 26979
diff changeset
1675 msg = "assignment to None";
24855
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1676 else
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1677 msg = "deleting None";
32723
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 32717
diff changeset
1678 com_error(c, PyExc_SyntaxError, msg);
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 32717
diff changeset
1679 return -1;
24855
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1680 }
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1681 return 0;
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1682 }
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1683
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1684 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1685 com_addop_varname(struct compiling *c, int kind, char *name)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1686 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1687 PyObject *v;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1688 int i, reftype;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1689 int scope = NAME_DEFAULT;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1690 int op = STOP_CODE;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1691 char buffer[MANGLE_LEN];
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1692
24855
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1693 if (kind != VAR_LOAD &&
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1694 none_assignment_check(c, name, kind == VAR_STORE))
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1695 {
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1696 i = 255;
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1697 goto done;
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
1698 }
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23868
diff changeset
1699 if (_Py_Mangle(c->c_private, name, buffer, sizeof(buffer)))
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1700 name = buffer;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1701 if (name == NULL || (v = PyString_InternFromString(name)) == NULL) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1702 c->c_errors++;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1703 i = 255;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1704 goto done;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1705 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1706
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1707 reftype = get_ref_type(c, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1708 switch (reftype) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1709 case LOCAL:
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
1710 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1711 scope = NAME_LOCAL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1712 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1713 case GLOBAL_EXPLICIT:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1714 scope = NAME_GLOBAL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1715 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1716 case GLOBAL_IMPLICIT:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1717 if (c->c_flags & CO_OPTIMIZED)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1718 scope = NAME_GLOBAL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1719 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1720 case FREE:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1721 case CELL:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1722 scope = NAME_CLOSURE;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1723 break;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1724 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1725
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1726 i = com_addname(c, v);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1727 if (scope == NAME_LOCAL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1728 i = com_lookup_arg(c->c_locals, v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1729 else if (reftype == FREE)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1730 i = com_lookup_arg(c->c_freevars, v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1731 else if (reftype == CELL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1732 i = com_lookup_arg(c->c_cellvars, v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1733 if (i == -1) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1734 c->c_errors++; /* XXX no exception set */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1735 i = 255;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1736 goto done;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1737 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1738 Py_DECREF(v);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1739
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1740 switch (kind) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1741 case VAR_LOAD:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1742 switch (scope) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1743 case NAME_LOCAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1744 op = LOAD_FAST;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1745 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1746 case NAME_GLOBAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1747 op = LOAD_GLOBAL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1748 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1749 case NAME_DEFAULT:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1750 op = LOAD_NAME;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1751 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1752 case NAME_CLOSURE:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1753 op = LOAD_DEREF;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1754 break;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
1755 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1756 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1757 case VAR_STORE:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1758 switch (scope) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1759 case NAME_LOCAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1760 op = STORE_FAST;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1761 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1762 case NAME_GLOBAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1763 op = STORE_GLOBAL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1764 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1765 case NAME_DEFAULT:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1766 op = STORE_NAME;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1767 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1768 case NAME_CLOSURE:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1769 op = STORE_DEREF;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1770 break;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1771 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1772 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1773 case VAR_DELETE:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1774 switch (scope) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1775 case NAME_LOCAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1776 op = DELETE_FAST;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1777 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1778 case NAME_GLOBAL:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1779 op = DELETE_GLOBAL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1780 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1781 case NAME_DEFAULT:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1782 op = DELETE_NAME;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1783 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1784 case NAME_CLOSURE: {
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
1785 char buf[500];
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
1786 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
1787 DEL_CLOSURE_ERROR, name);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1788 com_error(c, PyExc_SyntaxError, buf);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1789 i = 255;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1790 break;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
1791 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1792 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1793 break;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
1794 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1795 done:
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1796 com_addoparg(c, op, i);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1797 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1798
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1799 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
1800 com_addopname(struct compiling *c, int op, node *n)
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1801 {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1802 char *name;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1803 char buffer[1000];
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1804 /* XXX it is possible to write this code without the 1000
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1805 chars on the total length of dotted names, I just can't be
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1806 bothered right now */
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1807 if (TYPE(n) == STAR)
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1808 name = "*";
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1809 else if (TYPE(n) == dotted_name) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1810 char *p = buffer;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1811 int i;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1812 name = buffer;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1813 for (i = 0; i < NCH(n); i += 2) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1814 char *s = STR(CHILD(n, i));
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1815 if (p + strlen(s) > buffer + (sizeof buffer) - 2) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1816 com_error(c, PyExc_MemoryError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
1817 "dotted_name too long");
2367
915fabec64a3 fix typo (== for =) in assignment
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
1818 name = NULL;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1819 break;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1820 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1821 if (p != buffer)
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1822 *p++ = '.';
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1823 strcpy(p, s);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1824 p = strchr(p, '\0');
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1825 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1826 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1827 else {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1828 REQ(n, NAME);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1829 name = STR(n);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1830 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
1831 com_addop_name(c, op, name);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1832 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
1833
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1834 static PyObject *
24854
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
1835 parsenumber(struct compiling *c, char *s)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1836 {
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
1837 char *end;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1838 long x;
5119
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1839 double dx;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1840 #ifndef WITHOUT_COMPLEX
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1841 int imflag;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1842 #endif
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1843
176
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
1844 errno = 0;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
1845 end = s + strlen(s) - 1;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1846 #ifndef WITHOUT_COMPLEX
3366
c25865884649 only use 'j' for imaginary constants
Guido van Rossum <guido@python.org>
parents: 3349
diff changeset
1847 imflag = *end == 'j' || *end == 'J';
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1848 #endif
744
834dd4a77ee2 fix what lint found
Guido van Rossum <guido@python.org>
parents: 703
diff changeset
1849 if (*end == 'l' || *end == 'L')
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1850 return PyLong_FromString(s, (char **)0, 0);
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24630
diff changeset
1851 if (s[0] == '0') {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1852 x = (long) PyOS_strtoul(s, &end, 0);
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24630
diff changeset
1853 if (x < 0 && errno == 0) {
31042
9c846c3df6f3 - Removed FutureWarnings related to hex/oct literals and conversions
Guido van Rossum <guido@python.org>
parents: 30713
diff changeset
1854 return PyLong_FromString(s, (char **)0, 0);
24736
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24630
diff changeset
1855 }
ff05eef3a6d1 Implement stage B0 of PEP 237: add warnings for operations that
Guido van Rossum <guido@python.org>
parents: 24630
diff changeset
1856 }
617
755bb8efcc2d Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents: 584
diff changeset
1857 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1858 x = PyOS_strtol(s, &end, 0);
176
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
1859 if (*end == '\0') {
19502
62bf9401a1ac If an integer constant can't be generated from an integer literal
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19343
diff changeset
1860 if (errno != 0)
62bf9401a1ac If an integer constant can't be generated from an integer literal
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19343
diff changeset
1861 return PyLong_FromString(s, (char **)0, 0);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1862 return PyInt_FromLong(x);
176
229168a2fe28 Added error checking for numeric constants; added local/global variable
Guido van Rossum <guido@python.org>
parents: 160
diff changeset
1863 }
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1864 /* XXX Huge floats may silently fail */
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1865 #ifndef WITHOUT_COMPLEX
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1866 if (imflag) {
24854
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
1867 Py_complex z;
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
1868 z.real = 0.;
5119
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1869 PyFPE_START_PROTECT("atof", return 0)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32121
diff changeset
1870 z.imag = PyOS_ascii_atof(s);
24854
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
1871 PyFPE_END_PROTECT(z)
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
1872 return PyComplex_FromCComplex(z);
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1873 }
6453
c9b139b9c5ff Fixed for WITHOUT_COMPLEX compilation (Jack)
Guido van Rossum <guido@python.org>
parents: 6186
diff changeset
1874 else
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
1875 #endif
6453
c9b139b9c5ff Fixed for WITHOUT_COMPLEX compilation (Jack)
Guido van Rossum <guido@python.org>
parents: 6186
diff changeset
1876 {
5119
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1877 PyFPE_START_PROTECT("atof", return 0)
32305
13656d58d450 Patch #774665: Make Python LC_NUMERIC agnostic.
Martin v. Löwis <martin@v.loewis.de>
parents: 32121
diff changeset
1878 dx = PyOS_ascii_atof(s);
5197
74c12704a0bd New form of PyFPE_END_PROTECT macro.
Guido van Rossum <guido@python.org>
parents: 5183
diff changeset
1879 PyFPE_END_PROTECT(dx)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1880 return PyFloat_FromDouble(dx);
5119
bda01a374762 Changes for Lee Busby's SIGFPE patch set.
Guido van Rossum <guido@python.org>
parents: 5041
diff changeset
1881 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1882 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1883
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1884 static PyObject *
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1885 decode_utf8(char **sPtr, char *end, char* encoding)
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1886 {
24624
6445e44fcb78 Fix PEP 263 code --without-unicode. Fixes #591943.
Martin v. Löwis <martin@v.loewis.de>
parents: 24539
diff changeset
1887 #ifndef Py_USING_UNICODE
24630
43caa212b959 Use Py_FatalError instead of abort.
Martin v. Löwis <martin@v.loewis.de>
parents: 24624
diff changeset
1888 Py_FatalError("decode_utf8 should not be called in this build.");
43caa212b959 Use Py_FatalError instead of abort.
Martin v. Löwis <martin@v.loewis.de>
parents: 24624
diff changeset
1889 return NULL;
24624
6445e44fcb78 Fix PEP 263 code --without-unicode. Fixes #591943.
Martin v. Löwis <martin@v.loewis.de>
parents: 24539
diff changeset
1890 #else
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1891 PyObject *u, *v;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1892 char *s, *t;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1893 t = s = *sPtr;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1894 /* while (s < end && *s != '\\') s++; */ /* inefficient for u".." */
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1895 while (s < end && (*s & 0x80)) s++;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1896 *sPtr = s;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1897 u = PyUnicode_DecodeUTF8(t, s - t, NULL);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1898 if (u == NULL)
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1899 return NULL;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1900 v = PyUnicode_AsEncodedString(u, encoding, NULL);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1901 Py_DECREF(u);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1902 return v;
24624
6445e44fcb78 Fix PEP 263 code --without-unicode. Fixes #591943.
Martin v. Löwis <martin@v.loewis.de>
parents: 24539
diff changeset
1903 #endif
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1904 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1905
26979
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26756
diff changeset
1906 /* compiler.transformer.Transformer.decode_literal depends on what
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26756
diff changeset
1907 might seem like minor details of this function -- changes here
784e7fdf8c52 SF patch [ 597919 ] compiler package and SET_LINENO
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 26756
diff changeset
1908 must be reflected there. */
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1909 static PyObject *
24854
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
1910 parsestr(struct compiling *c, char *s)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1911 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1912 PyObject *v;
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1913 size_t len;
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24770
diff changeset
1914 int quote = *s;
11974
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1915 int rawmode = 0;
24854
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
1916 char* encoding = ((c == NULL) ? NULL : c->c_encoding);
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1917 int need_encoding;
11974
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1918 int unicode = 0;
23555
c4fa37f89783 Accept u"..." literals even when Unicode is disabled. But these
Guido van Rossum <guido@python.org>
parents: 23538
diff changeset
1919
11974
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1920 if (isalpha(quote) || quote == '_') {
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1921 if (quote == 'u' || quote == 'U') {
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1922 quote = *++s;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1923 unicode = 1;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1924 }
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1925 if (quote == 'r' || quote == 'R') {
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1926 quote = *++s;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1927 rawmode = 1;
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1928 }
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
1929 }
1364
132602d854d8 Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1930 if (quote != '\'' && quote != '\"') {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1931 PyErr_BadInternalCall();
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1932 return NULL;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1933 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1934 s++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1935 len = strlen(s);
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1936 if (len > INT_MAX) {
24854
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
1937 com_error(c, PyExc_OverflowError,
22120
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
1938 "string to parse is too long");
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1939 return NULL;
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 12576
diff changeset
1940 }
1364
132602d854d8 Changes to accept double-quoted strings on input.
Guido van Rossum <guido@python.org>
parents: 1354
diff changeset
1941 if (s[--len] != quote) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1942 PyErr_BadInternalCall();
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1943 return NULL;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1944 }
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1945 if (len >= 4 && s[0] == quote && s[1] == quote) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1946 s += 2;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1947 len -= 2;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1948 if (s[--len] != quote || s[--len] != quote) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
1949 PyErr_BadInternalCall();
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1950 return NULL;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1951 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
1952 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
1953 #ifdef Py_USING_UNICODE
12538
859e500f0760 Marc-Andre Lemburg:
Guido van Rossum <guido@python.org>
parents: 12522
diff changeset
1954 if (unicode || Py_UnicodeFlag) {
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1955 PyObject *u, *w;
26319
90e9a3ecda44 Move three variables that are only used inside an if block into the block,
Walter Dörwald <walter@livinglogic.de>
parents: 25538
diff changeset
1956 char *buf;
90e9a3ecda44 Move three variables that are only used inside an if block into the block,
Walter Dörwald <walter@livinglogic.de>
parents: 25538
diff changeset
1957 char *p;
90e9a3ecda44 Move three variables that are only used inside an if block into the block,
Walter Dörwald <walter@livinglogic.de>
parents: 25538
diff changeset
1958 char *end;
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1959 if (encoding == NULL) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1960 buf = s;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1961 u = NULL;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1962 } else if (strcmp(encoding, "iso-8859-1") == 0) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1963 buf = s;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1964 u = NULL;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1965 } else {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1966 /* "\XX" may become "\u005c\uHHLL" (12 bytes) */
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1967 u = PyString_FromStringAndSize((char *)NULL, len * 4);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1968 if (u == NULL)
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1969 return NULL;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1970 p = buf = PyString_AsString(u);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1971 end = s + len;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1972 while (s < end) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1973 if (*s == '\\') {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1974 *p++ = *s++;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1975 if (*s & 0x80) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1976 strcpy(p, "u005c");
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1977 p += 5;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1978 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1979 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1980 if (*s & 0x80) { /* XXX inefficient */
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1981 char *r;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1982 int rn, i;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1983 w = decode_utf8(&s, end, "utf-16-be");
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1984 if (w == NULL) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1985 Py_DECREF(u);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1986 return NULL;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1987 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1988 r = PyString_AsString(w);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1989 rn = PyString_Size(w);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1990 assert(rn % 2 == 0);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1991 for (i = 0; i < rn; i += 2) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1992 sprintf(p, "\\u%02x%02x",
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1993 r[i + 0] & 0xFF,
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1994 r[i + 1] & 0xFF);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1995 p += 6;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1996 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1997 Py_DECREF(w);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1998 } else {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
1999 *p++ = *s++;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2000 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2001 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2002 len = p - buf;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2003 }
11974
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
2004 if (rawmode)
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2005 v = PyUnicode_DecodeRawUnicodeEscape(buf, len, NULL);
11974
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
2006 else
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2007 v = PyUnicode_DecodeUnicodeEscape(buf, len, NULL);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2008 Py_XDECREF(u);
22120
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
2009 if (v == NULL)
24854
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
2010 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
22120
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
2011 return v;
b2a2cc0c2a63 Patch #50002: Display line information for bad \x escapes:
Martin v. Löwis <martin@v.loewis.de>
parents: 21834
diff changeset
2012
11974
28003afa1a2f Marc-Andre Lemburg: support for Unicode string literals (u"...", ur"...").
Guido van Rossum <guido@python.org>
parents: 11598
diff changeset
2013 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
2014 #endif
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2015 need_encoding = (encoding != NULL &&
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2016 strcmp(encoding, "utf-8") != 0 &&
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2017 strcmp(encoding, "iso-8859-1") != 0);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2018 if (rawmode || strchr(s, '\\') == NULL) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2019 if (need_encoding) {
24624
6445e44fcb78 Fix PEP 263 code --without-unicode. Fixes #591943.
Martin v. Löwis <martin@v.loewis.de>
parents: 24539
diff changeset
2020 #ifndef Py_USING_UNICODE
6445e44fcb78 Fix PEP 263 code --without-unicode. Fixes #591943.
Martin v. Löwis <martin@v.loewis.de>
parents: 24539
diff changeset
2021 /* This should not happen - we never see any other
6445e44fcb78 Fix PEP 263 code --without-unicode. Fixes #591943.
Martin v. Löwis <martin@v.loewis.de>
parents: 24539
diff changeset
2022 encoding. */
24630
43caa212b959 Use Py_FatalError instead of abort.
Martin v. Löwis <martin@v.loewis.de>
parents: 24624
diff changeset
2023 Py_FatalError("cannot deal with encodings in this build.");
24624
6445e44fcb78 Fix PEP 263 code --without-unicode. Fixes #591943.
Martin v. Löwis <martin@v.loewis.de>
parents: 24539
diff changeset
2024 #else
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2025 PyObject* u = PyUnicode_DecodeUTF8(s, len, NULL);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2026 if (u == NULL)
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2027 return NULL;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2028 v = PyUnicode_AsEncodedString(u, encoding, NULL);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2029 Py_DECREF(u);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2030 return v;
24624
6445e44fcb78 Fix PEP 263 code --without-unicode. Fixes #591943.
Martin v. Löwis <martin@v.loewis.de>
parents: 24539
diff changeset
2031 #endif
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2032 } else {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2033 return PyString_FromStringAndSize(s, len);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2034 }
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
2035 }
24808
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24770
diff changeset
2036
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24770
diff changeset
2037 v = PyString_DecodeEscape(s, len, NULL, unicode,
ab63af3ec349 Patch #505705: Remove eval in pickle and cPickle.
Martin v. Löwis <martin@v.loewis.de>
parents: 24770
diff changeset
2038 need_encoding ? encoding : NULL);
14382
2d6cf68aa64a changed \x to consume exactly two hex digits. implements PEP-223
Fredrik Lundh <fredrik@pythonware.com>
parents: 14375
diff changeset
2039 if (v == NULL)
24854
136b853622cc Minor cleanup of parsename() and parsestr(): the 'struct compiling *'
Guido van Rossum <guido@python.org>
parents: 24844
diff changeset
2040 PyErr_SyntaxLocation(c->c_filename, c->c_lineno);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2041 return v;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2042 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2043
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2044 static PyObject *
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
2045 parsestrplus(struct compiling* c, node *n)
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2046 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2047 PyObject *v;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2048 int i;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2049 REQ(CHILD(n, 0), STRING);
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
2050 if ((v = parsestr(c, STR(CHILD(n, 0)))) != NULL) {
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2051 /* String literal concatenation */
12388
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2052 for (i = 1; i < NCH(n); i++) {
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2053 PyObject *s;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
2054 s = parsestr(c, STR(CHILD(n, i)));
12388
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2055 if (s == NULL)
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2056 goto onError;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2057 if (PyString_Check(v) && PyString_Check(s)) {
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2058 PyString_ConcatAndDel(&v, s);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2059 if (v == NULL)
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2060 goto onError;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2061 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
2062 #ifdef Py_USING_UNICODE
12388
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2063 else {
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2064 PyObject *temp;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2065 temp = PyUnicode_Concat(v, s);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2066 Py_DECREF(s);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2067 if (temp == NULL)
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2068 goto onError;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2069 Py_DECREF(v);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2070 v = temp;
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2071 }
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
2072 #endif
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2073 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2074 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2075 return v;
12388
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2076
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2077 onError:
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2078 Py_XDECREF(v);
c48172af13dc M.-A. Lemburg <mal@lemburg.com>:
Fred Drake <fdrake@acm.org>
parents: 12333
diff changeset
2079 return NULL;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2080 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
2081
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2082 static void
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2083 com_list_for(struct compiling *c, node *n, node *e, char *t)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2084 {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2085 int anchor = 0;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2086 int save_begin = c->c_begin;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2087
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2088 /* list_for: for v in expr [list_iter] */
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2089 com_node(c, CHILD(n, 3)); /* expr */
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
2090 com_addbyte(c, GET_ITER);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2091 c->c_begin = c->c_nexti;
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
2092 com_addfwref(c, FOR_ITER, &anchor);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2093 com_push(c, 1);
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2094 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2095 c->c_loops++;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2096 com_list_iter(c, n, e, t);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2097 c->c_loops--;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2098 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2099 c->c_begin = save_begin;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2100 com_backpatch(c, anchor);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
2101 com_pop(c, 1); /* FOR_ITER has popped this */
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2102 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2103
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2104 static void
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2105 com_gen_for(struct compiling *c, node *n, node *t, int is_outmost)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2106 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2107 int break_anchor = 0;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2108 int anchor = 0;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2109 int save_begin = c->c_begin;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2110
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2111 REQ(n, gen_for);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2112 /* gen_for: for v in test [gen_iter] */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2113
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2114 com_addfwref(c, SETUP_LOOP, &break_anchor);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2115 block_push(c, SETUP_LOOP);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2116
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2117 if (is_outmost) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2118 com_addop_varname(c, VAR_LOAD, "[outmost-iterable]");
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2119 com_push(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2120 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2121 else {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2122 com_node(c, CHILD(n, 3));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2123 com_addbyte(c, GET_ITER);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2124 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2125
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2126 c->c_begin = c->c_nexti;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2127 com_set_lineno(c, c->c_last_line);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2128 com_addfwref(c, FOR_ITER, &anchor);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2129 com_push(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2130 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2131
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2132 if (NCH(n) == 5)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2133 com_gen_iter(c, CHILD(n, 4), t);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2134 else {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2135 com_test(c, t);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2136 com_addbyte(c, YIELD_VALUE);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2137 com_pop(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2138 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2139
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2140 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2141 c->c_begin = save_begin;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2142
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2143 com_backpatch(c, anchor);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2144 com_pop(c, 1); /* FOR_ITER has popped this */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2145 com_addbyte(c, POP_BLOCK);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2146 block_pop(c, SETUP_LOOP);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2147 com_backpatch(c, break_anchor);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2148 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2149
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2150 static void
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2151 com_list_if(struct compiling *c, node *n, node *e, char *t)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2152 {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2153 int anchor = 0;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2154 int a = 0;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2155 /* list_iter: 'if' test [list_iter] */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2156 com_node(c, CHILD(n, 1));
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2157 com_addfwref(c, JUMP_IF_FALSE, &a);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2158 com_addbyte(c, POP_TOP);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2159 com_pop(c, 1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2160 com_list_iter(c, n, e, t);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2161 com_addfwref(c, JUMP_FORWARD, &anchor);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2162 com_backpatch(c, a);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2163 /* We jump here with an extra entry which we now pop */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2164 com_addbyte(c, POP_TOP);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2165 com_backpatch(c, anchor);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2166 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2167
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2168 static void
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2169 com_gen_if(struct compiling *c, node *n, node *t)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2170 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2171 /* gen_if: 'if' test [gen_iter] */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2172 int anchor = 0;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2173 int a=0;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2174
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2175 com_node(c, CHILD(n, 1));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2176 com_addfwref(c, JUMP_IF_FALSE, &a);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2177 com_addbyte(c, POP_TOP);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2178 com_pop(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2179
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2180 if (NCH(n) == 3)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2181 com_gen_iter(c, CHILD(n, 2), t);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2182 else {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2183 com_test(c, t);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2184 com_addbyte(c, YIELD_VALUE);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2185 com_pop(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2186 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2187 com_addfwref(c, JUMP_FORWARD, &anchor);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2188 com_backpatch(c, a);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2189 /* We jump here with an extra entry which we now pop */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2190 com_addbyte(c, POP_TOP);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2191 com_backpatch(c, anchor);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2192 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2193
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2194 static void
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2195 com_list_iter(struct compiling *c,
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2196 node *p, /* parent of list_iter node */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2197 node *e, /* element expression node */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2198 char *t /* name of result list temp local */)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2199 {
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2200 /* list_iter is the last child in a listmaker, list_for, or list_if */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2201 node *n = CHILD(p, NCH(p)-1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2202 if (TYPE(n) == list_iter) {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2203 n = CHILD(n, 0);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2204 switch (TYPE(n)) {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2205 case list_for:
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2206 com_list_for(c, n, e, t);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2207 break;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2208 case list_if:
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2209 com_list_if(c, n, e, t);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2210 break;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2211 default:
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2212 com_error(c, PyExc_SystemError,
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2213 "invalid list_iter node type");
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2214 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2215 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2216 else {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2217 com_addop_varname(c, VAR_LOAD, t);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2218 com_push(c, 1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2219 com_node(c, e);
31618
bc006682acb2 SF patch #910929: Optimize list comprehensions
Raymond Hettinger <python@rcn.com>
parents: 31042
diff changeset
2220 com_addbyte(c, LIST_APPEND);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2221 com_pop(c, 2);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2222 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2223 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2224
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2225 static void
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2226 com_gen_iter(struct compiling *c, node *n, node *t)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2227 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2228 /* gen_iter: gen_for | gen_if */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2229 node *ch;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2230 REQ(n, gen_iter);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2231
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2232 ch = CHILD(n, 0);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2233
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2234 switch (TYPE(ch)) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2235 case gen_for:
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2236 com_gen_for(c, ch, t, 0);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2237 break;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2238 case gen_if:
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2239 com_gen_if(c, ch, t);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2240 break;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2241 default:
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2242 com_error(c, PyExc_SystemError,
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2243 "invalid gen_iter node type");
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2244 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2245 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2246
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2247 static void
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2248 com_list_comprehension(struct compiling *c, node *n)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2249 {
14117
ac8809959d01 require list comprehensions to start with a for clause
Skip Montanaro <skip@pobox.com>
parents: 14105
diff changeset
2250 /* listmaker: test list_for */
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
2251 char tmpname[30];
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
2252
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
2253 REQ(n, listmaker);
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
2254 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", ++c->c_tmpname);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2255 com_addoparg(c, BUILD_LIST, 0);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2256 com_addbyte(c, DUP_TOP); /* leave the result on the stack */
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2257 com_push(c, 2);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2258 com_addop_varname(c, VAR_STORE, tmpname);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2259 com_pop(c, 1);
14117
ac8809959d01 require list comprehensions to start with a for clause
Skip Montanaro <skip@pobox.com>
parents: 14105
diff changeset
2260 com_list_for(c, CHILD(n, 1), CHILD(n, 0), tmpname);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2261 com_addop_varname(c, VAR_DELETE, tmpname);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2262 --c->c_tmpname;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2263 }
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2264
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2265 static void
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2266 com_listmaker(struct compiling *c, node *n)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2267 {
14117
ac8809959d01 require list comprehensions to start with a for clause
Skip Montanaro <skip@pobox.com>
parents: 14105
diff changeset
2268 /* listmaker: test ( list_for | (',' test)* [','] ) */
ac8809959d01 require list comprehensions to start with a for clause
Skip Montanaro <skip@pobox.com>
parents: 14105
diff changeset
2269 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for)
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2270 com_list_comprehension(c, n);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2271 else {
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2272 int len = 0;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2273 int i;
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2274 for (i = 0; i < NCH(n); i += 2, len++)
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2275 com_node(c, CHILD(n, i));
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2276 com_addoparg(c, BUILD_LIST, len);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2277 com_pop(c, len-1);
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2278 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2279 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2280
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2281 static void
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2282 com_generator_expression(struct compiling *c, node *n)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2283 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2284 /* testlist_gexp: test gen_for */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2285 /* argument: test gen_for */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2286 PyCodeObject *co;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2287
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2288 REQ(CHILD(n, 0), test);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2289 REQ(CHILD(n, 1), gen_for);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2290
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2291 symtable_enter_scope(c->c_symtable, "<genexpr>", TYPE(n),
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2292 n->n_lineno);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2293 co = icompile(n, c);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2294 symtable_exit_scope(c->c_symtable);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2295
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2296 if (co == NULL)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2297 c->c_errors++;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2298 else {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2299 int closure = com_make_closure(c, co);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2300 int i = com_addconst(c, (PyObject *)co);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2301
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2302 com_addoparg(c, LOAD_CONST, i);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2303 com_push(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2304 if (closure)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2305 com_addoparg(c, MAKE_CLOSURE, 0);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2306 else
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2307 com_addoparg(c, MAKE_FUNCTION, 0);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2308
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2309 com_test(c, CHILD(CHILD(n, 1), 3));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2310 com_addbyte(c, GET_ITER);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2311 com_addoparg(c, CALL_FUNCTION, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2312 com_pop(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2313
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2314 Py_DECREF(co);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2315 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2316 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2317
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2318 static void
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2319 com_testlist_gexp(struct compiling *c, node *n)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2320 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2321 /* testlist_gexp: test ( gen_for | (',' test)* [','] ) */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2322 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2323 com_generator_expression(c, n);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2324 else com_list(c, n, 0);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2325 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2326
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
2327
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2328 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2329 com_dictmaker(struct compiling *c, node *n)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2330 {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2331 int i;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2332 /* dictmaker: test ':' test (',' test ':' value)* [','] */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2333 for (i = 0; i+2 < NCH(n); i += 4) {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2334 /* We must arrange things just right for STORE_SUBSCR.
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2335 It wants the stack to look like (value) (dict) (key) */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2336 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2337 com_push(c, 1);
26670
1312297887a3 Fixing bug
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 26601
diff changeset
2338 com_node(c, CHILD(n, i)); /* key */
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2339 com_node(c, CHILD(n, i+2)); /* value */
26670
1312297887a3 Fixing bug
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 26601
diff changeset
2340 com_addbyte(c, ROT_THREE);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2341 com_addbyte(c, STORE_SUBSCR);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2342 com_pop(c, 3);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2343 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2344 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2345
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2346 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2347 com_atom(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2348 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2349 node *ch;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2350 PyObject *v;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2351 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2352 REQ(n, atom);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2353 ch = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2354 switch (TYPE(ch)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2355 case LPAR:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2356 if (TYPE(CHILD(n, 1)) == RPAR) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2357 com_addoparg(c, BUILD_TUPLE, 0);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2358 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2359 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2360 else
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2361 com_testlist_gexp(c, CHILD(n, 1));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2362 break;
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2363 case LSQB: /* '[' [listmaker] ']' */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2364 if (TYPE(CHILD(n, 1)) == RSQB) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2365 com_addoparg(c, BUILD_LIST, 0);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2366 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2367 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2368 else
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2369 com_listmaker(c, CHILD(n, 1));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2370 break;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2371 case LBRACE: /* '{' [dictmaker] '}' */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2372 com_addoparg(c, BUILD_MAP, 0);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2373 com_push(c, 1);
13936
25fd9846bd54 list comprehensions. see
Skip Montanaro <skip@pobox.com>
parents: 13930
diff changeset
2374 if (TYPE(CHILD(n, 1)) == dictmaker)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
2375 com_dictmaker(c, CHILD(n, 1));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2376 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2377 case BACKQUOTE:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2378 com_node(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2379 com_addbyte(c, UNARY_CONVERT);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2380 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2381 case NUMBER:
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2382 if ((v = parsenumber(c, STR(ch))) == NULL) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2383 i = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2384 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2385 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2386 i = com_addconst(c, v);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2387 Py_DECREF(v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2388 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2389 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2390 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2391 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2392 case STRING:
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
2393 v = parsestrplus(c, n);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
2394 if (v == NULL) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2395 c->c_errors++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2396 i = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2397 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2398 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2399 i = com_addconst(c, v);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2400 Py_DECREF(v);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2401 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2402 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2403 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2404 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2405 case NAME:
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
2406 com_addop_varname(c, VAR_LOAD, STR(ch));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2407 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2408 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2409 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2410 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2411 "com_atom: unexpected node type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2412 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2413 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2414
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2415 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2416 com_slice(struct compiling *c, node *n, int op)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2417 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2418 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2419 com_addbyte(c, op);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2420 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2421 else if (NCH(n) == 2) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2422 if (TYPE(CHILD(n, 0)) != COLON) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2423 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2424 com_addbyte(c, op+1);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2425 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2426 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2427 com_node(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2428 com_addbyte(c, op+2);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2429 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2430 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2431 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2432 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2433 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2434 com_node(c, CHILD(n, 2));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2435 com_addbyte(c, op+3);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2436 com_pop(c, 2);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2437 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2438 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2439
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2440 static void
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2441 com_augassign_slice(struct compiling *c, node *n, int opcode, node *augn)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2442 {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2443 if (NCH(n) == 1) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2444 com_addbyte(c, DUP_TOP);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2445 com_push(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2446 com_addbyte(c, SLICE);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2447 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2448 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2449 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2450 com_addbyte(c, ROT_TWO);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2451 com_addbyte(c, STORE_SLICE);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2452 com_pop(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2453 } else if (NCH(n) == 2 && TYPE(CHILD(n, 0)) != COLON) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2454 com_node(c, CHILD(n, 0));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2455 com_addoparg(c, DUP_TOPX, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2456 com_push(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2457 com_addbyte(c, SLICE+1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2458 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2459 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2460 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2461 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2462 com_addbyte(c, ROT_THREE);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2463 com_addbyte(c, STORE_SLICE+1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2464 com_pop(c, 3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2465 } else if (NCH(n) == 2) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2466 com_node(c, CHILD(n, 1));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2467 com_addoparg(c, DUP_TOPX, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2468 com_push(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2469 com_addbyte(c, SLICE+2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2470 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2471 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2472 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2473 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2474 com_addbyte(c, ROT_THREE);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2475 com_addbyte(c, STORE_SLICE+2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2476 com_pop(c, 3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2477 } else {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2478 com_node(c, CHILD(n, 0));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2479 com_node(c, CHILD(n, 2));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2480 com_addoparg(c, DUP_TOPX, 3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2481 com_push(c, 3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2482 com_addbyte(c, SLICE+3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2483 com_pop(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2484 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2485 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2486 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2487 com_addbyte(c, ROT_FOUR);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2488 com_addbyte(c, STORE_SLICE+3);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2489 com_pop(c, 4);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2490 }
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2491 }
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2492
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2493 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2494 com_argument(struct compiling *c, node *n, PyObject **pkeywords)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2495 {
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2496 node *m;
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2497 REQ(n, argument); /* [test '='] test [gen_for]; really [keyword '='] test */
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2498 if (NCH(n) == 1) {
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2499 if (*pkeywords != NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2500 com_error(c, PyExc_SyntaxError,
13993
716e1c4e3f78 When raising a SyntaxError, make a best-effort attempt to set the
Fred Drake <fdrake@acm.org>
parents: 13956
diff changeset
2501 "non-keyword arg after keyword arg");
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2502 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2503 else {
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2504 com_node(c, CHILD(n, 0));
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2505 }
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2506 return;
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2507 }
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2508 if (NCH(n) == 2) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2509 com_generator_expression(c, n);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2510 return;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2511 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
2512
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2513 m = n;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2514 do {
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2515 m = CHILD(m, 0);
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2516 } while (NCH(m) == 1);
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2517 if (TYPE(m) != NAME) {
16658
1a5a441772ff Bug #132313 error message confusing for assignment in lambda.
Tim Peters <tim.peters@gmail.com>
parents: 16638
diff changeset
2518 /* f(lambda x: x[0] = 3) ends up getting parsed with
1a5a441772ff Bug #132313 error message confusing for assignment in lambda.
Tim Peters <tim.peters@gmail.com>
parents: 16638
diff changeset
2519 * LHS test = lambda x: x[0], and RHS test = 3.
1a5a441772ff Bug #132313 error message confusing for assignment in lambda.
Tim Peters <tim.peters@gmail.com>
parents: 16638
diff changeset
2520 * SF bug 132313 points out that complaining about a keyword
1a5a441772ff Bug #132313 error message confusing for assignment in lambda.
Tim Peters <tim.peters@gmail.com>
parents: 16638
diff changeset
2521 * then is very confusing.
1a5a441772ff Bug #132313 error message confusing for assignment in lambda.
Tim Peters <tim.peters@gmail.com>
parents: 16638
diff changeset
2522 */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2523 com_error(c, PyExc_SyntaxError,
16658
1a5a441772ff Bug #132313 error message confusing for assignment in lambda.
Tim Peters <tim.peters@gmail.com>
parents: 16638
diff changeset
2524 TYPE(m) == lambdef ?
1a5a441772ff Bug #132313 error message confusing for assignment in lambda.
Tim Peters <tim.peters@gmail.com>
parents: 16638
diff changeset
2525 "lambda cannot contain assignment" :
1a5a441772ff Bug #132313 error message confusing for assignment in lambda.
Tim Peters <tim.peters@gmail.com>
parents: 16638
diff changeset
2526 "keyword can't be an expression");
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2527 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2528 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2529 PyObject *v = PyString_InternFromString(STR(m));
24856
87448a228cca Add warning for None used as keyword argument name in function call.
Guido van Rossum <guido@python.org>
parents: 24855
diff changeset
2530 (void) none_assignment_check(c, STR(m), 1);
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2531 if (v != NULL && *pkeywords == NULL)
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2532 *pkeywords = PyDict_New();
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
2533 if (v == NULL)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2534 c->c_errors++;
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
2535 else if (*pkeywords == NULL) {
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
2536 c->c_errors++;
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
2537 Py_DECREF(v);
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
2538 } else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2539 if (PyDict_GetItem(*pkeywords, v) != NULL)
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2540 com_error(c, PyExc_SyntaxError,
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2541 "duplicate keyword argument");
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2542 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2543 if (PyDict_SetItem(*pkeywords, v, v) != 0)
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2544 c->c_errors++;
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2545 com_addoparg(c, LOAD_CONST, com_addconst(c, v));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2546 com_push(c, 1);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2547 Py_DECREF(v);
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2548 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2549 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2550 com_node(c, CHILD(n, 2));
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2551 }
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2552
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2553 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2554 com_call_function(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2555 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2556 if (TYPE(n) == RPAR) {
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2557 com_addoparg(c, CALL_FUNCTION, 0);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2558 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2559 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2560 PyObject *keywords = NULL;
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2561 int i, na, nk;
9625
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
2562 int lineno = n->n_lineno;
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2563 int star_flag = 0;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2564 int starstar_flag = 0;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2565 int opcode;
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2566 REQ(n, arglist);
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2567 na = 0;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2568 nk = 0;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2569 for (i = 0; i < NCH(n); i += 2) {
9625
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
2570 node *ch = CHILD(n, i);
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2571 if (TYPE(ch) == STAR ||
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2572 TYPE(ch) == DOUBLESTAR)
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2573 break;
9625
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
2574 if (ch->n_lineno != lineno) {
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
2575 lineno = ch->n_lineno;
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
2576 com_set_lineno(c, lineno);
9625
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
2577 }
1fe48009d8ea Add more SET_LINENO instructions in long argument lists
Guido van Rossum <guido@python.org>
parents: 9293
diff changeset
2578 com_argument(c, ch, &keywords);
4905
32220c5728f5 Check for duplicate keyword arguments at compile time.
Guido van Rossum <guido@python.org>
parents: 4596
diff changeset
2579 if (keywords == NULL)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2580 na++;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2581 else
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2582 nk++;
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2583 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2584 Py_XDECREF(keywords);
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2585 while (i < NCH(n)) {
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2586 node *tok = CHILD(n, i);
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2587 node *ch = CHILD(n, i+1);
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2588 i += 3;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2589 switch (TYPE(tok)) {
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2590 case STAR: star_flag = 1; break;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2591 case DOUBLESTAR: starstar_flag = 1; break;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2592 }
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2593 com_node(c, ch);
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2594 }
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2595 if (na > 255 || nk > 255) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2596 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2597 "more than 255 arguments");
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
2598 }
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2599 if (star_flag || starstar_flag)
12086
96ea1a800b23 remove reference (vestigal) to CALL_FUNCTION_STAR
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 12082
diff changeset
2600 opcode = CALL_FUNCTION_VAR - 1 +
12082
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2601 star_flag + (starstar_flag << 1);
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2602 else
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2603 opcode = CALL_FUNCTION;
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2604 com_addoparg(c, opcode, na | (nk << 8));
603db0d0aed1 slightly modified version of Greg Ewing's extended call syntax patch
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11974
diff changeset
2605 com_pop(c, na + 2*nk + star_flag + starstar_flag);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2606 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2607 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2608
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2609 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2610 com_select_member(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2611 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2612 com_addopname(c, LOAD_ATTR, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2613 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2614
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2615 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2616 com_sliceobj(struct compiling *c, node *n)
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2617 {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2618 int i=0;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2619 int ns=2; /* number of slice arguments */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2620 node *ch;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2621
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2622 /* first argument */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2623 if (TYPE(CHILD(n,i)) == COLON) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2624 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2625 com_push(c, 1);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2626 i++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2627 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2628 else {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2629 com_node(c, CHILD(n,i));
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2630 i++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2631 REQ(CHILD(n,i),COLON);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2632 i++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2633 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2634 /* second argument */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2635 if (i < NCH(n) && TYPE(CHILD(n,i)) == test) {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2636 com_node(c, CHILD(n,i));
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2637 i++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2638 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2639 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2640 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2641 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2642 }
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2643 /* remaining arguments */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2644 for (; i < NCH(n); i++) {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2645 ns++;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2646 ch=CHILD(n,i);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2647 REQ(ch, sliceop);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2648 if (NCH(ch) == 1) {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2649 /* right argument of ':' missing */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2650 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2651 com_push(c, 1);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2652 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2653 else
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2654 com_node(c, CHILD(ch,1));
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2655 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2656 com_addoparg(c, BUILD_SLICE, ns);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2657 com_pop(c, 1 + (ns == 3));
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2658 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2659
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2660 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2661 com_subscript(struct compiling *c, node *n)
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2662 {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2663 node *ch;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2664 REQ(n, subscript);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2665 ch = CHILD(n,0);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2666 /* check for rubber index */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2667 if (TYPE(ch) == DOT && TYPE(CHILD(n,1)) == DOT) {
4403
3b3f7c3f6da2 Ellipses -> Ellipsis rename (the dictionary really says that it should
Guido van Rossum <guido@python.org>
parents: 4315
diff changeset
2668 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_Ellipsis));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2669 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2670 }
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2671 else {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2672 /* check for slice */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2673 if ((TYPE(ch) == COLON || NCH(n) > 1))
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2674 com_sliceobj(c, n);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2675 else {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2676 REQ(ch, test);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2677 com_node(c, ch);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2678 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2679 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2680 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2681
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2682 static void
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2683 com_subscriptlist(struct compiling *c, node *n, int assigning, node *augn)
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2684 {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2685 int i, op;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2686 REQ(n, subscriptlist);
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2687 /* Check to make backward compatible slice behavior for '[i:j]' */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2688 if (NCH(n) == 1) {
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2689 node *sub = CHILD(n, 0); /* subscript */
13873
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
2690 /* 'Basic' slice, should have exactly one colon. */
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
2691 if ((TYPE(CHILD(sub, 0)) == COLON
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
2692 || (NCH(sub) > 1 && TYPE(CHILD(sub, 1)) == COLON))
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
2693 && (TYPE(CHILD(sub,NCH(sub)-1)) != sliceop))
d014c8ab6a82 Fix some strange indentation and grammar that have been bugging me for
Thomas Wouters <thomas@python.org>
parents: 13658
diff changeset
2694 {
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2695 switch (assigning) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2696 case OP_DELETE:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2697 op = DELETE_SLICE;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2698 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2699 case OP_ASSIGN:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2700 op = STORE_SLICE;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2701 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2702 case OP_APPLY:
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2703 op = SLICE;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2704 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2705 default:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2706 com_augassign_slice(c, sub, assigning, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2707 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2708 }
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2709 com_slice(c, sub, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2710 if (op == STORE_SLICE)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2711 com_pop(c, 2);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2712 else if (op == DELETE_SLICE)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2713 com_pop(c, 1);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2714 return;
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2715 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2716 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2717 /* Else normal subscriptlist. Compile each subscript. */
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2718 for (i = 0; i < NCH(n); i += 2)
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2719 com_subscript(c, CHILD(n, i));
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2720 /* Put multiple subscripts into a tuple */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2721 if (NCH(n) > 1) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2722 i = (NCH(n)+1) / 2;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2723 com_addoparg(c, BUILD_TUPLE, i);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2724 com_pop(c, i-1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2725 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2726 switch (assigning) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2727 case OP_DELETE:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2728 op = DELETE_SUBSCR;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2729 i = 2;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2730 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2731 default:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2732 case OP_ASSIGN:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2733 op = STORE_SUBSCR;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2734 i = 3;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2735 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2736 case OP_APPLY:
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2737 op = BINARY_SUBSCR;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2738 i = 1;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2739 break;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2740 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2741 if (assigning > OP_APPLY) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2742 com_addoparg(c, DUP_TOPX, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2743 com_push(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2744 com_addbyte(c, BINARY_SUBSCR);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2745 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2746 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2747 com_addbyte(c, assigning);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2748 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2749 com_addbyte(c, ROT_THREE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2750 }
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2751 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2752 com_pop(c, i);
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2753 }
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2754
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
2755 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2756 com_apply_trailer(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2757 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2758 REQ(n, trailer);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2759 switch (TYPE(CHILD(n, 0))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2760 case LPAR:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2761 com_call_function(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2762 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2763 case DOT:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2764 com_select_member(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2765 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2766 case LSQB:
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
2767 com_subscriptlist(c, CHILD(n, 1), OP_APPLY, NULL);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2768 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2769 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2770 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2771 "com_apply_trailer: unknown trailer type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2772 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2773 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2774
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2775 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2776 com_power(struct compiling *c, node *n)
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2777 {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2778 int i;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2779 REQ(n, power);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2780 com_atom(c, CHILD(n, 0));
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2781 for (i = 1; i < NCH(n); i++) {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2782 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2783 com_factor(c, CHILD(n, i+1));
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2784 com_addbyte(c, BINARY_POWER);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2785 com_pop(c, 1);
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2786 break;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2787 }
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2788 else
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2789 com_apply_trailer(c, CHILD(n, i));
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2790 }
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2791 }
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2792
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2793 static void
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2794 com_invert_constant(struct compiling *c, node *n)
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2795 {
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2796 /* Compute the inverse of int and longs and use them directly,
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2797 but be prepared to generate code for all other
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2798 possibilities (invalid numbers, floats, complex).
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2799 */
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2800 PyObject *num, *inv = NULL;
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2801 int i;
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2802
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2803 REQ(n, NUMBER);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2804 num = parsenumber(c, STR(n));
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2805 if (num == NULL)
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2806 i = 255;
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2807 else {
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2808 inv = PyNumber_Invert(num);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2809 if (inv == NULL) {
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2810 PyErr_Clear();
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2811 i = com_addconst(c, num);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2812 } else {
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2813 i = com_addconst(c, inv);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2814 Py_DECREF(inv);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2815 }
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2816 Py_DECREF(num);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2817 }
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2818 com_addoparg(c, LOAD_CONST, i);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2819 com_push(c, 1);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2820 if (num != NULL && inv == NULL)
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2821 com_addbyte(c, UNARY_INVERT);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2822 }
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2823
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2824 static int
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2825 is_float_zero(const char *p)
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2826 {
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2827 int found_radix_point = 0;
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2828 int ch;
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2829 while ((ch = Py_CHARMASK(*p++)) != '\0') {
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2830 switch (ch) {
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2831 case '0':
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2832 /* no reason to believe it's not 0 -- continue */
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2833 break;
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2834
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2835 case 'e': case 'E': case 'j': case 'J':
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2836 /* If this was a hex constant, we already would have
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2837 returned 0 due to the 'x' or 'X', so 'e' or 'E'
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2838 must be an exponent marker, and we haven't yet
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2839 seen a non-zero digit, and it doesn't matter what
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2840 the exponent is then. For 'j' or 'J' similarly,
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2841 except that this is an imaginary 0 then. */
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2842 return 1;
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2843
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2844 case '.':
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2845 found_radix_point = 1;
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2846 break;
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2847
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2848 default:
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2849 return 0;
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2850 }
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2851 }
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2852 return found_radix_point;
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2853 }
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2854
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2855 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2856 com_factor(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2857 {
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2858 int childtype = TYPE(CHILD(n, 0));
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2859 node *pfactor, *ppower, *patom, *pnum;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2860 REQ(n, factor);
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2861 /* If the unary +, -, or ~ operator is applied to a constant,
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2862 don't generate a UNARY_xxx opcode. Just store the
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2863 approriate value as a constant. If the value is negative,
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2864 extend the string containing the constant and insert a
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2865 negative in the 0th position -- unless we're doing unary minus
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2866 of a floating zero! In that case the sign is significant, but
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2867 the const dict can't distinguish +0.0 from -0.0.
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2868 */
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2869 if ((childtype == PLUS || childtype == MINUS || childtype == TILDE)
19613
f7cf8961c667 When re-writing a factor containing a unary negation of a literal, only
Fred Drake <fdrake@acm.org>
parents: 19601
diff changeset
2870 && NCH(n) == 2
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2871 && TYPE((pfactor = CHILD(n, 1))) == factor
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2872 && NCH(pfactor) == 1
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2873 && TYPE((ppower = CHILD(pfactor, 0))) == power
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2874 && NCH(ppower) == 1
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2875 && TYPE((patom = CHILD(ppower, 0))) == atom
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2876 && TYPE((pnum = CHILD(patom, 0))) == NUMBER
27846
e9d7102f8278 SF #660455 : patch by NNorwitz.
Guido van Rossum <guido@python.org>
parents: 27781
diff changeset
2877 && !(childtype == MINUS &&
e9d7102f8278 SF #660455 : patch by NNorwitz.
Guido van Rossum <guido@python.org>
parents: 27781
diff changeset
2878 (STR(pnum)[0] == '0' || is_float_zero(STR(pnum))))) {
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2879 if (childtype == TILDE) {
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2880 com_invert_constant(c, pnum);
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2881 return;
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2882 }
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2883 if (childtype == MINUS) {
24529
3444005b6527 SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 24373
diff changeset
2884 char *s = PyObject_MALLOC(strlen(STR(pnum)) + 2);
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2885 if (s == NULL) {
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2886 com_error(c, PyExc_MemoryError, "");
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2887 com_addbyte(c, 255);
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2888 return;
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2889 }
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2890 s[0] = '-';
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2891 strcpy(s + 1, STR(pnum));
24529
3444005b6527 SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 24373
diff changeset
2892 PyObject_FREE(STR(pnum));
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2893 STR(pnum) = s;
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2894 }
19793
e2e9f1722c4f SF bug [#458941] Looks like a unary minus bug.
Tim Peters <tim.peters@gmail.com>
parents: 19613
diff changeset
2895 com_atom(c, patom);
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2896 }
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2897 else if (childtype == PLUS) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2898 com_factor(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2899 com_addbyte(c, UNARY_POSITIVE);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2900 }
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2901 else if (childtype == MINUS) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2902 com_factor(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2903 com_addbyte(c, UNARY_NEGATIVE);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2904 }
19187
30f8b654c2e5 SF Patch [ 429024 ] deal with some unary ops at compile time
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19177
diff changeset
2905 else if (childtype == TILDE) {
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2906 com_factor(c, CHILD(n, 1));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2907 com_addbyte(c, UNARY_INVERT);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2908 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2909 else {
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
2910 com_power(c, CHILD(n, 0));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2911 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2912 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2913
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2914 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2915 com_term(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2916 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2917 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2918 int op;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2919 REQ(n, term);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2920 com_factor(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2921 for (i = 2; i < NCH(n); i += 2) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2922 com_factor(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2923 switch (TYPE(CHILD(n, i-1))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2924 case STAR:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2925 op = BINARY_MULTIPLY;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2926 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2927 case SLASH:
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
2928 if (c->c_flags & CO_FUTURE_DIVISION)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
2929 op = BINARY_TRUE_DIVIDE;
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
2930 else
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
2931 op = BINARY_DIVIDE;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2932 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2933 case PERCENT:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2934 op = BINARY_MODULO;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2935 break;
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
2936 case DOUBLESLASH:
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
2937 op = BINARY_FLOOR_DIVIDE;
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
2938 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2939 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2940 com_error(c, PyExc_SystemError,
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
2941 "com_term: operator not *, /, // or %");
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2942 op = 255;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2943 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2944 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2945 com_pop(c, 1);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2946 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2947 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2948
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2949 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2950 com_arith_expr(struct compiling *c, node *n)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2951 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2952 int i;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2953 int op;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2954 REQ(n, arith_expr);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2955 com_term(c, CHILD(n, 0));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2956 for (i = 2; i < NCH(n); i += 2) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2957 com_term(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2958 switch (TYPE(CHILD(n, i-1))) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2959 case PLUS:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2960 op = BINARY_ADD;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2961 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2962 case MINUS:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2963 op = BINARY_SUBTRACT;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2964 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2965 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2966 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2967 "com_arith_expr: operator not + or -");
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2968 op = 255;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2969 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2970 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2971 com_pop(c, 1);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2972 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2973 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2974
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2975 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
2976 com_shift_expr(struct compiling *c, node *n)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2977 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2978 int i;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2979 int op;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2980 REQ(n, shift_expr);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2981 com_arith_expr(c, CHILD(n, 0));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2982 for (i = 2; i < NCH(n); i += 2) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2983 com_arith_expr(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2984 switch (TYPE(CHILD(n, i-1))) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2985 case LEFTSHIFT:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2986 op = BINARY_LSHIFT;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2987 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2988 case RIGHTSHIFT:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2989 op = BINARY_RSHIFT;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2990 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2991 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
2992 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
2993 "com_shift_expr: operator not << or >>");
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2994 op = 255;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2995 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2996 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
2997 com_pop(c, 1);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2998 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
2999 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3000
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3001 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3002 com_and_expr(struct compiling *c, node *n)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3003 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3004 int i;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3005 int op;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3006 REQ(n, and_expr);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3007 com_shift_expr(c, CHILD(n, 0));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3008 for (i = 2; i < NCH(n); i += 2) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3009 com_shift_expr(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3010 if (TYPE(CHILD(n, i-1)) == AMPER) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3011 op = BINARY_AND;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3012 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3013 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3014 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3015 "com_and_expr: operator not &");
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3016 op = 255;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3017 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3018 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3019 com_pop(c, 1);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3020 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3021 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3022
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3023 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3024 com_xor_expr(struct compiling *c, node *n)
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3025 {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3026 int i;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3027 int op;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3028 REQ(n, xor_expr);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3029 com_and_expr(c, CHILD(n, 0));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3030 for (i = 2; i < NCH(n); i += 2) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3031 com_and_expr(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3032 if (TYPE(CHILD(n, i-1)) == CIRCUMFLEX) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3033 op = BINARY_XOR;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3034 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3035 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3036 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3037 "com_xor_expr: operator not ^");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3038 op = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3039 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3040 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3041 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3042 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3043 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3044
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3045 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3046 com_expr(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3047 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3048 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3049 int op;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3050 REQ(n, expr);
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3051 com_xor_expr(c, CHILD(n, 0));
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3052 for (i = 2; i < NCH(n); i += 2) {
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3053 com_xor_expr(c, CHILD(n, i));
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3054 if (TYPE(CHILD(n, i-1)) == VBAR) {
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3055 op = BINARY_OR;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3056 }
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3057 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3058 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3059 "com_expr: expr operator not |");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3060 op = 255;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3061 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3062 com_addbyte(c, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3063 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3064 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3065 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3066
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3067 static enum cmp_op
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3068 cmp_type(node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3069 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3070 REQ(n, comp_op);
29084
ce1d7e10f49e cmp_type(): The grammar stopped allowing '=' as a comparison operator
Tim Peters <tim.peters@gmail.com>
parents: 29066
diff changeset
3071 /* comp_op: '<' | '>' | '>=' | '<=' | '<>' | '!=' | '=='
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3072 | 'in' | 'not' 'in' | 'is' | 'is' not' */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3073 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3074 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3075 switch (TYPE(n)) {
21609
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3076 case LESS: return PyCmp_LT;
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3077 case GREATER: return PyCmp_GT;
29084
ce1d7e10f49e cmp_type(): The grammar stopped allowing '=' as a comparison operator
Tim Peters <tim.peters@gmail.com>
parents: 29066
diff changeset
3078 case EQEQUAL: return PyCmp_EQ;
21609
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3079 case LESSEQUAL: return PyCmp_LE;
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3080 case GREATEREQUAL: return PyCmp_GE;
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3081 case NOTEQUAL: return PyCmp_NE; /* <> or != */
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3082 case NAME: if (strcmp(STR(n), "in") == 0) return PyCmp_IN;
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3083 if (strcmp(STR(n), "is") == 0) return PyCmp_IS;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3084 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3085 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3086 else if (NCH(n) == 2) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3087 switch (TYPE(CHILD(n, 0))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3088 case NAME: if (strcmp(STR(CHILD(n, 1)), "in") == 0)
21609
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3089 return PyCmp_NOT_IN;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3090 if (strcmp(STR(CHILD(n, 0)), "is") == 0)
21609
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3091 return PyCmp_IS_NOT;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3092 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3093 }
21609
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3094 return PyCmp_BAD;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3095 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3096
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3097 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3098 com_comparison(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3099 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3100 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3101 enum cmp_op op;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3102 int anchor;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3103 REQ(n, comparison); /* comparison: expr (comp_op expr)* */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3104 com_expr(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3105 if (NCH(n) == 1)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3106 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3107
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3108 /****************************************************************
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3109 The following code is generated for all but the last
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3110 comparison in a chain:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3111
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3112 label: on stack: opcode: jump to:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3113
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3114 a <code to load b>
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3115 a, b DUP_TOP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3116 a, b, b ROT_THREE
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3117 b, a, b COMPARE_OP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3118 b, 0-or-1 JUMP_IF_FALSE L1
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3119 b, 1 POP_TOP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3120 b
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3121
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3122 We are now ready to repeat this sequence for the next
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3123 comparison in the chain.
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3124
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3125 For the last we generate:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3126
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3127 b <code to load c>
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3128 b, c COMPARE_OP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3129 0-or-1
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3130
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3131 If there were any jumps to L1 (i.e., there was more than one
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3132 comparison), we generate:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3133
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3134 0-or-1 JUMP_FORWARD L2
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3135 L1: b, 0 ROT_TWO
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3136 0, b POP_TOP
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3137 0
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3138 L2: 0-or-1
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3139 ****************************************************************/
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3140
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3141 anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3142
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3143 for (i = 2; i < NCH(n); i += 2) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3144 com_expr(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3145 if (i+2 < NCH(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3146 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3147 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3148 com_addbyte(c, ROT_THREE);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3149 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3150 op = cmp_type(CHILD(n, i-1));
21609
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
3151 if (op == PyCmp_BAD) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3152 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3153 "com_comparison: unknown comparison op");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3154 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3155 com_addoparg(c, COMPARE_OP, op);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3156 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3157 if (i+2 < NCH(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3158 com_addfwref(c, JUMP_IF_FALSE, &anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3159 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3160 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3161 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3162 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3163
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3164 if (anchor) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3165 int anchor2 = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3166 com_addfwref(c, JUMP_FORWARD, &anchor2);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3167 com_backpatch(c, anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3168 com_addbyte(c, ROT_TWO);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3169 com_addbyte(c, POP_TOP);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3170 com_backpatch(c, anchor2);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3171 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3172 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3173
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3174 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3175 com_not_test(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3176 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3177 REQ(n, not_test); /* 'not' not_test | comparison */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3178 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3179 com_comparison(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3180 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3181 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3182 com_not_test(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3183 com_addbyte(c, UNARY_NOT);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3184 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3185 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3186
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3187 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3188 com_and_test(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3189 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3190 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3191 int anchor;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3192 REQ(n, and_test); /* not_test ('and' not_test)* */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3193 anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3194 i = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3195 for (;;) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3196 com_not_test(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3197 if ((i += 2) >= NCH(n))
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3198 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3199 com_addfwref(c, JUMP_IF_FALSE, &anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3200 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3201 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3202 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3203 if (anchor)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3204 com_backpatch(c, anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3205 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3206
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3207 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3208 com_make_closure(struct compiling *c, PyCodeObject *co)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3209 {
21398
de125792bfaa Fix for SF bug [ #492403 ] exec() segfaults on closure's func_code
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21179
diff changeset
3210 int i, free = PyCode_GetNumFree(co);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3211 if (free == 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3212 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3213 for (i = 0; i < free; ++i) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3214 /* Bypass com_addop_varname because it will generate
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3215 LOAD_DEREF but LOAD_CLOSURE is needed.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3216 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3217 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3218 int arg, reftype;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3219
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3220 /* Special case: If a class contains a method with a
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3221 free variable that has the same name as a method,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3222 the name will be considered free *and* local in the
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3223 class. It should be handled by the closure, as
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3224 well as by the normal name loookup logic.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3225 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3226 reftype = get_ref_type(c, PyString_AS_STRING(name));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3227 if (reftype == CELL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3228 arg = com_lookup_arg(c->c_cellvars, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3229 else /* (reftype == FREE) */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3230 arg = com_lookup_arg(c->c_freevars, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3231 if (arg == -1) {
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
3232 fprintf(stderr, "lookup %s in %s %d %d\n"
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
3233 "freevars of %s: %s\n",
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
3234 PyObject_REPR(name),
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
3235 c->c_name,
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
3236 reftype, arg,
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
3237 PyString_AS_STRING(co->co_name),
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
3238 PyObject_REPR(co->co_freevars));
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3239 Py_FatalError("com_make_closure()");
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3240 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3241 com_addoparg(c, LOAD_CLOSURE, arg);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3242
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3243 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3244 com_push(c, free);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3245 return 1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3246 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3247
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3248 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3249 com_test(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3250 {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3251 REQ(n, test); /* and_test ('or' and_test)* | lambdef */
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3252 if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) {
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
3253 PyCodeObject *co;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3254 int i, closure;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
3255 int ndefs = com_argdefs(c, CHILD(n, 0));
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
3256 symtable_enter_scope(c->c_symtable, "lambda", lambdef,
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
3257 n->n_lineno);
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
3258 co = icompile(CHILD(n, 0), c);
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
3259 if (co == NULL) {
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
3260 c->c_errors++;
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
3261 return;
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
3262 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3263 symtable_exit_scope(c->c_symtable);
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
3264 i = com_addconst(c, (PyObject *)co);
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
3265 closure = com_make_closure(c, co);
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3266 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3267 com_push(c, 1);
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
3268 if (closure) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3269 com_addoparg(c, MAKE_CLOSURE, ndefs);
21398
de125792bfaa Fix for SF bug [ #492403 ] exec() segfaults on closure's func_code
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21179
diff changeset
3270 com_pop(c, PyCode_GetNumFree(co));
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
3271 } else
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3272 com_addoparg(c, MAKE_FUNCTION, ndefs);
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
3273 Py_DECREF(co);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3274 com_pop(c, ndefs);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3275 }
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3276 else {
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3277 int anchor = 0;
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3278 int i = 0;
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3279 for (;;) {
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3280 com_and_test(c, CHILD(n, i));
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3281 if ((i += 2) >= NCH(n))
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3282 break;
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3283 com_addfwref(c, JUMP_IF_TRUE, &anchor);
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3284 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3285 com_pop(c, 1);
1417
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3286 }
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3287 if (anchor)
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3288 com_backpatch(c, anchor);
f7edfa6d0dcb change syntactical position of lambdef (was an atom, now is a test)
Guido van Rossum <guido@python.org>
parents: 1415
diff changeset
3289 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3290 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3291
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3292 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3293 com_list(struct compiling *c, node *n, int toplevel)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3294 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3295 /* exprlist: expr (',' expr)* [',']; likewise for testlist */
482
ee899a81570d New argument passing.
Guido van Rossum <guido@python.org>
parents: 466
diff changeset
3296 if (NCH(n) == 1 && !toplevel) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3297 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3298 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3299 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3300 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3301 int len;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3302 len = (NCH(n) + 1) / 2;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3303 for (i = 0; i < NCH(n); i += 2)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3304 com_node(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3305 com_addoparg(c, BUILD_TUPLE, len);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3306 com_pop(c, len-1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3307 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3308 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3309
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3310
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3311 /* Begin of assignment compilation */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3312
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3313
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3314 static void
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3315 com_augassign_attr(struct compiling *c, node *n, int opcode, node *augn)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3316 {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3317 com_addbyte(c, DUP_TOP);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3318 com_push(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3319 com_addopname(c, LOAD_ATTR, n);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3320 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3321 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3322 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3323 com_addbyte(c, ROT_TWO);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3324 com_addopname(c, STORE_ATTR, n);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3325 com_pop(c, 2);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3326 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3327
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3328 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3329 com_assign_attr(struct compiling *c, node *n, int assigning)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3330 {
24855
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
3331 if (none_assignment_check(c, STR(n), assigning))
bed5d5e536bc Add warnings for assignment or deletion of variables and attributes
Guido van Rossum <guido@python.org>
parents: 24854
diff changeset
3332 return;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3333 com_addopname(c, assigning ? STORE_ATTR : DELETE_ATTR, n);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3334 com_pop(c, assigning ? 2 : 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3335 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3336
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3337 static void
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3338 com_assign_trailer(struct compiling *c, node *n, int assigning, node *augn)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3339 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3340 REQ(n, trailer);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3341 switch (TYPE(CHILD(n, 0))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3342 case LPAR: /* '(' [exprlist] ')' */
23589
b0658feef939 Fix SF bug [ 561825 ] Confusing error for "del f()"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23555
diff changeset
3343 if (assigning == OP_DELETE)
b0658feef939 Fix SF bug [ 561825 ] Confusing error for "del f()"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23555
diff changeset
3344 com_error(c, PyExc_SyntaxError,
b0658feef939 Fix SF bug [ 561825 ] Confusing error for "del f()"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23555
diff changeset
3345 "can't delete function call");
b0658feef939 Fix SF bug [ 561825 ] Confusing error for "del f()"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23555
diff changeset
3346 else
b0658feef939 Fix SF bug [ 561825 ] Confusing error for "del f()"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23555
diff changeset
3347 com_error(c, PyExc_SyntaxError,
b0658feef939 Fix SF bug [ 561825 ] Confusing error for "del f()"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23555
diff changeset
3348 "can't assign to function call");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3349 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3350 case DOT: /* '.' NAME */
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3351 if (assigning > OP_APPLY)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3352 com_augassign_attr(c, CHILD(n, 1), assigning, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3353 else
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3354 com_assign_attr(c, CHILD(n, 1), assigning);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3355 break;
3839
22e29b1747e1 Changes for slice and ellipses
Guido van Rossum <guido@python.org>
parents: 3766
diff changeset
3356 case LSQB: /* '[' subscriptlist ']' */
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3357 com_subscriptlist(c, CHILD(n, 1), assigning, augn);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3358 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3359 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3360 com_error(c, PyExc_SystemError, "unknown trailer type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3361 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3362 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3363
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3364 static void
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
3365 com_assign_sequence(struct compiling *c, node *n, int assigning)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3366 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3367 int i;
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
3368 if (TYPE(n) != testlist && TYPE(n) != testlist_gexp &&
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
3369 TYPE(n) != listmaker)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3370 REQ(n, exprlist);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3371 if (assigning) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3372 i = (NCH(n)+1)/2;
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
3373 com_addoparg(c, UNPACK_SEQUENCE, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3374 com_push(c, i-1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3375 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3376 for (i = 0; i < NCH(n); i += 2)
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3377 com_assign(c, CHILD(n, i), assigning, NULL);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3378 }
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3379
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3380 static void
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3381 com_augassign_name(struct compiling *c, node *n, int opcode, node *augn)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3382 {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3383 REQ(n, NAME);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3384 com_addop_varname(c, VAR_LOAD, STR(n));
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3385 com_push(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3386 com_node(c, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3387 com_addbyte(c, opcode);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3388 com_pop(c, 1);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3389 com_assign_name(c, n, OP_ASSIGN);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3390 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3391
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3392 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3393 com_assign_name(struct compiling *c, node *n, int assigning)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3394 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3395 REQ(n, NAME);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3396 com_addop_varname(c, assigning ? VAR_STORE : VAR_DELETE, STR(n));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3397 if (assigning)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3398 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3399 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3400
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3401 static void
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3402 com_assign(struct compiling *c, node *n, int assigning, node *augn)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3403 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3404 /* Loop to avoid trivial recursion */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3405 for (;;) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3406 switch (TYPE(n)) {
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3407
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3408 case exprlist:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3409 case testlist:
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 23280
diff changeset
3410 case testlist1:
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
3411 case testlist_gexp:
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3412 if (NCH(n) > 1) {
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
3413 if (TYPE(CHILD(n, 1)) == gen_for) {
33626
cd58a9b4ba66 Fix two erroneous error messages.
Raymond Hettinger <python@rcn.com>
parents: 33620
diff changeset
3414 com_error(c, PyExc_SyntaxError,
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
3415 "assign to generator expression not possible");
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
3416 return;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
3417 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3418 if (assigning > OP_APPLY) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3419 com_error(c, PyExc_SyntaxError,
33626
cd58a9b4ba66 Fix two erroneous error messages.
Raymond Hettinger <python@rcn.com>
parents: 33620
diff changeset
3420 "augmented assign to generator expression not possible");
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3421 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3422 }
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
3423 com_assign_sequence(c, n, assigning);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3424 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3425 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3426 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3427 break;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3428
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3429 case test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3430 case and_test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3431 case not_test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3432 case comparison:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3433 case expr:
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3434 case xor_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3435 case and_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3436 case shift_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
3437 case arith_expr:
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3438 case term:
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3439 case factor:
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3440 if (NCH(n) > 1) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3441 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3442 "can't assign to operator");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3443 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3444 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3445 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3446 break;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3447
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
3448 case power: /* atom trailer* ('**' power)*
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
3449 ('+'|'-'|'~') factor | atom trailer* */
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3450 if (TYPE(CHILD(n, 0)) != atom) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3451 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3452 "can't assign to operator");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3453 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3454 }
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3455 if (NCH(n) > 1) { /* trailer or exponent present */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3456 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3457 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3458 for (i = 1; i+1 < NCH(n); i++) {
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3459 if (TYPE(CHILD(n, i)) == DOUBLESTAR) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3460 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3461 "can't assign to operator");
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3462 return;
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
3463 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3464 com_apply_trailer(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3465 } /* NB i is still alive */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3466 com_assign_trailer(c,
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3467 CHILD(n, i), assigning, augn);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3468 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3469 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3470 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3471 break;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3472
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3473 case atom:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3474 switch (TYPE(CHILD(n, 0))) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3475 case LPAR:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3476 n = CHILD(n, 1);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3477 if (TYPE(n) == RPAR) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3478 /* XXX Should allow () = () ??? */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3479 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3480 "can't assign to ()");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3481 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3482 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3483 if (assigning > OP_APPLY) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3484 com_error(c, PyExc_SyntaxError,
33627
0ae0bef10014 Improve error message for augmented assignments to genexps or listcomps.
Raymond Hettinger <python@rcn.com>
parents: 33626
diff changeset
3485 "augmented assign to tuple literal or generator expression not possible");
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3486 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3487 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3488 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3489 case LSQB:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3490 n = CHILD(n, 1);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3491 if (TYPE(n) == RSQB) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3492 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3493 "can't assign to []");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3494 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3495 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3496 if (assigning > OP_APPLY) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3497 com_error(c, PyExc_SyntaxError,
33627
0ae0bef10014 Improve error message for augmented assignments to genexps or listcomps.
Raymond Hettinger <python@rcn.com>
parents: 33626
diff changeset
3498 "augmented assign to list literal or comprehension not possible");
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3499 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3500 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3501 if (NCH(n) > 1
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3502 && TYPE(CHILD(n, 1)) == list_for) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3503 com_error(c, PyExc_SyntaxError,
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
3504 "can't assign to list comprehension");
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3505 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3506 }
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
3507 com_assign_sequence(c, n, assigning);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3508 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3509 case NAME:
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3510 if (assigning > OP_APPLY)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3511 com_augassign_name(c, CHILD(n, 0),
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3512 assigning, augn);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3513 else
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3514 com_assign_name(c, CHILD(n, 0),
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3515 assigning);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3516 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3517 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3518 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
3519 "can't assign to literal");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3520 return;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3521 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3522 break;
3963
00722990b80c Removed unused var; added error check for ``lambda: x=1''.
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
3523
00722990b80c Removed unused var; added error check for ``lambda: x=1''.
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
3524 case lambdef:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3525 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3526 "can't assign to lambda");
3963
00722990b80c Removed unused var; added error check for ``lambda: x=1''.
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
3527 return;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3528
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3529 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3530 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3531 "com_assign: bad node");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3532 return;
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
3533
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3534 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3535 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3536 }
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3537
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3538 static void
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3539 com_augassign(struct compiling *c, node *n)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3540 {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3541 int opcode;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3542
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3543 switch (STR(CHILD(CHILD(n, 1), 0))[0]) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3544 case '+': opcode = INPLACE_ADD; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3545 case '-': opcode = INPLACE_SUBTRACT; break;
19051
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
3546 case '/':
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
3547 if (STR(CHILD(CHILD(n, 1), 0))[1] == '/')
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
3548 opcode = INPLACE_FLOOR_DIVIDE;
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
3549 else if (c->c_flags & CO_FUTURE_DIVISION)
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
3550 opcode = INPLACE_TRUE_DIVIDE;
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
3551 else
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
3552 opcode = INPLACE_DIVIDE;
f20727cc3d0f Implement PEP 238 in its (almost) full glory.
Guido van Rossum <guido@python.org>
parents: 19021
diff changeset
3553 break;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3554 case '%': opcode = INPLACE_MODULO; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3555 case '<': opcode = INPLACE_LSHIFT; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3556 case '>': opcode = INPLACE_RSHIFT; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3557 case '&': opcode = INPLACE_AND; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3558 case '^': opcode = INPLACE_XOR; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3559 case '|': opcode = INPLACE_OR; break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3560 case '*':
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3561 if (STR(CHILD(CHILD(n, 1), 0))[1] == '*')
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3562 opcode = INPLACE_POWER;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3563 else
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3564 opcode = INPLACE_MULTIPLY;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3565 break;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3566 default:
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3567 com_error(c, PyExc_SystemError, "com_augassign: bad operator");
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3568 return;
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3569 }
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3570 com_assign(c, CHILD(n, 0), opcode, CHILD(n, 2));
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3571 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3572
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3573 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3574 com_expr_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3575 {
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3576 REQ(n, expr_stmt);
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3577 /* testlist (('=' testlist)* | augassign testlist) */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3578 /* Forget it if we have just a doc string here */
5264
38878f1cd4f1 Support for alternative string quotes (a"xx", b"xx", c"xx", ...).
Guido van Rossum <guido@python.org>
parents: 5233
diff changeset
3579 if (!c->c_interactive && NCH(n) == 1 && get_rawdocstring(n) != NULL)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3580 return;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3581 if (NCH(n) == 1) {
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3582 com_node(c, CHILD(n, NCH(n)-1));
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3583 if (c->c_interactive)
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3584 com_addbyte(c, PRINT_EXPR);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3585 else
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
3586 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3587 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3588 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3589 else if (TYPE(CHILD(n,1)) == augassign)
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3590 com_augassign(c, n);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3591 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3592 int i;
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3593 com_node(c, CHILD(n, NCH(n)-1));
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3594 for (i = 0; i < NCH(n)-2; i+=2) {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3595 if (i+2 < NCH(n)-2) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3596 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3597 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3598 }
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
3599 com_assign(c, CHILD(n, i), OP_ASSIGN, NULL);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3600 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3601 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3602 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3603
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3604 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3605 com_assert_stmt(struct compiling *c, node *n)
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3606 {
23280
9e22a8702de2 If Py_OptimizeFlag is false then always evaluate assert conditions, don't
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23211
diff changeset
3607 int a = 0;
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3608 int i;
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3609 REQ(n, assert_stmt); /* 'assert' test [',' test] */
23280
9e22a8702de2 If Py_OptimizeFlag is false then always evaluate assert conditions, don't
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23211
diff changeset
3610 if (Py_OptimizeFlag)
9e22a8702de2 If Py_OptimizeFlag is false then always evaluate assert conditions, don't
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23211
diff changeset
3611 return;
9e22a8702de2 If Py_OptimizeFlag is false then always evaluate assert conditions, don't
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23211
diff changeset
3612 /* Generate code like
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3613
23280
9e22a8702de2 If Py_OptimizeFlag is false then always evaluate assert conditions, don't
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23211
diff changeset
3614 if not <test>:
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3615 raise AssertionError [, <message>]
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3616
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3617 where <message> is the second test, if present.
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3618 */
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3619 com_node(c, CHILD(n, 1));
23280
9e22a8702de2 If Py_OptimizeFlag is false then always evaluate assert conditions, don't
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23211
diff changeset
3620 com_addfwref(c, JUMP_IF_TRUE, &a);
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3621 com_addbyte(c, POP_TOP);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3622 com_pop(c, 1);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3623 /* Raise that exception! */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3624 com_addop_name(c, LOAD_GLOBAL, "AssertionError");
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3625 com_push(c, 1);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3626 i = NCH(n)/2; /* Either 2 or 4 */
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3627 if (i > 1)
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3628 com_node(c, CHILD(n, 3));
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3629 com_addoparg(c, RAISE_VARARGS, i);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3630 com_pop(c, i);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3631 /* The interpreter does not fall through */
23280
9e22a8702de2 If Py_OptimizeFlag is false then always evaluate assert conditions, don't
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 23211
diff changeset
3632 /* Jump ends up here */
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3633 com_backpatch(c, a);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3634 com_addbyte(c, POP_TOP);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3635 }
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3636
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
3637 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3638 com_print_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3639 {
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3640 int i = 1;
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3641 node* stream = NULL;
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3642
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3643 REQ(n, print_stmt); /* 'print' (test ',')* [test] */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3644
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3645 /* are we using the extended print form? */
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3646 if (NCH(n) >= 2 && TYPE(CHILD(n, 1)) == RIGHTSHIFT) {
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3647 stream = CHILD(n, 2);
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3648 com_node(c, stream);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3649 /* stack: [...] => [... stream] */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3650 com_push(c, 1);
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3651 if (NCH(n) > 3 && TYPE(CHILD(n, 3)) == COMMA)
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3652 i = 4;
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3653 else
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3654 i = 3;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3655 }
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3656 for (; i < NCH(n); i += 2) {
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3657 if (stream != NULL) {
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3658 com_addbyte(c, DUP_TOP);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3659 /* stack: [stream] => [stream stream] */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3660 com_push(c, 1);
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3661 com_node(c, CHILD(n, i));
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3662 /* stack: [stream stream] => [stream stream obj] */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3663 com_addbyte(c, ROT_TWO);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3664 /* stack: [stream stream obj] => [stream obj stream] */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3665 com_addbyte(c, PRINT_ITEM_TO);
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3666 /* stack: [stream obj stream] => [stream] */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3667 com_pop(c, 2);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3668 }
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3669 else {
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3670 com_node(c, CHILD(n, i));
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3671 /* stack: [...] => [... obj] */
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3672 com_addbyte(c, PRINT_ITEM);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3673 com_pop(c, 1);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3674 }
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3675 }
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3676 /* XXX Alternatively, LOAD_CONST '\n' and then PRINT_ITEM */
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3677 if (TYPE(CHILD(n, NCH(n)-1)) == COMMA) {
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3678 if (stream != NULL) {
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3679 /* must pop the extra stream object off the stack */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3680 com_addbyte(c, POP_TOP);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3681 /* stack: [... stream] => [...] */
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3682 com_pop(c, 1);
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3683 }
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3684 }
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3685 else {
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3686 if (stream != NULL) {
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3687 /* this consumes the last stream object on stack */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3688 com_addbyte(c, PRINT_NEWLINE_TO);
14105
d809b9b75b99 com_print_stmt(): Guido rightly points out that the stream expression
Barry Warsaw <barry@python.org>
parents: 14099
diff changeset
3689 /* stack: [... stream] => [...] */
14099
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3690 com_pop(c, 1);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3691 }
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3692 else
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3693 com_addbyte(c, PRINT_NEWLINE);
468481135c49 PEP 214, Extended print Statement, has been accepted by the BDFL.
Barry Warsaw <barry@python.org>
parents: 14077
diff changeset
3694 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3695 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3696
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3697 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3698 com_return_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3699 {
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
3700 REQ(n, return_stmt); /* 'return' [testlist] */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3701 if (!c->c_infunction) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3702 com_error(c, PyExc_SyntaxError, "'return' outside function");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
3703 }
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3704 if (c->c_flags & CO_GENERATOR) {
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3705 if (NCH(n) > 1) {
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3706 com_error(c, PyExc_SyntaxError,
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3707 "'return' with argument inside generator");
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3708 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3709 }
18243
47717e5667eb Change the semantics of "return" in generators, as discussed on the
Tim Peters <tim.peters@gmail.com>
parents: 18228
diff changeset
3710 if (NCH(n) < 2) {
47717e5667eb Change the semantics of "return" in generators, as discussed on the
Tim Peters <tim.peters@gmail.com>
parents: 18228
diff changeset
3711 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
47717e5667eb Change the semantics of "return" in generators, as discussed on the
Tim Peters <tim.peters@gmail.com>
parents: 18228
diff changeset
3712 com_push(c, 1);
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3713 }
18243
47717e5667eb Change the semantics of "return" in generators, as discussed on the
Tim Peters <tim.peters@gmail.com>
parents: 18228
diff changeset
3714 else
47717e5667eb Change the semantics of "return" in generators, as discussed on the
Tim Peters <tim.peters@gmail.com>
parents: 18228
diff changeset
3715 com_node(c, CHILD(n, 1));
47717e5667eb Change the semantics of "return" in generators, as discussed on the
Tim Peters <tim.peters@gmail.com>
parents: 18228
diff changeset
3716 com_addbyte(c, RETURN_VALUE);
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3717 com_pop(c, 1);
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3718 }
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3719
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3720 static void
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3721 com_yield_stmt(struct compiling *c, node *n)
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3722 {
18228
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3723 int i;
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3724 REQ(n, yield_stmt); /* 'yield' testlist */
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3725 if (!c->c_infunction) {
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3726 com_error(c, PyExc_SyntaxError, "'yield' outside function");
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3727 }
18228
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3728
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3729 for (i = 0; i < c->c_nblocks; ++i) {
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3730 if (c->c_block[i] == SETUP_FINALLY) {
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3731 com_error(c, PyExc_SyntaxError,
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3732 "'yield' not allowed in a 'try' block "
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3733 "with a 'finally' clause");
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3734 return;
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3735 }
4ebd292c975f Disallow 'yield' in a 'try' block when there's a 'finally' clause.
Tim Peters <tim.peters@gmail.com>
parents: 18148
diff changeset
3736 }
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3737 com_node(c, CHILD(n, 1));
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
3738 com_addbyte(c, YIELD_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3739 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3740 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3741
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3742 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3743 com_raise_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3744 {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3745 int i;
7946
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
3746 REQ(n, raise_stmt); /* 'raise' [test [',' test [',' test]]] */
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
3747 if (NCH(n) > 1) {
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
3748 com_node(c, CHILD(n, 1));
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
3749 if (NCH(n) > 3) {
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
3750 com_node(c, CHILD(n, 3));
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
3751 if (NCH(n) > 5)
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
3752 com_node(c, CHILD(n, 5));
ad114620f8c2 Make first raise argument optional
Guido van Rossum <guido@python.org>
parents: 6501
diff changeset
3753 }
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
3754 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3755 i = NCH(n)/2;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3756 com_addoparg(c, RAISE_VARARGS, i);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3757 com_pop(c, i);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3758 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3759
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3760 static void
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3761 com_from_import(struct compiling *c, node *n)
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3762 {
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3763 com_addopname(c, IMPORT_FROM, CHILD(n, 0));
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3764 com_push(c, 1);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3765 if (NCH(n) > 1) {
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3766 if (strcmp(STR(CHILD(n, 1)), "as") != 0) {
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3767 com_error(c, PyExc_SyntaxError, "invalid syntax");
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3768 return;
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3769 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3770 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 2)));
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3771 } else
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3772 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3773 com_pop(c, 1);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3774 }
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3775
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3776 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3777 com_import_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3778 {
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3779 node *nn;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3780 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3781 REQ(n, import_stmt);
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3782 n = CHILD(n, 0);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3783 /* import_stmt: import_name | import_from */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3784 if (TYPE(n) == import_from) {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3785 /* 'from' dotted_name 'import' ('*' |
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3786 '(' import_as_names ')' | import_as_names) */
15438
433458651eb4 Plug a memory leak in com_import_stmt(): the tuple created to hold the
Guido van Rossum <guido@python.org>
parents: 15408
diff changeset
3787 PyObject *tup;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
3788 REQ(CHILD(n, 1), dotted_name);
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3789 nn = CHILD(n, 3 + (TYPE(CHILD(n, 3)) == LPAR));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3790 if (TYPE(nn) == STAR)
14232
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
3791 tup = Py_BuildValue("(s)", "*");
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3792 else {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3793 if (TYPE(CHILD(nn, NCH(nn) - 1)) == COMMA &&
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3794 TYPE(CHILD(n, 3)) != LPAR) {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3795 com_error(c, PyExc_SyntaxError,
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3796 "trailing comma not allowed "
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3797 "without surrounding parentheses");
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3798 return;
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3799 }
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3800 REQ(nn, import_as_names);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3801 tup = PyTuple_New((NCH(nn) + 1) / 2);
33925
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3802 for (i = 0; i < NCH(nn); i += 2) {
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3803 PyObject *s = PyString_FromString(
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3804 STR(CHILD(CHILD(nn, i), 0)));
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3805 if (s == NULL) {
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3806 Py_CLEAR(tup);
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3807 break;
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3808 } else
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3809 PyTuple_SET_ITEM(tup, i / 2, s);
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3810 }
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3811 if (tup == NULL) {
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3812 /* Assume that failue above was MemoryError */
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3813 com_error(c, PyExc_MemoryError, "");
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3814 return;
e4404046e552 SF patch 1025636: Check for NULL returns in compile.c:com_import_stmt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 33880
diff changeset
3815 }
14232
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
3816 }
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
3817 com_addoparg(c, LOAD_CONST, com_addconst(c, tup));
15438
433458651eb4 Plug a memory leak in com_import_stmt(): the tuple created to hold the
Guido van Rossum <guido@python.org>
parents: 15408
diff changeset
3818 Py_DECREF(tup);
14232
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
3819 com_push(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3820 com_addopname(c, IMPORT_NAME, CHILD(n, 1));
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3821 if (TYPE(nn) == STAR)
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3822 com_addbyte(c, IMPORT_STAR);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3823 else {
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3824 for (i = 0; i < NCH(nn); i += 2)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3825 com_from_import(c, CHILD(nn, i));
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3826 com_addbyte(c, POP_TOP);
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3827 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3828 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3829 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3830 else {
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3831 /* 'import' dotted_as_names */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3832 nn = CHILD(n, 1);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3833 REQ(nn, dotted_as_names);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3834 for (i = 0; i < NCH(nn); i += 2) {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
3835 node *subn = CHILD(nn, i);
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3836 REQ(subn, dotted_as_name);
14232
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
3837 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
1f1a156ed5af Replace the run-time 'future-bytecode-stream-inspection' hack to find out
Thomas Wouters <thomas@python.org>
parents: 14230
diff changeset
3838 com_push(c, 1);
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3839 com_addopname(c, IMPORT_NAME, CHILD(subn, 0));
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3840 if (NCH(subn) > 1) {
14230
31925ab46b23 Re-allow 'import mod.submod as s', and change its meaning to what it should
Thomas Wouters <thomas@python.org>
parents: 14226
diff changeset
3841 int j;
31925ab46b23 Re-allow 'import mod.submod as s', and change its meaning to what it should
Thomas Wouters <thomas@python.org>
parents: 14226
diff changeset
3842 if (strcmp(STR(CHILD(subn, 1)), "as") != 0) {
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3843 com_error(c, PyExc_SyntaxError,
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3844 "invalid syntax");
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3845 return;
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3846 }
14230
31925ab46b23 Re-allow 'import mod.submod as s', and change its meaning to what it should
Thomas Wouters <thomas@python.org>
parents: 14226
diff changeset
3847 for (j=2 ; j < NCH(CHILD(subn, 0)); j += 2)
31925ab46b23 Re-allow 'import mod.submod as s', and change its meaning to what it should
Thomas Wouters <thomas@python.org>
parents: 14226
diff changeset
3848 com_addopname(c, LOAD_ATTR,
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3849 CHILD(CHILD(subn, 0),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3850 j));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3851 com_addop_varname(c, VAR_STORE,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3852 STR(CHILD(subn, 2)));
14038
18385172fac0 Apply SF patch #101135, adding 'import module as m' and 'from module import
Thomas Wouters <thomas@python.org>
parents: 13997
diff changeset
3853 } else
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3854 com_addop_varname(c, VAR_STORE,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3855 STR(CHILD(CHILD(subn, 0),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3856 0)));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3857 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3858 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3859 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3860 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3861
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3862 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3863 com_exec_stmt(struct compiling *c, node *n)
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3864 {
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3865 REQ(n, exec_stmt);
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3866 /* exec_stmt: 'exec' expr ['in' expr [',' expr]] */
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3867 com_node(c, CHILD(n, 1));
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3868 if (NCH(n) >= 4)
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3869 com_node(c, CHILD(n, 3));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3870 else {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3871 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3872 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3873 }
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3874 if (NCH(n) >= 6)
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3875 com_node(c, CHILD(n, 5));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3876 else {
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3877 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3878 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3879 }
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3880 com_addbyte(c, EXEC_STMT);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
3881 com_pop(c, 3);
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3882 }
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
3883
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3884 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
3885 is_constant_false(struct compiling *c, node *n)
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3886 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3887 PyObject *v;
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3888 int i;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
3889 /* argument c will be NULL when called from symtable_node() */
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3890
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3891 /* Label to avoid tail recursion */
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3892 next:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3893 switch (TYPE(n)) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3894
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3895 case suite:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3896 if (NCH(n) == 1) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3897 n = CHILD(n, 0);
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3898 goto next;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3899 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3900 /* Fall through */
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3901 case file_input:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3902 for (i = 0; i < NCH(n); i++) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3903 node *ch = CHILD(n, i);
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3904 if (TYPE(ch) == stmt) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3905 n = ch;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3906 goto next;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3907 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3908 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3909 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3910
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3911 case stmt:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3912 case simple_stmt:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3913 case small_stmt:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3914 n = CHILD(n, 0);
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3915 goto next;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3916
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3917 case expr_stmt:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3918 case testlist:
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 23280
diff changeset
3919 case testlist1:
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3920 case test:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3921 case and_test:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3922 case not_test:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3923 case comparison:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3924 case expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3925 case xor_expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3926 case and_expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3927 case shift_expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3928 case arith_expr:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3929 case term:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3930 case factor:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3931 case power:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3932 case atom:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3933 if (NCH(n) == 1) {
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3934 n = CHILD(n, 0);
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3935 goto next;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3936 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3937 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3938
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3939 case NAME:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3940 if (Py_OptimizeFlag && strcmp(STR(n), "__debug__") == 0)
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3941 return 1;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3942 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3943
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3944 case NUMBER:
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3945 v = parsenumber(c, STR(n));
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3946 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3947 PyErr_Clear();
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3948 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3949 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3950 i = PyObject_IsTrue(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3951 Py_DECREF(v);
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3952 return i == 0;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3953
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3954 case STRING:
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
3955 v = parsestr(c, STR(n));
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3956 if (v == NULL) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3957 PyErr_Clear();
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3958 break;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3959 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3960 i = PyObject_IsTrue(v);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
3961 Py_DECREF(v);
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3962 return i == 0;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3963
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3964 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3965 return 0;
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3966 }
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
3967
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3968
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3969 /* Look under n for a return stmt with an expression.
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3970 * This hack is used to find illegal returns under "if 0:" blocks in
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3971 * functions already known to be generators (as determined by the symtable
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3972 * pass).
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3973 * Return the offending return node if found, else NULL.
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3974 */
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3975 static node *
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3976 look_for_offending_return(node *n)
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3977 {
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3978 int i;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3979
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3980 for (i = 0; i < NCH(n); ++i) {
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3981 node *kid = CHILD(n, i);
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3982
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3983 switch (TYPE(kid)) {
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3984 case classdef:
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3985 case funcdef:
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3986 case lambdef:
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3987 /* Stuff in nested functions & classes doesn't
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3988 affect the code block we started in. */
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3989 return NULL;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3990
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3991 case return_stmt:
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3992 if (NCH(kid) > 1)
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3993 return kid;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3994 break;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3995
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3996 default: {
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3997 node *bad = look_for_offending_return(kid);
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3998 if (bad != NULL)
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
3999 return bad;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4000 }
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4001 }
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4002 }
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4003
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4004 return NULL;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4005 }
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4006
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
4007 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4008 com_if_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4009 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4010 int i;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4011 int anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4012 REQ(n, if_stmt);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4013 /*'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4014 for (i = 0; i+3 < NCH(n); i+=4) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4015 int a = 0;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4016 node *ch = CHILD(n, i+1);
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4017 if (is_constant_false(c, ch)) {
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4018 /* We're going to skip this block. However, if this
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4019 is a generator, we have to check the dead code
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4020 anyway to make sure there aren't any return stmts
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4021 with expressions, in the same scope. */
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4022 if (c->c_flags & CO_GENERATOR) {
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4023 node *p = look_for_offending_return(n);
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4024 if (p != NULL) {
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4025 int savelineno = c->c_lineno;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4026 c->c_lineno = p->n_lineno;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4027 com_error(c, PyExc_SyntaxError,
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4028 "'return' with argument "
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4029 "inside generator");
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4030 c->c_lineno = savelineno;
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4031 }
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4032 }
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
4033 continue;
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
4034 }
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4035 if (i > 0)
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
4036 com_set_lineno(c, ch->n_lineno);
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
4037 com_node(c, ch);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4038 com_addfwref(c, JUMP_IF_FALSE, &a);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4039 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4040 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4041 com_node(c, CHILD(n, i+3));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4042 com_addfwref(c, JUMP_FORWARD, &anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4043 com_backpatch(c, a);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4044 /* We jump here with an extra entry which we now pop */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4045 com_addbyte(c, POP_TOP);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4046 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4047 if (i+2 < NCH(n))
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4048 com_node(c, CHILD(n, i+2));
5181
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
4049 if (anchor)
8a0b80dc6e71 Added support for ``if __debug__:'' -- if -O is given, this form is
Guido van Rossum <guido@python.org>
parents: 5164
diff changeset
4050 com_backpatch(c, anchor);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4051 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4052
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4053 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4054 com_while_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4055 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4056 int break_anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4057 int anchor = 0;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4058 int save_begin = c->c_begin;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4059 REQ(n, while_stmt); /* 'while' test ':' suite ['else' ':' suite] */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4060 com_addfwref(c, SETUP_LOOP, &break_anchor);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4061 block_push(c, SETUP_LOOP);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4062 c->c_begin = c->c_nexti;
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
4063 com_set_lineno(c, n->n_lineno);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4064 com_node(c, CHILD(n, 1));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4065 com_addfwref(c, JUMP_IF_FALSE, &anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4066 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4067 com_pop(c, 1);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4068 c->c_loops++;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4069 com_node(c, CHILD(n, 3));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4070 c->c_loops--;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4071 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4072 c->c_begin = save_begin;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4073 com_backpatch(c, anchor);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4074 /* We jump here with one entry more on the stack */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4075 com_addbyte(c, POP_TOP);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4076 com_addbyte(c, POP_BLOCK);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4077 block_pop(c, SETUP_LOOP);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4078 if (NCH(n) > 4)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4079 com_node(c, CHILD(n, 6));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4080 com_backpatch(c, break_anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4081 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4082
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4083 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4084 com_for_stmt(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4085 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4086 int break_anchor = 0;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4087 int anchor = 0;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4088 int save_begin = c->c_begin;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4089 REQ(n, for_stmt);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4090 /* 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4091 com_addfwref(c, SETUP_LOOP, &break_anchor);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4092 block_push(c, SETUP_LOOP);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4093 com_node(c, CHILD(n, 3));
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
4094 com_addbyte(c, GET_ITER);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4095 c->c_begin = c->c_nexti;
28887
e4eabe5b3da1 Use Tim's suggestion to fix
Michael W. Hudson <mwh@python.net>
parents: 28759
diff changeset
4096 com_set_lineno(c, c->c_last_line);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
4097 com_addfwref(c, FOR_ITER, &anchor);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4098 com_push(c, 1);
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
4099 com_assign(c, CHILD(n, 1), OP_ASSIGN, NULL);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4100 c->c_loops++;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4101 com_node(c, CHILD(n, 5));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4102 c->c_loops--;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4103 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4104 c->c_begin = save_begin;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4105 com_backpatch(c, anchor);
17597
32e7d0898eab Iterators phase 1. This comprises:
Guido van Rossum <guido@python.org>
parents: 17518
diff changeset
4106 com_pop(c, 1); /* FOR_ITER has popped this */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4107 com_addbyte(c, POP_BLOCK);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4108 block_pop(c, SETUP_LOOP);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4109 if (NCH(n) > 8)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4110 com_node(c, CHILD(n, 8));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4111 com_backpatch(c, break_anchor);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4112 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4113
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4114 /* Code generated for "try: S finally: Sf" is as follows:
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4115
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4116 SETUP_FINALLY L
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4117 <code for S>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4118 POP_BLOCK
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4119 LOAD_CONST <nil>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4120 L: <code for Sf>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4121 END_FINALLY
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4122
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4123 The special instructions use the block stack. Each block
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4124 stack entry contains the instruction that created it (here
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4125 SETUP_FINALLY), the level of the value stack at the time the
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4126 block stack entry was created, and a label (here L).
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4127
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4128 SETUP_FINALLY:
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4129 Pushes the current value stack level and the label
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4130 onto the block stack.
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4131 POP_BLOCK:
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4132 Pops en entry from the block stack, and pops the value
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4133 stack until its level is the same as indicated on the
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4134 block stack. (The label is ignored.)
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4135 END_FINALLY:
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4136 Pops a variable number of entries from the *value* stack
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4137 and re-raises the exception they specify. The number of
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4138 entries popped depends on the (pseudo) exception type.
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4139
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4140 The block stack is unwound when an exception is raised:
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4141 when a SETUP_FINALLY entry is found, the exception is pushed
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4142 onto the value stack (and the exception condition is cleared),
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4143 and the interpreter jumps to the label gotten from the block
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4144 stack.
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4145
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4146 Code generated for "try: S except E1, V1: S1 except E2, V2: S2 ...":
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4147 (The contents of the value stack is shown in [], with the top
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4148 at the right; 'tb' is trace-back info, 'val' the exception's
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4149 associated value, and 'exc' the exception.)
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4150
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4151 Value stack Label Instruction Argument
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4152 [] SETUP_EXCEPT L1
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4153 [] <code for S>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4154 [] POP_BLOCK
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4155 [] JUMP_FORWARD L0
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4156
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4157 [tb, val, exc] L1: DUP )
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4158 [tb, val, exc, exc] <evaluate E1> )
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4159 [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4160 [tb, val, exc, 1-or-0] JUMP_IF_FALSE L2 )
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4161 [tb, val, exc, 1] POP )
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4162 [tb, val, exc] POP
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4163 [tb, val] <assign to V1> (or POP if no V1)
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4164 [tb] POP
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4165 [] <code for S1>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4166 JUMP_FORWARD L0
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4167
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4168 [tb, val, exc, 0] L2: POP
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4169 [tb, val, exc] DUP
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4170 .............................etc.......................
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4171
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4172 [tb, val, exc, 0] Ln+1: POP
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4173 [tb, val, exc] END_FINALLY # re-raise exception
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4174
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4175 [] L0: <next statement>
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4176
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4177 Of course, parts are not generated if Vi or Ei is not present.
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4178 */
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4179
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4180 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4181 com_try_except(struct compiling *c, node *n)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4182 {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4183 int except_anchor = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4184 int end_anchor = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4185 int else_anchor = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4186 int i;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4187 node *ch;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4188
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4189 com_addfwref(c, SETUP_EXCEPT, &except_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4190 block_push(c, SETUP_EXCEPT);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4191 com_node(c, CHILD(n, 2));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4192 com_addbyte(c, POP_BLOCK);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4193 block_pop(c, SETUP_EXCEPT);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4194 com_addfwref(c, JUMP_FORWARD, &else_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4195 com_backpatch(c, except_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4196 for (i = 3;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4197 i < NCH(n) && TYPE(ch = CHILD(n, i)) == except_clause;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4198 i += 3) {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4199 /* except_clause: 'except' [expr [',' var]] */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4200 if (except_anchor == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4201 com_error(c, PyExc_SyntaxError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
4202 "default 'except:' must be last");
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4203 break;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4204 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4205 except_anchor = 0;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4206 com_push(c, 3); /* tb, val, exc pushed by exception */
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
4207 com_set_lineno(c, ch->n_lineno);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4208 if (NCH(ch) > 1) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4209 com_addbyte(c, DUP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4210 com_push(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4211 com_node(c, CHILD(ch, 1));
21609
3ba0f1b20320 Patch #494783: Rename cmp_op enumerators.
Martin v. Löwis <martin@v.loewis.de>
parents: 21398
diff changeset
4212 com_addoparg(c, COMPARE_OP, PyCmp_EXC_MATCH);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4213 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4214 com_addfwref(c, JUMP_IF_FALSE, &except_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4215 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4216 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4217 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4218 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4219 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4220 if (NCH(ch) > 3)
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
4221 com_assign(c, CHILD(ch, 3), OP_ASSIGN, NULL);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4222 else {
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4223 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4224 com_pop(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4225 }
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4226 com_addbyte(c, POP_TOP);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4227 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4228 com_node(c, CHILD(n, i+2));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4229 com_addfwref(c, JUMP_FORWARD, &end_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4230 if (except_anchor) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4231 com_backpatch(c, except_anchor);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4232 /* We come in with [tb, val, exc, 0] on the
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4233 stack; one pop and it's the same as
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4234 expected at the start of the loop */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4235 com_addbyte(c, POP_TOP);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4236 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4237 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4238 /* We actually come in here with [tb, val, exc] but the
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4239 END_FINALLY will zap those and jump around.
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4240 The c_stacklevel does not reflect them so we need not pop
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4241 anything. */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4242 com_addbyte(c, END_FINALLY);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4243 com_backpatch(c, else_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4244 if (i < NCH(n))
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4245 com_node(c, CHILD(n, i+2));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4246 com_backpatch(c, end_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4247 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4248
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4249 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4250 com_try_finally(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4251 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4252 int finally_anchor = 0;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4253 node *ch;
792
dc3b455f7632 Only * can be used for varargs argument lists
Guido van Rossum <guido@python.org>
parents: 744
diff changeset
4254
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4255 com_addfwref(c, SETUP_FINALLY, &finally_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4256 block_push(c, SETUP_FINALLY);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4257 com_node(c, CHILD(n, 2));
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4258 com_addbyte(c, POP_BLOCK);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4259 block_pop(c, SETUP_FINALLY);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4260 block_push(c, END_FINALLY);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4261 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4262 /* While the generated code pushes only one item,
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4263 the try-finally handling can enter here with
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4264 up to three items. OK, here are the details:
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4265 3 for an exception, 2 for RETURN, 1 for BREAK. */
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4266 com_push(c, 3);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4267 com_backpatch(c, finally_anchor);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4268 ch = CHILD(n, NCH(n)-1);
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
4269 com_set_lineno(c, ch->n_lineno);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4270 com_node(c, ch);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4271 com_addbyte(c, END_FINALLY);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4272 block_pop(c, END_FINALLY);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4273 com_pop(c, 3); /* Matches the com_push above */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4274 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4275
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4276 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4277 com_try_stmt(struct compiling *c, node *n)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4278 {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4279 REQ(n, try_stmt);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4280 /* 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4281 | 'try' ':' suite 'finally' ':' suite */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4282 if (TYPE(CHILD(n, 3)) != except_clause)
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4283 com_try_finally(c, n);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4284 else
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4285 com_try_except(c, n);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4286 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4287
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4288 static node *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4289 get_rawdocstring(node *n)
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4290 {
2241
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
4291 int i;
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
4292
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4293 /* Label to avoid tail recursion */
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4294 next:
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4295 switch (TYPE(n)) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4296
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4297 case suite:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4298 if (NCH(n) == 1) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4299 n = CHILD(n, 0);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4300 goto next;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4301 }
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4302 /* Fall through */
2241
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
4303 case file_input:
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
4304 for (i = 0; i < NCH(n); i++) {
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
4305 node *ch = CHILD(n, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4306 if (TYPE(ch) == stmt) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4307 n = ch;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4308 goto next;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4309 }
2241
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
4310 }
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
4311 break;
fefae37c053c added missing case to get_docstring
Guido van Rossum <guido@python.org>
parents: 2203
diff changeset
4312
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4313 case stmt:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4314 case simple_stmt:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4315 case small_stmt:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4316 n = CHILD(n, 0);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4317 goto next;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4318
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4319 case expr_stmt:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4320 case testlist:
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 23280
diff changeset
4321 case testlist1:
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4322 case test:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4323 case and_test:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4324 case not_test:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4325 case comparison:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4326 case expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4327 case xor_expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4328 case and_expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4329 case shift_expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4330 case arith_expr:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4331 case term:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4332 case factor:
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
4333 case power:
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4334 if (NCH(n) == 1) {
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4335 n = CHILD(n, 0);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4336 goto next;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4337 }
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4338 break;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4339
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4340 case atom:
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4341 if (TYPE(CHILD(n, 0)) == STRING)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4342 return n;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4343 break;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4344
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4345 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4346 return NULL;
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4347 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4348
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4349 static PyObject *
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
4350 get_docstring(struct compiling *c, node *n)
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4351 {
9987
b7bcc2cda1b3 Implement -OO; "unsafe" optimization that removes docstrings.
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
4352 /* Don't generate doc-strings if run with -OO */
b7bcc2cda1b3 Implement -OO; "unsafe" optimization that removes docstrings.
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
4353 if (Py_OptimizeFlag > 1)
b7bcc2cda1b3 Implement -OO; "unsafe" optimization that removes docstrings.
Guido van Rossum <guido@python.org>
parents: 9626
diff changeset
4354 return NULL;
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4355 n = get_rawdocstring(n);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4356 if (n == NULL)
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4357 return NULL;
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
4358 return parsestrplus(c, n);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4359 }
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4360
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4361 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4362 com_suite(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4363 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4364 REQ(n, suite);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4365 /* simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4366 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4367 com_node(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4368 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4369 else {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4370 int i;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
4371 for (i = 0; i < NCH(n) && c->c_errors == 0; i++) {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4372 node *ch = CHILD(n, i);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4373 if (TYPE(ch) == stmt)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4374 com_node(c, ch);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4375 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4376 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4377 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4378
744
834dd4a77ee2 fix what lint found
Guido van Rossum <guido@python.org>
parents: 703
diff changeset
4379 /* ARGSUSED */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4380 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4381 com_continue_stmt(struct compiling *c, node *n)
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4382 {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4383 int i = c->c_nblocks;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4384 if (i-- > 0 && c->c_block[i] == SETUP_LOOP) {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4385 com_addoparg(c, JUMP_ABSOLUTE, c->c_begin);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4386 }
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4387 else if (i <= 0) {
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4388 /* at the outer level */
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4389 com_error(c, PyExc_SyntaxError,
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4390 "'continue' not properly in loop");
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4391 }
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4392 else {
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4393 int j;
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4394 for (j = i-1; j >= 0; --j) {
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4395 if (c->c_block[j] == SETUP_LOOP)
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4396 break;
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4397 }
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4398 if (j >= 0) {
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4399 /* there is a loop, but something interferes */
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4400 for (; i > j; --i) {
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4401 if (c->c_block[i] == SETUP_EXCEPT ||
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4402 c->c_block[i] == SETUP_FINALLY) {
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4403 com_addoparg(c, CONTINUE_LOOP,
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4404 c->c_begin);
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4405 return;
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4406 }
16398
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4407 if (c->c_block[i] == END_FINALLY) {
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4408 com_error(c, PyExc_SyntaxError,
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4409 "'continue' not supported inside 'finally' clause");
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4410 return;
80c0b280a4f9 Allow 'continue' inside 'try' clause
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16394
diff changeset
4411 }
14471
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4412 }
c3c49859e80b com_continue_stmt(): Improve error message when continue is found
Fred Drake <fdrake@acm.org>
parents: 14382
diff changeset
4413 }
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4414 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4415 "'continue' not properly in loop");
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4416 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4417 /* XXX Could allow it inside a 'finally' clause
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4418 XXX if we could pop the exception still on the stack */
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4419 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4420
30226
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4421 /* Return the number of default values in the argument list.
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4422
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4423 If a non-default argument follows a default argument, set an
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4424 exception and return -1.
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4425 */
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4426
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4427 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4428 com_argdefs(struct compiling *c, node *n)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4429 {
30226
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4430 int i, nch, ndefs;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4431 if (TYPE(n) == lambdef) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4432 /* lambdef: 'lambda' [varargslist] ':' test */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4433 n = CHILD(n, 1);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4434 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4435 else {
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4436 REQ(n, funcdef);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4437 /* funcdef: [decorators] 'def' NAME parameters ':' suite */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4438 n = RCHILD(n, -3);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4439 REQ(n, parameters); /* parameters: '(' [varargslist] ')' */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4440 n = CHILD(n, 1);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4441 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4442 if (TYPE(n) != varargslist)
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4443 return 0;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4444 /* varargslist:
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4445 (fpdef ['=' test] ',')* '*' ....... |
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4446 fpdef ['=' test] (',' fpdef ['=' test])* [','] */
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4447 nch = NCH(n);
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4448 ndefs = 0;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4449 for (i = 0; i < nch; i++) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4450 int t;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4451 if (TYPE(CHILD(n, i)) == STAR ||
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4452 TYPE(CHILD(n, i)) == DOUBLESTAR)
2813
999b5bdc18ce 3rd arg for raise; INCOMPLETE keyword parameter passing (currently f(kw=value) is seen as f('kw', value))
Guido van Rossum <guido@python.org>
parents: 2367
diff changeset
4453 break;
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4454 i++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4455 if (i >= nch)
3103
e51e17b2d88b fix bug with missing default for last arg (discovered by Tommy Burnette)
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
4456 t = RPAR; /* Anything except EQUAL or COMMA */
e51e17b2d88b fix bug with missing default for last arg (discovered by Tommy Burnette)
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
4457 else
e51e17b2d88b fix bug with missing default for last arg (discovered by Tommy Burnette)
Guido van Rossum <guido@python.org>
parents: 2844
diff changeset
4458 t = TYPE(CHILD(n, i));
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4459 if (t == EQUAL) {
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4460 i++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4461 ndefs++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4462 com_node(c, CHILD(n, i));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4463 i++;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4464 if (i >= nch)
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4465 break;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4466 t = TYPE(CHILD(n, i));
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4467 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4468 else {
9214
c259c3c4d0cc Treat def f(a, b=1, c): ... as an error (missing default for c)
Guido van Rossum <guido@python.org>
parents: 9049
diff changeset
4469 /* Treat "(a=1, b)" as an error */
30226
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4470 if (ndefs) {
9214
c259c3c4d0cc Treat def f(a, b=1, c): ... as an error (missing default for c)
Guido van Rossum <guido@python.org>
parents: 9049
diff changeset
4471 com_error(c, PyExc_SyntaxError,
9215
1995cdbe3384 Eh, better error message for the previous change. It now says
Guido van Rossum <guido@python.org>
parents: 9214
diff changeset
4472 "non-default argument follows default argument");
30226
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4473 return -1;
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4474 }
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4475 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4476 if (t != COMMA)
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4477 break;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4478 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4479 return ndefs;
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4480 }
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4481
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4482 static void
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4483 com_decorator_name(struct compiling *c, node *n)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4484 {
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4485 /* dotted_name: NAME ('.' NAME)* */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4486
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4487 int i, nch;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4488 node *varname;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4489
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4490 REQ(n, dotted_name);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4491 nch = NCH(n);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4492 assert(nch >= 1 && nch % 2 == 1);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4493
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4494 varname = CHILD(n, 0);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4495 REQ(varname, NAME);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4496 com_addop_varname(c, VAR_LOAD, STR(varname));
32892
685a948b3bc9 fix for @decorators under a debug build.
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32888
diff changeset
4497 com_push(c, 1);
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4498
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4499 for (i = 1; i < nch; i += 2) {
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4500 node *attrname;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4501
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4502 REQ(CHILD(n, i), DOT);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4503
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4504 attrname = CHILD(n, i + 1);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4505 REQ(attrname, NAME);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4506 com_addop_name(c, LOAD_ATTR, STR(attrname));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4507 }
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4508 }
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4509
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4510 static void
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4511 com_decorator(struct compiling *c, node *n)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4512 {
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4513 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE */
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4514 int nch = NCH(n);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4515 assert(nch >= 3);
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4516 REQ(CHILD(n, 0), AT);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4517 REQ(RCHILD(n, -1), NEWLINE);
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4518 com_decorator_name(c, CHILD(n, 1));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4519
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4520 if (nch > 3) {
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4521 assert(nch == 5 || nch == 6);
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4522 REQ(CHILD(n, 2), LPAR);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4523 REQ(RCHILD(n, -2), RPAR);
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4524 com_call_function(c, CHILD(n, 3));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4525 }
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4526 }
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4527
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4528 static int
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4529 com_decorators(struct compiling *c, node *n)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4530 {
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4531 int i, nch;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4532
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4533 /* decorator+ */
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4534 nch = NCH(n);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4535 assert(nch >= 1);
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4536
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4537 for (i = 0; i < nch; ++i) {
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4538 node *ch = CHILD(n, i);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4539 REQ(ch, decorator);
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4540
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4541 com_decorator(c, ch);
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4542 }
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4543
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4544 return nch;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4545 }
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4546
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4547 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4548 com_funcdef(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4549 {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4550 PyObject *co;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4551 int ndefs, ndecorators;
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4552
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4553 REQ(n, funcdef);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4554 /* -6 -5 -4 -3 -2 -1
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4555 funcdef: [decorators] 'def' NAME parameters ':' suite */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4556
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4557 if (NCH(n) == 6)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4558 ndecorators = com_decorators(c, CHILD(n, 0));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4559 else
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4560 ndecorators = 0;
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4561
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4562 ndefs = com_argdefs(c, n);
30226
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4563 if (ndefs < 0)
806179400f35 Fix for SF bug [ 784075 ] Fatal Python error: unknown scope
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29869
diff changeset
4564 return;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4565 symtable_enter_scope(c->c_symtable, STR(RCHILD(n, -4)), TYPE(n),
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
4566 n->n_lineno);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4567 co = (PyObject *)icompile(n, c);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4568 symtable_exit_scope(c->c_symtable);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4569 if (co == NULL)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4570 c->c_errors++;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4571 else {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4572 int closure = com_make_closure(c, (PyCodeObject *)co);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4573 int i = com_addconst(c, co);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4574 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4575 com_push(c, 1);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4576 if (closure)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4577 com_addoparg(c, MAKE_CLOSURE, ndefs);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4578 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4579 com_addoparg(c, MAKE_FUNCTION, ndefs);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4580 com_pop(c, ndefs);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4581
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4582 while (ndecorators > 0) {
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4583 com_addoparg(c, CALL_FUNCTION, 1);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4584 com_pop(c, 1);
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4585 --ndecorators;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4586 }
33166
2e8d2bdd33de This is Mark Russell's patch:
Michael W. Hudson <mwh@python.net>
parents: 33133
diff changeset
4587
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4588 com_addop_varname(c, VAR_STORE, STR(RCHILD(n, -4)));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4589 com_pop(c, 1);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4590 Py_DECREF(co);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4591 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4592 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4593
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4594 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4595 com_bases(struct compiling *c, node *n)
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4596 {
744
834dd4a77ee2 fix what lint found
Guido van Rossum <guido@python.org>
parents: 703
diff changeset
4597 int i;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4598 REQ(n, testlist);
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4599 /* testlist: test (',' test)* [','] */
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4600 for (i = 0; i < NCH(n); i += 2)
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4601 com_node(c, CHILD(n, i));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4602 i = (NCH(n)+1) / 2;
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4603 com_addoparg(c, BUILD_TUPLE, i);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4604 com_pop(c, i-1);
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4605 }
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4606
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4607 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4608 com_classdef(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4609 {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4610 int i;
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
4611 PyObject *v;
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
4612 PyCodeObject *co;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4613 char *name;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4614
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4615 REQ(n, classdef);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4616 /* classdef: class NAME ['(' testlist ')'] ':' suite */
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
4617 if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4618 c->c_errors++;
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4619 return;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4620 }
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4621 /* Push the class name on the stack */
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4622 i = com_addconst(c, v);
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4623 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4624 com_push(c, 1);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4625 Py_DECREF(v);
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4626 /* Push the tuple of base classes on the stack */
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4627 if (TYPE(CHILD(n, 2)) != LPAR) {
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4628 com_addoparg(c, BUILD_TUPLE, 0);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4629 com_push(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4630 }
1288
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4631 else
9fed5726a36d Several changes in one:
Guido van Rossum <guido@python.org>
parents: 1262
diff changeset
4632 com_bases(c, CHILD(n, 3));
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4633 name = STR(CHILD(n, 1));
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
4634 symtable_enter_scope(c->c_symtable, name, TYPE(n), n->n_lineno);
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
4635 co = icompile(n, c);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4636 symtable_exit_scope(c->c_symtable);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4637 if (co == NULL)
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4638 c->c_errors++;
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4639 else {
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
4640 int closure = com_make_closure(c, co);
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
4641 i = com_addconst(c, (PyObject *)co);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4642 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4643 com_push(c, 1);
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
4644 if (closure) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4645 com_addoparg(c, MAKE_CLOSURE, 0);
21398
de125792bfaa Fix for SF bug [ #492403 ] exec() segfaults on closure's func_code
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21179
diff changeset
4646 com_pop(c, PyCode_GetNumFree(co));
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
4647 } else
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4648 com_addoparg(c, MAKE_FUNCTION, 0);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4649 com_addoparg(c, CALL_FUNCTION, 0);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4650 com_addbyte(c, BUILD_CLASS);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4651 com_pop(c, 2);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4652 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 1)));
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
4653 com_pop(c, 1);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
4654 Py_DECREF(co);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
4655 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4656 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4657
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4658 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4659 com_node(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4660 {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4661 loop:
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
4662 if (c->c_errors)
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
4663 return;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4664 switch (TYPE(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4665
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4666 /* Definition nodes */
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4667
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4668 case funcdef:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4669 com_funcdef(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4670 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4671 case classdef:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4672 com_classdef(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4673 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4674
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4675 /* Trivial parse tree nodes */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4676
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4677 case stmt:
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4678 case small_stmt:
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4679 case flow_stmt:
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4680 n = CHILD(n, 0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4681 goto loop;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4682
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4683 case simple_stmt:
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4684 /* small_stmt (';' small_stmt)* [';'] NEWLINE */
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
4685 com_set_lineno(c, n->n_lineno);
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4686 {
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4687 int i;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4688 for (i = 0; i < NCH(n)-1; i += 2)
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4689 com_node(c, CHILD(n, i));
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4690 }
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4691 break;
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4692
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4693 case compound_stmt:
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
4694 com_set_lineno(c, n->n_lineno);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4695 n = CHILD(n, 0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4696 goto loop;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4697
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4698 /* Statement nodes */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4699
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4700 case expr_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4701 com_expr_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4702 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4703 case print_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4704 com_print_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4705 break;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4706 case del_stmt: /* 'del' exprlist */
14163
1b9fd0cbd914 Support for three-token characters (**=, >>=, <<=) which was written by
Thomas Wouters <thomas@python.org>
parents: 14147
diff changeset
4707 com_assign(c, CHILD(n, 1), OP_DELETE, NULL);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4708 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4709 case pass_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4710 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4711 case break_stmt:
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4712 if (c->c_loops == 0) {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4713 com_error(c, PyExc_SyntaxError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4714 "'break' outside loop");
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4715 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4716 com_addbyte(c, BREAK_LOOP);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4717 break;
317
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4718 case continue_stmt:
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4719 com_continue_stmt(c, n);
325b40d77235 New syntax: semicolons, continue statement.
Guido van Rossum <guido@python.org>
parents: 303
diff changeset
4720 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4721 case return_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4722 com_return_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4723 break;
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
4724 case yield_stmt:
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
4725 com_yield_stmt(c, n);
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
4726 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4727 case raise_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4728 com_raise_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4729 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4730 case import_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4731 com_import_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4732 break;
466
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4733 case global_stmt:
90cbcb0b97dc Implement 'global' and new class syntax.
Guido van Rossum <guido@python.org>
parents: 409
diff changeset
4734 break;
1354
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
4735 case exec_stmt:
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
4736 com_exec_stmt(c, n);
fccd415e2eb8 * bltinmodule.c: removed exec() built-in function.
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
4737 break;
5233
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
4738 case assert_stmt:
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
4739 com_assert_stmt(c, n);
b749d2dd7582 Added assert statement.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
4740 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4741 case if_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4742 com_if_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4743 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4744 case while_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4745 com_while_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4746 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4747 case for_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4748 com_for_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4749 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4750 case try_stmt:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4751 com_try_stmt(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4752 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4753 case suite:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4754 com_suite(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4755 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4756
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4757 /* Expression nodes */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4758
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4759 case testlist:
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 23280
diff changeset
4760 case testlist1:
20432
56141b86a7f2 Very subtle syntax change: in a list comprehension, the testlist in
Guido van Rossum <guido@python.org>
parents: 20012
diff changeset
4761 case testlist_safe:
482
ee899a81570d New argument passing.
Guido van Rossum <guido@python.org>
parents: 466
diff changeset
4762 com_list(c, n, 0);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4763 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4764 case test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4765 com_test(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4766 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4767 case and_test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4768 com_and_test(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4769 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4770 case not_test:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4771 com_not_test(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4772 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4773 case comparison:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4774 com_comparison(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4775 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4776 case exprlist:
482
ee899a81570d New argument passing.
Guido van Rossum <guido@python.org>
parents: 466
diff changeset
4777 com_list(c, n, 0);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4778 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4779 case expr:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4780 com_expr(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4781 break;
409
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4782 case xor_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4783 com_xor_expr(c, n);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4784 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4785 case and_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4786 com_and_expr(c, n);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4787 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4788 case shift_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4789 com_shift_expr(c, n);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4790 break;
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4791 case arith_expr:
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4792 com_arith_expr(c, n);
b5fe6482072f Added shift and mask ops.
Guido van Rossum <guido@python.org>
parents: 388
diff changeset
4793 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4794 case term:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4795 com_term(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4796 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4797 case factor:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4798 com_factor(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4799 break;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
4800 case power:
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
4801 com_power(c, n);
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
4802 break;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4803 case atom:
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4804 com_atom(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4805 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4806
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4807 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4808 com_error(c, PyExc_SystemError,
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4809 "com_node: unexpected node type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4810 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4811 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4812
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13330
diff changeset
4813 static void com_fplist(struct compiling *, node *);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4814
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4815 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4816 com_fpdef(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4817 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4818 REQ(n, fpdef); /* fpdef: NAME | '(' fplist ')' */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4819 if (TYPE(CHILD(n, 0)) == LPAR)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4820 com_fplist(c, CHILD(n, 1));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4821 else {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4822 com_addop_varname(c, VAR_STORE, STR(CHILD(n, 0)));
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4823 com_pop(c, 1);
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4824 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4825 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4826
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4827 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4828 com_fplist(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4829 {
544
d0baa8300005 Minor changes.
Guido van Rossum <guido@python.org>
parents: 493
diff changeset
4830 REQ(n, fplist); /* fplist: fpdef (',' fpdef)* [','] */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4831 if (NCH(n) == 1) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4832 com_fpdef(c, CHILD(n, 0));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4833 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4834 else {
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4835 int i = (NCH(n)+1)/2;
13930
4eaa463c031a Merge UNPACK_LIST and UNPACK_TUPLE into a single UNPACK_SEQUENCE, since they
Thomas Wouters <thomas@python.org>
parents: 13884
diff changeset
4836 com_addoparg(c, UNPACK_SEQUENCE, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4837 com_push(c, i-1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4838 for (i = 0; i < NCH(n); i += 2)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4839 com_fpdef(c, CHILD(n, i));
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4840 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4841 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4842
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4843 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4844 com_arglist(struct compiling *c, node *n)
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4845 {
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4846 int nch, i, narg;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4847 int complex = 0;
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4848 char nbuf[30];
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4849 REQ(n, varargslist);
617
755bb8efcc2d Use strtoul() for oct/hex constants.
Guido van Rossum <guido@python.org>
parents: 584
diff changeset
4850 /* varargslist:
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4851 (fpdef ['=' test] ',')* (fpdef ['=' test] | '*' .....) */
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4852 nch = NCH(n);
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4853 /* Enter all arguments in table of locals */
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4854 for (i = 0, narg = 0; i < nch; i++) {
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4855 node *ch = CHILD(n, i);
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4856 node *fp;
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4857 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4858 break;
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4859 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4860 fp = CHILD(ch, 0);
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4861 if (TYPE(fp) != NAME) {
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4862 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
3766
516490c8192c Py_complex; and WITHOUT_COMPLEX added to getargs.c
Guido van Rossum <guido@python.org>
parents: 3366
diff changeset
4863 complex = 1;
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4864 }
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4865 narg++;
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4866 /* all name updates handled by symtable */
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4867 if (++i >= nch)
26754
8c3ef524faa0 Add warning for assignment to None, True and False. This is patch
Guido van Rossum <guido@python.org>
parents: 26701
diff changeset
4868 break;
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4869 ch = CHILD(n, i);
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4870 if (TYPE(ch) == EQUAL)
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4871 i += 2;
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4872 else
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4873 REQ(ch, COMMA);
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4874 }
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4875 if (complex) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4876 /* Generate code for complex arguments only after
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4877 having counted the simple arguments */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4878 int ilocal = 0;
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4879 for (i = 0; i < nch; i++) {
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4880 node *ch = CHILD(n, i);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4881 node *fp;
3324
36920bbd81a8 changes for complex and power (**) operator
Guido van Rossum <guido@python.org>
parents: 3166
diff changeset
4882 if (TYPE(ch) == STAR || TYPE(ch) == DOUBLESTAR)
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
4883 break;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4884 REQ(ch, fpdef); /* fpdef: NAME | '(' fplist ')' */
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4885 fp = CHILD(ch, 0);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4886 if (TYPE(fp) != NAME) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4887 com_addoparg(c, LOAD_FAST, ilocal);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4888 com_push(c, 1);
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4889 com_fpdef(c, ch);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4890 }
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4891 ilocal++;
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
4892 if (++i >= nch)
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4893 break;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4894 ch = CHILD(n, i);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4895 if (TYPE(ch) == EQUAL)
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4896 i += 2;
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4897 else
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4898 REQ(ch, COMMA);
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4899 }
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4900 }
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4901 }
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4902
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4903 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4904 com_file_input(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4905 {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4906 int i;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4907 PyObject *doc;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4908 REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
4909 doc = get_docstring(c, n);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4910 if (doc != NULL) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4911 int i = com_addconst(c, doc);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4912 Py_DECREF(doc);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4913 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4914 com_push(c, 1);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4915 com_addop_name(c, STORE_NAME, "__doc__");
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4916 com_pop(c, 1);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4917 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4918 for (i = 0; i < NCH(n); i++) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4919 node *ch = CHILD(n, i);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4920 if (TYPE(ch) != ENDMARKER && TYPE(ch) != NEWLINE)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4921 com_node(c, ch);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4922 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4923 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4924
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4925 /* Top-level compile-node interface */
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4926
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4927 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4928 compile_funcdef(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4929 {
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4930 PyObject *doc;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4931 node *ch;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4932 REQ(n, funcdef);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4933 /* -6 -5 -4 -3 -2 -1
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4934 funcdef: [decorators] 'def' NAME parameters ':' suite */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4935 c->c_name = STR(RCHILD(n, -4));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4936 doc = get_docstring(c, RCHILD(n, -1));
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4937 if (doc != NULL) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4938 (void) com_addconst(c, doc);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4939 Py_DECREF(doc);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4940 }
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4941 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4942 (void) com_addconst(c, Py_None); /* No docstring */
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4943 ch = RCHILD(n, -3); /* parameters: '(' [varargslist] ')' */
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4944 ch = CHILD(ch, 1); /* ')' | varargslist */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4945 if (TYPE(ch) == varargslist)
584
37aa67f0867c Added varargs code.
Guido van Rossum <guido@python.org>
parents: 568
diff changeset
4946 com_arglist(c, ch);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4947 c->c_infunction = 1;
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
4948 com_node(c, RCHILD(n, -1));
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
4949 c->c_infunction = 0;
31764
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
4950 com_strip_lnotab(c);
25078
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
4951 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
4952 com_push(c, 1);
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
4953 com_addbyte(c, RETURN_VALUE);
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
4954 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4955 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4956
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4957 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4958 compile_lambdef(struct compiling *c, node *n)
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
4959 {
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
4960 node *ch;
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4961 REQ(n, lambdef); /* lambdef: 'lambda' [varargslist] ':' test */
2005
ed5d0ad5ea2f set name to <lambda> instead of None for lambdas
Guido van Rossum <guido@python.org>
parents: 1833
diff changeset
4962 c->c_name = "<lambda>";
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
4963
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
4964 ch = CHILD(n, 1);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4965 (void) com_addconst(c, Py_None); /* No docstring */
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4966 if (TYPE(ch) == varargslist) {
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4967 com_arglist(c, ch);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4968 ch = CHILD(n, 3);
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
4969 }
2844
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4970 else
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4971 ch = CHILD(n, 2);
0261bf5b3819 keyword arguments and faster calls
Guido van Rossum <guido@python.org>
parents: 2813
diff changeset
4972 com_node(c, ch);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
4973 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4974 com_pop(c, 1);
1369
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
4975 }
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
4976
89e1e5d9ccbf * compile.[ch]: support for lambda()
Guido van Rossum <guido@python.org>
parents: 1364
diff changeset
4977 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
4978 compile_classdef(struct compiling *c, node *n)
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4979 {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4980 node *ch;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4981 PyObject *doc;
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4982 REQ(n, classdef);
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4983 /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4984 c->c_name = STR(CHILD(n, 1));
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
4985 c->c_private = c->c_name;
21725
5d2bfbd47f1b A tentative fix for SF bug #503837 (Roeland Rengelink):
Guido van Rossum <guido@python.org>
parents: 21609
diff changeset
4986 /* Initialize local __module__ from global __name__ */
5d2bfbd47f1b A tentative fix for SF bug #503837 (Roeland Rengelink):
Guido van Rossum <guido@python.org>
parents: 21609
diff changeset
4987 com_addop_name(c, LOAD_GLOBAL, "__name__");
5d2bfbd47f1b A tentative fix for SF bug #503837 (Roeland Rengelink):
Guido van Rossum <guido@python.org>
parents: 21609
diff changeset
4988 com_addop_name(c, STORE_NAME, "__module__");
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4989 ch = CHILD(n, NCH(n)-1); /* The suite */
19343
7257e87e0720 Patch #445762: Support --disable-unicode
Martin v. Löwis <martin@v.loewis.de>
parents: 19252
diff changeset
4990 doc = get_docstring(c, ch);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4991 if (doc != NULL) {
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4992 int i = com_addconst(c, doc);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
4993 Py_DECREF(doc);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4994 com_addoparg(c, LOAD_CONST, i);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4995 com_push(c, 1);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
4996 com_addop_name(c, STORE_NAME, "__doc__");
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
4997 com_pop(c, 1);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4998 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
4999 else
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5000 (void) com_addconst(c, Py_None);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
5001 com_node(c, ch);
31764
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
5002 com_strip_lnotab(c);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
5003 com_addbyte(c, LOAD_LOCALS);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
5004 com_push(c, 1);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
5005 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
5006 com_pop(c, 1);
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
5007 }
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
5008
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
5009 static void
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5010 compile_generator_expression(struct compiling *c, node *n)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5011 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5012 /* testlist_gexp: test gen_for */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5013 /* argument: test gen_for */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5014 REQ(CHILD(n, 0), test);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5015 REQ(CHILD(n, 1), gen_for);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5016
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5017 c->c_name = "<generator expression>";
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5018 com_gen_for(c, CHILD(n, 1), CHILD(n, 0), 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5019
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5020 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5021 com_push(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5022 com_addbyte(c, RETURN_VALUE);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5023 com_pop(c, 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5024 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5025
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5026 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
5027 compile_node(struct compiling *c, node *n)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5028 {
24844
cf5928fab108 This is my patch
Michael W. Hudson <mwh@python.net>
parents: 24818
diff changeset
5029 com_set_lineno(c, n->n_lineno);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 73
diff changeset
5030
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5031 switch (TYPE(n)) {
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5032
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5033 case single_input: /* One interactive command */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5034 /* NEWLINE | simple_stmt | compound_stmt NEWLINE */
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
5035 c->c_interactive++;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5036 n = CHILD(n, 0);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5037 if (TYPE(n) != NEWLINE)
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5038 com_node(c, n);
31764
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
5039 com_strip_lnotab(c);
25078
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
5040 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
5041 com_push(c, 1);
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
5042 com_addbyte(c, RETURN_VALUE);
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
5043 com_pop(c, 1);
1833
39a4c15acb32 * Python/compile.c (com_argdefs, com_arglist): avoid referencing
Guido van Rossum <guido@python.org>
parents: 1417
diff changeset
5044 c->c_interactive--;
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5045 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5046
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5047 case file_input: /* A whole file, or built-in function exec() */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5048 com_file_input(c, n);
31764
5e916d46a3fb Fix for line events in the case:
Armin Rigo <arigo@tunes.org>
parents: 31728
diff changeset
5049 com_strip_lnotab(c);
25078
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
5050 com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None));
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
5051 com_push(c, 1);
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
5052 com_addbyte(c, RETURN_VALUE);
be2868a63ed5 Further SET_LINENO reomval fixes. See comments in patch #587933.
Michael W. Hudson <mwh@python.net>
parents: 24858
diff changeset
5053 com_pop(c, 1);
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5054 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5055
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
5056 case eval_input: /* Built-in function input() */
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5057 com_node(c, CHILD(n, 0));
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5058 com_addbyte(c, RETURN_VALUE);
4991
8276916e1ea8 Add co_stacksize field to codeobject structure, and stacksize argument
Guido van Rossum <guido@python.org>
parents: 4905
diff changeset
5059 com_pop(c, 1);
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5060 break;
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5061
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
5062 case lambdef: /* anonymous function definition */
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
5063 compile_lambdef(c, n);
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
5064 break;
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
5065
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5066 case funcdef: /* A function definition */
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5067 compile_funcdef(c, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5068 break;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5069
120
c9a5fe1660f5 Fix bug in input(); add comments to cases in compile().
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5070 case classdef: /* A class definition */
2089
53aa51433f7f Two independent chages:
Guido van Rossum <guido@python.org>
parents: 2062
diff changeset
5071 compile_classdef(c, n);
73
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
5072 break;
3028acaa56b0 Compile class definitions.
Guido van Rossum <guido@python.org>
parents: 69
diff changeset
5073
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5074 case testlist_gexp: /* A generator expression */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5075 case argument: /* A generator expression */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5076 compile_generator_expression(c, n);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5077 break;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
5078
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5079 default:
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5080 com_error(c, PyExc_SystemError,
4315
46d736fb9da3 Added line number to most compile-time error messages.
Guido van Rossum <guido@python.org>
parents: 4131
diff changeset
5081 "compile_node: unexpected node type");
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5082 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5083 }
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5084
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5085 static PyObject *
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5086 dict_keys_inorder(PyObject *dict, int offset)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5087 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5088 PyObject *tuple, *k, *v;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5089 int i, pos = 0, size = PyDict_Size(dict);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5090
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5091 tuple = PyTuple_New(size);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5092 if (tuple == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5093 return NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5094 while (PyDict_Next(dict, &pos, &k, &v)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5095 i = PyInt_AS_LONG(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5096 Py_INCREF(k);
17078
7e181eba6b9b Fixup handling of free variables in methods when the class scope also
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17076
diff changeset
5097 assert((i - offset) < size);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5098 PyTuple_SET_ITEM(tuple, i - offset, k);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5099 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5100 return tuple;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5101 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5102
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5103 PyCodeObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
5104 PyNode_Compile(node *n, const char *filename)
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5105 {
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5106 return PyNode_CompileFlags(n, filename, NULL);
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5107 }
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5108
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5109 PyCodeObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
5110 PyNode_CompileFlags(node *n, const char *filename, PyCompilerFlags *flags)
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5111 {
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5112 return jcompile(n, filename, NULL, flags);
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
5113 }
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
5114
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5115 struct symtable *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
5116 PyNode_CompileSymtable(node *n, const char *filename)
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5117 {
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5118 struct symtable *st;
17112
76eab2ebce58 Update PyNode_CompileSymtable() to understand future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17104
diff changeset
5119 PyFutureFeatures *ff;
76eab2ebce58 Update PyNode_CompileSymtable() to understand future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17104
diff changeset
5120
76eab2ebce58 Update PyNode_CompileSymtable() to understand future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17104
diff changeset
5121 ff = PyNode_Future(n, filename);
76eab2ebce58 Update PyNode_CompileSymtable() to understand future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17104
diff changeset
5122 if (ff == NULL)
76eab2ebce58 Update PyNode_CompileSymtable() to understand future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17104
diff changeset
5123 return NULL;
29869
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5124 st = symtable_build(n, ff, filename);
20867
40a88989d6cb Part of SF bug #478003 possible memory leaks in err handling.
Tim Peters <tim.peters@gmail.com>
parents: 20517
diff changeset
5125 if (st == NULL) {
24529
3444005b6527 SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 24373
diff changeset
5126 PyObject_FREE((void *)ff);
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5127 return NULL;
20867
40a88989d6cb Part of SF bug #478003 possible memory leaks in err handling.
Tim Peters <tim.peters@gmail.com>
parents: 20517
diff changeset
5128 }
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5129 return st;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5130 }
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5131
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5132 static PyCodeObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
5133 icompile(node *n, struct compiling *base)
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
5134 {
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5135 return jcompile(n, base->c_filename, base, NULL);
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
5136 }
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
5137
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5138 static PyCodeObject *
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
5139 jcompile(node *n, const char *filename, struct compiling *base,
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5140 PyCompilerFlags *flags)
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
5141 {
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5142 struct compiling sc;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5143 PyCodeObject *co;
1415
06570429f062 * import.c (get_module): pass .py filename to parse_file, not .pyc filename!
Guido van Rossum <guido@python.org>
parents: 1405
diff changeset
5144 if (!com_init(&sc, filename))
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5145 return NULL;
27781
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27712
diff changeset
5146 if (flags && flags->cf_flags & PyCF_SOURCE_IS_UTF8) {
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27712
diff changeset
5147 sc.c_encoding = "utf-8";
cc01fb654ce3 patch #683515: "Add unicode support to compile(), eval() and exec"
Just van Rossum <just@letterror.com>
parents: 27712
diff changeset
5148 } else if (TYPE(n) == encoding_decl) {
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5149 sc.c_encoding = STR(n);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5150 n = CHILD(n, 0);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5151 } else {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5152 sc.c_encoding = NULL;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5153 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5154 if (base) {
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
5155 sc.c_private = base->c_private;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5156 sc.c_symtable = base->c_symtable;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5157 /* c_symtable still points to parent's symbols */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5158 if (base->c_nested
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5159 || (sc.c_symtable->st_cur->ste_type == TYPE_FUNCTION))
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5160 sc.c_nested = 1;
20491
149a65fa427c Fix computation of stack depth for classdef and closures.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20432
diff changeset
5161 sc.c_flags |= base->c_flags & PyCF_MASK;
24539
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5162 if (base->c_encoding != NULL) {
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5163 assert(sc.c_encoding == NULL);
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5164 sc.c_encoding = base->c_encoding;
06f1e2fd05bc Patch #534304: Implement phase 1 of PEP 263.
Martin v. Löwis <martin@v.loewis.de>
parents: 24529
diff changeset
5165 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5166 } else {
4129
eedaa59341b9 Name mangling, what the heck!
Guido van Rossum <guido@python.org>
parents: 4003
diff changeset
5167 sc.c_private = NULL;
16799
08d937107fe6 Improved __future__ parser; still more to do
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16783
diff changeset
5168 sc.c_future = PyNode_Future(n, filename);
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5169 if (sc.c_future == NULL) {
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5170 com_free(&sc);
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5171 return NULL;
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
5172 }
19252
fbf1ae769306 Fix SF bug [ #450909 ] __future__.division fails at prompt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19187
diff changeset
5173 if (flags) {
fbf1ae769306 Fix SF bug [ #450909 ] __future__.division fails at prompt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19187
diff changeset
5174 int merged = sc.c_future->ff_features |
fbf1ae769306 Fix SF bug [ #450909 ] __future__.division fails at prompt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19187
diff changeset
5175 flags->cf_flags;
fbf1ae769306 Fix SF bug [ #450909 ] __future__.division fails at prompt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19187
diff changeset
5176 sc.c_future->ff_features = merged;
fbf1ae769306 Fix SF bug [ #450909 ] __future__.division fails at prompt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19187
diff changeset
5177 flags->cf_flags = merged;
fbf1ae769306 Fix SF bug [ #450909 ] __future__.division fails at prompt
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19187
diff changeset
5178 }
29869
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5179 sc.c_symtable = symtable_build(n, sc.c_future, sc.c_filename);
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5180 if (sc.c_symtable == NULL) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5181 com_free(&sc);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5182 return NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5183 }
29869
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5184 /* reset symbol table for second pass */
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5185 sc.c_symtable->st_nscopes = 1;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5186 sc.c_symtable->st_pass = 2;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5187 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5188 co = NULL;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5189 if (symtable_load_symbols(&sc) < 0) {
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5190 sc.c_errors++;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5191 goto exit;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5192 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5193 compile_node(&sc, n);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5194 com_done(&sc);
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1029
diff changeset
5195 if (sc.c_errors == 0) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5196 PyObject *consts, *names, *varnames, *filename, *name,
33169
5778f6331334 Move the bytecode optimizer upstream so that its results are saved in pyc
Raymond Hettinger <python@rcn.com>
parents: 33166
diff changeset
5197 *freevars, *cellvars, *code;
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5198 names = PyList_AsTuple(sc.c_names);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5199 varnames = PyList_AsTuple(sc.c_varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5200 cellvars = dict_keys_inorder(sc.c_cellvars, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5201 freevars = dict_keys_inorder(sc.c_freevars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5202 PyTuple_GET_SIZE(cellvars));
32956
071d3381753b Revert 2.312; turns out interning the file name did do some good (reducing
Michael W. Hudson <mwh@python.net>
parents: 32917
diff changeset
5203 filename = PyString_InternFromString(sc.c_filename);
5009
402e36c83d20 Intern all names and varnames in newcodeobject(), plus those string
Guido van Rossum <guido@python.org>
parents: 4991
diff changeset
5204 name = PyString_InternFromString(sc.c_name);
33539
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
5205 code = optimize_code(sc.c_code, sc.c_consts, names, sc.c_lnotab);
2b49b4a85d9e SF patch #1031667: Fold tuples of constants into a single constant
Raymond Hettinger <python@rcn.com>
parents: 33353
diff changeset
5206 consts = PyList_AsTuple(sc.c_consts);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5207 if (!PyErr_Occurred())
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5208 co = PyCode_New(sc.c_argcount,
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5209 sc.c_nlocals,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5210 sc.c_maxstacklevel,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5211 sc.c_flags,
33169
5778f6331334 Move the bytecode optimizer upstream so that its results are saved in pyc
Raymond Hettinger <python@rcn.com>
parents: 33166
diff changeset
5212 code,
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5213 consts,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5214 names,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5215 varnames,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5216 freevars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5217 cellvars,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5218 filename,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5219 name,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5220 sc.c_firstlineno,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5221 sc.c_lnotab);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5222 Py_XDECREF(consts);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5223 Py_XDECREF(names);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5224 Py_XDECREF(varnames);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5225 Py_XDECREF(freevars);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5226 Py_XDECREF(cellvars);
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5227 Py_XDECREF(filename);
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5287
diff changeset
5228 Py_XDECREF(name);
33169
5778f6331334 Move the bytecode optimizer upstream so that its results are saved in pyc
Raymond Hettinger <python@rcn.com>
parents: 33166
diff changeset
5229 Py_XDECREF(code);
266
b5826bf29677 Changed and exported newcodeobject() interface, for ".pyc" files.
Guido van Rossum <guido@python.org>
parents: 238
diff changeset
5230 }
13884
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
5231 else if (!PyErr_Occurred()) {
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
5232 /* This could happen if someone called PyErr_Clear() after an
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
5233 error was reported above. That's not supposed to happen,
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
5234 but I just plugged one case and I'm not sure there can't be
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
5235 others. In that case, raise SystemError so that at least
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
5236 it gets reported instead dumping core. */
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
5237 PyErr_SetString(PyExc_SystemError, "lost syntax error");
8eeaff96b4c2 When returning an error from jcompile() (which is passed through by
Guido van Rossum <guido@python.org>
parents: 13873
diff changeset
5238 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5239 exit:
16753
51977af230fa Fix for bug 133489: compiler leaks memory
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16699
diff changeset
5240 if (base == NULL) {
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5241 PySymtable_Free(sc.c_symtable);
16753
51977af230fa Fix for bug 133489: compiler leaks memory
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16699
diff changeset
5242 sc.c_symtable = NULL;
51977af230fa Fix for bug 133489: compiler leaks memory
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16699
diff changeset
5243 }
69
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5244 com_free(&sc);
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5245 return co;
cccd534f2280 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5246 }
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5247
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5248 int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13585
diff changeset
5249 PyCode_Addr2Line(PyCodeObject *co, int addrq)
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5250 {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5251 int size = PyString_Size(co->co_lnotab) / 2;
11290
ccdf5dc2acf6 Tim Peters writes:
Guido van Rossum <guido@python.org>
parents: 9987
diff changeset
5252 unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
5041
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5253 int line = co->co_firstlineno;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5254 int addr = 0;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5255 while (--size >= 0) {
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5256 addr += *p++;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5257 if (addr > addrq)
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5258 break;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5259 line += *p++;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5260 }
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5261 return line;
c497929b2371 Instead of emitting SET_LINENO instructions, generate a line number
Guido van Rossum <guido@python.org>
parents: 5011
diff changeset
5262 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5263
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5264 /* The test for LOCAL must come before the test for FREE in order to
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5265 handle classes where name is both local and free. The local var is
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5266 a method and the free var is a free var referenced within a method.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5267 */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5268
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5269 static int
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5270 get_ref_type(struct compiling *c, char *name)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5271 {
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5272 char buf[350];
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5273 PyObject *v;
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5274
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5275 if (PyDict_GetItemString(c->c_cellvars, name) != NULL)
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5276 return CELL;
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5277 if (PyDict_GetItemString(c->c_locals, name) != NULL)
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5278 return LOCAL;
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5279 if (PyDict_GetItemString(c->c_freevars, name) != NULL)
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5280 return FREE;
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5281 v = PyDict_GetItemString(c->c_globals, name);
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5282 if (v) {
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5283 if (v == Py_None)
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5284 return GLOBAL_EXPLICIT;
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5285 else {
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5286 return GLOBAL_IMPLICIT;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5287 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5288 }
21066
225c7dd17e37 Fixes for possible buffer overflows in sprintf() usages.
Marc-André Lemburg <mal@egenix.com>
parents: 20947
diff changeset
5289 PyOS_snprintf(buf, sizeof(buf),
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5290 "unknown scope for %.100s in %.100s(%s) "
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5291 "in %s\nsymbols: %s\nlocals: %s\nglobals: %s\n",
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5292 name, c->c_name,
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5293 PyObject_REPR(c->c_symtable->st_cur->ste_id),
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5294 c->c_filename,
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5295 PyObject_REPR(c->c_symtable->st_cur->ste_symbols),
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5296 PyObject_REPR(c->c_locals),
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5297 PyObject_REPR(c->c_globals)
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5298 );
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5299
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5300 Py_FatalError(buf);
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5301 return -1;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5302 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5303
16933
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5304 /* Helper functions to issue warnings */
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5305
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5306 static int
26601
cc05c2c8660e Constify filenames and scripts. Fixes #651362.
Martin v. Löwis <martin@v.loewis.de>
parents: 26319
diff changeset
5307 issue_warning(const char *msg, const char *filename, int lineno)
16933
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5308 {
29869
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5309 if (PyErr_Occurred()) {
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5310 /* This can happen because symtable_node continues
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5311 processing even after raising a SyntaxError.
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5312 Calling PyErr_WarnExplicit now would clobber the
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5313 pending exception; instead we fail and let that
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5314 exception propagate.
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5315 */
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5316 return -1;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5317 }
16933
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5318 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, filename,
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5319 lineno, NULL, NULL) < 0) {
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5320 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5321 PyErr_SetString(PyExc_SyntaxError, msg);
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5322 PyErr_SyntaxLocation(filename, lineno);
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5323 }
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5324 return -1;
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5325 }
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5326 return 0;
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5327 }
16855
ec9083ac26df Let's have some sanity. Introduce a helper to issue a symbol table
Guido van Rossum <guido@python.org>
parents: 16853
diff changeset
5328
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5329 static int
16855
ec9083ac26df Let's have some sanity. Introduce a helper to issue a symbol table
Guido van Rossum <guido@python.org>
parents: 16853
diff changeset
5330 symtable_warn(struct symtable *st, char *msg)
ec9083ac26df Let's have some sanity. Introduce a helper to issue a symbol table
Guido van Rossum <guido@python.org>
parents: 16853
diff changeset
5331 {
16933
b046a31af2e0 Refactored the warning-issuing code more.
Guido van Rossum <guido@python.org>
parents: 16916
diff changeset
5332 if (issue_warning(msg, st->st_filename, st->st_cur->ste_lineno) < 0) {
16855
ec9083ac26df Let's have some sanity. Introduce a helper to issue a symbol table
Guido van Rossum <guido@python.org>
parents: 16853
diff changeset
5333 st->st_errors++;
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5334 return -1;
16855
ec9083ac26df Let's have some sanity. Introduce a helper to issue a symbol table
Guido van Rossum <guido@python.org>
parents: 16853
diff changeset
5335 }
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5336 return 0;
16855
ec9083ac26df Let's have some sanity. Introduce a helper to issue a symbol table
Guido van Rossum <guido@python.org>
parents: 16853
diff changeset
5337 }
ec9083ac26df Let's have some sanity. Introduce a helper to issue a symbol table
Guido van Rossum <guido@python.org>
parents: 16853
diff changeset
5338
16831
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
5339 /* Helper function for setting lineno and filename */
27f04d714ecb Print the offending line of code in the traceback for SyntaxErrors
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16823
diff changeset
5340
29869
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5341 static struct symtable *
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5342 symtable_build(node *n, PyFutureFeatures *ff, const char *filename)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5343 {
29869
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5344 struct symtable *st;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5345
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5346 st = symtable_init();
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5347 if (st == NULL)
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5348 return NULL;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5349 st->st_future = ff;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5350 st->st_filename = filename;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5351 symtable_enter_scope(st, TOP, TYPE(n), n->n_lineno);
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5352 if (st->st_errors > 0)
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5353 goto fail;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5354 symtable_node(st, n);
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5355 if (st->st_errors > 0)
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5356 goto fail;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5357 return st;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5358 fail:
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5359 if (!PyErr_Occurred()) {
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5360 /* This could happen because after a syntax error is
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5361 detected, the symbol-table-building continues for
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5362 a while, and PyErr_Clear() might erroneously be
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5363 called during that process. One such case has been
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5364 fixed, but there might be more (now or later).
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5365 */
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5366 PyErr_SetString(PyExc_SystemError, "lost exception");
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5367 }
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5368 st->st_future = NULL;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5369 st->st_filename = NULL;
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5370 PySymtable_Free(st);
3442e7cdace8 SF patch 763201: handling of SyntaxErrors in symbol table build
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29559
diff changeset
5371 return NULL;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5372 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5373
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5374 static int
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5375 symtable_init_compiling_symbols(struct compiling *c)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5376 {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5377 PyObject *varnames;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5378
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5379 varnames = c->c_symtable->st_cur->ste_varnames;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5380 if (varnames == NULL) {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5381 varnames = PyList_New(0);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5382 if (varnames == NULL)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5383 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5384 c->c_symtable->st_cur->ste_varnames = varnames;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5385 Py_INCREF(varnames);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5386 } else
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5387 Py_INCREF(varnames);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5388 c->c_varnames = varnames;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5389
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5390 c->c_globals = PyDict_New();
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5391 if (c->c_globals == NULL)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5392 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5393 c->c_freevars = PyDict_New();
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5394 if (c->c_freevars == NULL)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5395 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5396 c->c_cellvars = PyDict_New();
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5397 if (c->c_cellvars == NULL)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5398 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5399 return 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5400 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5401
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5402 struct symbol_info {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5403 int si_nlocals;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5404 int si_ncells;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5405 int si_nfrees;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5406 int si_nimplicit;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5407 };
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5408
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5409 static void
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5410 symtable_init_info(struct symbol_info *si)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5411 {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5412 si->si_nlocals = 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5413 si->si_ncells = 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5414 si->si_nfrees = 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5415 si->si_nimplicit = 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5416 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5417
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5418 static int
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5419 symtable_resolve_free(struct compiling *c, PyObject *name, int flags,
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5420 struct symbol_info *si)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5421 {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5422 PyObject *dict, *v;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5423
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5424 /* Seperate logic for DEF_FREE. If it occurs in a function,
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5425 it indicates a local that we must allocate storage for (a
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5426 cell var). If it occurs in a class, then the class has a
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5427 method and a free variable with the same name.
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5428 */
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5429 if (c->c_symtable->st_cur->ste_type == TYPE_FUNCTION) {
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5430 /* If it isn't declared locally, it can't be a cell. */
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5431 if (!(flags & (DEF_LOCAL | DEF_PARAM)))
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5432 return 0;
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5433 v = PyInt_FromLong(si->si_ncells++);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5434 dict = c->c_cellvars;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5435 } else {
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5436 /* If it is free anyway, then there is no need to do
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5437 anything here.
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5438 */
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5439 if (is_free(flags ^ DEF_FREE_CLASS)
17702
e3f0fd38c24b Several small changes. Mostly reformatting, adding parens.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
5440 || (flags == DEF_FREE_CLASS))
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5441 return 0;
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5442 v = PyInt_FromLong(si->si_nfrees++);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5443 dict = c->c_freevars;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5444 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5445 if (v == NULL)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5446 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5447 if (PyDict_SetItem(dict, name, v) < 0) {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5448 Py_DECREF(v);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5449 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5450 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5451 Py_DECREF(v);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5452 return 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5453 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5454
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5455 /* If a variable is a cell and an argument, make sure that appears in
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5456 co_cellvars before any variable to its right in varnames.
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5457 */
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5458
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5459
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5460 static int
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5461 symtable_cellvar_offsets(PyObject **cellvars, int argcount,
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5462 PyObject *varnames, int flags)
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5463 {
28402
5f5364ac4adb symtable_cellvar_offsets(): This leaked references to little integers
Tim Peters <tim.peters@gmail.com>
parents: 27846
diff changeset
5464 PyObject *v = NULL;
5f5364ac4adb symtable_cellvar_offsets(): This leaked references to little integers
Tim Peters <tim.peters@gmail.com>
parents: 27846
diff changeset
5465 PyObject *w, *d, *list = NULL;
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5466 int i, pos;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5467
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5468 if (flags & CO_VARARGS)
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5469 argcount++;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5470 if (flags & CO_VARKEYWORDS)
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5471 argcount++;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5472 for (i = argcount; --i >= 0; ) {
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5473 v = PyList_GET_ITEM(varnames, i);
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5474 if (PyDict_GetItem(*cellvars, v)) {
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5475 if (list == NULL) {
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5476 list = PyList_New(1);
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5477 if (list == NULL)
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5478 return -1;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5479 PyList_SET_ITEM(list, 0, v);
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5480 Py_INCREF(v);
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5481 } else {
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5482 if (PyList_Insert(list, 0, v) < 0) {
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5483 Py_DECREF(list);
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5484 return -1;
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5485 }
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5486 }
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5487 }
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5488 }
29223
ea2958bd4271 Remove comment with very long lines that explained what the code used
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29199
diff changeset
5489 if (list == NULL)
ea2958bd4271 Remove comment with very long lines that explained what the code used
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29199
diff changeset
5490 return 0;
ea2958bd4271 Remove comment with very long lines that explained what the code used
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29199
diff changeset
5491
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5492 /* There are cellvars that are also arguments. Create a dict
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5493 to replace cellvars and put the args at the front.
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5494 */
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5495 d = PyDict_New();
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5496 if (d == NULL)
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5497 return -1;
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5498 for (i = PyList_GET_SIZE(list); --i >= 0; ) {
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5499 v = PyInt_FromLong(i);
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5500 if (v == NULL)
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5501 goto fail;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5502 if (PyDict_SetItem(d, PyList_GET_ITEM(list, i), v) < 0)
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5503 goto fail;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5504 if (PyDict_DelItem(*cellvars, PyList_GET_ITEM(list, i)) < 0)
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5505 goto fail;
28402
5f5364ac4adb symtable_cellvar_offsets(): This leaked references to little integers
Tim Peters <tim.peters@gmail.com>
parents: 27846
diff changeset
5506 Py_DECREF(v);
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5507 }
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5508 pos = 0;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5509 i = PyList_GET_SIZE(list);
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5510 Py_DECREF(list);
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5511 while (PyDict_Next(*cellvars, &pos, &v, &w)) {
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5512 w = PyInt_FromLong(i++); /* don't care about the old key */
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5513 if (w == NULL)
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5514 goto fail;
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5515 if (PyDict_SetItem(d, v, w) < 0) {
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5516 Py_DECREF(w);
28402
5f5364ac4adb symtable_cellvar_offsets(): This leaked references to little integers
Tim Peters <tim.peters@gmail.com>
parents: 27846
diff changeset
5517 v = NULL;
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5518 goto fail;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5519 }
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5520 Py_DECREF(w);
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5521 }
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5522 Py_DECREF(*cellvars);
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5523 *cellvars = d;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5524 return 1;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5525 fail:
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5526 Py_DECREF(d);
28402
5f5364ac4adb symtable_cellvar_offsets(): This leaked references to little integers
Tim Peters <tim.peters@gmail.com>
parents: 27846
diff changeset
5527 Py_XDECREF(v);
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5528 return -1;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5529 }
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5530
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5531 static int
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5532 symtable_freevar_offsets(PyObject *freevars, int offset)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5533 {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5534 PyObject *name, *v;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5535 int pos;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5536
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5537 /* The cell vars are the first elements of the closure,
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5538 followed by the free vars. Update the offsets in
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5539 c_freevars to account for number of cellvars. */
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5540 pos = 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5541 while (PyDict_Next(freevars, &pos, &name, &v)) {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5542 int i = PyInt_AS_LONG(v) + offset;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5543 PyObject *o = PyInt_FromLong(i);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5544 if (o == NULL)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5545 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5546 if (PyDict_SetItem(freevars, name, o) < 0) {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5547 Py_DECREF(o);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5548 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5549 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5550 Py_DECREF(o);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5551 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5552 return 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5553 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5554
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5555 static int
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5556 symtable_check_unoptimized(struct compiling *c,
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5557 PySymtableEntryObject *ste,
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5558 struct symbol_info *si)
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5559 {
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5560 char buf[300];
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5561
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5562 if (!(si->si_ncells || si->si_nfrees || ste->ste_child_free
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5563 || (ste->ste_nested && si->si_nimplicit)))
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5564 return 0;
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5565
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5566 #define ILLEGAL_CONTAINS "contains a nested function with free variables"
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5567
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5568 #define ILLEGAL_IS "is a nested function"
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5569
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5570 #define ILLEGAL_IMPORT_STAR \
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5571 "import * is not allowed in function '%.100s' because it %s"
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5572
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5573 #define ILLEGAL_BARE_EXEC \
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5574 "unqualified exec is not allowed in function '%.100s' it %s"
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5575
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5576 #define ILLEGAL_EXEC_AND_IMPORT_STAR \
21834
c1d55a627897 Fix missing space between words. Bugfix candidate.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21833
diff changeset
5577 "function '%.100s' uses import * and bare exec, which are illegal " \
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5578 "because it %s"
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5579
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5580 /* XXX perhaps the linenos for these opt-breaking statements
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5581 should be stored so the exception can point to them. */
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5582
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5583 if (ste->ste_child_free) {
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5584 if (ste->ste_optimized == OPT_IMPORT_STAR)
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5585 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5586 ILLEGAL_IMPORT_STAR,
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5587 PyString_AS_STRING(ste->ste_name),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5588 ILLEGAL_CONTAINS);
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5589 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5590 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5591 ILLEGAL_BARE_EXEC,
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5592 PyString_AS_STRING(ste->ste_name),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5593 ILLEGAL_CONTAINS);
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5594 else {
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5595 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5596 ILLEGAL_EXEC_AND_IMPORT_STAR,
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5597 PyString_AS_STRING(ste->ste_name),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5598 ILLEGAL_CONTAINS);
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5599 }
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5600 } else {
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5601 if (ste->ste_optimized == OPT_IMPORT_STAR)
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5602 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5603 ILLEGAL_IMPORT_STAR,
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5604 PyString_AS_STRING(ste->ste_name),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5605 ILLEGAL_IS);
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5606 else if (ste->ste_optimized == (OPT_BARE_EXEC | OPT_EXEC))
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5607 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5608 ILLEGAL_BARE_EXEC,
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5609 PyString_AS_STRING(ste->ste_name),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5610 ILLEGAL_IS);
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5611 else {
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5612 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5613 ILLEGAL_EXEC_AND_IMPORT_STAR,
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5614 PyString_AS_STRING(ste->ste_name),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
5615 ILLEGAL_IS);
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5616 }
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5617 }
17131
1c90a1cf795c Make error messages clearer for illegal combinations of nested
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17124
diff changeset
5618
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5619 PyErr_SetString(PyExc_SyntaxError, buf);
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5620 PyErr_SyntaxLocation(c->c_symtable->st_filename,
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5621 ste->ste_opt_lineno);
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5622 return -1;
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5623 }
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5624
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5625 static int
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5626 symtable_update_flags(struct compiling *c, PySymtableEntryObject *ste,
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5627 struct symbol_info *si)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5628 {
19158
97a3d3d7029f Refactor future feature handling
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19051
diff changeset
5629 if (c->c_future)
97a3d3d7029f Refactor future feature handling
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19051
diff changeset
5630 c->c_flags |= c->c_future->ff_features;
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
5631 if (ste->ste_generator)
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
5632 c->c_flags |= CO_GENERATOR;
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5633 if (ste->ste_type != TYPE_MODULE)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5634 c->c_flags |= CO_NEWLOCALS;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5635 if (ste->ste_type == TYPE_FUNCTION) {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5636 c->c_nlocals = si->si_nlocals;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5637 if (ste->ste_optimized == 0)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5638 c->c_flags |= CO_OPTIMIZED;
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5639 else if (ste->ste_optimized != OPT_EXEC)
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5640 return symtable_check_unoptimized(c, ste, si);
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5641 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5642 return 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5643 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5644
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5645 static int
30488
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5646 symtable_error(struct symtable *st, int lineno)
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5647 {
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5648 if (lineno == 0)
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5649 lineno = st->st_cur->ste_lineno;
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5650 PyErr_SyntaxLocation(st->st_filename, lineno);
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5651 st->st_errors++;
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5652 return -1;
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5653 }
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5654
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5655 static int
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5656 symtable_load_symbols(struct compiling *c)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5657 {
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5658 struct symtable *st = c->c_symtable;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5659 PySymtableEntryObject *ste = st->st_cur;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5660 PyObject *name, *varnames, *v;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5661 int i, flags, pos;
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5662 struct symbol_info si;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5663
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5664 v = NULL;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5665
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5666 if (symtable_init_compiling_symbols(c) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5667 goto fail;
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5668 symtable_init_info(&si);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5669 varnames = st->st_cur->ste_varnames;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5670 si.si_nlocals = PyList_GET_SIZE(varnames);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5671 c->c_argcount = si.si_nlocals;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5672
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5673 for (i = 0; i < si.si_nlocals; ++i) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5674 v = PyInt_FromLong(i);
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5675 if (v == NULL)
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5676 goto fail;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5677 if (PyDict_SetItem(c->c_locals,
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5678 PyList_GET_ITEM(varnames, i), v) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5679 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5680 Py_DECREF(v);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5681 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5682
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5683 /* XXX The cases below define the rules for whether a name is
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5684 local or global. The logic could probably be clearer. */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5685 pos = 0;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5686 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5687 flags = PyInt_AS_LONG(v);
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5688
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5689 if (flags & DEF_FREE_GLOBAL)
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5690 /* undo the original DEF_FREE */
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5691 flags &= ~(DEF_FREE | DEF_FREE_CLASS);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5692
17078
7e181eba6b9b Fixup handling of free variables in methods when the class scope also
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17076
diff changeset
5693 /* Deal with names that need two actions:
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5694 1. Cell variables that are also locals.
17078
7e181eba6b9b Fixup handling of free variables in methods when the class scope also
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17076
diff changeset
5695 2. Free variables in methods that are also class
7e181eba6b9b Fixup handling of free variables in methods when the class scope also
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17076
diff changeset
5696 variables or declared global.
7e181eba6b9b Fixup handling of free variables in methods when the class scope also
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17076
diff changeset
5697 */
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5698 if (flags & (DEF_FREE | DEF_FREE_CLASS))
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5699 symtable_resolve_free(c, name, flags, &si);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5700
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5701 if (flags & DEF_STAR) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5702 c->c_argcount--;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5703 c->c_flags |= CO_VARARGS;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5704 } else if (flags & DEF_DOUBLESTAR) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5705 c->c_argcount--;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5706 c->c_flags |= CO_VARKEYWORDS;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5707 } else if (flags & DEF_INTUPLE)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5708 c->c_argcount--;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5709 else if (flags & DEF_GLOBAL) {
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5710 if (flags & DEF_PARAM) {
30713
9c7e0227c835 Patch #792869: Clarify error message for parameters declared global,
Martin v. Löwis <martin@v.loewis.de>
parents: 30683
diff changeset
5711 PyErr_Format(PyExc_SyntaxError, PARAM_GLOBAL,
16428
6496844c4a42 Fix symbol table pass to generation SyntaxError exceptions that
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16421
diff changeset
5712 PyString_AS_STRING(name));
30488
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
5713 symtable_error(st, 0);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5714 goto fail;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5715 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5716 if (PyDict_SetItem(c->c_globals, name, Py_None) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5717 goto fail;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5718 } else if (flags & DEF_FREE_GLOBAL) {
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5719 si.si_nimplicit++;
26701
45ab31127bf9 SF # 654960, remove unnecessary static variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 26670
diff changeset
5720 if (PyDict_SetItem(c->c_globals, name, Py_True) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5721 goto fail;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5722 } else if ((flags & DEF_LOCAL) && !(flags & DEF_PARAM)) {
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5723 v = PyInt_FromLong(si.si_nlocals++);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5724 if (v == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5725 goto fail;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5726 if (PyDict_SetItem(c->c_locals, name, v) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5727 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5728 Py_DECREF(v);
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5729 if (ste->ste_type != TYPE_CLASS)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5730 if (PyList_Append(c->c_varnames, name) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5731 goto fail;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5732 } else if (is_free(flags)) {
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5733 if (ste->ste_nested) {
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5734 v = PyInt_FromLong(si.si_nfrees++);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5735 if (v == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5736 goto fail;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5737 if (PyDict_SetItem(c->c_freevars, name, v) < 0)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5738 goto fail;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5739 Py_DECREF(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5740 } else {
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5741 si.si_nimplicit++;
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5742 if (PyDict_SetItem(c->c_globals, name,
26701
45ab31127bf9 SF # 654960, remove unnecessary static variable
Neal Norwitz <nnorwitz@gmail.com>
parents: 26670
diff changeset
5743 Py_True) < 0)
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5744 goto fail;
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5745 if (st->st_nscopes != 1) {
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5746 v = PyInt_FromLong(flags);
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5747 if (v == NULL)
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5748 goto fail;
19177
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5749 if (PyDict_SetItem(st->st_global,
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5750 name, v))
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5751 goto fail;
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5752 Py_DECREF(v);
50f88ee71d45 Remove st_nested_scopes from struct symtable,
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19160
diff changeset
5753 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5754 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5755 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5756 }
17637
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5757 assert(PyDict_Size(c->c_freevars) == si.si_nfrees);
8ff76cf30a8c Fix 2.1 nested scopes crash reported by Evan Simpson
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17597
diff changeset
5758
17104
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5759 if (si.si_ncells > 1) { /* one cell is always in order */
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5760 if (symtable_cellvar_offsets(&c->c_cellvars, c->c_argcount,
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5761 c->c_varnames, c->c_flags) < 0)
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5762 return -1;
a488a63cb867 Fix PyFrame_FastToLocals() and counterpart to deal with cells and
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17078
diff changeset
5763 }
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5764 if (symtable_freevar_offsets(c->c_freevars, si.si_ncells) < 0)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5765 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5766 return symtable_update_flags(c, ste, &si);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5767 fail:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5768 /* is this always the right thing to do? */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5769 Py_XDECREF(v);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5770 return -1;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5771 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5772
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5773 static struct symtable *
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5774 symtable_init()
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5775 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5776 struct symtable *st;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5777
24529
3444005b6527 SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 24373
diff changeset
5778 st = (struct symtable *)PyObject_MALLOC(sizeof(struct symtable));
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5779 if (st == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5780 return NULL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5781 st->st_pass = 1;
19160
9d024ae05837 st_nested_scopes was uninitialized trash. Jeremy should fix in a better
Tim Peters <tim.peters@gmail.com>
parents: 19158
diff changeset
5782
16428
6496844c4a42 Fix symbol table pass to generation SyntaxError exceptions that
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16421
diff changeset
5783 st->st_filename = NULL;
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5784 st->st_symbols = NULL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5785 if ((st->st_stack = PyList_New(0)) == NULL)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5786 goto fail;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5787 if ((st->st_symbols = PyDict_New()) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5788 goto fail;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5789 st->st_cur = NULL;
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5790 st->st_nscopes = 0;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5791 st->st_errors = 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5792 st->st_private = NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5793 return st;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5794 fail:
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5795 PySymtable_Free(st);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5796 return NULL;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5797 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5798
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5799 void
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5800 PySymtable_Free(struct symtable *st)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5801 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5802 Py_XDECREF(st->st_symbols);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5803 Py_XDECREF(st->st_stack);
16753
51977af230fa Fix for bug 133489: compiler leaks memory
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16699
diff changeset
5804 Py_XDECREF(st->st_cur);
24529
3444005b6527 SF patch #578297:
Andrew MacIntyre <andymac@bullseye.apana.org.au>
parents: 24373
diff changeset
5805 PyObject_FREE((void *)st);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5806 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5807
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5808 /* When the compiler exits a scope, it must should update the scope's
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5809 free variable information with the list of free variables in its
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5810 children.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5811
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5812 Variables that are free in children and defined in the current
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5813 scope are cellvars.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5814
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5815 If the scope being exited is defined at the top-level (ste_nested is
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5816 false), free variables in children that are not defined here are
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5817 implicit globals.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5818
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5819 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5820
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5821 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5822 symtable_update_free_vars(struct symtable *st)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5823 {
16571
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5824 int i, j, def;
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5825 PyObject *o, *name, *list = NULL;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5826 PySymtableEntryObject *child, *ste = st->st_cur;
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5827
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5828 if (ste->ste_type == TYPE_CLASS)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5829 def = DEF_FREE_CLASS;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5830 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5831 def = DEF_FREE;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5832 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5833 int pos = 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5834
29224
858d568ebf76 Use macro to get length of list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29223
diff changeset
5835 if (list && PyList_SetSlice(list, 0,
858d568ebf76 Use macro to get length of list.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29223
diff changeset
5836 PyList_GET_SIZE(list), 0) < 0)
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5837 return -1;
16754
23c7a0108f59 symtable_update_free_vars(), symtable_undo_free(),
Barry Warsaw <barry@python.org>
parents: 16753
diff changeset
5838 child = (PySymtableEntryObject *)
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5839 PyList_GET_ITEM(ste->ste_children, i);
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5840 while (PyDict_Next(child->ste_symbols, &pos, &name, &o)) {
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5841 int flags = PyInt_AS_LONG(o);
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5842 if (!(is_free(flags)))
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5843 continue; /* avoids indentation */
16571
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5844 if (list == NULL) {
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5845 list = PyList_New(0);
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5846 if (list == NULL)
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5847 return -1;
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5848 }
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5849 ste->ste_child_free = 1;
16571
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5850 if (PyList_Append(list, name) < 0) {
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5851 Py_DECREF(list);
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5852 return -1;
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5853 }
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5854 }
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5855 for (j = 0; list && j < PyList_GET_SIZE(list); j++) {
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5856 PyObject *v;
16571
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5857 name = PyList_GET_ITEM(list, j);
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5858 v = PyDict_GetItem(ste->ste_symbols, name);
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5859 /* If a name N is declared global in scope A and
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5860 referenced in scope B contained (perhaps
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5861 indirectly) in A and there are no scopes
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5862 with bindings for N between B and A, then N
17702
e3f0fd38c24b Several small changes. Mostly reformatting, adding parens.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
5863 is global in B. Unless A is a class scope,
e3f0fd38c24b Several small changes. Mostly reformatting, adding parens.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
5864 because class scopes are not considered for
e3f0fd38c24b Several small changes. Mostly reformatting, adding parens.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17637
diff changeset
5865 nested scopes.
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5866 */
17078
7e181eba6b9b Fixup handling of free variables in methods when the class scope also
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17076
diff changeset
5867 if (v && (ste->ste_type != TYPE_CLASS)) {
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5868 int flags = PyInt_AS_LONG(v);
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5869 if (flags & DEF_GLOBAL) {
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5870 symtable_undo_free(st, child->ste_id,
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5871 name);
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5872 continue;
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5873 }
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5874 }
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5875 if (ste->ste_nested) {
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5876 if (symtable_add_def_o(st, ste->ste_symbols,
16571
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5877 name, def) < 0) {
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5878 Py_DECREF(list);
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5879 return -1;
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5880 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5881 } else {
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5882 if (symtable_check_global(st, child->ste_id,
16571
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5883 name) < 0) {
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5884 Py_DECREF(list);
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5885 return -1;
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5886 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5887 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5888 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5889 }
16571
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5890
2c6417ef4628 In symtable_update_free_vars() do not modify the dictionary while
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16540
diff changeset
5891 Py_XDECREF(list);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5892 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5893 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5894
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5895 /* If the current scope is a non-nested class or if name is not
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5896 defined in the current, non-nested scope, then it is an implicit
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5897 global in all nested scopes.
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5898 */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5899
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5900 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5901 symtable_check_global(struct symtable *st, PyObject *child, PyObject *name)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5902 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5903 PyObject *o;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5904 int v;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5905 PySymtableEntryObject *ste = st->st_cur;
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5906
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5907 if (ste->ste_type == TYPE_CLASS)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5908 return symtable_undo_free(st, child, name);
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5909 o = PyDict_GetItem(ste->ste_symbols, name);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5910 if (o == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5911 return symtable_undo_free(st, child, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5912 v = PyInt_AS_LONG(o);
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5913
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
5914 if (is_free(v) || (v & DEF_GLOBAL))
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5915 return symtable_undo_free(st, child, name);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5916 else
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5917 return symtable_add_def_o(st, ste->ste_symbols,
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5918 name, DEF_FREE);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5919 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5920
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5921 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5922 symtable_undo_free(struct symtable *st, PyObject *id,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5923 PyObject *name)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5924 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5925 int i, v, x;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5926 PyObject *info;
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5927 PySymtableEntryObject *ste;
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5928
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5929 ste = (PySymtableEntryObject *)PyDict_GetItem(st->st_symbols, id);
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5930 if (ste == NULL)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5931 return -1;
16892
f27d3299df39 Fix core dump in example from Samuele Pedroni:
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16878
diff changeset
5932
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5933 info = PyDict_GetItem(ste->ste_symbols, name);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5934 if (info == NULL)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5935 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5936 v = PyInt_AS_LONG(info);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5937 if (is_free(v)) {
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5938 if (symtable_add_def_o(st, ste->ste_symbols, name,
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5939 DEF_FREE_GLOBAL) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5940 return -1;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5941 } else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5942 /* If the name is defined here or declared global,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5943 then the recursion stops. */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5944 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5945
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5946 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5947 PySymtableEntryObject *child;
16754
23c7a0108f59 symtable_update_free_vars(), symtable_undo_free(),
Barry Warsaw <barry@python.org>
parents: 16753
diff changeset
5948 child = (PySymtableEntryObject *)
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5949 PyList_GET_ITEM(ste->ste_children, i);
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5950 x = symtable_undo_free(st, child->ste_id, name);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5951 if (x < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5952 return x;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5953 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5954 return 0;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5955 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5956
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5957 /* symtable_enter_scope() gets a reference via PySymtableEntry_New().
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5958 This reference is released when the scope is exited, via the DECREF
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5959 in symtable_exit_scope().
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5960 */
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
5961
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5962 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5963 symtable_exit_scope(struct symtable *st)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5964 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5965 int end;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5966
16862
77a5470299db Add warning/error handlin for problematic nested scopes cases as
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16855
diff changeset
5967 if (st->st_pass == 1)
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5968 symtable_update_free_vars(st);
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5969 Py_DECREF(st->st_cur);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5970 end = PyList_GET_SIZE(st->st_stack) - 1;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5971 st->st_cur = (PySymtableEntryObject *)PyList_GET_ITEM(st->st_stack,
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5972 end);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5973 if (PySequence_DelItem(st->st_stack, end) < 0)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5974 return -1;
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5975 return 0;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5976 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5977
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5978 static void
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5979 symtable_enter_scope(struct symtable *st, char *name, int type,
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5980 int lineno)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
5981 {
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5982 PySymtableEntryObject *prev = NULL;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5983
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5984 if (st->st_cur) {
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5985 prev = st->st_cur;
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5986 if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) {
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5987 st->st_errors++;
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
5988 return;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5989 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
5990 }
16754
23c7a0108f59 symtable_update_free_vars(), symtable_undo_free(),
Barry Warsaw <barry@python.org>
parents: 16753
diff changeset
5991 st->st_cur = (PySymtableEntryObject *)
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5992 PySymtableEntry_New(st, name, type, lineno);
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5993 if (st->st_cur == NULL) {
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5994 st->st_errors++;
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5995 return;
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
5996 }
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5997 if (strcmp(name, TOP) == 0)
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
5998 st->st_global = st->st_cur->ste_symbols;
16753
51977af230fa Fix for bug 133489: compiler leaks memory
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16699
diff changeset
5999 if (prev && st->st_pass == 1) {
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
6000 if (PyList_Append(prev->ste_children,
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
6001 (PyObject *)st->st_cur) < 0)
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
6002 st->st_errors++;
16753
51977af230fa Fix for bug 133489: compiler leaks memory
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16699
diff changeset
6003 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6004 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6005
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6006 static int
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6007 symtable_lookup(struct symtable *st, char *name)
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6008 {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6009 char buffer[MANGLE_LEN];
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6010 PyObject *v;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6011 int flags;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6012
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23868
diff changeset
6013 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6014 name = buffer;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6015 v = PyDict_GetItemString(st->st_cur->ste_symbols, name);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6016 if (v == NULL) {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6017 if (PyErr_Occurred())
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6018 return -1;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6019 else
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6020 return 0;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6021 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6022
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6023 flags = PyInt_AS_LONG(v);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6024 return flags;
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6025 }
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6026
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6027 static int
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6028 symtable_add_def(struct symtable *st, char *name, int flag)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6029 {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6030 PyObject *s;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6031 char buffer[MANGLE_LEN];
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6032 int ret;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6033
24858
592406e0c9b0 Add warnings for arguments named None. All set. (I could add a
Guido van Rossum <guido@python.org>
parents: 24856
diff changeset
6034 /* Warn about None, except inside a tuple (where the assignment
592406e0c9b0 Add warnings for arguments named None. All set. (I could add a
Guido van Rossum <guido@python.org>
parents: 24856
diff changeset
6035 code already issues a warning). */
592406e0c9b0 Add warnings for arguments named None. All set. (I could add a
Guido van Rossum <guido@python.org>
parents: 24856
diff changeset
6036 if ((flag & DEF_PARAM) && !(flag & DEF_INTUPLE) &&
592406e0c9b0 Add warnings for arguments named None. All set. (I could add a
Guido van Rossum <guido@python.org>
parents: 24856
diff changeset
6037 *name == 'N' && strcmp(name, "None") == 0)
592406e0c9b0 Add warnings for arguments named None. All set. (I could add a
Guido van Rossum <guido@python.org>
parents: 24856
diff changeset
6038 {
32723
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 32717
diff changeset
6039 PyErr_SetString(PyExc_SyntaxError,
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 32717
diff changeset
6040 "Invalid syntax. Assignment to None.");
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 32717
diff changeset
6041 symtable_error(st, 0);
efca55bbba0a Upgrade None assignment SyntaxWarning to a SyntaxError.
Raymond Hettinger <python@rcn.com>
parents: 32717
diff changeset
6042 return -1;
24858
592406e0c9b0 Add warnings for arguments named None. All set. (I could add a
Guido van Rossum <guido@python.org>
parents: 24856
diff changeset
6043 }
23950
f7ce696979e1 SF 569257 -- Name mangle double underscored variable names in __slots__.
Raymond Hettinger <python@rcn.com>
parents: 23868
diff changeset
6044 if (_Py_Mangle(st->st_private, name, buffer, sizeof(buffer)))
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6045 name = buffer;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6046 if ((s = PyString_InternFromString(name)) == NULL)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6047 return -1;
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6048 ret = symtable_add_def_o(st, st->st_cur->ste_symbols, s, flag);
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6049 Py_DECREF(s);
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6050 return ret;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6051 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6052
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6053 /* Must only be called with mangled names */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6054
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6055 static int
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6056 symtable_add_def_o(struct symtable *st, PyObject *dict,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6057 PyObject *name, int flag)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6058 {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6059 PyObject *o;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6060 int val;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6061
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6062 if ((o = PyDict_GetItem(dict, name))) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6063 val = PyInt_AS_LONG(o);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6064 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6065 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT,
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
6066 PyString_AsString(name));
30488
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
6067 return symtable_error(st, 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6068 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6069 val |= flag;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6070 } else
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6071 val = flag;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6072 o = PyInt_FromLong(val);
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
6073 if (o == NULL)
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
6074 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6075 if (PyDict_SetItem(dict, name, o) < 0) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6076 Py_DECREF(o);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6077 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6078 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6079 Py_DECREF(o);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6080
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6081 if (flag & DEF_PARAM) {
16539
a47d93ef103a Relax the rules for using 'from ... import *' and exec in the presence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16523
diff changeset
6082 if (PyList_Append(st->st_cur->ste_varnames, name) < 0)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6083 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6084 } else if (flag & DEF_GLOBAL) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6085 /* XXX need to update DEF_GLOBAL for other flags too;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6086 perhaps only DEF_FREE_GLOBAL */
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6087 if ((o = PyDict_GetItem(st->st_global, name))) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6088 val = PyInt_AS_LONG(o);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6089 val |= flag;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6090 } else
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6091 val = flag;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6092 o = PyInt_FromLong(val);
28955
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
6093 if (o == NULL)
1763d9d3fd9d Patch #708604: Check more function results. Will backport to 2.2.
Martin v. Löwis <martin@v.loewis.de>
parents: 28887
diff changeset
6094 return -1;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6095 if (PyDict_SetItem(st->st_global, name, o) < 0) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6096 Py_DECREF(o);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6097 return -1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6098 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6099 Py_DECREF(o);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6100 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6101 return 0;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6102 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6103
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6104 #define symtable_add_use(ST, NAME) symtable_add_def((ST), (NAME), USE)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6105
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
6106 /* Look for a yield stmt under n. Return 1 if found, else 0.
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
6107 This hack is used to look inside "if 0:" blocks (which are normally
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
6108 ignored) in case those are the only places a yield occurs (so that this
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
6109 function is a generator). */
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6110 static int
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6111 look_for_yield(node *n)
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6112 {
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6113 int i;
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6114
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6115 for (i = 0; i < NCH(n); ++i) {
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6116 node *kid = CHILD(n, i);
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6117
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6118 switch (TYPE(kid)) {
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6119
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6120 case classdef:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6121 case funcdef:
18333
41243cb4ea05 Another "if 0:" hack, this time to complain about otherwise invisible
Tim Peters <tim.peters@gmail.com>
parents: 18274
diff changeset
6122 case lambdef:
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6123 /* Stuff in nested functions and classes can't make
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6124 the parent a generator. */
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6125 return 0;
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6126
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6127 case yield_stmt:
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6128 return GENERATOR;
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6129
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6130 default:
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6131 if (look_for_yield(kid))
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6132 return GENERATOR;
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6133 }
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6134 }
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6135 return 0;
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6136 }
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6137
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6138 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6139 symtable_node(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6140 {
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6141 int i;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6142
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6143 loop:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6144 switch (TYPE(n)) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6145 case funcdef: {
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6146 char *func_name;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6147 if (NCH(n) == 6)
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6148 symtable_node(st, CHILD(n, 0));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6149 func_name = STR(RCHILD(n, -4));
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6150 symtable_add_def(st, func_name, DEF_LOCAL);
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6151 symtable_default_args(st, RCHILD(n, -3));
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
6152 symtable_enter_scope(st, func_name, TYPE(n), n->n_lineno);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6153 symtable_funcdef(st, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6154 symtable_exit_scope(st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6155 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6156 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6157 case lambdef:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6158 if (NCH(n) == 4)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6159 symtable_default_args(st, CHILD(n, 1));
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
6160 symtable_enter_scope(st, "lambda", TYPE(n), n->n_lineno);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6161 symtable_funcdef(st, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6162 symtable_exit_scope(st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6163 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6164 case classdef: {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6165 char *tmp, *class_name = STR(CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6166 symtable_add_def(st, class_name, DEF_LOCAL);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6167 if (TYPE(CHILD(n, 2)) == LPAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6168 node *bases = CHILD(n, 3);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6169 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6170 for (i = 0; i < NCH(bases); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6171 symtable_node(st, CHILD(bases, i));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6172 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6173 }
16421
aa844711463a Move a bunch of definitions that were internal to compile.c to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16408
diff changeset
6174 symtable_enter_scope(st, class_name, TYPE(n), n->n_lineno);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6175 tmp = st->st_private;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6176 st->st_private = class_name;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6177 symtable_node(st, CHILD(n, NCH(n) - 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6178 st->st_private = tmp;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6179 symtable_exit_scope(st);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6180 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6181 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6182 case if_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6183 for (i = 0; i + 3 < NCH(n); i += 4) {
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6184 if (is_constant_false(NULL, (CHILD(n, i + 1)))) {
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6185 if (st->st_cur->ste_generator == 0)
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6186 st->st_cur->ste_generator =
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6187 look_for_yield(CHILD(n, i+3));
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6188 continue;
18274
86dab0d43b73 SF bug #436207: "if 0: yield x" is ignored.
Tim Peters <tim.peters@gmail.com>
parents: 18243
diff changeset
6189 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6190 symtable_node(st, CHILD(n, i + 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6191 symtable_node(st, CHILD(n, i + 3));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6192 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6193 if (i + 2 < NCH(n))
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6194 symtable_node(st, CHILD(n, i + 2));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6195 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6196 case global_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6197 symtable_global(st, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6198 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6199 case import_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6200 symtable_import(st, n);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6201 break;
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
6202 case exec_stmt: {
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6203 st->st_cur->ste_optimized |= OPT_EXEC;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6204 symtable_node(st, CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6205 if (NCH(n) > 2)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6206 symtable_node(st, CHILD(n, 3));
17132
55218e0495bc Set the line number correctly for a nested function with an exec or
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17131
diff changeset
6207 else {
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6208 st->st_cur->ste_optimized |= OPT_BARE_EXEC;
17132
55218e0495bc Set the line number correctly for a nested function with an exec or
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17131
diff changeset
6209 st->st_cur->ste_opt_lineno = n->n_lineno;
55218e0495bc Set the line number correctly for a nested function with an exec or
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17131
diff changeset
6210 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6211 if (NCH(n) > 4)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6212 symtable_node(st, CHILD(n, 5));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6213 break;
16394
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
6214
f700e6b8de1f Undo recent change that banned using import to bind a global, as per
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16357
diff changeset
6215 }
16680
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6216 case assert_stmt:
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6217 if (Py_OptimizeFlag)
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6218 return;
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6219 if (NCH(n) == 2) {
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6220 n = CHILD(n, 1);
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6221 goto loop;
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6222 } else {
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6223 symtable_node(st, CHILD(n, 1));
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6224 n = CHILD(n, 3);
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6225 goto loop;
17ed61fd5b65 When running python -O, do not include blocks defined in asserts in
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16677
diff changeset
6226 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6227 case except_clause:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6228 if (NCH(n) == 4)
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6229 symtable_assign(st, CHILD(n, 3), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6230 if (NCH(n) > 1) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6231 n = CHILD(n, 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6232 goto loop;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6233 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6234 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6235 case del_stmt:
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6236 symtable_assign(st, CHILD(n, 1), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6237 break;
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
6238 case yield_stmt:
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
6239 st->st_cur->ste_generator = 1;
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
6240 n = CHILD(n, 1);
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 18065
diff changeset
6241 goto loop;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6242 case expr_stmt:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6243 if (NCH(n) == 1)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6244 n = CHILD(n, 0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6245 else {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6246 if (TYPE(CHILD(n, 1)) == augassign) {
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6247 symtable_assign(st, CHILD(n, 0), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6248 symtable_node(st, CHILD(n, 2));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6249 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6250 } else {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6251 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6252 for (i = 0; i < NCH(n) - 2; i += 2)
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6253 symtable_assign(st, CHILD(n, i), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6254 n = CHILD(n, NCH(n) - 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6255 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6256 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6257 goto loop;
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6258 case list_iter:
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6259 /* only occurs when there are multiple for loops
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6260 in a list comprehension */
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6261 n = CHILD(n, 0);
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6262 if (TYPE(n) == list_for)
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6263 symtable_list_for(st, n);
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6264 else {
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6265 REQ(n, list_if);
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6266 symtable_node(st, CHILD(n, 1));
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6267 if (NCH(n) == 3) {
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6268 n = CHILD(n, 2);
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6269 goto loop;
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6270 }
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6271 }
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6272 break;
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6273 case for_stmt:
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6274 symtable_assign(st, CHILD(n, 1), 0);
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6275 for (i = 3; i < NCH(n); ++i)
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6276 if (TYPE(CHILD(n, i)) >= single_input)
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6277 symtable_node(st, CHILD(n, i));
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6278 break;
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6279 case arglist:
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6280 if (NCH(n) > 1)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6281 for (i = 0; i < NCH(n); ++i) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6282 node *ch = CHILD(n, i);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6283 if (TYPE(ch) == argument && NCH(ch) == 2 &&
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6284 TYPE(CHILD(ch, 1)) == gen_for) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6285 PyErr_SetString(PyExc_SyntaxError,
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6286 "invalid syntax");
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6287 symtable_error(st, n->n_lineno);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6288 return;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6289 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6290 }
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6291 /* The remaining cases fall through to default except in
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6292 special circumstances. This requires the individual cases
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6293 to be coded with great care, even though they look like
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6294 rather innocuous. Each case must double-check TYPE(n).
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6295 */
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6296 case decorator:
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6297 if (TYPE(n) == decorator) {
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6298 /* decorator: '@' dotted_name [ '(' [arglist] ')' ] */
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6299 node *name, *varname;
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6300 name = CHILD(n, 1);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6301 REQ(name, dotted_name);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6302 varname = CHILD(name, 0);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6303 REQ(varname, NAME);
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6304 symtable_add_use(st, STR(varname));
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6305 }
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6306 /* fall through */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6307 case argument:
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6308 if (TYPE(n) == argument && NCH(n) == 3) {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6309 n = CHILD(n, 2);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6310 goto loop;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6311 }
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6312 else if (TYPE(n) == argument && NCH(n) == 2 &&
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6313 TYPE(CHILD(n, 1)) == gen_for) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6314 symtable_generator_expression(st, n);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6315 break;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6316 }
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6317 /* fall through */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6318 case listmaker:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6319 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6320 symtable_list_comprehension(st, n);
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6321 break;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6322 }
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6323 /* fall through */
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6324 case testlist_gexp:
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6325 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6326 symtable_generator_expression(st, n);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6327 break;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6328 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6329 /* fall through */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6330
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6331 case atom:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6332 if (TYPE(n) == atom && TYPE(CHILD(n, 0)) == NAME) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6333 symtable_add_use(st, STR(CHILD(n, 0)));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6334 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6335 }
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6336 /* fall through */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6337 default:
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6338 /* Walk over every non-token child with a special case
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6339 for one child.
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6340 */
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6341 if (NCH(n) == 1) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6342 n = CHILD(n, 0);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6343 goto loop;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6344 }
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6345 for (i = 0; i < NCH(n); ++i)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6346 if (TYPE(CHILD(n, i)) >= single_input)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6347 symtable_node(st, CHILD(n, i));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6348 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6349 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6350
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6351 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6352 symtable_funcdef(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6353 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6354 node *body;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6355
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6356 if (TYPE(n) == lambdef) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6357 if (NCH(n) == 4)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6358 symtable_params(st, CHILD(n, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6359 } else
32888
c7e03cf8333e PEP-0318, @decorator-style. In Guido's words:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 32723
diff changeset
6360 symtable_params(st, RCHILD(n, -3));
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6361 body = CHILD(n, NCH(n) - 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6362 symtable_node(st, body);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6363 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6364
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6365 /* The next two functions parse the argument tuple.
24858
592406e0c9b0 Add warnings for arguments named None. All set. (I could add a
Guido van Rossum <guido@python.org>
parents: 24856
diff changeset
6366 symtable_default_args() checks for names in the default arguments,
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6367 which are references in the defining scope. symtable_params()
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6368 parses the parameter names, which are defined in the function's
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6369 body.
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6370
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6371 varargslist:
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6372 (fpdef ['=' test] ',')* ('*' NAME [',' '**' NAME] | '**' NAME)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6373 | fpdef ['=' test] (',' fpdef ['=' test])* [',']
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6374 */
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6375
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6376 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6377 symtable_default_args(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6378 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6379 node *c;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6380 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6381
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6382 if (TYPE(n) == parameters) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6383 n = CHILD(n, 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6384 if (TYPE(n) == RPAR)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6385 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6386 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6387 REQ(n, varargslist);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6388 for (i = 0; i < NCH(n); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6389 c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6390 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6391 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6392 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6393 if (i > 0 && (TYPE(CHILD(n, i - 1)) == EQUAL))
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6394 symtable_node(st, CHILD(n, i));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6395 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6396 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6397
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6398 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6399 symtable_params(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6400 {
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6401 int i, complex = -1, ext = 0;
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
6402 node *c = NULL;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6403
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6404 if (TYPE(n) == parameters) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6405 n = CHILD(n, 1);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6406 if (TYPE(n) == RPAR)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6407 return;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6408 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6409 REQ(n, varargslist);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6410 for (i = 0; i < NCH(n); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6411 c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6412 if (TYPE(c) == STAR || TYPE(c) == DOUBLESTAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6413 ext = 1;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6414 break;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6415 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6416 if (TYPE(c) == test) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6417 continue;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6418 }
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
6419 if (TYPE(CHILD(c, 0)) == NAME)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6420 symtable_add_def(st, STR(CHILD(c, 0)), DEF_PARAM);
26756
ccf0b027b585 Oops. Roll back that last change. It wasn't ready for release. :-(
Guido van Rossum <guido@python.org>
parents: 26754
diff changeset
6421 else {
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6422 char nbuf[30];
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6423 PyOS_snprintf(nbuf, sizeof(nbuf), ".%d", i);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6424 symtable_add_def(st, nbuf, DEF_PARAM);
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6425 complex = i;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6426 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6427 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6428 if (ext) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6429 c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6430 if (TYPE(c) == STAR) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6431 i++;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6432 symtable_add_def(st, STR(CHILD(n, i)),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6433 DEF_PARAM | DEF_STAR);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6434 i += 2;
16137
883cb0e31bb2 prevent symtable_params() from dereferencing off the end of the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16096
diff changeset
6435 if (i >= NCH(n))
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6436 c = NULL;
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6437 else
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6438 c = CHILD(n, i);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6439 }
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6440 if (c && TYPE(c) == DOUBLESTAR) {
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6441 i++;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6442 symtable_add_def(st, STR(CHILD(n, i)),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6443 DEF_PARAM | DEF_DOUBLESTAR);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6444 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6445 }
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6446 if (complex >= 0) {
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6447 int j;
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6448 for (j = 0; j <= complex; j++) {
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6449 c = CHILD(n, j);
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6450 if (TYPE(c) == COMMA)
16699
7334b2cedda8 Fix for implicit tuple + default arguments, courtesy of Michael Hudson.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16680
diff changeset
6451 c = CHILD(n, ++j);
7334b2cedda8 Fix for implicit tuple + default arguments, courtesy of Michael Hudson.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16680
diff changeset
6452 else if (TYPE(c) == EQUAL)
7334b2cedda8 Fix for implicit tuple + default arguments, courtesy of Michael Hudson.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16680
diff changeset
6453 c = CHILD(n, j += 3);
16250
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6454 if (TYPE(CHILD(c, 0)) == LPAR)
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6455 symtable_params_fplist(st, CHILD(c, 1));
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6456 }
aab871635ffa Fix bug reported by Ka-Ping Yee: The compiler botched parsing function
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16138
diff changeset
6457 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6458 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6459
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6460 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6461 symtable_params_fplist(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6462 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6463 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6464 node *c;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6465
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6466 REQ(n, fplist);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6467 for (i = 0; i < NCH(n); i += 2) {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6468 c = CHILD(n, i);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6469 REQ(c, fpdef);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6470 if (NCH(c) == 1)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6471 symtable_add_def(st, STR(CHILD(c, 0)),
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6472 DEF_PARAM | DEF_INTUPLE);
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6473 else
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6474 symtable_params_fplist(st, CHILD(c, 1));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6475 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6476
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6477 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6478
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6479 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6480 symtable_global(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6481 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6482 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6483
16916
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
6484 /* XXX It might be helpful to warn about module-level global
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
6485 statements, but it's hard to tell the difference between
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
6486 module-level and a string passed to exec.
b0456d3a0f1f Useful future statement support for the interactive interpreter
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16892
diff changeset
6487 */
16871
d675d75af839 Warn about global statement at the module level.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16862
diff changeset
6488
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6489 for (i = 1; i < NCH(n); i += 2) {
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6490 char *name = STR(CHILD(n, i));
16783
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6491 int flags;
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6492
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6493 flags = symtable_lookup(st, name);
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6494 if (flags < 0)
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6495 continue;
16783
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6496 if (flags && flags != DEF_GLOBAL) {
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6497 char buf[500];
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6498 if (flags & DEF_PARAM) {
30713
9c7e0227c835 Patch #792869: Clarify error message for parameters declared global,
Martin v. Löwis <martin@v.loewis.de>
parents: 30683
diff changeset
6499 PyErr_Format(PyExc_SyntaxError, PARAM_GLOBAL,
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6500 name);
30488
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
6501 symtable_error(st, 0);
16783
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6502 return;
16853
05785bdf289c Use the new PyErr_WarnExplicit() API to issue better warnings for
Guido van Rossum <guido@python.org>
parents: 16838
diff changeset
6503 }
05785bdf289c Use the new PyErr_WarnExplicit() API to issue better warnings for
Guido van Rossum <guido@python.org>
parents: 16838
diff changeset
6504 else {
05785bdf289c Use the new PyErr_WarnExplicit() API to issue better warnings for
Guido van Rossum <guido@python.org>
parents: 16838
diff changeset
6505 if (flags & DEF_LOCAL)
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6506 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6507 GLOBAL_AFTER_ASSIGN,
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6508 name);
16853
05785bdf289c Use the new PyErr_WarnExplicit() API to issue better warnings for
Guido van Rossum <guido@python.org>
parents: 16838
diff changeset
6509 else
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6510 PyOS_snprintf(buf, sizeof(buf),
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6511 GLOBAL_AFTER_USE, name);
16855
ec9083ac26df Let's have some sanity. Introduce a helper to issue a symbol table
Guido van Rossum <guido@python.org>
parents: 16853
diff changeset
6512 symtable_warn(st, buf);
16783
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6513 }
4feea99be777 Add warnings about undefined "global"
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16781
diff changeset
6514 }
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6515 symtable_add_def(st, name, DEF_GLOBAL);
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6516 }
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6517 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6518
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6519 static void
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6520 symtable_list_comprehension(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6521 {
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6522 /* listmaker: test list_for */
21086
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6523 char tmpname[30];
ea5b0f0b716f code_repr(), com_addop_varname(), com_list_comprehension(),
Barry Warsaw <barry@python.org>
parents: 21066
diff changeset
6524
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6525 REQ(n, listmaker);
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6526 PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]",
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6527 ++st->st_cur->ste_tmpname);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6528 symtable_add_def(st, tmpname, DEF_LOCAL);
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6529 symtable_list_for(st, CHILD(n, 1));
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6530 symtable_node(st, CHILD(n, 0));
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6531 --st->st_cur->ste_tmpname;
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6532 }
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6533
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6534 static void
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6535 symtable_generator_expression(struct symtable *st, node *n)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6536 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6537 /* testlist_gexp: test gen_for */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6538 REQ(CHILD(n, 0), test);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6539 REQ(CHILD(n, 1), gen_for);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6540
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6541 symtable_enter_scope(st, "<genexpr>", TYPE(n), n->n_lineno);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6542 st->st_cur->ste_generator = GENERATOR_EXPRESSION;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6543
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6544 symtable_add_def(st, "[outmost-iterable]", DEF_PARAM);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6545
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6546 symtable_gen_for(st, CHILD(n, 1), 1);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6547 symtable_node(st, CHILD(n, 0));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6548 symtable_exit_scope(st);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6549
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6550 /* for outmost iterable precomputation */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6551 symtable_node(st, CHILD(CHILD(n, 1), 3));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6552 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6553
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6554 static void
29199
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6555 symtable_list_for(struct symtable *st, node *n)
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6556 {
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6557 REQ(n, list_for);
cd1c826c98a2 Fix for SF [ 734869 ] Lambda functions in list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29084
diff changeset
6558 /* list_for: for v in expr [list_iter] */
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6559 symtable_assign(st, CHILD(n, 1), 0);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6560 symtable_node(st, CHILD(n, 3));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6561 if (NCH(n) == 5)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6562 symtable_node(st, CHILD(n, 4));
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6563 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6564
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6565 static void
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6566 symtable_gen_for(struct symtable *st, node *n, int is_outmost)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6567 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6568 REQ(n, gen_for);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6569
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6570 /* gen_for: for v in test [gen_iter] */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6571 symtable_assign(st, CHILD(n, 1), 0);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6572 if (is_outmost)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6573 symtable_add_use(st, "[outmost-iterable]");
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6574 else
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6575 symtable_node(st, CHILD(n, 3));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6576
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6577 if (NCH(n) == 5)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6578 symtable_gen_iter(st, CHILD(n, 4));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6579 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6580
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6581 static void
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6582 symtable_gen_iter(struct symtable *st, node *n)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6583 {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6584 REQ(n, gen_iter);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6585
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6586 n = CHILD(n, 0);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6587 if (TYPE(n) == gen_for)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6588 symtable_gen_for(st, n, 0);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6589 else {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6590 REQ(n, gen_if);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6591 symtable_node(st, CHILD(n, 1));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6592
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6593 if (NCH(n) == 3)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6594 symtable_gen_iter(st, CHILD(n, 2));
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6595 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6596 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6597
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6598 static void
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6599 symtable_import(struct symtable *st, node *n)
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6600 {
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6601 node *nn;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6602 int i;
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6603 /* import_stmt: import_name | import_from */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6604 n = CHILD(n, 0);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6605 if (TYPE(n) == import_from) {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6606 /* import_from: 'from' dotted_name 'import' ('*' |
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6607 | '(' import_as_names ')' | import_as_names) */
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6608 node *dotname = CHILD(n, 1);
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6609 REQ(dotname, dotted_name);
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6610 if (strcmp(STR(CHILD(dotname, 0)), "__future__") == 0) {
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6611 /* check for bogus imports */
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6612 if (n->n_lineno >= st->st_future->ff_last_lineno) {
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6613 PyErr_SetString(PyExc_SyntaxError,
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6614 LATE_FUTURE);
30488
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
6615 symtable_error(st, n->n_lineno);
16823
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6616 return;
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6617 }
acbe74f3a786 Presumed correct compiler pass for future statements
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16799
diff changeset
6618 }
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6619 nn = CHILD(n, 3 + (TYPE(CHILD(n, 3)) == LPAR));
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6620 if (TYPE(nn) == STAR) {
19018
821e35706c07 Fix SF bug [ #445474 ] warn about import * inside functions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18631
diff changeset
6621 if (st->st_cur->ste_type != TYPE_MODULE) {
19021
e5754f048f0b Another bug fix for recent import * warning (caught by Thomas Wouters)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19019
diff changeset
6622 if (symtable_warn(st,
e5754f048f0b Another bug fix for recent import * warning (caught by Thomas Wouters)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19019
diff changeset
6623 "import * only allowed at module level") < 0)
e5754f048f0b Another bug fix for recent import * warning (caught by Thomas Wouters)
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 19019
diff changeset
6624 return;
19018
821e35706c07 Fix SF bug [ #445474 ] warn about import * inside functions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 18631
diff changeset
6625 }
16781
5a1230de436a Preliminary support for future nested scopes
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16763
diff changeset
6626 st->st_cur->ste_optimized |= OPT_IMPORT_STAR;
17132
55218e0495bc Set the line number correctly for a nested function with an exec or
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17131
diff changeset
6627 st->st_cur->ste_opt_lineno = n->n_lineno;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6628 } else {
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6629 REQ(nn, import_as_names);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6630 for (i = 0; i < NCH(nn); i += 2) {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6631 node *c = CHILD(nn, i);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6632 if (NCH(c) > 1) /* import as */
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6633 symtable_assign(st, CHILD(c, 2),
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6634 DEF_IMPORT);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6635 else
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6636 symtable_assign(st, CHILD(c, 0),
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6637 DEF_IMPORT);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6638 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6639 }
33353
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6640 } else {
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6641 /* 'import' dotted_as_names */
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6642 nn = CHILD(n, 1);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6643 REQ(nn, dotted_as_names);
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6644 for (i = 0; i < NCH(nn); i += 2)
f7474d31ba3f SF patch #1007189, multi-line imports, for instance:
Anthony Baxter <anthonybaxter@gmail.com>
parents: 33272
diff changeset
6645 symtable_assign(st, CHILD(nn, i), DEF_IMPORT);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6646 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6647 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6648
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6649 /* The third argument to symatble_assign() is a flag to be passed to
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6650 symtable_add_def() if it is eventually called. The flag is useful
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6651 to specify the particular type of assignment that should be
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6652 recorded, e.g. an assignment caused by import.
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6653 */
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6654
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6655 static void
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6656 symtable_assign(struct symtable *st, node *n, int def_flag)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6657 {
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6658 node *tmp;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6659 int i;
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6660
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6661 loop:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6662 switch (TYPE(n)) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6663 case lambdef:
16357
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6664 /* invalid assignment, e.g. lambda x:x=2. The next
9f2ca855fa44 Enforce two illegal import statements that were outlawed in the
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16353
diff changeset
6665 pass will catch this error. */
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6666 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6667 case power:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6668 if (NCH(n) > 2) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6669 for (i = 2; i < NCH(n); ++i)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6670 if (TYPE(CHILD(n, i)) != DOUBLESTAR)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6671 symtable_node(st, CHILD(n, i));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6672 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6673 if (NCH(n) > 1) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6674 symtable_node(st, CHILD(n, 0));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6675 symtable_node(st, CHILD(n, 1));
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6676 } else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6677 n = CHILD(n, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6678 goto loop;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6679 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6680 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6681 case listmaker:
17076
85d74f040712 Fix crashes in nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16933
diff changeset
6682 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == list_for) {
85d74f040712 Fix crashes in nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16933
diff changeset
6683 /* XXX This is an error, but the next pass
85d74f040712 Fix crashes in nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16933
diff changeset
6684 will catch it. */
85d74f040712 Fix crashes in nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16933
diff changeset
6685 return;
85d74f040712 Fix crashes in nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16933
diff changeset
6686 } else {
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6687 for (i = 0; i < NCH(n); i += 2)
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6688 symtable_assign(st, CHILD(n, i), def_flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6689 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6690 return;
32121
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6691 case testlist_gexp:
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6692 if (NCH(n) > 1 && TYPE(CHILD(n, 1)) == gen_for) {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6693 /* XXX This is an error, but the next pass
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6694 will catch it. */
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6695 return;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6696 } else {
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6697 for (i = 0; i < NCH(n); i += 2)
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6698 symtable_assign(st, CHILD(n, i), def_flag);
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6699 }
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6700 return;
302d399cfd37 SF patch #872326: Generator expression implementation
Raymond Hettinger <python@rcn.com>
parents: 31764
diff changeset
6701
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6702 case exprlist:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6703 case testlist:
23538
9e67a0d5705f Disambiguate the grammar for backtick.
Guido van Rossum <guido@python.org>
parents: 23280
diff changeset
6704 case testlist1:
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6705 if (NCH(n) == 1) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6706 n = CHILD(n, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6707 goto loop;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6708 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6709 else {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6710 int i;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6711 for (i = 0; i < NCH(n); i += 2)
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6712 symtable_assign(st, CHILD(n, i), def_flag);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6713 return;
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6714 }
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6715 case atom:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6716 tmp = CHILD(n, 0);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6717 if (TYPE(tmp) == LPAR || TYPE(tmp) == LSQB) {
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6718 n = CHILD(n, 1);
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6719 goto loop;
17185
f9a30015e8b0 Make it illegal to assign to __debug__ as per Guido's request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17132
diff changeset
6720 } else if (TYPE(tmp) == NAME) {
20939
13df8b6c2b80 Fix SF buf #480096: Assign to __debug__ still allowed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20867
diff changeset
6721 if (strcmp(STR(tmp), "__debug__") == 0) {
13df8b6c2b80 Fix SF buf #480096: Assign to __debug__ still allowed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20867
diff changeset
6722 PyErr_SetString(PyExc_SyntaxError,
13df8b6c2b80 Fix SF buf #480096: Assign to __debug__ still allowed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20867
diff changeset
6723 ASSIGN_DEBUG);
30488
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
6724 symtable_error(st, n->n_lineno);
b0c3d986f63f Fix SF bug [ 808594 ] leak on lambda with duplicate arguments error.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 30432
diff changeset
6725 return;
20939
13df8b6c2b80 Fix SF buf #480096: Assign to __debug__ still allowed
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20867
diff changeset
6726 }
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6727 symtable_add_def(st, STR(tmp), DEF_LOCAL | def_flag);
17185
f9a30015e8b0 Make it illegal to assign to __debug__ as per Guido's request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 17132
diff changeset
6728 }
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6729 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6730 case dotted_as_name:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6731 if (NCH(n) == 3)
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6732 symtable_add_def(st, STR(CHILD(n, 2)),
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6733 DEF_LOCAL | def_flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6734 else
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6735 symtable_add_def(st,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6736 STR(CHILD(CHILD(n,
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6737 0), 0)),
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6738 DEF_LOCAL | def_flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6739 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6740 case dotted_name:
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6741 symtable_add_def(st, STR(CHILD(n, 0)), DEF_LOCAL | def_flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6742 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6743 case NAME:
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6744 symtable_add_def(st, STR(n), DEF_LOCAL | def_flag);
16257
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6745 return;
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6746 default:
fd8c7203251f PEP 227 implementation
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16250
diff changeset
6747 if (NCH(n) == 0)
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6748 return;
16677
b955e74955d5 Tolerate ill-formed trees in symtable_assign(). Fixes SF bug 132510.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16658
diff changeset
6749 if (NCH(n) == 1) {
b955e74955d5 Tolerate ill-formed trees in symtable_assign(). Fixes SF bug 132510.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16658
diff changeset
6750 n = CHILD(n, 0);
b955e74955d5 Tolerate ill-formed trees in symtable_assign(). Fixes SF bug 132510.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16658
diff changeset
6751 goto loop;
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6752 }
16677
b955e74955d5 Tolerate ill-formed trees in symtable_assign(). Fixes SF bug 132510.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16658
diff changeset
6753 /* Should only occur for errors like x + 1 = 1,
b955e74955d5 Tolerate ill-formed trees in symtable_assign(). Fixes SF bug 132510.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16658
diff changeset
6754 which will be caught in the next pass. */
b955e74955d5 Tolerate ill-formed trees in symtable_assign(). Fixes SF bug 132510.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16658
diff changeset
6755 for (i = 0; i < NCH(n); ++i)
b955e74955d5 Tolerate ill-formed trees in symtable_assign(). Fixes SF bug 132510.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16658
diff changeset
6756 if (TYPE(CHILD(n, i)) >= single_input)
20517
d51fe520e5fd Fix for SF bug [ #471928 ] global made w/nested list comprehensions
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 20491
diff changeset
6757 symtable_assign(st, CHILD(n, i), def_flag);
16015
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6758 }
c832ef72499d This patch introduces an extra pass to the compiler that generates a
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16006
diff changeset
6759 }