enumobject.c.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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(enum_new__doc__,
  9. "enumerate(iterable, start=0)\n"
  10. "--\n"
  11. "\n"
  12. "Return an enumerate object.\n"
  13. "\n"
  14. " iterable\n"
  15. " an object supporting iteration\n"
  16. "\n"
  17. "The enumerate object yields pairs containing a count (from start, which\n"
  18. "defaults to zero) and a value yielded by the iterable argument.\n"
  19. "\n"
  20. "enumerate is useful for obtaining an indexed list:\n"
  21. " (0, seq[0]), (1, seq[1]), (2, seq[2]), ...");
  22. static PyObject *
  23. enum_new_impl(PyTypeObject *type, PyObject *iterable, PyObject *start);
  24. static PyObject *
  25. enum_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  26. {
  27. PyObject *return_value = NULL;
  28. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  29. #define NUM_KEYWORDS 2
  30. static struct {
  31. PyGC_Head _this_is_not_used;
  32. PyObject_VAR_HEAD
  33. PyObject *ob_item[NUM_KEYWORDS];
  34. } _kwtuple = {
  35. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  36. .ob_item = { &_Py_ID(iterable), &_Py_ID(start), },
  37. };
  38. #undef NUM_KEYWORDS
  39. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  40. #else // !Py_BUILD_CORE
  41. # define KWTUPLE NULL
  42. #endif // !Py_BUILD_CORE
  43. static const char * const _keywords[] = {"iterable", "start", NULL};
  44. static _PyArg_Parser _parser = {
  45. .keywords = _keywords,
  46. .fname = "enumerate",
  47. .kwtuple = KWTUPLE,
  48. };
  49. #undef KWTUPLE
  50. PyObject *argsbuf[2];
  51. PyObject * const *fastargs;
  52. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  53. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
  54. PyObject *iterable;
  55. PyObject *start = 0;
  56. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 2, 0, argsbuf);
  57. if (!fastargs) {
  58. goto exit;
  59. }
  60. iterable = fastargs[0];
  61. if (!noptargs) {
  62. goto skip_optional_pos;
  63. }
  64. start = fastargs[1];
  65. skip_optional_pos:
  66. return_value = enum_new_impl(type, iterable, start);
  67. exit:
  68. return return_value;
  69. }
  70. PyDoc_STRVAR(reversed_new__doc__,
  71. "reversed(sequence, /)\n"
  72. "--\n"
  73. "\n"
  74. "Return a reverse iterator over the values of the given sequence.");
  75. static PyObject *
  76. reversed_new_impl(PyTypeObject *type, PyObject *seq);
  77. static PyObject *
  78. reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  79. {
  80. PyObject *return_value = NULL;
  81. PyTypeObject *base_tp = &PyReversed_Type;
  82. PyObject *seq;
  83. if ((type == base_tp || type->tp_init == base_tp->tp_init) &&
  84. !_PyArg_NoKeywords("reversed", kwargs)) {
  85. goto exit;
  86. }
  87. if (!_PyArg_CheckPositional("reversed", PyTuple_GET_SIZE(args), 1, 1)) {
  88. goto exit;
  89. }
  90. seq = PyTuple_GET_ITEM(args, 0);
  91. return_value = reversed_new_impl(type, seq);
  92. exit:
  93. return return_value;
  94. }
  95. /*[clinic end generated code: output=aba0ddbeab1601e3 input=a9049054013a1b77]*/