_testinternalcapi.c.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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(_testinternalcapi_compiler_codegen__doc__,
  9. "compiler_codegen($module, /, ast, filename, optimize, compile_mode=0)\n"
  10. "--\n"
  11. "\n"
  12. "Apply compiler code generation to an AST.");
  13. #define _TESTINTERNALCAPI_COMPILER_CODEGEN_METHODDEF \
  14. {"compiler_codegen", _PyCFunction_CAST(_testinternalcapi_compiler_codegen), METH_FASTCALL|METH_KEYWORDS, _testinternalcapi_compiler_codegen__doc__},
  15. static PyObject *
  16. _testinternalcapi_compiler_codegen_impl(PyObject *module, PyObject *ast,
  17. PyObject *filename, int optimize,
  18. int compile_mode);
  19. static PyObject *
  20. _testinternalcapi_compiler_codegen(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  21. {
  22. PyObject *return_value = NULL;
  23. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  24. #define NUM_KEYWORDS 4
  25. static struct {
  26. PyGC_Head _this_is_not_used;
  27. PyObject_VAR_HEAD
  28. PyObject *ob_item[NUM_KEYWORDS];
  29. } _kwtuple = {
  30. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  31. .ob_item = { &_Py_ID(ast), &_Py_ID(filename), &_Py_ID(optimize), &_Py_ID(compile_mode), },
  32. };
  33. #undef NUM_KEYWORDS
  34. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  35. #else // !Py_BUILD_CORE
  36. # define KWTUPLE NULL
  37. #endif // !Py_BUILD_CORE
  38. static const char * const _keywords[] = {"ast", "filename", "optimize", "compile_mode", NULL};
  39. static _PyArg_Parser _parser = {
  40. .keywords = _keywords,
  41. .fname = "compiler_codegen",
  42. .kwtuple = KWTUPLE,
  43. };
  44. #undef KWTUPLE
  45. PyObject *argsbuf[4];
  46. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 3;
  47. PyObject *ast;
  48. PyObject *filename;
  49. int optimize;
  50. int compile_mode = 0;
  51. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 4, 0, argsbuf);
  52. if (!args) {
  53. goto exit;
  54. }
  55. ast = args[0];
  56. filename = args[1];
  57. optimize = _PyLong_AsInt(args[2]);
  58. if (optimize == -1 && PyErr_Occurred()) {
  59. goto exit;
  60. }
  61. if (!noptargs) {
  62. goto skip_optional_pos;
  63. }
  64. compile_mode = _PyLong_AsInt(args[3]);
  65. if (compile_mode == -1 && PyErr_Occurred()) {
  66. goto exit;
  67. }
  68. skip_optional_pos:
  69. return_value = _testinternalcapi_compiler_codegen_impl(module, ast, filename, optimize, compile_mode);
  70. exit:
  71. return return_value;
  72. }
  73. PyDoc_STRVAR(_testinternalcapi_optimize_cfg__doc__,
  74. "optimize_cfg($module, /, instructions, consts, nlocals)\n"
  75. "--\n"
  76. "\n"
  77. "Apply compiler optimizations to an instruction list.");
  78. #define _TESTINTERNALCAPI_OPTIMIZE_CFG_METHODDEF \
  79. {"optimize_cfg", _PyCFunction_CAST(_testinternalcapi_optimize_cfg), METH_FASTCALL|METH_KEYWORDS, _testinternalcapi_optimize_cfg__doc__},
  80. static PyObject *
  81. _testinternalcapi_optimize_cfg_impl(PyObject *module, PyObject *instructions,
  82. PyObject *consts, int nlocals);
  83. static PyObject *
  84. _testinternalcapi_optimize_cfg(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  85. {
  86. PyObject *return_value = NULL;
  87. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  88. #define NUM_KEYWORDS 3
  89. static struct {
  90. PyGC_Head _this_is_not_used;
  91. PyObject_VAR_HEAD
  92. PyObject *ob_item[NUM_KEYWORDS];
  93. } _kwtuple = {
  94. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  95. .ob_item = { &_Py_ID(instructions), &_Py_ID(consts), &_Py_ID(nlocals), },
  96. };
  97. #undef NUM_KEYWORDS
  98. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  99. #else // !Py_BUILD_CORE
  100. # define KWTUPLE NULL
  101. #endif // !Py_BUILD_CORE
  102. static const char * const _keywords[] = {"instructions", "consts", "nlocals", NULL};
  103. static _PyArg_Parser _parser = {
  104. .keywords = _keywords,
  105. .fname = "optimize_cfg",
  106. .kwtuple = KWTUPLE,
  107. };
  108. #undef KWTUPLE
  109. PyObject *argsbuf[3];
  110. PyObject *instructions;
  111. PyObject *consts;
  112. int nlocals;
  113. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
  114. if (!args) {
  115. goto exit;
  116. }
  117. instructions = args[0];
  118. consts = args[1];
  119. nlocals = _PyLong_AsInt(args[2]);
  120. if (nlocals == -1 && PyErr_Occurred()) {
  121. goto exit;
  122. }
  123. return_value = _testinternalcapi_optimize_cfg_impl(module, instructions, consts, nlocals);
  124. exit:
  125. return return_value;
  126. }
  127. PyDoc_STRVAR(_testinternalcapi_assemble_code_object__doc__,
  128. "assemble_code_object($module, /, filename, instructions, metadata)\n"
  129. "--\n"
  130. "\n"
  131. "Create a code object for the given instructions.");
  132. #define _TESTINTERNALCAPI_ASSEMBLE_CODE_OBJECT_METHODDEF \
  133. {"assemble_code_object", _PyCFunction_CAST(_testinternalcapi_assemble_code_object), METH_FASTCALL|METH_KEYWORDS, _testinternalcapi_assemble_code_object__doc__},
  134. static PyObject *
  135. _testinternalcapi_assemble_code_object_impl(PyObject *module,
  136. PyObject *filename,
  137. PyObject *instructions,
  138. PyObject *metadata);
  139. static PyObject *
  140. _testinternalcapi_assemble_code_object(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  141. {
  142. PyObject *return_value = NULL;
  143. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  144. #define NUM_KEYWORDS 3
  145. static struct {
  146. PyGC_Head _this_is_not_used;
  147. PyObject_VAR_HEAD
  148. PyObject *ob_item[NUM_KEYWORDS];
  149. } _kwtuple = {
  150. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  151. .ob_item = { &_Py_ID(filename), &_Py_ID(instructions), &_Py_ID(metadata), },
  152. };
  153. #undef NUM_KEYWORDS
  154. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  155. #else // !Py_BUILD_CORE
  156. # define KWTUPLE NULL
  157. #endif // !Py_BUILD_CORE
  158. static const char * const _keywords[] = {"filename", "instructions", "metadata", NULL};
  159. static _PyArg_Parser _parser = {
  160. .keywords = _keywords,
  161. .fname = "assemble_code_object",
  162. .kwtuple = KWTUPLE,
  163. };
  164. #undef KWTUPLE
  165. PyObject *argsbuf[3];
  166. PyObject *filename;
  167. PyObject *instructions;
  168. PyObject *metadata;
  169. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
  170. if (!args) {
  171. goto exit;
  172. }
  173. filename = args[0];
  174. instructions = args[1];
  175. metadata = args[2];
  176. return_value = _testinternalcapi_assemble_code_object_impl(module, filename, instructions, metadata);
  177. exit:
  178. return return_value;
  179. }
  180. PyDoc_STRVAR(gh_119213_getargs__doc__,
  181. "gh_119213_getargs($module, /, spam=None)\n"
  182. "--\n"
  183. "\n"
  184. "Test _PyArg_Parser.kwtuple");
  185. #define GH_119213_GETARGS_METHODDEF \
  186. {"gh_119213_getargs", _PyCFunction_CAST(gh_119213_getargs), METH_FASTCALL|METH_KEYWORDS, gh_119213_getargs__doc__},
  187. static PyObject *
  188. gh_119213_getargs_impl(PyObject *module, PyObject *spam);
  189. static PyObject *
  190. gh_119213_getargs(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  191. {
  192. PyObject *return_value = NULL;
  193. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  194. #define NUM_KEYWORDS 1
  195. static struct {
  196. PyGC_Head _this_is_not_used;
  197. PyObject_VAR_HEAD
  198. PyObject *ob_item[NUM_KEYWORDS];
  199. } _kwtuple = {
  200. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  201. .ob_item = { &_Py_ID(spam), },
  202. };
  203. #undef NUM_KEYWORDS
  204. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  205. #else // !Py_BUILD_CORE
  206. # define KWTUPLE NULL
  207. #endif // !Py_BUILD_CORE
  208. static const char * const _keywords[] = {"spam", NULL};
  209. static _PyArg_Parser _parser = {
  210. .keywords = _keywords,
  211. .fname = "gh_119213_getargs",
  212. .kwtuple = KWTUPLE,
  213. };
  214. #undef KWTUPLE
  215. PyObject *argsbuf[1];
  216. Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  217. PyObject *spam = Py_None;
  218. args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  219. if (!args) {
  220. goto exit;
  221. }
  222. if (!noptargs) {
  223. goto skip_optional_pos;
  224. }
  225. spam = args[0];
  226. skip_optional_pos:
  227. return_value = gh_119213_getargs_impl(module, spam);
  228. exit:
  229. return return_value;
  230. }
  231. /*[clinic end generated code: output=1fa5cb831dbb391f input=a9049054013a1b77]*/