Skip to content

Commit aeba29e

Browse files
eendebakptclaude
andcommitted
Use _PyNumber_IndexAsSsize_t() in mmap and memoryview
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 31a7552 commit aeba29e

2 files changed

Lines changed: 4 additions & 36 deletions

File tree

‎Modules/mmapmodule.c‎

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,6 @@ typedef struct {
139139
#include "clinic/mmapmodule.c.h"
140140

141141

142-
/* Return a Py_ssize_t from the object arg. This conversion logic is similar
143-
to what AC uses for `Py_ssize_t` arguments.
144-
145-
Returns -1 on error. Use PyErr_Occurred() to disambiguate.
146-
*/
147-
static Py_ssize_t
148-
_As_Py_ssize_t(PyObject *arg) {
149-
assert(arg != NULL);
150-
Py_ssize_t ival = -1;
151-
PyObject *iobj = _PyNumber_Index(arg);
152-
if (iobj != NULL) {
153-
ival = PyLong_AsSsize_t(iobj);
154-
Py_DECREF(iobj);
155-
}
156-
return ival;
157-
}
158-
159142
static void
160143
mmap_object_dealloc(PyObject *op)
161144
{
@@ -603,13 +586,13 @@ mmap_gfind_lock_held(mmap_object *self, Py_buffer *view, PyObject *start_obj,
603586

604587
CHECK_VALID(NULL);
605588
if (start_obj != Py_None) {
606-
start = _As_Py_ssize_t(start_obj);
589+
start = _PyNumber_IndexAsSsize_t(start_obj);
607590
if (start == -1 && PyErr_Occurred()) {
608591
return NULL;
609592
}
610593

611594
if (end_obj != Py_None) {
612-
end = _As_Py_ssize_t(end_obj);
595+
end = _PyNumber_IndexAsSsize_t(end_obj);
613596
if (end == -1 && PyErr_Occurred()) {
614597
return NULL;
615598
}
@@ -1383,7 +1366,7 @@ mmap_mmap_madvise_impl(mmap_object *self, int option, Py_ssize_t start,
13831366
if (length_obj == Py_None) {
13841367
length = self->size;
13851368
} else {
1386-
length = _As_Py_ssize_t(length_obj);
1369+
length = _PyNumber_IndexAsSsize_t(length_obj);
13871370
if (length == -1 && PyErr_Occurred()) {
13881371
return NULL;
13891372
}

‎Objects/memoryobject.c‎

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,21 +1946,6 @@ pylong_as_llu(PyObject *item)
19461946
return llu;
19471947
}
19481948

1949-
static Py_ssize_t
1950-
pylong_as_zd(PyObject *item)
1951-
{
1952-
PyObject *tmp;
1953-
Py_ssize_t zd;
1954-
1955-
tmp = _PyNumber_Index(item);
1956-
if (tmp == NULL)
1957-
return -1;
1958-
1959-
zd = PyLong_AsSsize_t(tmp);
1960-
Py_DECREF(tmp);
1961-
return zd;
1962-
}
1963-
19641949
static size_t
19651950
pylong_as_zu(PyObject *item)
19661951
{
@@ -2187,7 +2172,7 @@ pack_single(PyMemoryViewObject *self, char *ptr, PyObject *item, const char *fmt
21872172

21882173
/* ssize_t and size_t */
21892174
case 'n':
2190-
zd = pylong_as_zd(item);
2175+
zd = _PyNumber_IndexAsSsize_t(item);
21912176
if (zd == -1 && PyErr_Occurred())
21922177
goto err_occurred;
21932178
CHECK_RELEASED_INT_AGAIN(self);

0 commit comments

Comments
 (0)