funcobject.c.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(func_new__doc__,
  9. "function(code, globals, name=None, argdefs=None, closure=None)\n"
  10. "--\n"
  11. "\n"
  12. "Create a function object.\n"
  13. "\n"
  14. " code\n"
  15. " a code object\n"
  16. " globals\n"
  17. " the globals dictionary\n"
  18. " name\n"
  19. " a string that overrides the name from the code object\n"
  20. " argdefs\n"
  21. " a tuple that specifies the default argument values\n"
  22. " closure\n"
  23. " a tuple that supplies the bindings for free variables");
  24. static PyObject *
  25. func_new_impl(PyTypeObject *type, PyCodeObject *code, PyObject *globals,
  26. PyObject *name, PyObject *defaults, PyObject *closure);
  27. static PyObject *
  28. func_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  29. {
  30. PyObject *return_value = NULL;
  31. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  32. #define NUM_KEYWORDS 5
  33. static struct {
  34. PyGC_Head _this_is_not_used;
  35. PyObject_VAR_HEAD
  36. PyObject *ob_item[NUM_KEYWORDS];
  37. } _kwtuple = {
  38. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  39. .ob_item = { &_Py_ID(code), &_Py_ID(globals), &_Py_ID(name), &_Py_ID(argdefs), &_Py_ID(closure), },
  40. };
  41. #undef NUM_KEYWORDS
  42. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  43. #else // !Py_BUILD_CORE
  44. # define KWTUPLE NULL
  45. #endif // !Py_BUILD_CORE
  46. static const char * const _keywords[] = {"code", "globals", "name", "argdefs", "closure", NULL};
  47. static _PyArg_Parser _parser = {
  48. .keywords = _keywords,
  49. .fname = "function",
  50. .kwtuple = KWTUPLE,
  51. };
  52. #undef KWTUPLE
  53. PyObject *argsbuf[5];
  54. PyObject * const *fastargs;
  55. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  56. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 2;
  57. PyCodeObject *code;
  58. PyObject *globals;
  59. PyObject *name = Py_None;
  60. PyObject *defaults = Py_None;
  61. PyObject *closure = Py_None;
  62. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 2, 5, 0, argsbuf);
  63. if (!fastargs) {
  64. goto exit;
  65. }
  66. if (!PyObject_TypeCheck(fastargs[0], &PyCode_Type)) {
  67. _PyArg_BadArgument("function", "argument 'code'", (&PyCode_Type)->tp_name, fastargs[0]);
  68. goto exit;
  69. }
  70. code = (PyCodeObject *)fastargs[0];
  71. if (!PyDict_Check(fastargs[1])) {
  72. _PyArg_BadArgument("function", "argument 'globals'", "dict", fastargs[1]);
  73. goto exit;
  74. }
  75. globals = fastargs[1];
  76. if (!noptargs) {
  77. goto skip_optional_pos;
  78. }
  79. if (fastargs[2]) {
  80. name = fastargs[2];
  81. if (!--noptargs) {
  82. goto skip_optional_pos;
  83. }
  84. }
  85. if (fastargs[3]) {
  86. defaults = fastargs[3];
  87. if (!--noptargs) {
  88. goto skip_optional_pos;
  89. }
  90. }
  91. closure = fastargs[4];
  92. skip_optional_pos:
  93. return_value = func_new_impl(type, code, globals, name, defaults, closure);
  94. exit:
  95. return return_value;
  96. }
  97. /*[clinic end generated code: output=777cead7b1f6fad3 input=a9049054013a1b77]*/