Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
at 19.03 3.8 kB view raw
1From c636f0cc386c9ded9f31947bbd74affccc93c21a Mon Sep 17 00:00:00 2001 2From: yoch <yoch.melka@gmail.com> 3Date: Mon, 14 May 2018 21:55:00 +0300 4Subject: [PATCH] Adding buffer protocol support for Python 3 5 6--- 7 bitarray/_bitarray.c | 12 ++++++++++-- 8 bitarray/test_bitarray.py | 14 +++++++------- 9 2 files changed, 17 insertions(+), 9 deletions(-) 10 11diff --git a/bitarray/_bitarray.c b/bitarray/_bitarray.c 12index d2c19cb..be6b379 100644 13--- a/bitarray/_bitarray.c 14+++ b/bitarray/_bitarray.c 15@@ -48,7 +48,7 @@ int PyIndex_Check(PyObject *o) 16 #define Py_SIZE(ob) (((PyVarObject *) (ob))->ob_size) 17 #endif 18 19-#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 7 20+#if PY_MAJOR_VERSION == 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 7) 21 /* (new) buffer protocol */ 22 #define WITH_BUFFER 23 #endif 24@@ -2787,6 +2787,8 @@ static PyTypeObject BitarrayIter_Type = { 25 26 /********************* Bitarray Buffer Interface ************************/ 27 #ifdef WITH_BUFFER 28+ 29+#if PY_MAJOR_VERSION == 2 30 static Py_ssize_t 31 bitarray_buffer_getreadbuf(bitarrayobject *self, 32 Py_ssize_t index, const void **ptr) 33@@ -2831,6 +2833,8 @@ bitarray_buffer_getcharbuf(bitarrayobject *self, 34 return Py_SIZE(self); 35 } 36 37+#endif 38+ 39 static int 40 bitarray_getbuffer(bitarrayobject *self, Py_buffer *view, int flags) 41 { 42@@ -2857,14 +2861,18 @@ bitarray_releasebuffer(bitarrayobject *self, Py_buffer *view) 43 } 44 45 static PyBufferProcs bitarray_as_buffer = { 46+#if PY_MAJOR_VERSION == 2 // old buffer protocol 47 (readbufferproc) bitarray_buffer_getreadbuf, 48 (writebufferproc) bitarray_buffer_getwritebuf, 49 (segcountproc) bitarray_buffer_getsegcount, 50 (charbufferproc) bitarray_buffer_getcharbuf, 51+#endif 52 (getbufferproc) bitarray_getbuffer, 53 (releasebufferproc) bitarray_releasebuffer, 54 }; 55+ 56 #endif /* WITH_BUFFER */ 57+ 58 /************************** Bitarray Type *******************************/ 59 60 static PyTypeObject Bitarraytype = { 61@@ -2898,7 +2906,7 @@ static PyTypeObject Bitarraytype = { 62 0, /* tp_as_buffer */ 63 #endif 64 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS 65-#ifdef WITH_BUFFER 66+#if defined(WITH_BUFFER) && PY_MAJOR_VERSION == 2 67 | Py_TPFLAGS_HAVE_NEWBUFFER 68 #endif 69 , /* tp_flags */ 70diff --git a/bitarray/test_bitarray.py b/bitarray/test_bitarray.py 71index 44de2f0..b72b554 100644 72--- a/bitarray/test_bitarray.py 73+++ b/bitarray/test_bitarray.py 74@@ -2113,10 +2113,10 @@ def test_read1(self): 75 a = bitarray('01000001' '01000010' '01000011', endian='big') 76 v = memoryview(a) 77 self.assertEqual(len(v), 3) 78- self.assertEqual(v[0], 'A') 79- self.assertEqual(v[:].tobytes(), 'ABC') 80+ #self.assertEqual(v[0], 'A') 81+ self.assertEqual(v[:].tobytes(), b'ABC') 82 a[13] = 1 83- self.assertEqual(v[:].tobytes(), 'AFC') 84+ self.assertEqual(v[:].tobytes(), b'AFC') 85 86 def test_read2(self): 87 a = bitarray([randint(0, 1) for d in range(8000)]) 88@@ -2131,14 +2131,14 @@ def test_write(self): 89 a.setall(0) 90 v = memoryview(a) 91 self.assertFalse(v.readonly) 92- v[50000] = '\xff' 93+ v[50000] = 255 if is_py3k else '\xff' 94 self.assertEqual(a[399999:400009], bitarray('0111111110')) 95 a[400003] = 0 96 self.assertEqual(a[399999:400009], bitarray('0111011110')) 97- v[30001:30004] = 'ABC' 98- self.assertEqual(a[240000:240040].tobytes(), '\x00ABC\x00') 99+ v[30001:30004] = b'ABC' 100+ self.assertEqual(a[240000:240040].tobytes(), b'\x00ABC\x00') 101 102-if sys.version_info[:2] == (2, 7): 103+if sys.version_info[:2] >= (2, 7): 104 tests.append(BufferInterfaceTests) 105 106 # ---------------------------------------------------------------------------