Skip to content

Commit 0dd7911

Browse files
committed
Replace blocklist with allowlist
1 parent 2294542 commit 0dd7911

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

Objects/memoryobject.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,25 +3101,6 @@ cmp_rec(const char *p, const char *q,
31013101
return 1;
31023102
}
31033103

3104-
static int
3105-
is_float_format(const char *format)
3106-
{
3107-
if (format == NULL) {
3108-
return 0;
3109-
}
3110-
if (strcmp("d", format) == 0) {
3111-
return 1;
3112-
}
3113-
if (strcmp("f", format) == 0) {
3114-
return 1;
3115-
}
3116-
if (strcmp("e", format) == 0) {
3117-
return 1;
3118-
}
3119-
return 0;
3120-
}
3121-
3122-
31233104
static PyObject *
31243105
memory_richcompare(PyObject *v, PyObject *w, int op)
31253106
{
@@ -3143,10 +3124,24 @@ memory_richcompare(PyObject *v, PyObject *w, int op)
31433124

31443125
// A memoryview is equal to itself: there is no need to compare individual
31453126
// values. This is not true for float values since they can be NaN, and NaN
3146-
// is not equal to itself.
3147-
if (v == w && !is_float_format(vv->format)) {
3148-
equal = 1;
3149-
goto result;
3127+
// is not equal to itself. So only use this optimization on format known to
3128+
// not use floats.
3129+
if (v == w) {
3130+
int can_compare_ptrs;
3131+
const char *format = vv->format;
3132+
if (format != NULL) {
3133+
// Exclude formats "d" (double), "f" (float), "e" (16-bit float)
3134+
// and "P" (void*)
3135+
can_compare_ptrs = (strchr("bBchHiIlLnNqQ?", format[0]) != NULL
3136+
&& format[1] == 0);
3137+
}
3138+
else {
3139+
can_compare_ptrs = 1;
3140+
}
3141+
if (can_compare_ptrs) {
3142+
equal = 1;
3143+
goto result;
3144+
}
31503145
}
31513146

31523147
if (PyMemoryView_Check(w)) {

0 commit comments

Comments
 (0)