Skip to content

Commit 2316fab

Browse files
committed
Address review
* Optimize also "P" format * Test also "m != m" * Handle native formats such as "@b"
1 parent 102f26d commit 2316fab

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Lib/test/test_memoryview.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,17 +584,20 @@ def test_compare_equal(self):
584584
a = array.array(int_format, [1, 2, 3])
585585
m = memoryview(a)
586586
self.assertTrue(m == m)
587+
self.assertFalse(m != m)
587588

588589
for float_format in 'fd':
589590
with self.subTest(format=int_format):
590591
a = array.array(float_format, [1.0, 2.0, float('nan')])
591592
m = memoryview(a)
592593
# nan is not equal to nan
593594
self.assertFalse(m == m)
595+
self.assertTrue(m != m)
594596

595597
a = array.array(float_format, [1.0, 2.0, 3.0])
596598
m = memoryview(a)
597599
self.assertTrue(m == m)
600+
self.assertFalse(m != m)
598601

599602

600603
class BytesMemorySliceTest(unittest.TestCase,

Objects/memoryobject.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3131,9 +3131,12 @@ memory_richcompare(PyObject *v, PyObject *w, int op)
31313131
int can_compare_ptr;
31323132
const char *format = vv->format;
31333133
if (format != NULL) {
3134+
if (*format == '@') {
3135+
format++;
3136+
}
31343137
// Include only formats known by struct, exclude formats "d" (double),
3135-
// "f" (float), "e" (16-bit float) and "P" (void*)
3136-
can_compare_ptr = (strchr("bBchHiIlLnNqQ?", format[0]) != NULL
3138+
// "f" (float), "e" (16-bit float)
3139+
can_compare_ptr = (strchr("bBchHiIlLnNPqQ?", format[0]) != NULL
31373140
&& format[1] == 0);
31383141
}
31393142
else {

0 commit comments

Comments
 (0)