Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,14 @@ accepts integers that meet the value restriction ``0 <= x <= 255``).
| ``s.reverse()`` | reverses the items of *s* in | \(4) |
| | place | |
+------------------------------+--------------------------------+---------------------+
| ``s.get(i)`` or |Returns the value for the index | |
| ``s.get(i, x)`` |if the index is in the list, | |
| |otherwise the default value. If | |
| |no default value is set, the | |
| |default value is None, so this | |
| |method will never raise an | |
| |`IndexError` | |
+------------------------------+--------------------------------+---------------------+


Notes:
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_descrtut.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def merge(self, other):
'copy',
'count',
'extend',
'get',
'index',
'insert',
'pop',
Expand Down
20 changes: 20 additions & 0 deletions Objects/listobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3497,6 +3497,24 @@
static PyObject *list_iter(PyObject *seq);
static PyObject *list_subscript(PyObject*, PyObject*);

static PyObject *
list_get(PyListObject *self, PyObject *args) {
Py_ssize_t index;
PyObject *default_value = Py_None;

if (!PyArg_ParseTuple(args, "n|O:get", &index, &default_value)) {
return NULL;
}

if (index < 0 || index >= Py_SIZE(self)) {
Py_INCREF(default_value);
return default_value;
}

Py_INCREF(self->ob_item[index]);
return self->ob_item[index];
}

static PyMethodDef list_methods[] = {
{"__getitem__", list_subscript, METH_O|METH_COEXIST,
PyDoc_STR("__getitem__($self, index, /)\n--\n\nReturn self[index].")},
Expand All @@ -3513,6 +3531,8 @@
LIST_COUNT_METHODDEF
LIST_REVERSE_METHODDEF
LIST_SORT_METHODDEF
{"get", list_get, METH_VARARGS,

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Address sanitizer (ubuntu-24.04)

initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyListObject *, PyObject *)’ {aka ‘struct _object * (*)(PyListObject *, struct _object *)’} [-Wincompatible-pointer-types]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Cross build Linux

initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyListObject *, PyObject *)’ {aka ‘struct _object * (*)(PyListObject *, struct _object *)’} [-Wincompatible-pointer-types]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Hypothesis tests on Ubuntu

initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyListObject *, PyObject *)’ {aka ‘struct _object * (*)(PyListObject *, struct _object *)’} [-Wincompatible-pointer-types]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04-arm)

initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyListObject *, PyObject *)’ {aka ‘struct _object * (*)(PyListObject *, struct _object *)’} [-Wincompatible-pointer-types]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu / build and test (ubuntu-24.04)

initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyListObject *, PyObject *)’ {aka ‘struct _object * (*)(PyListObject *, struct _object *)’} [-Wincompatible-pointer-types]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04)

initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyListObject *, PyObject *)’ {aka ‘struct _object * (*)(PyListObject *, struct _object *)’} [-Wincompatible-pointer-types]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (free-threading) / build and test (ubuntu-24.04-arm)

initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyListObject *, PyObject *)’ {aka ‘struct _object * (*)(PyListObject *, struct _object *)’} [-Wincompatible-pointer-types]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Ubuntu (bolt) / build and test (ubuntu-24.04)

initialization of ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} from incompatible pointer type ‘PyObject * (*)(PyListObject *, PyObject *)’ {aka ‘struct _object * (*)(PyListObject *, struct _object *)’} [-Wincompatible-pointer-types]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'PyObject *(__cdecl *)(PyListObject *,PyObject *)' differs in parameter lists from 'PyCFunction' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Windows / build (arm64)

'PyObject *(__cdecl *)(PyListObject *,PyObject *)' differs in parameter lists from 'PyCFunction' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'PyObject *(__cdecl *)(PyListObject *,PyObject *)' differs in parameter lists from 'PyCFunction' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build (arm64)

'PyObject *(__cdecl *)(PyListObject *,PyObject *)' differs in parameter lists from 'PyCFunction' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'PyObject *(__cdecl *)(PyListObject *,PyObject *)' differs in parameter lists from 'PyCFunction' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Windows / build and test (x64)

'PyObject *(__cdecl *)(PyListObject *,PyObject *)' differs in parameter lists from 'PyCFunction' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'PyObject *(__cdecl *)(PyListObject *,PyObject *)' differs in parameter lists from 'PyCFunction' [D:\a\cpython\cpython\PCbuild\_freeze_module.vcxproj]

Check warning on line 3534 in Objects/listobject.c

View workflow job for this annotation

GitHub Actions / Windows (free-threading) / build and test (x64)

'PyObject *(__cdecl *)(PyListObject *,PyObject *)' differs in parameter lists from 'PyCFunction' [D:\a\cpython\cpython\PCbuild\pythoncore.vcxproj]
PyDoc_STR("get(self, index, default=None)\n--\n\nReturn item at index or default value if index is out of range.")},
{"__class_getitem__", Py_GenericAlias, METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
{NULL, NULL} /* sentinel */
};
Expand Down
Loading