From 1d3afb7ed238368ddc7cf8334319ff4eec317ad4 Mon Sep 17 00:00:00 2001 From: Donghee Na Date: Wed, 14 Feb 2024 23:51:56 +0900 Subject: [PATCH] Address code review --- .../2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst | 2 ++ Python/bytecodes.c | 6 ++++-- Python/executor_cases.c.h | 4 +++- Python/generated_cases.c.h | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst b/Misc/NEWS.d/next/Core and Builtins/2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst new file mode 100644 index 000000000000000..54ed7baa952668c --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-02-14-23-50-55.gh-issue-112087.H_4W_v.rst @@ -0,0 +1,2 @@ +For an empty reverse iterator for list will be reduced to :func:`reversed`. +Patch by Donghee N diff --git a/Python/bytecodes.c b/Python/bytecodes.c index a453de5d6d7d377..ff47ade1a0757fa 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -2614,7 +2614,7 @@ dummy_func( assert(Py_TYPE(iter) == &PyListIter_Type); STAT_INC(FOR_ITER, hit); PyListObject *seq = it->it_seq; - if (seq == NULL || it->it_index >= PyList_GET_SIZE(seq)) { + if ((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)) { #ifndef Py_GIL_DISABLED if (seq != NULL) { it->it_seq = NULL; @@ -2635,8 +2635,10 @@ dummy_func( _PyListIterObject *it = (_PyListIterObject *)iter; assert(Py_TYPE(iter) == &PyListIter_Type); PyListObject *seq = it->it_seq; + #ifndef Py_GIL_DISABLED DEOPT_IF(seq == NULL); - DEOPT_IF(it->it_index >= PyList_GET_SIZE(seq)); + #endif + DEOPT_IF((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)); } op(_ITER_NEXT_LIST, (iter -- iter, next)) { diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 2d914b82dbf88f4..1dfeafc8b1a9905 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -2201,8 +2201,10 @@ _PyListIterObject *it = (_PyListIterObject *)iter; assert(Py_TYPE(iter) == &PyListIter_Type); PyListObject *seq = it->it_seq; + #ifndef Py_GIL_DISABLED if (seq == NULL) goto deoptimize; - if (it->it_index >= PyList_GET_SIZE(seq)) goto deoptimize; + #endif + if ((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)) goto deoptimize; break; } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index c23c8bf99c8affd..6a2adc5cbb2b3fc 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2552,7 +2552,7 @@ assert(Py_TYPE(iter) == &PyListIter_Type); STAT_INC(FOR_ITER, hit); PyListObject *seq = it->it_seq; - if (seq == NULL || it->it_index >= PyList_GET_SIZE(seq)) { + if ((size_t)it->it_index >= (size_t)PyList_GET_SIZE(seq)) { #ifndef Py_GIL_DISABLED if (seq != NULL) { it->it_seq = NULL;