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.

581 lines
18 KiB

  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(unsigned char *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 (ok_name_char[*s++] == 0)
  19. return 0;
  20. }
  21. return 1;
  22. }
  23. static void
  24. intern_strings(PyObject *tuple)
  25. {
  26. Py_ssize_t i;
  27. for (i = PyTuple_GET_SIZE(tuple); --i >= 0; ) {
  28. PyObject *v = PyTuple_GET_ITEM(tuple, i);
  29. if (v == NULL || !PyString_CheckExact(v)) {
  30. Py_FatalError("non-string found in code slot");
  31. }
  32. PyString_InternInPlace(&PyTuple_GET_ITEM(tuple, i));
  33. }
  34. }
  35. PyCodeObject *
  36. PyCode_New(int argcount, int nlocals, int stacksize, int flags,
  37. PyObject *code, PyObject *consts, PyObject *names,
  38. PyObject *varnames, PyObject *freevars, PyObject *cellvars,
  39. PyObject *filename, PyObject *name, int firstlineno,
  40. PyObject *lnotab)
  41. {
  42. PyCodeObject *co;
  43. Py_ssize_t i;
  44. /* Check argument types */
  45. if (argcount < 0 || nlocals < 0 ||
  46. code == NULL ||
  47. consts == NULL || !PyTuple_Check(consts) ||
  48. names == NULL || !PyTuple_Check(names) ||
  49. varnames == NULL || !PyTuple_Check(varnames) ||
  50. freevars == NULL || !PyTuple_Check(freevars) ||
  51. cellvars == NULL || !PyTuple_Check(cellvars) ||
  52. name == NULL || !PyString_Check(name) ||
  53. filename == NULL || !PyString_Check(filename) ||
  54. lnotab == NULL || !PyString_Check(lnotab) ||
  55. !PyObject_CheckReadBuffer(code)) {
  56. PyErr_BadInternalCall();
  57. return NULL;
  58. }
  59. intern_strings(names);
  60. intern_strings(varnames);
  61. intern_strings(freevars);
  62. intern_strings(cellvars);
  63. /* Intern selected string constants */
  64. for (i = PyTuple_Size(consts); --i >= 0; ) {
  65. PyObject *v = PyTuple_GetItem(consts, i);
  66. if (!PyString_Check(v))
  67. continue;
  68. if (!all_name_chars((unsigned char *)PyString_AS_STRING(v)))
  69. continue;
  70. PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i));
  71. }
  72. co = PyObject_NEW(PyCodeObject, &PyCode_Type);
  73. if (co != NULL) {
  74. co->co_argcount = argcount;
  75. co->co_nlocals = nlocals;
  76. co->co_stacksize = stacksize;
  77. co->co_flags = flags;
  78. Py_INCREF(code);
  79. co->co_code = code;
  80. Py_INCREF(consts);
  81. co->co_consts = consts;
  82. Py_INCREF(names);
  83. co->co_names = names;
  84. Py_INCREF(varnames);
  85. co->co_varnames = varnames;
  86. Py_INCREF(freevars);
  87. co->co_freevars = freevars;
  88. Py_INCREF(cellvars);
  89. co->co_cellvars = cellvars;
  90. Py_INCREF(filename);
  91. co->co_filename = filename;
  92. Py_INCREF(name);
  93. co->co_name = name;
  94. co->co_firstlineno = firstlineno;
  95. Py_INCREF(lnotab);
  96. co->co_lnotab = lnotab;
  97. co->co_zombieframe = NULL;
  98. co->co_weakreflist = NULL;
  99. }
  100. return co;
  101. }
  102. PyCodeObject *
  103. PyCode_NewEmpty(const char *filename, const char *funcname, int firstlineno)
  104. {
  105. static PyObject *emptystring = NULL;
  106. static PyObject *nulltuple = NULL;
  107. PyObject *filename_ob = NULL;
  108. PyObject *funcname_ob = NULL;
  109. PyCodeObject *result = NULL;
  110. if (emptystring == NULL) {
  111. emptystring = PyString_FromString("");
  112. if (emptystring == NULL)
  113. goto failed;
  114. }
  115. if (nulltuple == NULL) {
  116. nulltuple = PyTuple_New(0);
  117. if (nulltuple == NULL)
  118. goto failed;
  119. }
  120. funcname_ob = PyString_FromString(funcname);
  121. if (funcname_ob == NULL)
  122. goto failed;
  123. filename_ob = PyString_FromString(filename);
  124. if (filename_ob == NULL)
  125. goto failed;
  126. result = PyCode_New(0, /* argcount */
  127. 0, /* nlocals */
  128. 0, /* stacksize */
  129. 0, /* flags */
  130. emptystring, /* code */
  131. nulltuple, /* consts */
  132. nulltuple, /* names */
  133. nulltuple, /* varnames */
  134. nulltuple, /* freevars */
  135. nulltuple, /* cellvars */
  136. filename_ob, /* filename */
  137. funcname_ob, /* name */
  138. firstlineno, /* firstlineno */
  139. emptystring /* lnotab */
  140. );
  141. failed:
  142. Py_XDECREF(funcname_ob);
  143. Py_XDECREF(filename_ob);
  144. return result;
  145. }
  146. #define OFF(x) offsetof(PyCodeObject, x)
  147. static PyMemberDef code_memberlist[] = {
  148. {"co_argcount", T_INT, OFF(co_argcount), READONLY},
  149. {"co_nlocals", T_INT, OFF(co_nlocals), READONLY},
  150. {"co_stacksize",T_INT, OFF(co_stacksize), READONLY},
  151. {"co_flags", T_INT, OFF(co_flags), READONLY},
  152. {"co_code", T_OBJECT, OFF(co_code), READONLY},
  153. {"co_consts", T_OBJECT, OFF(co_consts), READONLY},
  154. {"co_names", T_OBJECT, OFF(co_names), READONLY},
  155. {"co_varnames", T_OBJECT, OFF(co_varnames), READONLY},
  156. {"co_freevars", T_OBJECT, OFF(co_freevars), READONLY},
  157. {"co_cellvars", T_OBJECT, OFF(co_cellvars), READONLY},
  158. {"co_filename", T_OBJECT, OFF(co_filename), READONLY},
  159. {"co_name", T_OBJECT, OFF(co_name), READONLY},
  160. {"co_firstlineno", T_INT, OFF(co_firstlineno), READONLY},
  161. {"co_lnotab", T_OBJECT, OFF(co_lnotab), READONLY},
  162. {NULL} /* Sentinel */
  163. };
  164. /* Helper for code_new: return a shallow copy of a tuple that is
  165. guaranteed to contain exact strings, by converting string subclasses
  166. to exact strings and complaining if a non-string is found. */
  167. static PyObject*
  168. validate_and_copy_tuple(PyObject *tup)
  169. {
  170. PyObject *newtuple;
  171. PyObject *item;
  172. Py_ssize_t i, len;
  173. len = PyTuple_GET_SIZE(tup);
  174. newtuple = PyTuple_New(len);
  175. if (newtuple == NULL)
  176. return NULL;
  177. for (i = 0; i < len; i++) {
  178. item = PyTuple_GET_ITEM(tup, i);
  179. if (PyString_CheckExact(item)) {
  180. Py_INCREF(item);
  181. }
  182. else if (!PyString_Check(item)) {
  183. PyErr_Format(
  184. PyExc_TypeError,
  185. "name tuples must contain only "
  186. "strings, not '%.500s'",
  187. item->ob_type->tp_name);
  188. Py_DECREF(newtuple);
  189. return NULL;
  190. }
  191. else {
  192. item = PyString_FromStringAndSize(
  193. PyString_AS_STRING(item),
  194. PyString_GET_SIZE(item));
  195. if (item == NULL) {
  196. Py_DECREF(newtuple);
  197. return NULL;
  198. }
  199. }
  200. PyTuple_SET_ITEM(newtuple, i, item);
  201. }
  202. return newtuple;
  203. }
  204. PyDoc_STRVAR(code_doc,
  205. "code(argcount, nlocals, stacksize, flags, codestring, constants, names,\n\
  206. varnames, filename, name, firstlineno, lnotab[, freevars[, cellvars]])\n\
  207. \n\
  208. Create a code object. Not for the faint of heart.");
  209. static PyObject *
  210. code_new(PyTypeObject *type, PyObject *args, PyObject *kw)
  211. {
  212. int argcount;
  213. int nlocals;
  214. int stacksize;
  215. int flags;
  216. PyObject *co = NULL;
  217. PyObject *code;
  218. PyObject *consts;
  219. PyObject *names, *ournames = NULL;
  220. PyObject *varnames, *ourvarnames = NULL;
  221. PyObject *freevars = NULL, *ourfreevars = NULL;
  222. PyObject *cellvars = NULL, *ourcellvars = NULL;
  223. PyObject *filename;
  224. PyObject *name;
  225. int firstlineno;
  226. PyObject *lnotab;
  227. if (!PyArg_ParseTuple(args, "iiiiSO!O!O!SSiS|O!O!:code",
  228. &argcount, &nlocals, &stacksize, &flags,
  229. &code,
  230. &PyTuple_Type, &consts,
  231. &PyTuple_Type, &names,
  232. &PyTuple_Type, &varnames,
  233. &filename, &name,
  234. &firstlineno, &lnotab,
  235. &PyTuple_Type, &freevars,
  236. &PyTuple_Type, &cellvars))
  237. return NULL;
  238. if (argcount < 0) {
  239. PyErr_SetString(
  240. PyExc_ValueError,
  241. "code: argcount must not be negative");
  242. goto cleanup;
  243. }
  244. if (nlocals < 0) {
  245. PyErr_SetString(
  246. PyExc_ValueError,
  247. "code: nlocals must not be negative");
  248. goto cleanup;
  249. }
  250. ournames = validate_and_copy_tuple(names);
  251. if (ournames == NULL)
  252. goto cleanup;
  253. ourvarnames = validate_and_copy_tuple(varnames);
  254. if (ourvarnames == NULL)
  255. goto cleanup;
  256. if (freevars)
  257. ourfreevars = validate_and_copy_tuple(freevars);
  258. else
  259. ourfreevars = PyTuple_New(0);
  260. if (ourfreevars == NULL)
  261. goto cleanup;
  262. if (cellvars)
  263. ourcellvars = validate_and_copy_tuple(cellvars);
  264. else
  265. ourcellvars = PyTuple_New(0);
  266. if (ourcellvars == NULL)
  267. goto cleanup;
  268. co = (PyObject *)PyCode_New(argcount, nlocals, stacksize, flags,
  269. code, consts, ournames, ourvarnames,
  270. ourfreevars, ourcellvars, filename,
  271. name, firstlineno, lnotab);
  272. cleanup:
  273. Py_XDECREF(ournames);
  274. Py_XDECREF(ourvarnames);
  275. Py_XDECREF(ourfreevars);
  276. Py_XDECREF(ourcellvars);
  277. return co;
  278. }
  279. static void
  280. code_dealloc(PyCodeObject *co)
  281. {
  282. Py_XDECREF(co->co_code);
  283. Py_XDECREF(co->co_consts);
  284. Py_XDECREF(co->co_names);
  285. Py_XDECREF(co->co_varnames);
  286. Py_XDECREF(co->co_freevars);
  287. Py_XDECREF(co->co_cellvars);
  288. Py_XDECREF(co->co_filename);
  289. Py_XDECREF(co->co_name);
  290. Py_XDECREF(co->co_lnotab);
  291. if (co->co_zombieframe != NULL)
  292. PyObject_GC_Del(co->co_zombieframe);
  293. if (co->co_weakreflist != NULL)
  294. PyObject_ClearWeakRefs((PyObject*)co);
  295. PyObject_DEL(co);
  296. }
  297. static PyObject *
  298. code_repr(PyCodeObject *co)
  299. {
  300. char buf[500];
  301. int lineno = -1;
  302. char *filename = "???";
  303. char *name = "???";
  304. if (co->co_firstlineno != 0)
  305. lineno = co->co_firstlineno;
  306. if (co->co_filename && PyString_Check(co->co_filename))
  307. filename = PyString_AS_STRING(co->co_filename);
  308. if (co->co_name && PyString_Check(co->co_name))
  309. name = PyString_AS_STRING(co->co_name);
  310. PyOS_snprintf(buf, sizeof(buf),
  311. "<code object %.100s at %p, file \"%.300s\", line %d>",
  312. name, co, filename, lineno);
  313. return PyString_FromString(buf);
  314. }
  315. static int
  316. code_compare(PyCodeObject *co, PyCodeObject *cp)
  317. {
  318. int cmp;
  319. cmp = PyObject_Compare(co->co_name, cp->co_name);
  320. if (cmp) return cmp;
  321. cmp = co->co_argcount - cp->co_argcount;
  322. if (cmp) goto normalize;
  323. cmp = co->co_nlocals - cp->co_nlocals;
  324. if (cmp) goto normalize;
  325. cmp = co->co_flags - cp->co_flags;
  326. if (cmp) goto normalize;
  327. cmp = co->co_firstlineno - cp->co_firstlineno;
  328. if (cmp) goto normalize;
  329. cmp = PyObject_Compare(co->co_code, cp->co_code);
  330. if (cmp) return cmp;
  331. cmp = PyObject_Compare(co->co_consts, cp->co_consts);
  332. if (cmp) return cmp;
  333. cmp = PyObject_Compare(co->co_names, cp->co_names);
  334. if (cmp) return cmp;
  335. cmp = PyObject_Compare(co->co_varnames, cp->co_varnames);
  336. if (cmp) return cmp;
  337. cmp = PyObject_Compare(co->co_freevars, cp->co_freevars);
  338. if (cmp) return cmp;
  339. cmp = PyObject_Compare(co->co_cellvars, cp->co_cellvars);
  340. return cmp;
  341. normalize:
  342. if (cmp > 0)
  343. return 1;
  344. else if (cmp < 0)
  345. return -1;
  346. else
  347. return 0;
  348. }
  349. static PyObject *
  350. code_richcompare(PyObject *self, PyObject *other, int op)
  351. {
  352. PyCodeObject *co, *cp;
  353. int eq;
  354. PyObject *res;
  355. if ((op != Py_EQ && op != Py_NE) ||
  356. !PyCode_Check(self) ||
  357. !PyCode_Check(other)) {
  358. /* Py3K warning if types are not equal and comparison
  359. isn't == or != */
  360. if (PyErr_WarnPy3k("code inequality comparisons not supported "
  361. "in 3.x", 1) < 0) {
  362. return NULL;
  363. }
  364. Py_INCREF(Py_NotImplemented);
  365. return Py_NotImplemented;
  366. }
  367. co = (PyCodeObject *)self;
  368. cp = (PyCodeObject *)other;
  369. eq = PyObject_RichCompareBool(co->co_name, cp->co_name, Py_EQ);
  370. if (eq <= 0) goto unequal;
  371. eq = co->co_argcount == cp->co_argcount;
  372. if (!eq) goto unequal;
  373. eq = co->co_nlocals == cp->co_nlocals;
  374. if (!eq) goto unequal;
  375. eq = co->co_flags == cp->co_flags;
  376. if (!eq) goto unequal;
  377. eq = co->co_firstlineno == cp->co_firstlineno;
  378. if (!eq) goto unequal;
  379. eq = PyObject_RichCompareBool(co->co_code, cp->co_code, Py_EQ);
  380. if (eq <= 0) goto unequal;
  381. eq = PyObject_RichCompareBool(co->co_consts, cp->co_consts, Py_EQ);
  382. if (eq <= 0) goto unequal;
  383. eq = PyObject_RichCompareBool(co->co_names, cp->co_names, Py_EQ);
  384. if (eq <= 0) goto unequal;
  385. eq = PyObject_RichCompareBool(co->co_varnames, cp->co_varnames, Py_EQ);
  386. if (eq <= 0) goto unequal;
  387. eq = PyObject_RichCompareBool(co->co_freevars, cp->co_freevars, Py_EQ);
  388. if (eq <= 0) goto unequal;
  389. eq = PyObject_RichCompareBool(co->co_cellvars, cp->co_cellvars, Py_EQ);
  390. if (eq <= 0) goto unequal;
  391. if (op == Py_EQ)
  392. res = Py_True;
  393. else
  394. res = Py_False;
  395. goto done;
  396. unequal:
  397. if (eq < 0)
  398. return NULL;
  399. if (op == Py_NE)
  400. res = Py_True;
  401. else
  402. res = Py_False;
  403. done:
  404. Py_INCREF(res);
  405. return res;
  406. }
  407. static long
  408. code_hash(PyCodeObject *co)
  409. {
  410. long h, h0, h1, h2, h3, h4, h5, h6;
  411. h0 = PyObject_Hash(co->co_name);
  412. if (h0 == -1) return -1;
  413. h1 = PyObject_Hash(co->co_code);
  414. if (h1 == -1) return -1;
  415. h2 = PyObject_Hash(co->co_consts);
  416. if (h2 == -1) return -1;
  417. h3 = PyObject_Hash(co->co_names);
  418. if (h3 == -1) return -1;
  419. h4 = PyObject_Hash(co->co_varnames);
  420. if (h4 == -1) return -1;
  421. h5 = PyObject_Hash(co->co_freevars);
  422. if (h5 == -1) return -1;
  423. h6 = PyObject_Hash(co->co_cellvars);
  424. if (h6 == -1) return -1;
  425. h = h0 ^ h1 ^ h2 ^ h3 ^ h4 ^ h5 ^ h6 ^
  426. co->co_argcount ^ co->co_nlocals ^ co->co_flags;
  427. if (h == -1) h = -2;
  428. return h;
  429. }
  430. /* XXX code objects need to participate in GC? */
  431. PyTypeObject PyCode_Type = {
  432. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  433. "code",
  434. sizeof(PyCodeObject),
  435. 0,
  436. (destructor)code_dealloc, /* tp_dealloc */
  437. 0, /* tp_print */
  438. 0, /* tp_getattr */
  439. 0, /* tp_setattr */
  440. (cmpfunc)code_compare, /* tp_compare */
  441. (reprfunc)code_repr, /* tp_repr */
  442. 0, /* tp_as_number */
  443. 0, /* tp_as_sequence */
  444. 0, /* tp_as_mapping */
  445. (hashfunc)code_hash, /* tp_hash */
  446. 0, /* tp_call */
  447. 0, /* tp_str */
  448. PyObject_GenericGetAttr, /* tp_getattro */
  449. 0, /* tp_setattro */
  450. 0, /* tp_as_buffer */
  451. Py_TPFLAGS_DEFAULT, /* tp_flags */
  452. code_doc, /* tp_doc */
  453. 0, /* tp_traverse */
  454. 0, /* tp_clear */
  455. code_richcompare, /* tp_richcompare */
  456. offsetof(PyCodeObject, co_weakreflist), /* tp_weaklistoffset */
  457. 0, /* tp_iter */
  458. 0, /* tp_iternext */
  459. 0, /* tp_methods */
  460. code_memberlist, /* tp_members */
  461. 0, /* tp_getset */
  462. 0, /* tp_base */
  463. 0, /* tp_dict */
  464. 0, /* tp_descr_get */
  465. 0, /* tp_descr_set */
  466. 0, /* tp_dictoffset */
  467. 0, /* tp_init */
  468. 0, /* tp_alloc */
  469. code_new, /* tp_new */
  470. };
  471. /* Use co_lnotab to compute the line number from a bytecode index, addrq. See
  472. lnotab_notes.txt for the details of the lnotab representation.
  473. */
  474. int
  475. PyCode_Addr2Line(PyCodeObject *co, int addrq)
  476. {
  477. int size = PyString_Size(co->co_lnotab) / 2;
  478. unsigned char *p = (unsigned char*)PyString_AsString(co->co_lnotab);
  479. int line = co->co_firstlineno;
  480. int addr = 0;
  481. while (--size >= 0) {
  482. addr += *p++;
  483. if (addr > addrq)
  484. break;
  485. line += *p++;
  486. }
  487. return line;
  488. }
  489. /* Update *bounds to describe the first and one-past-the-last instructions in
  490. the same line as lasti. Return the number of that line. */
  491. int
  492. _PyCode_CheckLineNumber(PyCodeObject* co, int lasti, PyAddrPair *bounds)
  493. {
  494. int size, addr, line;
  495. unsigned char* p;
  496. p = (unsigned char*)PyString_AS_STRING(co->co_lnotab);
  497. size = PyString_GET_SIZE(co->co_lnotab) / 2;
  498. addr = 0;
  499. line = co->co_firstlineno;
  500. assert(line > 0);
  501. /* possible optimization: if f->f_lasti == instr_ub
  502. (likely to be a common case) then we already know
  503. instr_lb -- if we stored the matching value of p
  504. somwhere we could skip the first while loop. */
  505. /* See lnotab_notes.txt for the description of
  506. co_lnotab. A point to remember: increments to p
  507. come in (addr, line) pairs. */
  508. bounds->ap_lower = 0;
  509. while (size > 0) {
  510. if (addr + *p > lasti)
  511. break;
  512. addr += *p++;
  513. if (*p)
  514. bounds->ap_lower = addr;
  515. line += *p++;
  516. --size;
  517. }
  518. if (size > 0) {
  519. while (--size >= 0) {
  520. addr += *p++;
  521. if (*p++)
  522. break;
  523. }
  524. bounds->ap_upper = addr;
  525. }
  526. else {
  527. bounds->ap_upper = INT_MAX;
  528. }
  529. return line;
  530. }