You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

626 lines
20 KiB

Merged revisions 73941-73943,74076,74094,74186,74211-74214,74247,74254,74262,74311,74334,74368 via svnmerge from svn+ssh://svn.python.org/python/branches/py3k ................ r73941 | georg.brandl | 2009-07-11 12:39:00 +0200 (Sa, 11 Jul 2009) | 9 lines Merged revisions 73940 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r73940 | georg.brandl | 2009-07-11 12:37:38 +0200 (Sa, 11 Jul 2009) | 1 line #6430: add note about size of "u" type. ........ ................ r73942 | georg.brandl | 2009-07-11 12:39:23 +0200 (Sa, 11 Jul 2009) | 1 line #6430: remove mention of "w" array typecode. ................ r73943 | georg.brandl | 2009-07-11 12:43:08 +0200 (Sa, 11 Jul 2009) | 1 line #6421: The self argument of module-level PyCFunctions is now a reference to the module object. ................ r74076 | georg.brandl | 2009-07-18 11:07:48 +0200 (Sa, 18 Jul 2009) | 1 line #6502: add missing comma in docstring. ................ r74094 | georg.brandl | 2009-07-19 09:25:56 +0200 (So, 19 Jul 2009) | 10 lines Recorded merge of revisions 74089 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74089 | senthil.kumaran | 2009-07-19 04:43:43 +0200 (So, 19 Jul 2009) | 3 lines Fix for issue5102, timeout value propages between redirects, proxy, digest and auth handlers. Fixed tests to reflect the same. ........ ................ r74186 | georg.brandl | 2009-07-23 11:19:09 +0200 (Do, 23 Jul 2009) | 9 lines Recorded merge of revisions 74185 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74185 | georg.brandl | 2009-07-23 11:17:09 +0200 (Do, 23 Jul 2009) | 1 line Fix the "pylocals" gdb command. ........ ................ r74211 | georg.brandl | 2009-07-26 16:48:09 +0200 (So, 26 Jul 2009) | 9 lines Recorded merge of revisions 74210 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74210 | georg.brandl | 2009-07-26 16:44:23 +0200 (So, 26 Jul 2009) | 1 line Move member descriptions inside the classes. ........ ................ r74212 | georg.brandl | 2009-07-26 16:54:51 +0200 (So, 26 Jul 2009) | 9 lines Merged revisions 74209 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74209 | georg.brandl | 2009-07-26 16:37:28 +0200 (So, 26 Jul 2009) | 1 line builtin -> built-in. ........ ................ r74213 | georg.brandl | 2009-07-26 17:02:41 +0200 (So, 26 Jul 2009) | 9 lines Merged revisions 74207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74207 | georg.brandl | 2009-07-26 16:19:57 +0200 (So, 26 Jul 2009) | 1 line #6577: fix (hopefully) all links to builtin instead of module/class-specific objects. ........ ................ r74214 | georg.brandl | 2009-07-26 17:03:49 +0200 (So, 26 Jul 2009) | 9 lines Merged revisions 74205 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74205 | georg.brandl | 2009-07-26 15:36:39 +0200 (So, 26 Jul 2009) | 1 line #6576: fix cross-refs in re docs. ........ ................ r74247 | georg.brandl | 2009-07-29 09:27:08 +0200 (Mi, 29 Jul 2009) | 9 lines Merged revisions 74239 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74239 | georg.brandl | 2009-07-28 18:55:32 +0000 (Di, 28 Jul 2009) | 1 line Clarify quote_plus() usage. ........ ................ r74254 | georg.brandl | 2009-07-29 18:14:16 +0200 (Mi, 29 Jul 2009) | 1 line #6586: fix return/argument type doc for os.read() and os.write(). ................ r74262 | alexandre.vassalotti | 2009-07-29 21:54:39 +0200 (Mi, 29 Jul 2009) | 57 lines Merged revisions 74074,74077,74111,74188,74192-74193,74200,74252-74253,74258-74261 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74074 | georg.brandl | 2009-07-18 05:03:10 -0400 (Sat, 18 Jul 2009) | 1 line #6513: fix example code: warning categories are classes, not instances. ........ r74077 | georg.brandl | 2009-07-18 05:43:40 -0400 (Sat, 18 Jul 2009) | 1 line #6489: fix an ambiguity in getiterator() documentation. ........ r74111 | benjamin.peterson | 2009-07-20 09:30:10 -0400 (Mon, 20 Jul 2009) | 1 line remove docs for deprecated -p option ........ r74188 | benjamin.peterson | 2009-07-23 10:25:31 -0400 (Thu, 23 Jul 2009) | 1 line use bools ........ r74192 | georg.brandl | 2009-07-24 12:28:38 -0400 (Fri, 24 Jul 2009) | 1 line Fix arg types of et#. ........ r74193 | georg.brandl | 2009-07-24 12:46:38 -0400 (Fri, 24 Jul 2009) | 1 line Dont put "void" in signature for nullary functions. ........ r74200 | georg.brandl | 2009-07-25 09:02:15 -0400 (Sat, 25 Jul 2009) | 1 line #6571: add index entries for more operators. ........ r74252 | georg.brandl | 2009-07-29 12:06:31 -0400 (Wed, 29 Jul 2009) | 1 line #6593: fix link targets. ........ r74253 | georg.brandl | 2009-07-29 12:09:17 -0400 (Wed, 29 Jul 2009) | 1 line #6591: add reference to ioctl in fcntl module for platforms other than Windows. ........ r74258 | georg.brandl | 2009-07-29 12:57:05 -0400 (Wed, 29 Jul 2009) | 1 line Add a link to readline, and mention IPython and bpython. ........ r74259 | georg.brandl | 2009-07-29 13:07:21 -0400 (Wed, 29 Jul 2009) | 1 line Fix some markup and small factual glitches found by M. Markert. ........ r74260 | georg.brandl | 2009-07-29 13:15:20 -0400 (Wed, 29 Jul 2009) | 1 line Fix a few markup glitches. ........ r74261 | georg.brandl | 2009-07-29 13:50:25 -0400 (Wed, 29 Jul 2009) | 1 line Rewrite the section about classes a bit; mostly tidbits, and a larger update to the section about "private" variables to reflect the Pythonic consensus better. ........ ................ r74311 | georg.brandl | 2009-08-04 22:29:27 +0200 (Di, 04 Aug 2009) | 1 line Slightly improve buffer-related error message. ................ r74334 | georg.brandl | 2009-08-06 19:51:03 +0200 (Do, 06 Aug 2009) | 1 line #6648: mention surrogateescape handler where all standard handlers are listed. ................ r74368 | georg.brandl | 2009-08-13 09:56:35 +0200 (Do, 13 Aug 2009) | 21 lines Merged revisions 74328,74332-74333,74365 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r74328 | georg.brandl | 2009-08-06 17:06:25 +0200 (Do, 06 Aug 2009) | 1 line Fix base keyword arg name for int() and long(). ........ r74332 | georg.brandl | 2009-08-06 19:23:21 +0200 (Do, 06 Aug 2009) | 1 line Fix punctuation and one copy-paste error. ........ r74333 | georg.brandl | 2009-08-06 19:43:55 +0200 (Do, 06 Aug 2009) | 1 line #6658: fix two typos. ........ r74365 | georg.brandl | 2009-08-13 09:48:05 +0200 (Do, 13 Aug 2009) | 1 line #6679: Remove mention that sub supports no flags. ........ ................
17 years ago
Restructure comparison dramatically. There is no longer a default *ordering* between objects; there is only a default equality test (defined by an object being equal to itself only). Read the comment in object.c. The current implementation never uses a three-way comparison to compute a rich comparison, but it does use a rich comparison to compute a three-way comparison. I'm not quite done ripping out all the calls to PyObject_Compare/Cmp, or replacing tp_compare implementations with tp_richcompare implementations; but much of that has happened (to make most unit tests pass). The following tests still fail, because I need help deciding or understanding: test_codeop -- depends on comparing code objects test_datetime -- need Tim Peters' opinion test_marshal -- depends on comparing code objects test_mutants -- need help understanding it The problem with test_codeop and test_marshal is this: these tests compare two different code objects and expect them to be equal. Is that still a feature we'd like to support? I've temporarily removed the comparison and hash code from code objects, so they use the default (equality by pointer only) comparison. For the other two tests, run them to see for yourself. (There may be more failing test with "-u all".) A general problem with getting lots of these tests to pass is the reality that for object types that have a natural total ordering, implementing __cmp__ is much more convenient than implementing __eq__, __ne__, __lt__, and so on. Should we go back to allowing __cmp__ to provide a total ordering? Should we provide some other way to implement rich comparison with a single method override? Alex proposed a __key__() method; I've considered a __richcmp__() method. Or perhaps __cmp__() just shouldn't be killed off...
20 years ago
  1. #include "Python.h"
  2. #include "code.h"
  3. #include "structmember.h"
  4. #define NAME_CHARS \
  5. "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz"
  6. /* all_name_chars(s): true iff all chars in s are valid NAME_CHARS */
  7. static int
  8. all_name_chars(Py_UNICODE *s)
  9. {
  10. static char ok_name_char[256];
  11. static unsigned char *name_chars = (unsigned char *)NAME_CHARS;
  12. if (ok_name_char[*name_chars] == 0) {
  13. unsigned char *p;
  14. for (p = name_chars; *p; p++)
  15. ok_name_char[*p] = 1;
  16. }
  17. while (*s) {
  18. if (*s >= 128)
  19. return 0;
  20. if (ok_name_char[*s++] == 0)
  21. return 0;
  22. }
  23. return 1;
  24. }
  25. static void
  26. intern_strings(PyObject *tuple)
  27. {
  28. Py_ssize_t i;
  29. for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
  30. PyObject *v = PyTuple_GET_ITEM(tuple, i);
  31. if (v == NULL || !PyUnicode_CheckExact(v)) {
  32. Py_FatalError("non-string found in code slot");
  33. }
  34. PyUnicode_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
  35. }
  36. }
  37. PyCodeObject *
  38. PyCode_New(int argcount, int kwonlyargcount,
  39. int nlocals, int stacksize, int flags,
  40. PyObject *code, PyObject *consts, PyObject *names,
  41. PyObject *varnames, PyObject *freevars, PyObject *cellvars,
  42. PyObject *filename, PyObject *name, int firstlineno,
  43. PyObject *lnotab)
  44. {
  45. PyCodeObject *co;
  46. Py_ssize_t i;
  47. /* Check argument types */
  48. if (argcount < 0 || nlocals < 0 ||
  49. code == NULL ||
  50. consts == NULL || !PyTuple_Check(consts) ||
  51. names == NULL || !PyTuple_Check(names) ||
  52. varnames == NULL || !PyTuple_Check(varnames) ||
  53. freevars == NULL || !PyTuple_Check(freevars) ||
  54. cellvars == NULL || !PyTuple_Check(cellvars) ||
  55. name == NULL || !PyUnicode_Check(name) ||
  56. filename == NULL || !PyUnicode_Check(filename) ||
  57. lnotab == NULL || !PyBytes_Check(lnotab) ||
  58. !PyObject_CheckReadBuffer(code)) {
  59. PyErr_BadInternalCall();
  60. return NULL;
  61. }
  62. intern_strings(names);
  63. intern_strings(varnames);
  64. intern_strings(freevars);
  65. intern_strings(cellvars);
  66. /* Intern selected string constants */
  67. for (i = PyTuple_Size(consts); --i >= 0; ) {
  68. PyObject *v = PyTuple_GetItem(consts, i);
  69. if (!PyUnicode_Check(v))
  70. continue;
  71. if (!all_name_chars(PyUnicode_AS_UNICODE(v)))
  72. continue;
  73. PyUnicode_InternInPlace(&PyTuple_GET_ITEM(consts, i));
  74. }
  75. co = PyObject_NEW(PyCodeObject, &PyCode_Type);
  76. if (co != NULL) {
  77. co->co_argcount = argcount;
  78. co->co_kwonlyargcount = kwonlyargcount;
  79. co->co_nlocals = nlocals;
  80. co->co_stacksize = stacksize;
  81. co->co_flags = flags;
  82. Py_INCREF(code);
  83. co->co_code = code;
  84. Py_INCREF(consts);
  85. co->co_consts = consts;
  86. Py_INCREF(names);
  87. co->co_names = names;
  88. Py_INCREF(varnames);
  89. co->co_varnames = varnames;
  90. Py_INCREF(freevars);
  91. co->co_freevars = freevars;
  92. Py_INCREF(cellvars);
  93. co->co_cellvars = cellvars;
  94. Py_INCREF(filename);
  95. co->co_filename = filename;
  96. Py_INCREF(name);
  97. co->co_name = name;
  98. co->co_firstlineno = firstlineno;
  99. Py_INCREF(lnotab);
  100. co->co_lnotab = lnotab;
  101. co->co_zombieframe = NULL;
  102. }
  103. return co;
  104. }
  105. #define OFF(x) offsetof(PyCodeObject, x)
  106. static PyMemberDef code_memberlist[] = {
  107. {"co_argcount", T_INT, OFF(co_argcount), READONLY},
  108. {"co_kwonlyargcount", T_INT, OFF(co_kwonlyargcount), READONLY},
  109. {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
  110. {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
  111. {"co_flags", T_INT, OFF(co_flags), READONLY},
  112. {"co_code", T_OBJECT, OFF(co_code), READONLY},
  113. {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
  114. {"co_names", T_OBJECT, OFF(co_names), READONLY},
  115. {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
  116. {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
  117. {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
  118. {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
  119. {"co_name", T_OBJECT, OFF(co_name), READONLY},
  120. {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
  121. {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
  122. {NULL} /* Sentinel */
  123. };
  124. /* Helper for code_new: return a shallow copy of a tuple that is
  125. guaranteed to contain exact strings, by converting string subclasses
  126. to exact strings and complaining if a non-string is found. */
  127. static PyObject*
  128. validate_and_copy_tuple(PyObject *tup)
  129. {
  130. PyObject *newtuple;
  131. PyObject *item;
  132. Py_ssize_t i, len;
  133. len = PyTuple_GET_SIZE(tup);
  134. newtuple = PyTuple_New(len);
  135. if (newtuple == NULL)
  136. return NULL;
  137. for (i = 0; i < len; i++) {
  138. item = PyTuple_GET_ITEM(tup, i);
  139. if (PyUnicode_CheckExact(item)) {
  140. Py_INCREF(item);
  141. }
  142. else if (!PyUnicode_Check(item)) {
  143. PyErr_Format(
  144. PyExc_TypeError,
  145. "name tuples must contain only "
  146. "strings, not '%.500s'",
  147. item->ob_type->tp_name);
  148. Py_DECREF(newtuple);
  149. return NULL;
  150. }
  151. else {
  152. item = PyUnicode_FromUnicode(
  153. PyUnicode_AS_UNICODE(item),
  154. PyUnicode_GET_SIZE(item));
  155. if (item == NULL) {
  156. Py_DECREF(newtuple);
  157. return NULL;
  158. }
  159. }
  160. PyTuple_SET_ITEM(newtuple, i, item);
  161. }
  162. return newtuple;
  163. }
  164. PyDoc_STRVAR(code_doc,
  165. "code(argcount, kwonlyargcount, nlocals, stacksize, flags, codestring,\n\
  166. constants, names, varnames, filename, name, firstlineno,\n\
  167. lnotab[, freevars[, cellvars]])\n\
  168. \n\
  169. Create a code object. Not for the faint of heart.");
  170. static PyObject *
  171. code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
  172. {
  173. int argcount;
  174. int kwonlyargcount;
  175. int nlocals;
  176. int stacksize;
  177. int flags;
  178. PyObject *co = NULL;
  179. PyObject *code;
  180. PyObject *consts;
  181. PyObject *names, *ournames = NULL;
  182. PyObject *varnames, *ourvarnames = NULL;
  183. PyObject *freevars = NULL, *ourfreevars = NULL;
  184. PyObject *cellvars = NULL, *ourcellvars = NULL;
  185. PyObject *filename;
  186. PyObject *name;
  187. int firstlineno;
  188. PyObject *lnotab;
  189. if (!PyArg_ParseTuple(args, "iiiiiSO!O!O!UUiS|O!O!:code",
  190. &argcount, &kwonlyargcount,
  191. &nlocals, &stacksize, &flags,
  192. &code,
  193. &PyTuple_Type, &consts,
  194. &PyTuple_Type, &names,
  195. &PyTuple_Type, &varnames,
  196. &filename, &name,
  197. &firstlineno, &lnotab,
  198. &PyTuple_Type, &freevars,
  199. &PyTuple_Type, &cellvars))
  200. return NULL;
  201. if (argcount < 0) {
  202. PyErr_SetString(
  203. PyExc_ValueError,
  204. "code: argcount must not be negative");
  205. goto cleanup;
  206. }
  207. if (kwonlyargcount < 0) {
  208. PyErr_SetString(
  209. PyExc_ValueError,
  210. "code: kwonlyargcount must not be negative");
  211. goto cleanup;
  212. }
  213. if (nlocals < 0) {
  214. PyErr_SetString(
  215. PyExc_ValueError,
  216. "code: nlocals must not be negative");
  217. goto cleanup;
  218. }
  219. ournames = validate_and_copy_tuple(names);
  220. if (ournames == NULL)
  221. goto cleanup;
  222. ourvarnames = validate_and_copy_tuple(varnames);
  223. if (ourvarnames == NULL)
  224. goto cleanup;
  225. if (freevars)
  226. ourfreevars = validate_and_copy_tuple(freevars);
  227. else
  228. ourfreevars = PyTuple_New(0);
  229. if (ourfreevars == NULL)
  230. goto cleanup;
  231. if (cellvars)
  232. ourcellvars = validate_and_copy_tuple(cellvars);
  233. else
  234. ourcellvars = PyTuple_New(0);
  235. if (ourcellvars == NULL)
  236. goto cleanup;
  237. co = (PyObject *)PyCode_New(argcount, kwonlyargcount,
  238. nlocals, stacksize, flags,
  239. code, consts, ournames, ourvarnames,
  240. ourfreevars, ourcellvars, filename,
  241. name, firstlineno, lnotab);
  242. cleanup:
  243. Py_XDECREF(ournames);
  244. Py_XDECREF(ourvarnames);
  245. Py_XDECREF(ourfreevars);
  246. Py_XDECREF(ourcellvars);
  247. return co;
  248. }
  249. static void
  250. code_dealloc(PyCodeObject *co)
  251. {
  252. Py_XDECREF(co->co_code);
  253. Py_XDECREF(co->co_consts);
  254. Py_XDECREF(co->co_names);
  255. Py_XDECREF(co->co_varnames);
  256. Py_XDECREF(co->co_freevars);
  257. Py_XDECREF(co->co_cellvars);
  258. Py_XDECREF(co->co_filename);
  259. Py_XDECREF(co->co_name);
  260. Py_XDECREF(co->co_lnotab);
  261. if (co->co_zombieframe != NULL)
  262. PyObject_GC_Del(co->co_zombieframe);
  263. PyObject_DEL(co);
  264. }
  265. static PyObject *
  266. code_repr(PyCodeObject *co)
  267. {
  268. int lineno = -1;
  269. char *filename = "???";
  270. if (co->co_firstlineno != 0)
  271. lineno = co->co_firstlineno;
  272. if (co->co_filename && PyUnicode_Check(co->co_filename))
  273. filename = _PyUnicode_AsString(co->co_filename);
  274. return PyUnicode_FromFormat(
  275. "<code object %.100U at %p, file \"%.300s\", line %d>",
  276. co->co_name, co, filename, lineno);
  277. }
  278. static PyObject *
  279. code_richcompare(PyObject *self, PyObject *other, int op)
  280. {
  281. PyCodeObject *co, *cp;
  282. int eq;
  283. PyObject *res;
  284. if ((op != Py_EQ && op != Py_NE) ||
  285. !PyCode_Check(self) ||
  286. !PyCode_Check(other)) {
  287. Py_INCREF(Py_NotImplemented);
  288. return Py_NotImplemented;
  289. }
  290. co = (PyCodeObject *)self;
  291. cp = (PyCodeObject *)other;
  292. eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
  293. if (eq <= 0) goto unequal;
  294. eq = co->co_argcount == cp->co_argcount;
  295. if (!eq) goto unequal;
  296. eq = co->co_kwonlyargcount == cp->co_kwonlyargcount;
  297. if (!eq) goto unequal;
  298. eq = co->co_nlocals == cp->co_nlocals;
  299. if (!eq) goto unequal;
  300. eq = co->co_flags == cp->co_flags;
  301. if (!eq) goto unequal;
  302. eq = co->co_firstlineno == cp->co_firstlineno;
  303. if (!eq) goto unequal;
  304. eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
  305. if (eq <= 0) goto unequal;
  306. eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
  307. if (eq <= 0) goto unequal;
  308. eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
  309. if (eq <= 0) goto unequal;
  310. eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
  311. if (eq <= 0) goto unequal;
  312. eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
  313. if (eq <= 0) goto unequal;
  314. eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
  315. if (eq <= 0) goto unequal;
  316. if (op == Py_EQ)
  317. res = Py_True;
  318. else
  319. res = Py_False;
  320. goto done;
  321. unequal:
  322. if (eq < 0)
  323. return NULL;
  324. if (op == Py_NE)
  325. res = Py_True;
  326. else
  327. res = Py_False;
  328. done:
  329. Py_INCREF(res);
  330. return res;
  331. }
  332. static long
  333. code_hash(PyCodeObject *co)
  334. {
  335. long h, h0, h1, h2, h3, h4, h5, h6;
  336. h0 = PyObject_Hash(co->co_name);
  337. if (h0 == -1) return -1;
  338. h1 = PyObject_Hash(co->co_code);
  339. if (h1 == -1) return -1;
  340. h2 = PyObject_Hash(co->co_consts);
  341. if (h2 == -1) return -1;
  342. h3 = PyObject_Hash(co->co_names);
  343. if (h3 == -1) return -1;
  344. h4 = PyObject_Hash(co->co_varnames);
  345. if (h4 == -1) return -1;
  346. h5 = PyObject_Hash(co->co_freevars);
  347. if (h5 == -1) return -1;
  348. h6 = PyObject_Hash(co->co_cellvars);
  349. if (h6 == -1) return -1;
  350. h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
  351. co->co_argcount ^ co->co_kwonlyargcount ^
  352. co->co_nlocals ^ co->co_flags;
  353. if (h == -1) h = -2;
  354. return h;
  355. }
  356. /* XXX code objects need to participate in GC? */
  357. PyTypeObject PyCode_Type = {
  358. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  359. "code",
  360. sizeof(PyCodeObject),
  361. 0,
  362. (destructor)code_dealloc, /* tp_dealloc */
  363. 0, /* tp_print */
  364. 0, /* tp_getattr */
  365. 0, /* tp_setattr */
  366. 0, /* tp_reserved */
  367. (reprfunc)code_repr, /* tp_repr */
  368. 0, /* tp_as_number */
  369. 0, /* tp_as_sequence */
  370. 0, /* tp_as_mapping */
  371. (hashfunc)code_hash, /* tp_hash */
  372. 0, /* tp_call */
  373. 0, /* tp_str */
  374. PyObject_GenericGetAttr, /* tp_getattro */
  375. 0, /* tp_setattro */
  376. 0, /* tp_as_buffer */
  377. Py_TPFLAGS_DEFAULT, /* tp_flags */
  378. code_doc, /* tp_doc */
  379. 0, /* tp_traverse */
  380. 0, /* tp_clear */
  381. code_richcompare, /* tp_richcompare */
  382. 0, /* tp_weaklistoffset */
  383. 0, /* tp_iter */
  384. 0, /* tp_iternext */
  385. 0, /* tp_methods */
  386. code_memberlist, /* tp_members */
  387. 0, /* tp_getset */
  388. 0, /* tp_base */
  389. 0, /* tp_dict */
  390. 0, /* tp_descr_get */
  391. 0, /* tp_descr_set */
  392. 0, /* tp_dictoffset */
  393. 0, /* tp_init */
  394. 0, /* tp_alloc */
  395. code_new, /* tp_new */
  396. };
  397. /* All about c_lnotab.
  398. c_lnotab is an array of unsigned bytes disguised as a Python string. In -O
  399. mode, SET_LINENO opcodes aren't generated, and bytecode offsets are mapped
  400. to source code line #s (when needed for tracebacks) via c_lnotab instead.
  401. The array is conceptually a list of
  402. (bytecode offset increment, line number increment)
  403. pairs. The details are important and delicate, best illustrated by example:
  404. byte code offset source code line number
  405. 0 1
  406. 6 2
  407. 50 7
  408. 350 307
  409. 361 308
  410. The first trick is that these numbers aren't stored, only the increments
  411. from one row to the next (this doesn't really work, but it's a start):
  412. 0, 1, 6, 1, 44, 5, 300, 300, 11, 1
  413. The second trick is that an unsigned byte can't hold negative values, or
  414. values larger than 255, so (a) there's a deep assumption that byte code
  415. offsets and their corresponding line #s both increase monotonically, and (b)
  416. if at least one column jumps by more than 255 from one row to the next, more
  417. than one pair is written to the table. In case #b, there's no way to know
  418. from looking at the table later how many were written. That's the delicate
  419. part. A user of c_lnotab desiring to find the source line number
  420. corresponding to a bytecode address A should do something like this
  421. lineno = addr = 0
  422. for addr_incr, line_incr in c_lnotab:
  423. addr += addr_incr
  424. if addr > A:
  425. return lineno
  426. lineno += line_incr
  427. In order for this to work, when the addr field increments by more than 255,
  428. the line # increment in each pair generated must be 0 until the remaining addr
  429. increment is < 256. So, in the example above, com_set_lineno should not (as
  430. was actually done until 2.2) expand 300, 300 to 255, 255, 45, 45, but to
  431. 255, 0, 45, 255, 0, 45.
  432. */
  433. int
  434. PyCode_Addr2Line(PyCodeObject *co, int addrq)
  435. {
  436. int size = PyBytes_Size(co->co_lnotab) / 2;
  437. unsigned char *p = (unsigned char*)PyBytes_AsString(co->co_lnotab);
  438. int line = co->co_firstlineno;
  439. int addr = 0;
  440. while (--size >= 0) {
  441. addr += *p++;
  442. if (addr > addrq)
  443. break;
  444. line += *p++;
  445. }
  446. return line;
  447. }
  448. /*
  449. Check whether the current instruction is at the start of a line.
  450. */
  451. /* The theory of SET_LINENO-less tracing.
  452. In a nutshell, we use the co_lnotab field of the code object
  453. to tell when execution has moved onto a different line.
  454. As mentioned above, the basic idea is so set things up so
  455. that
  456. *instr_lb <= frame->f_lasti < *instr_ub
  457. is true so long as execution does not change lines.
  458. This is all fairly simple. Digging the information out of
  459. co_lnotab takes some work, but is conceptually clear.
  460. Somewhat harder to explain is why we don't *always* call the
  461. line trace function when the above test fails.
  462. Consider this code:
  463. 1: def f(a):
  464. 2: if a:
  465. 3: print 1
  466. 4: else:
  467. 5: print 2
  468. which compiles to this:
  469. 2 0 LOAD_FAST 0 (a)
  470. 3 JUMP_IF_FALSE 9 (to 15)
  471. 6 POP_TOP
  472. 3 7 LOAD_CONST 1 (1)
  473. 10 PRINT_ITEM
  474. 11 PRINT_NEWLINE
  475. 12 JUMP_FORWARD 6 (to 21)
  476. >> 15 POP_TOP
  477. 5 16 LOAD_CONST 2 (2)
  478. 19 PRINT_ITEM
  479. 20 PRINT_NEWLINE
  480. >> 21 LOAD_CONST 0 (None)
  481. 24 RETURN_VALUE
  482. If 'a' is false, execution will jump to instruction at offset
  483. 15 and the co_lnotab will claim that execution has moved to
  484. line 3. This is at best misleading. In this case we could
  485. associate the POP_TOP with line 4, but that doesn't make
  486. sense in all cases (I think).
  487. What we do is only call the line trace function if the co_lnotab
  488. indicates we have jumped to the *start* of a line, i.e. if the
  489. current instruction offset matches the offset given for the
  490. start of a line by the co_lnotab.
  491. This also takes care of the situation where 'a' is true.
  492. Execution will jump from instruction offset 12 to offset 21.
  493. Then the co_lnotab would imply that execution has moved to line
  494. 5, which is again misleading.
  495. Why do we set f_lineno when tracing? Well, consider the code
  496. above when 'a' is true. If stepping through this with 'n' in
  497. pdb, you would stop at line 1 with a "call" type event, then
  498. line events on lines 2 and 3, then a "return" type event -- but
  499. you would be shown line 5 during this event. This is a change
  500. from the behaviour in 2.2 and before, and I've found it
  501. confusing in practice. By setting and using f_lineno when
  502. tracing, one can report a line number different from that
  503. suggested by f_lasti on this one occasion where it's desirable.
  504. */
  505. int
  506. PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
  507. {
  508. int size, addr, line;
  509. unsigned char* p;
  510. p = (unsigned char*)PyBytes_AS_STRING(co->co_lnotab);
  511. size = PyBytes_GET_SIZE(co->co_lnotab) / 2;
  512. addr = 0;
  513. line = co->co_firstlineno;
  514. assert(line > 0);
  515. /* possible optimization: if f->f_lasti == instr_ub
  516. (likely to be a common case) then we already know
  517. instr_lb -- if we stored the matching value of p
  518. somwhere we could skip the first while loop. */
  519. /* see comments in compile.c for the description of
  520. co_lnotab. A point to remember: increments to p
  521. should come in pairs -- although we don't care about
  522. the line increments here, treating them as byte
  523. increments gets confusing, to say the least. */
  524. bounds->ap_lower = 0;
  525. while (size > 0) {
  526. if (addr + *p > lasti)
  527. break;
  528. addr += *p++;
  529. if (*p)
  530. bounds->ap_lower = addr;
  531. line += *p++;
  532. --size;
  533. }
  534. /* If lasti and addr don't match exactly, we don't want to
  535. change the lineno slot on the frame or execute a trace
  536. function. Return -1 instead.
  537. */
  538. if (addr != lasti)
  539. line = -1;
  540. if (size > 0) {
  541. while (--size >= 0) {
  542. addr += *p++;
  543. if (*p++)
  544. break;
  545. }
  546. bounds->ap_upper = addr;
  547. }
  548. else {
  549. bounds->ap_upper = INT_MAX;
  550. }
  551. return line;
  552. }