(root)/
Python-3.12.0/
Objects/
clinic/
listobject.c.h
       1  /*[clinic input]
       2  preserve
       3  [clinic start generated code]*/
       4  
       5  #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
       6  #  include "pycore_gc.h"            // PyGC_Head
       7  #  include "pycore_runtime.h"       // _Py_ID()
       8  #endif
       9  
      10  
      11  PyDoc_STRVAR(list_insert__doc__,
      12  "insert($self, index, object, /)\n"
      13  "--\n"
      14  "\n"
      15  "Insert object before index.");
      16  
      17  #define LIST_INSERT_METHODDEF    \
      18      {"insert", _PyCFunction_CAST(list_insert), METH_FASTCALL, list_insert__doc__},
      19  
      20  static PyObject *
      21  list_insert_impl(PyListObject *self, Py_ssize_t index, PyObject *object);
      22  
      23  static PyObject *
      24  list_insert(PyListObject *self, PyObject *const *args, Py_ssize_t nargs)
      25  {
      26      PyObject *return_value = NULL;
      27      Py_ssize_t index;
      28      PyObject *object;
      29  
      30      if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) {
      31          goto exit;
      32      }
      33      {
      34          Py_ssize_t ival = -1;
      35          PyObject *iobj = _PyNumber_Index(args[0]);
      36          if (iobj != NULL) {
      37              ival = PyLong_AsSsize_t(iobj);
      38              Py_DECREF(iobj);
      39          }
      40          if (ival == -1 && PyErr_Occurred()) {
      41              goto exit;
      42          }
      43          index = ival;
      44      }
      45      object = args[1];
      46      return_value = list_insert_impl(self, index, object);
      47  
      48  exit:
      49      return return_value;
      50  }
      51  
      52  PyDoc_STRVAR(list_clear__doc__,
      53  "clear($self, /)\n"
      54  "--\n"
      55  "\n"
      56  "Remove all items from list.");
      57  
      58  #define LIST_CLEAR_METHODDEF    \
      59      {"clear", (PyCFunction)list_clear, METH_NOARGS, list_clear__doc__},
      60  
      61  static PyObject *
      62  list_clear_impl(PyListObject *self);
      63  
      64  static PyObject *
      65  list_clear(PyListObject *self, PyObject *Py_UNUSED(ignored))
      66  {
      67      return list_clear_impl(self);
      68  }
      69  
      70  PyDoc_STRVAR(list_copy__doc__,
      71  "copy($self, /)\n"
      72  "--\n"
      73  "\n"
      74  "Return a shallow copy of the list.");
      75  
      76  #define LIST_COPY_METHODDEF    \
      77      {"copy", (PyCFunction)list_copy, METH_NOARGS, list_copy__doc__},
      78  
      79  static PyObject *
      80  list_copy_impl(PyListObject *self);
      81  
      82  static PyObject *
      83  list_copy(PyListObject *self, PyObject *Py_UNUSED(ignored))
      84  {
      85      return list_copy_impl(self);
      86  }
      87  
      88  PyDoc_STRVAR(list_append__doc__,
      89  "append($self, object, /)\n"
      90  "--\n"
      91  "\n"
      92  "Append object to the end of the list.");
      93  
      94  #define LIST_APPEND_METHODDEF    \
      95      {"append", (PyCFunction)list_append, METH_O, list_append__doc__},
      96  
      97  PyDoc_STRVAR(list_extend__doc__,
      98  "extend($self, iterable, /)\n"
      99  "--\n"
     100  "\n"
     101  "Extend list by appending elements from the iterable.");
     102  
     103  #define LIST_EXTEND_METHODDEF    \
     104      {"extend", (PyCFunction)list_extend, METH_O, list_extend__doc__},
     105  
     106  PyDoc_STRVAR(list_pop__doc__,
     107  "pop($self, index=-1, /)\n"
     108  "--\n"
     109  "\n"
     110  "Remove and return item at index (default last).\n"
     111  "\n"
     112  "Raises IndexError if list is empty or index is out of range.");
     113  
     114  #define LIST_POP_METHODDEF    \
     115      {"pop", _PyCFunction_CAST(list_pop), METH_FASTCALL, list_pop__doc__},
     116  
     117  static PyObject *
     118  list_pop_impl(PyListObject *self, Py_ssize_t index);
     119  
     120  static PyObject *
     121  list_pop(PyListObject *self, PyObject *const *args, Py_ssize_t nargs)
     122  {
     123      PyObject *return_value = NULL;
     124      Py_ssize_t index = -1;
     125  
     126      if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) {
     127          goto exit;
     128      }
     129      if (nargs < 1) {
     130          goto skip_optional;
     131      }
     132      {
     133          Py_ssize_t ival = -1;
     134          PyObject *iobj = _PyNumber_Index(args[0]);
     135          if (iobj != NULL) {
     136              ival = PyLong_AsSsize_t(iobj);
     137              Py_DECREF(iobj);
     138          }
     139          if (ival == -1 && PyErr_Occurred()) {
     140              goto exit;
     141          }
     142          index = ival;
     143      }
     144  skip_optional:
     145      return_value = list_pop_impl(self, index);
     146  
     147  exit:
     148      return return_value;
     149  }
     150  
     151  PyDoc_STRVAR(list_sort__doc__,
     152  "sort($self, /, *, key=None, reverse=False)\n"
     153  "--\n"
     154  "\n"
     155  "Sort the list in ascending order and return None.\n"
     156  "\n"
     157  "The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n"
     158  "order of two equal elements is maintained).\n"
     159  "\n"
     160  "If a key function is given, apply it once to each list item and sort them,\n"
     161  "ascending or descending, according to their function values.\n"
     162  "\n"
     163  "The reverse flag can be set to sort in descending order.");
     164  
     165  #define LIST_SORT_METHODDEF    \
     166      {"sort", _PyCFunction_CAST(list_sort), METH_FASTCALL|METH_KEYWORDS, list_sort__doc__},
     167  
     168  static PyObject *
     169  list_sort_impl(PyListObject *self, PyObject *keyfunc, int reverse);
     170  
     171  static PyObject *
     172  list_sort(PyListObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
     173  {
     174      PyObject *return_value = NULL;
     175      #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
     176  
     177      #define NUM_KEYWORDS 2
     178      static struct {
     179          PyGC_Head _this_is_not_used;
     180          PyObject_VAR_HEAD
     181          PyObject *ob_item[NUM_KEYWORDS];
     182      } _kwtuple = {
     183          .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
     184          .ob_item = { &_Py_ID(key), &_Py_ID(reverse), },
     185      };
     186      #undef NUM_KEYWORDS
     187      #define KWTUPLE (&_kwtuple.ob_base.ob_base)
     188  
     189      #else  // !Py_BUILD_CORE
     190      #  define KWTUPLE NULL
     191      #endif  // !Py_BUILD_CORE
     192  
     193      static const char * const _keywords[] = {"key", "reverse", NULL};
     194      static _PyArg_Parser _parser = {
     195          .keywords = _keywords,
     196          .fname = "sort",
     197          .kwtuple = KWTUPLE,
     198      };
     199      #undef KWTUPLE
     200      PyObject *argsbuf[2];
     201      Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
     202      PyObject *keyfunc = Py_None;
     203      int reverse = 0;
     204  
     205      args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 0, 0, argsbuf);
     206      if (!args) {
     207          goto exit;
     208      }
     209      if (!noptargs) {
     210          goto skip_optional_kwonly;
     211      }
     212      if (args[0]) {
     213          keyfunc = args[0];
     214          if (!--noptargs) {
     215              goto skip_optional_kwonly;
     216          }
     217      }
     218      reverse = PyObject_IsTrue(args[1]);
     219      if (reverse < 0) {
     220          goto exit;
     221      }
     222  skip_optional_kwonly:
     223      return_value = list_sort_impl(self, keyfunc, reverse);
     224  
     225  exit:
     226      return return_value;
     227  }
     228  
     229  PyDoc_STRVAR(list_reverse__doc__,
     230  "reverse($self, /)\n"
     231  "--\n"
     232  "\n"
     233  "Reverse *IN PLACE*.");
     234  
     235  #define LIST_REVERSE_METHODDEF    \
     236      {"reverse", (PyCFunction)list_reverse, METH_NOARGS, list_reverse__doc__},
     237  
     238  static PyObject *
     239  list_reverse_impl(PyListObject *self);
     240  
     241  static PyObject *
     242  list_reverse(PyListObject *self, PyObject *Py_UNUSED(ignored))
     243  {
     244      return list_reverse_impl(self);
     245  }
     246  
     247  PyDoc_STRVAR(list_index__doc__,
     248  "index($self, value, start=0, stop=sys.maxsize, /)\n"
     249  "--\n"
     250  "\n"
     251  "Return first index of value.\n"
     252  "\n"
     253  "Raises ValueError if the value is not present.");
     254  
     255  #define LIST_INDEX_METHODDEF    \
     256      {"index", _PyCFunction_CAST(list_index), METH_FASTCALL, list_index__doc__},
     257  
     258  static PyObject *
     259  list_index_impl(PyListObject *self, PyObject *value, Py_ssize_t start,
     260                  Py_ssize_t stop);
     261  
     262  static PyObject *
     263  list_index(PyListObject *self, PyObject *const *args, Py_ssize_t nargs)
     264  {
     265      PyObject *return_value = NULL;
     266      PyObject *value;
     267      Py_ssize_t start = 0;
     268      Py_ssize_t stop = PY_SSIZE_T_MAX;
     269  
     270      if (!_PyArg_CheckPositional("index", nargs, 1, 3)) {
     271          goto exit;
     272      }
     273      value = args[0];
     274      if (nargs < 2) {
     275          goto skip_optional;
     276      }
     277      if (!_PyEval_SliceIndexNotNone(args[1], &start)) {
     278          goto exit;
     279      }
     280      if (nargs < 3) {
     281          goto skip_optional;
     282      }
     283      if (!_PyEval_SliceIndexNotNone(args[2], &stop)) {
     284          goto exit;
     285      }
     286  skip_optional:
     287      return_value = list_index_impl(self, value, start, stop);
     288  
     289  exit:
     290      return return_value;
     291  }
     292  
     293  PyDoc_STRVAR(list_count__doc__,
     294  "count($self, value, /)\n"
     295  "--\n"
     296  "\n"
     297  "Return number of occurrences of value.");
     298  
     299  #define LIST_COUNT_METHODDEF    \
     300      {"count", (PyCFunction)list_count, METH_O, list_count__doc__},
     301  
     302  PyDoc_STRVAR(list_remove__doc__,
     303  "remove($self, value, /)\n"
     304  "--\n"
     305  "\n"
     306  "Remove first occurrence of value.\n"
     307  "\n"
     308  "Raises ValueError if the value is not present.");
     309  
     310  #define LIST_REMOVE_METHODDEF    \
     311      {"remove", (PyCFunction)list_remove, METH_O, list_remove__doc__},
     312  
     313  PyDoc_STRVAR(list___init____doc__,
     314  "list(iterable=(), /)\n"
     315  "--\n"
     316  "\n"
     317  "Built-in mutable sequence.\n"
     318  "\n"
     319  "If no argument is given, the constructor creates a new empty list.\n"
     320  "The argument must be an iterable if specified.");
     321  
     322  static int
     323  list___init___impl(PyListObject *self, PyObject *iterable);
     324  
     325  static int
     326  list___init__(PyObject *self, PyObject *args, PyObject *kwargs)
     327  {
     328      int return_value = -1;
     329      PyTypeObject *base_tp = &PyList_Type;
     330      PyObject *iterable = NULL;
     331  
     332      if ((Py_IS_TYPE(self, base_tp) ||
     333           Py_TYPE(self)->tp_new == base_tp->tp_new) &&
     334          !_PyArg_NoKeywords("list", kwargs)) {
     335          goto exit;
     336      }
     337      if (!_PyArg_CheckPositional("list", PyTuple_GET_SIZE(args), 0, 1)) {
     338          goto exit;
     339      }
     340      if (PyTuple_GET_SIZE(args) < 1) {
     341          goto skip_optional;
     342      }
     343      iterable = PyTuple_GET_ITEM(args, 0);
     344  skip_optional:
     345      return_value = list___init___impl((PyListObject *)self, iterable);
     346  
     347  exit:
     348      return return_value;
     349  }
     350  
     351  PyDoc_STRVAR(list___sizeof____doc__,
     352  "__sizeof__($self, /)\n"
     353  "--\n"
     354  "\n"
     355  "Return the size of the list in memory, in bytes.");
     356  
     357  #define LIST___SIZEOF___METHODDEF    \
     358      {"__sizeof__", (PyCFunction)list___sizeof__, METH_NOARGS, list___sizeof____doc__},
     359  
     360  static PyObject *
     361  list___sizeof___impl(PyListObject *self);
     362  
     363  static PyObject *
     364  list___sizeof__(PyListObject *self, PyObject *Py_UNUSED(ignored))
     365  {
     366      return list___sizeof___impl(self);
     367  }
     368  
     369  PyDoc_STRVAR(list___reversed____doc__,
     370  "__reversed__($self, /)\n"
     371  "--\n"
     372  "\n"
     373  "Return a reverse iterator over the list.");
     374  
     375  #define LIST___REVERSED___METHODDEF    \
     376      {"__reversed__", (PyCFunction)list___reversed__, METH_NOARGS, list___reversed____doc__},
     377  
     378  static PyObject *
     379  list___reversed___impl(PyListObject *self);
     380  
     381  static PyObject *
     382  list___reversed__(PyListObject *self, PyObject *Py_UNUSED(ignored))
     383  {
     384      return list___reversed___impl(self);
     385  }
     386  /*[clinic end generated code: output=2ca109d8acc775bc input=a9049054013a1b77]*/