Skip to content

Commit 17d7621

Browse files
committed
Array module's buffer interface can now handle empty arrays.
1 parent 2986cc8 commit 17d7621

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/test/test_re.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,13 @@ def test_bug_817234(self):
601601
self.assertEqual(iter.next().span(), (4, 4))
602602
self.assertRaises(StopIteration, iter.next)
603603

604+
def test_empty_array(self):
605+
# SF buf 1647541
606+
import array
607+
for typecode in 'cbBuhHiIlLfd':
608+
a = array.array(typecode)
609+
self.assertEqual(re.compile("bla").match(a), None)
610+
self.assertEqual(re.compile("").match(a).groups(), ())
604611

605612
def run_re_tests():
606613
from test.re_tests import benchmarks, tests, SUCCEED, FAIL, SYNTAX_ERROR

Modules/arraymodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,8 @@ static PyMappingMethods array_as_mapping = {
17451745
(objobjargproc)array_ass_subscr
17461746
};
17471747

1748+
static const void *emptybuf = "";
1749+
17481750
static Py_ssize_t
17491751
array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)
17501752
{
@@ -1754,6 +1756,8 @@ array_buffer_getreadbuf(arrayobject *self, Py_ssize_t index, const void **ptr)
17541756
return -1;
17551757
}
17561758
*ptr = (void *)self->ob_item;
1759+
if (*ptr == NULL)
1760+
*ptr = emptybuf;
17571761
return self->ob_size*self->ob_descr->itemsize;
17581762
}
17591763

@@ -1766,6 +1770,8 @@ array_buffer_getwritebuf(arrayobject *self, Py_ssize_t index, const void **ptr)
17661770
return -1;
17671771
}
17681772
*ptr = (void *)self->ob_item;
1773+
if (*ptr == NULL)
1774+
*ptr = emptybuf;
17691775
return self->ob_size*self->ob_descr->itemsize;
17701776
}
17711777

0 commit comments

Comments
 (0)