Mercurial > cpython-lru_cache-weakref
annotate Objects/codeobject.c @ 57685:2fdcd73c32aa
Merged revisions 76785 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r76785 | benjamin.peterson | 2009-12-13 08:18:08 -0600 (Sun, 13 Dec 2009) | 1 line
add 2.6.x point releases
........
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sun, 13 Dec 2009 14:21:46 +0000 |
| parents | 79718ee03986 |
| children | eea72b81a23c |
| rev | line source |
|---|---|
| 35742 | 1 #include "Python.h" |
| 2 #include "code.h" | |
| 3 #include "structmember.h" | |
| 4 | |
| 5 #define NAME_CHARS \ | |
| 6 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" | |
| 7 | |
| 8 /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */ | |
| 9 | |
| 10 static int | |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43672
diff
changeset
|
11 all_name_chars(Py_UNICODE *s) |
| 35742 | 12 { |
| 13 static char ok_name_char[256]; | |
| 14 static unsigned char *name_chars = (unsigned char *)NAME_CHARS; | |
| 15 | |
| 16 if (ok_name_char[*name_chars] == 0) { | |
| 17 unsigned char *p; | |
| 18 for (p = name_chars; *p; p++) | |
| 19 ok_name_char[*p] = 1; | |
| 20 } | |
| 21 while (*s) { | |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43672
diff
changeset
|
22 if (*s >= 128) |
|
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43672
diff
changeset
|
23 return 0; |
| 35742 | 24 if (ok_name_char[*s++] == 0) |
| 25 return 0; | |
| 26 } | |
| 27 return 1; | |
| 28 } | |
| 29 | |
| 30 static void | |
| 31 intern_strings(PyObject *tuple) | |
| 32 { | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
33 Py_ssize_t i; |
| 35742 | 34 |
| 35 for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) { | |
| 36 PyObject *v = PyTuple_GET_ITEM(tuple, i); | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
37 if (v == NULL || !PyUnicode_CheckExact(v)) { |
| 35742 | 38 Py_FatalError("non-string found in code slot"); |
| 39 } | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
40 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i)); |
| 35742 | 41 } |
| 42 } | |
| 43 | |
| 44 | |
| 45 PyCodeObject * | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
46 PyCode_New(int argcount, int kwonlyargcount, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
47 int nlocals, int stacksize, int flags, |
| 35742 | 48 PyObject *code, PyObject *consts, PyObject *names, |
| 49 PyObject *varnames, PyObject *freevars, PyObject *cellvars, | |
| 50 PyObject *filename, PyObject *name, int firstlineno, | |
| 51 PyObject *lnotab) | |
| 52 { | |
| 53 PyCodeObject *co; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
54 Py_ssize_t i; |
|
43569
19319e70fc3e
Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents:
42802
diff
changeset
|
55 |
| 35742 | 56 /* Check argument types */ |
|
55372
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
57 if (argcount < 0 || kwonlyargcount < 0 || nlocals < 0 || |
| 35742 | 58 code == NULL || |
| 59 consts == NULL || !PyTuple_Check(consts) || | |
| 60 names == NULL || !PyTuple_Check(names) || | |
| 61 varnames == NULL || !PyTuple_Check(varnames) || | |
| 62 freevars == NULL || !PyTuple_Check(freevars) || | |
| 63 cellvars == NULL || !PyTuple_Check(cellvars) || | |
|
43569
19319e70fc3e
Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents:
42802
diff
changeset
|
64 name == NULL || !PyUnicode_Check(name) || |
|
19319e70fc3e
Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents:
42802
diff
changeset
|
65 filename == NULL || !PyUnicode_Check(filename) || |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
44965
diff
changeset
|
66 lnotab == NULL || !PyBytes_Check(lnotab) || |
| 35742 | 67 !PyObject_CheckReadBuffer(code)) { |
| 68 PyErr_BadInternalCall(); | |
| 69 return NULL; | |
| 70 } | |
| 71 intern_strings(names); | |
| 72 intern_strings(varnames); | |
| 73 intern_strings(freevars); | |
| 74 intern_strings(cellvars); | |
| 75 /* Intern selected string constants */ | |
| 76 for (i = PyTuple_Size(consts); --i >= 0; ) { | |
| 77 PyObject *v = PyTuple_GetItem(consts, i); | |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43672
diff
changeset
|
78 if (!PyUnicode_Check(v)) |
| 35742 | 79 continue; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43672
diff
changeset
|
80 if (!all_name_chars(PyUnicode_AS_UNICODE(v))) |
| 35742 | 81 continue; |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43672
diff
changeset
|
82 PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i)); |
| 35742 | 83 } |
| 84 co = PyObject_NEW(PyCodeObject, &PyCode_Type); | |
| 85 if (co != NULL) { | |
| 86 co->co_argcount = argcount; | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
87 co->co_kwonlyargcount = kwonlyargcount; |
| 35742 | 88 co->co_nlocals = nlocals; |
| 89 co->co_stacksize = stacksize; | |
| 90 co->co_flags = flags; | |
| 91 Py_INCREF(code); | |
| 92 co->co_code = code; | |
| 93 Py_INCREF(consts); | |
| 94 co->co_consts = consts; | |
| 95 Py_INCREF(names); | |
| 96 co->co_names = names; | |
| 97 Py_INCREF(varnames); | |
| 98 co->co_varnames = varnames; | |
| 99 Py_INCREF(freevars); | |
| 100 co->co_freevars = freevars; | |
| 101 Py_INCREF(cellvars); | |
| 102 co->co_cellvars = cellvars; | |
| 103 Py_INCREF(filename); | |
| 104 co->co_filename = filename; | |
| 105 Py_INCREF(name); | |
| 106 co->co_name = name; | |
| 107 co->co_firstlineno = firstlineno; | |
| 108 Py_INCREF(lnotab); | |
| 109 co->co_lnotab = lnotab; | |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
110 co->co_zombieframe = NULL; |
| 35742 | 111 } |
| 112 return co; | |
| 113 } | |
| 114 | |
|
55372
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
115 PyCodeObject * |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
116 PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno) |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
117 { |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
118 static PyObject *emptystring = NULL; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
119 static PyObject *nulltuple = NULL; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
120 PyObject *filename_ob = NULL; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
121 PyObject *funcname_ob = NULL; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
122 PyCodeObject *result = NULL; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
123 if (emptystring == NULL) { |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
124 emptystring = PyBytes_FromString(""); |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
125 if (emptystring == NULL) |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
126 goto failed; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
127 } |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
128 if (nulltuple == NULL) { |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
129 nulltuple = PyTuple_New(0); |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
130 if (nulltuple == NULL) |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
131 goto failed; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
132 } |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
133 funcname_ob = PyUnicode_FromString(funcname); |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
134 if (funcname_ob == NULL) |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
135 goto failed; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
136 filename_ob = PyUnicode_DecodeFSDefault(filename); |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
137 if (filename_ob == NULL) |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
138 goto failed; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
139 |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
140 result = PyCode_New(0, /* argcount */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
141 0, /* kwonlyargcount */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
142 0, /* nlocals */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
143 0, /* stacksize */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
144 0, /* flags */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
145 emptystring, /* code */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
146 nulltuple, /* consts */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
147 nulltuple, /* names */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
148 nulltuple, /* varnames */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
149 nulltuple, /* freevars */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
150 nulltuple, /* cellvars */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
151 filename_ob, /* filename */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
152 funcname_ob, /* name */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
153 firstlineno, /* firstlineno */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
154 emptystring /* lnotab */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
155 ); |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
156 |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
157 failed: |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
158 Py_XDECREF(funcname_ob); |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
159 Py_XDECREF(filename_ob); |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
160 return result; |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
161 } |
| 35742 | 162 |
| 163 #define OFF(x) offsetof(PyCodeObject, x) | |
| 164 | |
| 165 static PyMemberDef code_memberlist[] = { | |
| 166 {"co_argcount", T_INT, OFF(co_argcount), READONLY}, | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
167 {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY}, |
| 35742 | 168 {"co_nlocals", T_INT, OFF(co_nlocals), READONLY}, |
| 169 {"co_stacksize",T_INT, OFF(co_stacksize), READONLY}, | |
| 170 {"co_flags", T_INT, OFF(co_flags), READONLY}, | |
| 171 {"co_code", T_OBJECT, OFF(co_code), READONLY}, | |
| 172 {"co_consts", T_OBJECT, OFF(co_consts), READONLY}, | |
| 173 {"co_names", T_OBJECT, OFF(co_names), READONLY}, | |
| 174 {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY}, | |
| 175 {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY}, | |
| 176 {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY}, | |
| 177 {"co_filename", T_OBJECT, OFF(co_filename), READONLY}, | |
| 178 {"co_name", T_OBJECT, OFF(co_name), READONLY}, | |
| 179 {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY}, | |
| 180 {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY}, | |
| 181 {NULL} /* Sentinel */ | |
| 182 }; | |
| 183 | |
| 184 /* Helper for code_new: return a shallow copy of a tuple that is | |
| 185 guaranteed to contain exact strings, by converting string subclasses | |
| 186 to exact strings and complaining if a non-string is found. */ | |
| 187 static PyObject* | |
| 188 validate_and_copy_tuple(PyObject *tup) | |
| 189 { | |
| 190 PyObject *newtuple; | |
| 191 PyObject *item; | |
|
36501
7fdc639bc5b4
Merge ssize_t branch.
Martin v. Löwis <martin@v.loewis.de>
parents:
35742
diff
changeset
|
192 Py_ssize_t i, len; |
| 35742 | 193 |
| 194 len = PyTuple_GET_SIZE(tup); | |
| 195 newtuple = PyTuple_New(len); | |
| 196 if (newtuple == NULL) | |
| 197 return NULL; | |
| 198 | |
| 199 for (i = 0; i < len; i++) { | |
| 200 item = PyTuple_GET_ITEM(tup, i); | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
201 if (PyUnicode_CheckExact(item)) { |
| 35742 | 202 Py_INCREF(item); |
| 203 } | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
204 else if (!PyUnicode_Check(item)) { |
| 35742 | 205 PyErr_Format( |
| 206 PyExc_TypeError, | |
| 207 "name tuples must contain only " | |
| 208 "strings, not '%.500s'", | |
| 209 item->ob_type->tp_name); | |
| 210 Py_DECREF(newtuple); | |
| 211 return NULL; | |
| 212 } | |
| 213 else { | |
|
42013
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
214 item = PyUnicode_FromUnicode( |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
215 PyUnicode_AS_UNICODE(item), |
|
cd125fe83051
Make identifiers str (not str8) objects throughout.
Martin v. Löwis <martin@v.loewis.de>
parents:
41962
diff
changeset
|
216 PyUnicode_GET_SIZE(item)); |
| 35742 | 217 if (item == NULL) { |
| 218 Py_DECREF(newtuple); | |
| 219 return NULL; | |
| 220 } | |
| 221 } | |
| 222 PyTuple_SET_ITEM(newtuple, i, item); | |
| 223 } | |
| 224 | |
| 225 return newtuple; | |
| 226 } | |
| 227 | |
| 228 PyDoc_STRVAR(code_doc, | |
|
55580
7b495c8f2a24
Merged revisions 73941-73943,74076,74094,74186,74211-74214,74247,74254,74262,74311,74334,74368 via svnmerge from
Georg Brandl <georg@python.org>
parents:
51271
diff
changeset
|
229 "code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\ |
|
44965
b70485011214
#1939: update code object docstring.
Georg Brandl <georg@python.org>
parents:
43777
diff
changeset
|
230 constants, names, varnames, filename, name, firstlineno,\n\ |
|
b70485011214
#1939: update code object docstring.
Georg Brandl <georg@python.org>
parents:
43777
diff
changeset
|
231 lnotab[, freevars[, cellvars]])\n\ |
| 35742 | 232 \n\ |
| 233 Create a code object. Not for the faint of heart."); | |
| 234 | |
| 235 static PyObject * | |
| 236 code_new(PyTypeObject *type, PyObject *args, PyObject *kw) | |
| 237 { | |
| 238 int argcount; | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
239 int kwonlyargcount; |
| 35742 | 240 int nlocals; |
| 241 int stacksize; | |
| 242 int flags; | |
| 243 PyObject *co = NULL; | |
| 244 PyObject *code; | |
| 245 PyObject *consts; | |
| 246 PyObject *names, *ournames = NULL; | |
| 247 PyObject *varnames, *ourvarnames = NULL; | |
| 248 PyObject *freevars = NULL, *ourfreevars = NULL; | |
| 249 PyObject *cellvars = NULL, *ourcellvars = NULL; | |
| 250 PyObject *filename; | |
| 251 PyObject *name; | |
| 252 int firstlineno; | |
| 253 PyObject *lnotab; | |
| 254 | |
|
43777
0d462d789b18
Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents:
43672
diff
changeset
|
255 if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code", |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
256 &argcount, &kwonlyargcount, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
257 &nlocals, &stacksize, &flags, |
| 35742 | 258 &code, |
| 259 &PyTuple_Type, &consts, | |
| 260 &PyTuple_Type, &names, | |
| 261 &PyTuple_Type, &varnames, | |
| 262 &filename, &name, | |
| 263 &firstlineno, &lnotab, | |
| 264 &PyTuple_Type, &freevars, | |
| 265 &PyTuple_Type, &cellvars)) | |
| 266 return NULL; | |
| 267 | |
| 268 if (argcount < 0) { | |
| 269 PyErr_SetString( | |
| 270 PyExc_ValueError, | |
| 271 "code: argcount must not be negative"); | |
| 272 goto cleanup; | |
| 273 } | |
| 274 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
275 if (kwonlyargcount < 0) { |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
276 PyErr_SetString( |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
277 PyExc_ValueError, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
278 "code: kwonlyargcount must not be negative"); |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
279 goto cleanup; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
280 } |
| 35742 | 281 if (nlocals < 0) { |
| 282 PyErr_SetString( | |
| 283 PyExc_ValueError, | |
| 284 "code: nlocals must not be negative"); | |
| 285 goto cleanup; | |
| 286 } | |
| 287 | |
| 288 ournames = validate_and_copy_tuple(names); | |
| 289 if (ournames == NULL) | |
| 290 goto cleanup; | |
| 291 ourvarnames = validate_and_copy_tuple(varnames); | |
| 292 if (ourvarnames == NULL) | |
| 293 goto cleanup; | |
| 294 if (freevars) | |
| 295 ourfreevars = validate_and_copy_tuple(freevars); | |
| 296 else | |
| 297 ourfreevars = PyTuple_New(0); | |
| 298 if (ourfreevars == NULL) | |
| 299 goto cleanup; | |
| 300 if (cellvars) | |
| 301 ourcellvars = validate_and_copy_tuple(cellvars); | |
| 302 else | |
| 303 ourcellvars = PyTuple_New(0); | |
| 304 if (ourcellvars == NULL) | |
| 305 goto cleanup; | |
| 306 | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
307 co = (PyObject *)PyCode_New(argcount, kwonlyargcount, |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
308 nlocals, stacksize, flags, |
| 35742 | 309 code, consts, ournames, ourvarnames, |
| 310 ourfreevars, ourcellvars, filename, | |
| 311 name, firstlineno, lnotab); | |
| 312 cleanup: | |
| 313 Py_XDECREF(ournames); | |
| 314 Py_XDECREF(ourvarnames); | |
| 315 Py_XDECREF(ourfreevars); | |
| 316 Py_XDECREF(ourcellvars); | |
| 317 return co; | |
| 318 } | |
| 319 | |
| 320 static void | |
| 321 code_dealloc(PyCodeObject *co) | |
| 322 { | |
| 323 Py_XDECREF(co->co_code); | |
| 324 Py_XDECREF(co->co_consts); | |
| 325 Py_XDECREF(co->co_names); | |
| 326 Py_XDECREF(co->co_varnames); | |
| 327 Py_XDECREF(co->co_freevars); | |
| 328 Py_XDECREF(co->co_cellvars); | |
| 329 Py_XDECREF(co->co_filename); | |
| 330 Py_XDECREF(co->co_name); | |
| 331 Py_XDECREF(co->co_lnotab); | |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
332 if (co->co_zombieframe != NULL) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
333 PyObject_GC_Del(co->co_zombieframe); |
| 35742 | 334 PyObject_DEL(co); |
| 335 } | |
| 336 | |
| 337 static PyObject * | |
| 338 code_repr(PyCodeObject *co) | |
| 339 { | |
| 340 int lineno = -1; | |
| 341 char *filename = "???"; | |
| 342 | |
| 343 if (co->co_firstlineno != 0) | |
| 344 lineno = co->co_firstlineno; | |
|
43569
19319e70fc3e
Patch #1272, by Christian Heimes and Alexandre Vassalotti.
Guido van Rossum <guido@python.org>
parents:
42802
diff
changeset
|
345 if (co->co_filename && PyUnicode_Check(co->co_filename)) |
|
48660
85b67fb5672b
Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents:
47301
diff
changeset
|
346 filename = _PyUnicode_AsString(co->co_filename); |
|
41962
11930dcf9d4f
Use PyUnicode_FromFormat() directly.
Walter Dörwald <walter@livinglogic.de>
parents:
41769
diff
changeset
|
347 return PyUnicode_FromFormat( |
|
42802
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
348 "<code object %.100U at %p, file \"%.300s\", line %d>", |
|
01ee6311ba46
Ensure that code object names (co_name) are unicode.
Neal Norwitz <nnorwitz@gmail.com>
parents:
42334
diff
changeset
|
349 co->co_name, co, filename, lineno); |
| 35742 | 350 } |
| 351 | |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
352 static PyObject * |
|
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
353 code_richcompare(PyObject *self, PyObject *other, int op) |
| 35742 | 354 { |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
355 PyCodeObject *co, *cp; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
356 int eq; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
357 PyObject *res; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
358 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
359 if ((op != Py_EQ && op != Py_NE) || |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
360 !PyCode_Check(self) || |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
361 !PyCode_Check(other)) { |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
362 Py_INCREF(Py_NotImplemented); |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
363 return Py_NotImplemented; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
364 } |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
365 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
366 co = (PyCodeObject *)self; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
367 cp = (PyCodeObject *)other; |
|
39644
13d5e5f0e993
Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents:
39442
diff
changeset
|
368 |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
369 eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
370 if (eq <= 0) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
371 eq = co->co_argcount == cp->co_argcount; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
372 if (!eq) goto unequal; |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
373 eq = co->co_kwonlyargcount == cp->co_kwonlyargcount; |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
374 if (!eq) goto unequal; |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
375 eq = co->co_nlocals == cp->co_nlocals; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
376 if (!eq) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
377 eq = co->co_flags == cp->co_flags; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
378 if (!eq) goto unequal; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
379 eq = co->co_firstlineno == cp->co_firstlineno; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
380 if (!eq) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
381 eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
382 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
383 eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
384 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
385 eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
386 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
387 eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
388 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
389 eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
390 if (eq <= 0) goto unequal; |
|
39658
a5048c8d8f33
Fix a bunch of compiler warnings. In at least one case these were serious bugs!
Guido van Rossum <guido@python.org>
parents:
39650
diff
changeset
|
391 eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ); |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
392 if (eq <= 0) goto unequal; |
| 35742 | 393 |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
394 if (op == Py_EQ) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
395 res = Py_True; |
| 35742 | 396 else |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
397 res = Py_False; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
398 goto done; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
399 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
400 unequal: |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
401 if (eq < 0) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
402 return NULL; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
403 if (op == Py_NE) |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
404 res = Py_True; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
405 else |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
406 res = Py_False; |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
407 |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
408 done: |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
409 Py_INCREF(res); |
|
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
410 return res; |
| 35742 | 411 } |
| 412 | |
| 413 static long | |
| 414 code_hash(PyCodeObject *co) | |
| 415 { | |
| 416 long h, h0, h1, h2, h3, h4, h5, h6; | |
| 417 h0 = PyObject_Hash(co->co_name); | |
| 418 if (h0 == -1) return -1; | |
| 419 h1 = PyObject_Hash(co->co_code); | |
| 420 if (h1 == -1) return -1; | |
| 421 h2 = PyObject_Hash(co->co_consts); | |
| 422 if (h2 == -1) return -1; | |
| 423 h3 = PyObject_Hash(co->co_names); | |
| 424 if (h3 == -1) return -1; | |
| 425 h4 = PyObject_Hash(co->co_varnames); | |
| 426 if (h4 == -1) return -1; | |
| 427 h5 = PyObject_Hash(co->co_freevars); | |
| 428 if (h5 == -1) return -1; | |
| 429 h6 = PyObject_Hash(co->co_cellvars); | |
| 430 if (h6 == -1) return -1; | |
| 431 h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^ | |
|
40215
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
432 co->co_argcount ^ co->co_kwonlyargcount ^ |
|
1cce0526d9b0
Jiwon Seo's PEP 3102 implementation.
Guido van Rossum <guido@python.org>
parents:
39658
diff
changeset
|
433 co->co_nlocals ^ co->co_flags; |
| 35742 | 434 if (h == -1) h = -2; |
| 435 return h; | |
| 436 } | |
| 437 | |
| 438 /* XXX code objects need to participate in GC? */ | |
| 439 | |
| 440 PyTypeObject PyCode_Type = { | |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42013
diff
changeset
|
441 PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 35742 | 442 "code", |
| 443 sizeof(PyCodeObject), | |
| 444 0, | |
| 445 (destructor)code_dealloc, /* tp_dealloc */ | |
| 446 0, /* tp_print */ | |
| 447 0, /* tp_getattr */ | |
| 448 0, /* tp_setattr */ | |
|
51271
87fbbf22adf3
Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48660
diff
changeset
|
449 0, /* tp_reserved */ |
| 35742 | 450 (reprfunc)code_repr, /* tp_repr */ |
| 451 0, /* tp_as_number */ | |
| 452 0, /* tp_as_sequence */ | |
| 453 0, /* tp_as_mapping */ | |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
454 (hashfunc)code_hash, /* tp_hash */ |
| 35742 | 455 0, /* tp_call */ |
| 456 0, /* tp_str */ | |
| 457 PyObject_GenericGetAttr, /* tp_getattro */ | |
| 458 0, /* tp_setattro */ | |
| 459 0, /* tp_as_buffer */ | |
| 460 Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
| 461 code_doc, /* tp_doc */ | |
| 462 0, /* tp_traverse */ | |
| 463 0, /* tp_clear */ | |
|
39650
69f61f90c4fc
Implement == and != comparisons for code objects by value.
Guido van Rossum <guido@python.org>
parents:
39644
diff
changeset
|
464 code_richcompare, /* tp_richcompare */ |
| 35742 | 465 0, /* tp_weaklistoffset */ |
| 466 0, /* tp_iter */ | |
| 467 0, /* tp_iternext */ | |
| 468 0, /* tp_methods */ | |
| 469 code_memberlist, /* tp_members */ | |
| 470 0, /* tp_getset */ | |
| 471 0, /* tp_base */ | |
| 472 0, /* tp_dict */ | |
| 473 0, /* tp_descr_get */ | |
| 474 0, /* tp_descr_set */ | |
| 475 0, /* tp_dictoffset */ | |
| 476 0, /* tp_init */ | |
| 477 0, /* tp_alloc */ | |
| 478 code_new, /* tp_new */ | |
| 479 }; | |
| 480 | |
|
55372
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
481 /* Use co_lnotab to compute the line number from a bytecode index, addrq. See |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
482 lnotab_notes.txt for the details of the lnotab representation. |
| 35742 | 483 */ |
| 484 | |
| 485 int | |
| 486 PyCode_Addr2Line(PyCodeObject *co, int addrq) | |
| 487 { | |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
44965
diff
changeset
|
488 int size = PyBytes_Size(co->co_lnotab) / 2; |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
44965
diff
changeset
|
489 unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab); |
| 35742 | 490 int line = co->co_firstlineno; |
| 491 int addr = 0; | |
| 492 while (--size >= 0) { | |
| 493 addr += *p++; | |
| 494 if (addr > addrq) | |
| 495 break; | |
| 496 line += *p++; | |
| 497 } | |
| 498 return line; | |
| 499 } | |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
500 |
|
55372
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
501 /* Update *bounds to describe the first and one-past-the-last instructions in |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
502 the same line as lasti. Return the number of that line. */ |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
503 int |
|
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
504 _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds) |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
505 { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
506 int size, addr, line; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
507 unsigned char* p; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
508 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
44965
diff
changeset
|
509 p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab); |
|
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
44965
diff
changeset
|
510 size = PyBytes_GET_SIZE(co->co_lnotab) / 2; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
511 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
512 addr = 0; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
513 line = co->co_firstlineno; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
514 assert(line > 0); |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
515 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
516 /* possible optimization: if f->f_lasti == instr_ub |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
517 (likely to be a common case) then we already know |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
518 instr_lb -- if we stored the matching value of p |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
519 somwhere we could skip the first while loop. */ |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
520 |
|
55372
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
521 /* See lnotab_notes.txt for the description of |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
522 co_lnotab. A point to remember: increments to p |
|
55372
79718ee03986
Merged revisions 72487-72488,72879 via svnmerge from
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
55580
diff
changeset
|
523 come in (addr, line) pairs. */ |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
524 |
|
39442
654c380cf8b9
Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
525 bounds->ap_lower = 0; |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
526 while (size > 0) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
527 if (addr + *p > lasti) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
528 break; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
529 addr += *p++; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
530 if (*p) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
531 bounds->ap_lower = addr; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
532 line += *p++; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
533 --size; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
534 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
535 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
536 if (size > 0) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
537 while (--size >= 0) { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
538 addr += *p++; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
539 if (*p++) |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
540 break; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
541 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
542 bounds->ap_upper = addr; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
543 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
544 else { |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
545 bounds->ap_upper = INT_MAX; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
546 } |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
547 |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
548 return line; |
|
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
36501
diff
changeset
|
549 } |
