codeobject.c.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(code_new__doc__,
  9. "code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,\n"
  10. " flags, codestring, constants, names, varnames, filename, name,\n"
  11. " qualname, firstlineno, linetable, exceptiontable, freevars=(),\n"
  12. " cellvars=(), /)\n"
  13. "--\n"
  14. "\n"
  15. "Create a code object. Not for the faint of heart.");
  16. static PyObject *
  17. code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
  18. int kwonlyargcount, int nlocals, int stacksize, int flags,
  19. PyObject *code, PyObject *consts, PyObject *names,
  20. PyObject *varnames, PyObject *filename, PyObject *name,
  21. PyObject *qualname, int firstlineno, PyObject *linetable,
  22. PyObject *exceptiontable, PyObject *freevars,
  23. PyObject *cellvars);
  24. static PyObject *
  25. code_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  26. {
  27. PyObject *return_value = NULL;
  28. PyTypeObject *base_tp = &PyCode_Type;
  29. int argcount;
  30. int posonlyargcount;
  31. int kwonlyargcount;
  32. int nlocals;
  33. int stacksize;
  34. int flags;
  35. PyObject *code;
  36. PyObject *consts;
  37. PyObject *names;
  38. PyObject *varnames;
  39. PyObject *filename;
  40. PyObject *name;
  41. PyObject *qualname;
  42. int firstlineno;
  43. PyObject *linetable;
  44. PyObject *exceptiontable;
  45. PyObject *freevars = NULL;
  46. PyObject *cellvars = NULL;
  47. if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
  48. !_PyArg_NoKeywords("code", kwargs)) {
  49. goto exit;
  50. }
  51. if (!_PyArg_CheckPositional("code", PyTuple_GET_SIZE(args), 16, 18)) {
  52. goto exit;
  53. }
  54. argcount = _PyLong_AsInt(PyTuple_GET_ITEM(args, 0));
  55. if (argcount == -1 && PyErr_Occurred()) {
  56. goto exit;
  57. }
  58. posonlyargcount = _PyLong_AsInt(PyTuple_GET_ITEM(args, 1));
  59. if (posonlyargcount == -1 && PyErr_Occurred()) {
  60. goto exit;
  61. }
  62. kwonlyargcount = _PyLong_AsInt(PyTuple_GET_ITEM(args, 2));
  63. if (kwonlyargcount == -1 && PyErr_Occurred()) {
  64. goto exit;
  65. }
  66. nlocals = _PyLong_AsInt(PyTuple_GET_ITEM(args, 3));
  67. if (nlocals == -1 && PyErr_Occurred()) {
  68. goto exit;
  69. }
  70. stacksize = _PyLong_AsInt(PyTuple_GET_ITEM(args, 4));
  71. if (stacksize == -1 && PyErr_Occurred()) {
  72. goto exit;
  73. }
  74. flags = _PyLong_AsInt(PyTuple_GET_ITEM(args, 5));
  75. if (flags == -1 && PyErr_Occurred()) {
  76. goto exit;
  77. }
  78. if (!PyBytes_Check(PyTuple_GET_ITEM(args, 6))) {
  79. _PyArg_BadArgument("code", "argument 7", "bytes", PyTuple_GET_ITEM(args, 6));
  80. goto exit;
  81. }
  82. code = PyTuple_GET_ITEM(args, 6);
  83. if (!PyTuple_Check(PyTuple_GET_ITEM(args, 7))) {
  84. _PyArg_BadArgument("code", "argument 8", "tuple", PyTuple_GET_ITEM(args, 7));
  85. goto exit;
  86. }
  87. consts = PyTuple_GET_ITEM(args, 7);
  88. if (!PyTuple_Check(PyTuple_GET_ITEM(args, 8))) {
  89. _PyArg_BadArgument("code", "argument 9", "tuple", PyTuple_GET_ITEM(args, 8));
  90. goto exit;
  91. }
  92. names = PyTuple_GET_ITEM(args, 8);
  93. if (!PyTuple_Check(PyTuple_GET_ITEM(args, 9))) {
  94. _PyArg_BadArgument("code", "argument 10", "tuple", PyTuple_GET_ITEM(args, 9));
  95. goto exit;
  96. }
  97. varnames = PyTuple_GET_ITEM(args, 9);
  98. if (!PyUnicode_Check(PyTuple_GET_ITEM(args, 10))) {
  99. _PyArg_BadArgument("code", "argument 11", "str", PyTuple_GET_ITEM(args, 10));
  100. goto exit;
  101. }
  102. if (PyUnicode_READY(PyTuple_GET_ITEM(args, 10)) == -1) {
  103. goto exit;
  104. }
  105. filename = PyTuple_GET_ITEM(args, 10);
  106. if (!PyUnicode_Check(PyTuple_GET_ITEM(args, 11))) {
  107. _PyArg_BadArgument("code", "argument 12", "str", PyTuple_GET_ITEM(args, 11));
  108. goto exit;
  109. }
  110. if (PyUnicode_READY(PyTuple_GET_ITEM(args, 11)) == -1) {
  111. goto exit;
  112. }
  113. name = PyTuple_GET_ITEM(args, 11);
  114. if (!PyUnicode_Check(PyTuple_GET_ITEM(args, 12))) {
  115. _PyArg_BadArgument("code", "argument 13", "str", PyTuple_GET_ITEM(args, 12));
  116. goto exit;
  117. }
  118. if (PyUnicode_READY(PyTuple_GET_ITEM(args, 12)) == -1) {
  119. goto exit;
  120. }
  121. qualname = PyTuple_GET_ITEM(args, 12);
  122. firstlineno = _PyLong_AsInt(PyTuple_GET_ITEM(args, 13));
  123. if (firstlineno == -1 && PyErr_Occurred()) {
  124. goto exit;
  125. }
  126. if (!PyBytes_Check(PyTuple_GET_ITEM(args, 14))) {
  127. _PyArg_BadArgument("code", "argument 15", "bytes", PyTuple_GET_ITEM(args, 14));
  128. goto exit;
  129. }
  130. linetable = PyTuple_GET_ITEM(args, 14);
  131. if (!PyBytes_Check(PyTuple_GET_ITEM(args, 15))) {
  132. _PyArg_BadArgument("code", "argument 16", "bytes", PyTuple_GET_ITEM(args, 15));
  133. goto exit;
  134. }
  135. exceptiontable = PyTuple_GET_ITEM(args, 15);
  136. if (PyTuple_GET_SIZE(args) < 17) {
  137. goto skip_optional;
  138. }
  139. if (!PyTuple_Check(PyTuple_GET_ITEM(args, 16))) {
  140. _PyArg_BadArgument("code", "argument 17", "tuple", PyTuple_GET_ITEM(args, 16));
  141. goto exit;
  142. }
  143. freevars = PyTuple_GET_ITEM(args, 16);
  144. if (PyTuple_GET_SIZE(args) < 18) {
  145. goto skip_optional;
  146. }
  147. if (!PyTuple_Check(PyTuple_GET_ITEM(args, 17))) {
  148. _PyArg_BadArgument("code", "argument 18", "tuple", PyTuple_GET_ITEM(args, 17));
  149. goto exit;
  150. }
  151. cellvars = PyTuple_GET_ITEM(args, 17);
  152. skip_optional:
  153. return_value = code_new_impl(type, argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name, qualname, firstlineno, linetable, exceptiontable, freevars, cellvars);
  154. exit:
  155. return return_value;
  156. }
  157. PyDoc_STRVAR(code_replace__doc__,
  158. "replace($self, /, **changes)\n"
  159. "--\n"
  160. "\n"
  161. "Return a copy of the code object with new values for the specified fields.");
  162. #define CODE_REPLACE_METHODDEF \
  163. {"replace", _PyCFunction_CAST(code_replace), METH_FASTCALL|METH_KEYWORDS, code_replace__doc__},
  164. static PyObject *
  165. code_replace_impl(PyCodeObject *self, int co_argcount,
  166. int co_posonlyargcount, int co_kwonlyargcount,
  167. int co_nlocals, int co_stacksize, int co_flags,
  168. int co_firstlineno, PyObject *co_code, PyObject *co_consts,
  169. PyObject *co_names, PyObject *co_varnames,
  170. PyObject *co_freevars, PyObject *co_cellvars,
  171. PyObject *co_filename, PyObject *co_name,
  172. PyObject *co_qualname, PyObject *co_linetable,
  173. PyObject *co_exceptiontable);
  174. static PyObject *
  175. code_replace(PyCodeObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  176. {
  177. PyObject *return_value = NULL;
  178. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  179. #define NUM_KEYWORDS 18
  180. static struct {
  181. PyGC_Head _this_is_not_used;
  182. PyObject_VAR_HEAD
  183. PyObject *ob_item[NUM_KEYWORDS];
  184. } _kwtuple = {
  185. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  186. .ob_item = { &_Py_ID(co_argcount), &_Py_ID(co_posonlyargcount), &_Py_ID(co_kwonlyargcount), &_Py_ID(co_nlocals), &_Py_ID(co_stacksize), &_Py_ID(co_flags), &_Py_ID(co_firstlineno), &_Py_ID(co_code), &_Py_ID(co_consts), &_Py_ID(co_names), &_Py_ID(co_varnames), &_Py_ID(co_freevars), &_Py_ID(co_cellvars), &_Py_ID(co_filename), &_Py_ID(co_name), &_Py_ID(co_qualname), &_Py_ID(co_linetable), &_Py_ID(co_exceptiontable), },
  187. };
  188. #undef NUM_KEYWORDS
  189. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  190. #else // !Py_BUILD_CORE
  191. # define KWTUPLE NULL
  192. #endif // !Py_BUILD_CORE
  193. static const char * const _keywords[] = {"co_argcount", "co_posonlyargcount", "co_kwonlyargcount", "co_nlocals", "co_stacksize", "co_flags", "co_firstlineno", "co_code", "co_consts", "co_names", "co_varnames", "co_freevars", "co_cellvars", "co_filename", "co_name", "co_qualname", "co_linetable", "co_exceptiontable", NULL};
  194. static _PyArg_Parser _parser = {
  195. .keywords = _keywords,
  196. .fname = "replace",
  197. .kwtuple = KWTUPLE,
  198. };
  199. #undef KWTUPLE
  200. PyObject *argsbuf[18];
  201. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  202. int co_argcount = self->co_argcount;
  203. int co_posonlyargcount = self->co_posonlyargcount;
  204. int co_kwonlyargcount = self->co_kwonlyargcount;
  205. int co_nlocals = self->co_nlocals;
  206. int co_stacksize = self->co_stacksize;
  207. int co_flags = self->co_flags;
  208. int co_firstlineno = self->co_firstlineno;
  209. PyObject *co_code = NULL;
  210. PyObject *co_consts = self->co_consts;
  211. PyObject *co_names = self->co_names;
  212. PyObject *co_varnames = NULL;
  213. PyObject *co_freevars = NULL;
  214. PyObject *co_cellvars = NULL;
  215. PyObject *co_filename = self->co_filename;
  216. PyObject *co_name = self->co_name;
  217. PyObject *co_qualname = self->co_qualname;
  218. PyObject *co_linetable = self->co_linetable;
  219. PyObject *co_exceptiontable = self->co_exceptiontable;
  220. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
  221. if (!args) {
  222. goto exit;
  223. }
  224. if (!noptargs) {
  225. goto skip_optional_kwonly;
  226. }
  227. if (args[0]) {
  228. co_argcount = _PyLong_AsInt(args[0]);
  229. if (co_argcount == -1 && PyErr_Occurred()) {
  230. goto exit;
  231. }
  232. if (!--noptargs) {
  233. goto skip_optional_kwonly;
  234. }
  235. }
  236. if (args[1]) {
  237. co_posonlyargcount = _PyLong_AsInt(args[1]);
  238. if (co_posonlyargcount == -1 && PyErr_Occurred()) {
  239. goto exit;
  240. }
  241. if (!--noptargs) {
  242. goto skip_optional_kwonly;
  243. }
  244. }
  245. if (args[2]) {
  246. co_kwonlyargcount = _PyLong_AsInt(args[2]);
  247. if (co_kwonlyargcount == -1 && PyErr_Occurred()) {
  248. goto exit;
  249. }
  250. if (!--noptargs) {
  251. goto skip_optional_kwonly;
  252. }
  253. }
  254. if (args[3]) {
  255. co_nlocals = _PyLong_AsInt(args[3]);
  256. if (co_nlocals == -1 && PyErr_Occurred()) {
  257. goto exit;
  258. }
  259. if (!--noptargs) {
  260. goto skip_optional_kwonly;
  261. }
  262. }
  263. if (args[4]) {
  264. co_stacksize = _PyLong_AsInt(args[4]);
  265. if (co_stacksize == -1 && PyErr_Occurred()) {
  266. goto exit;
  267. }
  268. if (!--noptargs) {
  269. goto skip_optional_kwonly;
  270. }
  271. }
  272. if (args[5]) {
  273. co_flags = _PyLong_AsInt(args[5]);
  274. if (co_flags == -1 && PyErr_Occurred()) {
  275. goto exit;
  276. }
  277. if (!--noptargs) {
  278. goto skip_optional_kwonly;
  279. }
  280. }
  281. if (args[6]) {
  282. co_firstlineno = _PyLong_AsInt(args[6]);
  283. if (co_firstlineno == -1 && PyErr_Occurred()) {
  284. goto exit;
  285. }
  286. if (!--noptargs) {
  287. goto skip_optional_kwonly;
  288. }
  289. }
  290. if (args[7]) {
  291. if (!PyBytes_Check(args[7])) {
  292. _PyArg_BadArgument("replace", "argument 'co_code'", "bytes", args[7]);
  293. goto exit;
  294. }
  295. co_code = args[7];
  296. if (!--noptargs) {
  297. goto skip_optional_kwonly;
  298. }
  299. }
  300. if (args[8]) {
  301. if (!PyTuple_Check(args[8])) {
  302. _PyArg_BadArgument("replace", "argument 'co_consts'", "tuple", args[8]);
  303. goto exit;
  304. }
  305. co_consts = args[8];
  306. if (!--noptargs) {
  307. goto skip_optional_kwonly;
  308. }
  309. }
  310. if (args[9]) {
  311. if (!PyTuple_Check(args[9])) {
  312. _PyArg_BadArgument("replace", "argument 'co_names'", "tuple", args[9]);
  313. goto exit;
  314. }
  315. co_names = args[9];
  316. if (!--noptargs) {
  317. goto skip_optional_kwonly;
  318. }
  319. }
  320. if (args[10]) {
  321. if (!PyTuple_Check(args[10])) {
  322. _PyArg_BadArgument("replace", "argument 'co_varnames'", "tuple", args[10]);
  323. goto exit;
  324. }
  325. co_varnames = args[10];
  326. if (!--noptargs) {
  327. goto skip_optional_kwonly;
  328. }
  329. }
  330. if (args[11]) {
  331. if (!PyTuple_Check(args[11])) {
  332. _PyArg_BadArgument("replace", "argument 'co_freevars'", "tuple", args[11]);
  333. goto exit;
  334. }
  335. co_freevars = args[11];
  336. if (!--noptargs) {
  337. goto skip_optional_kwonly;
  338. }
  339. }
  340. if (args[12]) {
  341. if (!PyTuple_Check(args[12])) {
  342. _PyArg_BadArgument("replace", "argument 'co_cellvars'", "tuple", args[12]);
  343. goto exit;
  344. }
  345. co_cellvars = args[12];
  346. if (!--noptargs) {
  347. goto skip_optional_kwonly;
  348. }
  349. }
  350. if (args[13]) {
  351. if (!PyUnicode_Check(args[13])) {
  352. _PyArg_BadArgument("replace", "argument 'co_filename'", "str", args[13]);
  353. goto exit;
  354. }
  355. if (PyUnicode_READY(args[13]) == -1) {
  356. goto exit;
  357. }
  358. co_filename = args[13];
  359. if (!--noptargs) {
  360. goto skip_optional_kwonly;
  361. }
  362. }
  363. if (args[14]) {
  364. if (!PyUnicode_Check(args[14])) {
  365. _PyArg_BadArgument("replace", "argument 'co_name'", "str", args[14]);
  366. goto exit;
  367. }
  368. if (PyUnicode_READY(args[14]) == -1) {
  369. goto exit;
  370. }
  371. co_name = args[14];
  372. if (!--noptargs) {
  373. goto skip_optional_kwonly;
  374. }
  375. }
  376. if (args[15]) {
  377. if (!PyUnicode_Check(args[15])) {
  378. _PyArg_BadArgument("replace", "argument 'co_qualname'", "str", args[15]);
  379. goto exit;
  380. }
  381. if (PyUnicode_READY(args[15]) == -1) {
  382. goto exit;
  383. }
  384. co_qualname = args[15];
  385. if (!--noptargs) {
  386. goto skip_optional_kwonly;
  387. }
  388. }
  389. if (args[16]) {
  390. if (!PyBytes_Check(args[16])) {
  391. _PyArg_BadArgument("replace", "argument 'co_linetable'", "bytes", args[16]);
  392. goto exit;
  393. }
  394. co_linetable = args[16];
  395. if (!--noptargs) {
  396. goto skip_optional_kwonly;
  397. }
  398. }
  399. if (!PyBytes_Check(args[17])) {
  400. _PyArg_BadArgument("replace", "argument 'co_exceptiontable'", "bytes", args[17]);
  401. goto exit;
  402. }
  403. co_exceptiontable = args[17];
  404. skip_optional_kwonly:
  405. return_value = code_replace_impl(self, co_argcount, co_posonlyargcount, co_kwonlyargcount, co_nlocals, co_stacksize, co_flags, co_firstlineno, co_code, co_consts, co_names, co_varnames, co_freevars, co_cellvars, co_filename, co_name, co_qualname, co_linetable, co_exceptiontable);
  406. exit:
  407. return return_value;
  408. }
  409. PyDoc_STRVAR(code__varname_from_oparg__doc__,
  410. "_varname_from_oparg($self, /, oparg)\n"
  411. "--\n"
  412. "\n"
  413. "(internal-only) Return the local variable name for the given oparg.\n"
  414. "\n"
  415. "WARNING: this method is for internal use only and may change or go away.");
  416. #define CODE__VARNAME_FROM_OPARG_METHODDEF \
  417. {"_varname_from_oparg", _PyCFunction_CAST(code__varname_from_oparg), METH_FASTCALL|METH_KEYWORDS, code__varname_from_oparg__doc__},
  418. static PyObject *
  419. code__varname_from_oparg_impl(PyCodeObject *self, int oparg);
  420. static PyObject *
  421. code__varname_from_oparg(PyCodeObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  422. {
  423. PyObject *return_value = NULL;
  424. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  425. #define NUM_KEYWORDS 1
  426. static struct {
  427. PyGC_Head _this_is_not_used;
  428. PyObject_VAR_HEAD
  429. PyObject *ob_item[NUM_KEYWORDS];
  430. } _kwtuple = {
  431. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  432. .ob_item = { &_Py_ID(oparg), },
  433. };
  434. #undef NUM_KEYWORDS
  435. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  436. #else // !Py_BUILD_CORE
  437. # define KWTUPLE NULL
  438. #endif // !Py_BUILD_CORE
  439. static const char * const _keywords[] = {"oparg", NULL};
  440. static _PyArg_Parser _parser = {
  441. .keywords = _keywords,
  442. .fname = "_varname_from_oparg",
  443. .kwtuple = KWTUPLE,
  444. };
  445. #undef KWTUPLE
  446. PyObject *argsbuf[1];
  447. int oparg;
  448. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
  449. if (!args) {
  450. goto exit;
  451. }
  452. oparg = _PyLong_AsInt(args[0]);
  453. if (oparg == -1 && PyErr_Occurred()) {
  454. goto exit;
  455. }
  456. return_value = code__varname_from_oparg_impl(self, oparg);
  457. exit:
  458. return return_value;
  459. }
  460. /*[clinic end generated code: output=ff40f7bdd3851de3 input=a9049054013a1b77]*/