about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/python-modules/bitarray/0001-Buffer-Protocol-Py3.patch
blob: e1019115ac74647b634fc1b80f625a16441bae06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
From c636f0cc386c9ded9f31947bbd74affccc93c21a Mon Sep 17 00:00:00 2001
From: yoch <yoch.melka@gmail.com>
Date: Mon, 14 May 2018 21:55:00 +0300
Subject: [PATCH] Adding buffer protocol support for Python 3

---
 bitarray/_bitarray.c      | 12 ++++++++++--
 bitarray/test_bitarray.py | 14 +++++++-------
 2 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/bitarray/_bitarray.c b/bitarray/_bitarray.c
index d2c19cb..be6b379 100644
--- a/bitarray/_bitarray.c
+++ b/bitarray/_bitarray.c
@@ -48,7 +48,7 @@ int PyIndex_Check(PyObject *o)
 #define Py_SIZE(ob)   (((PyVarObject *) (ob))->ob_size)
 #endif
 
-#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 7
+#if PY_MAJOR_VERSION == 3 || (PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION == 7)
 /* (new) buffer protocol */
 #define WITH_BUFFER
 #endif
@@ -2787,6 +2787,8 @@ static PyTypeObject BitarrayIter_Type = {
 
 /********************* Bitarray Buffer Interface ************************/
 #ifdef WITH_BUFFER
+
+#if PY_MAJOR_VERSION == 2
 static Py_ssize_t
 bitarray_buffer_getreadbuf(bitarrayobject *self,
                            Py_ssize_t index, const void **ptr)
@@ -2831,6 +2833,8 @@ bitarray_buffer_getcharbuf(bitarrayobject *self,
     return Py_SIZE(self);
 }
 
+#endif
+
 static int
 bitarray_getbuffer(bitarrayobject *self, Py_buffer *view, int flags)
 {
@@ -2857,14 +2861,18 @@ bitarray_releasebuffer(bitarrayobject *self, Py_buffer *view)
 }
 
 static PyBufferProcs bitarray_as_buffer = {
+#if PY_MAJOR_VERSION == 2   // old buffer protocol
     (readbufferproc) bitarray_buffer_getreadbuf,
     (writebufferproc) bitarray_buffer_getwritebuf,
     (segcountproc) bitarray_buffer_getsegcount,
     (charbufferproc) bitarray_buffer_getcharbuf,
+#endif
     (getbufferproc) bitarray_getbuffer,
     (releasebufferproc) bitarray_releasebuffer,
 };
+
 #endif  /* WITH_BUFFER */
+
 /************************** Bitarray Type *******************************/
 
 static PyTypeObject Bitarraytype = {
@@ -2898,7 +2906,7 @@ static PyTypeObject Bitarraytype = {
     0,                                        /* tp_as_buffer */
 #endif
     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS
-#ifdef WITH_BUFFER
+#if defined(WITH_BUFFER) && PY_MAJOR_VERSION == 2
     | Py_TPFLAGS_HAVE_NEWBUFFER
 #endif
     ,                                         /* tp_flags */
diff --git a/bitarray/test_bitarray.py b/bitarray/test_bitarray.py
index 44de2f0..b72b554 100644
--- a/bitarray/test_bitarray.py
+++ b/bitarray/test_bitarray.py
@@ -2113,10 +2113,10 @@ def test_read1(self):
         a = bitarray('01000001' '01000010' '01000011', endian='big')
         v = memoryview(a)
         self.assertEqual(len(v), 3)
-        self.assertEqual(v[0], 'A')
-        self.assertEqual(v[:].tobytes(), 'ABC')
+        #self.assertEqual(v[0], 'A')
+        self.assertEqual(v[:].tobytes(), b'ABC')
         a[13] = 1
-        self.assertEqual(v[:].tobytes(), 'AFC')
+        self.assertEqual(v[:].tobytes(), b'AFC')
 
     def test_read2(self):
         a = bitarray([randint(0, 1) for d in range(8000)])
@@ -2131,14 +2131,14 @@ def test_write(self):
         a.setall(0)
         v = memoryview(a)
         self.assertFalse(v.readonly)
-        v[50000] = '\xff'
+        v[50000] = 255 if is_py3k else '\xff'
         self.assertEqual(a[399999:400009], bitarray('0111111110'))
         a[400003] = 0
         self.assertEqual(a[399999:400009], bitarray('0111011110'))
-        v[30001:30004] = 'ABC'
-        self.assertEqual(a[240000:240040].tobytes(), '\x00ABC\x00')
+        v[30001:30004] = b'ABC'
+        self.assertEqual(a[240000:240040].tobytes(), b'\x00ABC\x00')
 
-if sys.version_info[:2] == (2, 7):
+if sys.version_info[:2] >= (2, 7):
     tests.append(BufferInterfaceTests)
 
 # ---------------------------------------------------------------------------