projects/aports·blob master · refs

208 lines · 10.8 KiB

1Patch-Source: https://github.com/py-bson/bson/pull/118
2Backported for 0.5.10.
3---
4From d53b91a1148305a2e095d5e2537321cdc16ba546 Mon Sep 17 00:00:00 2001
5From: Daniel Garcia Moreno <[email protected]>
6Date: Thu, 3 Nov 2022 13:38:57 +0100
7Subject: [PATCH] Drop python2 support
8
9See https://github.com/py-bson/bson/issues/115
10---
11 bson/codec.py | 18 ++++++------------
12 bson/network.py | 4 ++--
13 bson/tests/test_array.py | 3 +--
14 bson/tests/test_random_tree.py | 24 +++++++++---------------
15 setup.py | 4 +---
16 5 files changed, 19 insertions(+), 34 deletions(-)
17
18diff --git a/bson/codec.py b/bson/codec.py
19index 6aa7e07..78449bc 100644
20--- a/bson/codec.py
21+++ b/bson/codec.py
22@@ -24,9 +24,6 @@
23 from dateutil.tz import tzutc
24 from binascii import b2a_hex
25
26-from six import integer_types, iterkeys, text_type, PY3
27-from six.moves import xrange
28-
29
30 utc = tzutc()
31
32@@ -132,7 +129,7 @@ def encode_string(value):
33
34 def encode_cstring(value):
35 if not isinstance(value, bytes):
36- value = text_type(value).encode("utf-8")
37+ value = str(value).encode("utf-8")
38 if b"\x00" in value:
39 raise ValueError("Element names may not include NUL bytes.")
40 # A NUL byte is used to delimit our string, accepting one would cause
41@@ -174,7 +171,7 @@ def encode_string_element(name, value):
42
43
44 def _is_string(value):
45- if isinstance(value, text_type):
46+ if isinstance(value, str):
47 return True
48 elif isinstance(value, str) or isinstance(value, bytes):
49 try:
50@@ -189,7 +186,7 @@ def encode_value(name, value, buf, traversal_stack,
51 generator_func, on_unknown=None):
52 if isinstance(value, bool):
53 buf.write(encode_boolean_element(name, value))
54- elif isinstance(value, integer_types):
55+ elif isinstance(value, int):
56 if value < -0x80000000 or 0x7FFFFFFFFFFFFFFF >= value > 0x7fffffff:
57 buf.write(encode_int64_element(name, value))
58 elif value > 0x7FFFFFFFFFFFFFFF:
59@@ -238,7 +235,7 @@ def encode_value(name, value, buf, traversal_stack,
60 def encode_document(obj, traversal_stack, traversal_parent=None,
61 generator_func=None, on_unknown=None):
62 buf = StringIO()
63- key_iter = iterkeys(obj)
64+ key_iter = iter(obj.keys())
65 if generator_func is not None:
66 key_iter = generator_func(obj, traversal_stack)
67 for name in key_iter:
68@@ -256,7 +253,7 @@ def encode_document(obj, traversal_stack, traversal_parent=None,
69 def encode_array(array, traversal_stack, traversal_parent=None,
70 generator_func=None, on_unknown=None):
71 buf = StringIO()
72- for i in xrange(0, len(array)):
73+ for i in range(0, len(array)):
74 value = array[i]
75 traversal_stack.append(TraversalStep(traversal_parent or array, i))
76 encode_value(str(i), value, buf, traversal_stack,
77@@ -295,10 +292,7 @@ def decode_document(data, base, as_array=False):
78
79 element_type = char_struct.unpack(data[base:base + 1])[0]
80
81- if PY3:
82- ll = data.index(0, base + 1) + 1
83- else:
84- ll = data.index("\x00", base + 1) + 1
85+ ll = data.index(0, base + 1) + 1
86 if decode_name:
87 name = data[base + 1:ll - 1]
88 try:
89diff --git a/bson/network.py b/bson/network.py
90index d24e5bd..ccd2ae4 100644
91--- a/bson/network.py
92+++ b/bson/network.py
93@@ -1,7 +1,7 @@
94 #!/usr/bin/env python
95 from struct import unpack
96
97-from six import BytesIO, b
98+from io import BytesIO
99
100 from . import dumps, loads
101
102@@ -56,7 +56,7 @@ def recvbytes(self, bytes_needed, sock_buf = None):
103 part_count = len(chunk)
104
105 if type(chunk) == str:
106- chunk = b(chunk)
107+ chunk = chunk.encode('latin-1')
108
109 if part_count < 1:
110 return None
111diff --git a/bson/tests/test_array.py b/bson/tests/test_array.py
112index 029c5a1..e0eee3e 100644
113--- a/bson/tests/test_array.py
114+++ b/bson/tests/test_array.py
115@@ -2,7 +2,6 @@
116 from unittest import TestCase
117
118 from bson import dumps, loads
119-from six import PY3
120
121
122 class TestArray(TestCase):
123@@ -74,5 +74,5 @@ class TestArray(TestCase):
124
125 def test_encoded_order(self):
126 serialized = dumps(self.doc)
127- expected = repr(serialized)[1:] if PY3 else repr(serialized)
128+ expected = repr(serialized)[1:]
129 self.assertEquals(expected, '\'\\xea\\x08\\x00\\x00\\x04lyrics\\x00\\xdd\\x08\\x00\\x00\\x020\\x00\\x14\\x00\\x00\\x00Viva La Vida lyrics\\x00\\x021\\x00\\x01\\x00\\x00\\x00\\x00\\x022\\x00!\\x00\\x00\\x00 I used to rule the world\\x00\\x023\\x00-\\x00\\x00\\x00 Seas would rise when I gave the word\\x00\\x024\\x00)\\x00\\x00\\x00 Now in the morning I sleep alone\\x00\\x025\\x00(\\x00\\x00\\x00 Sweep the streets I used to own\\x00\\x026\\x00\\x01\\x00\\x00\\x00\\x00\\x027\\x00 \\x00\\x00\\x00 I used to roll the dice\\x00\\x028\\x00)\\x00\\x00\\x00 Feel the fear in my enemy\\\'s eyes\\x00\\x029\\x00\\\'\\x00\\x00\\x00 Listen as the crowd would sing\\x00\\x0210\\x008\\x00\\x00\\x00 "Now the old king is dead! Long live the king!"\\x00\\x0211\\x00\\x01\\x00\\x00\\x00\\x00\\x0212\\x00"\\x00\\x00\\x00 One minute I held the key\\x00\\x0213\\x00)\\x00\\x00\\x00 Next the walls were closed on me\\x00\\x0214\\x00/\\x00\\x00\\x00 And I discovered that my castles stand\\x00\\x0215\\x001\\x00\\x00\\x00 Upon pillars of salt and pillars of sand\\x00\\x0216\\x00\\x01\\x00\\x00\\x00\\x00\\x0217\\x00)\\x00\\x00\\x00 I hear Jerusalem bells a ringing\\x00\\x0218\\x00)\\x00\\x00\\x00 Roman Cavalry choirs are singing\\x00\\x0219\\x00*\\x00\\x00\\x00 Be my mirror, my sword and shield\\x00\\x0220\\x00+\\x00\\x00\\x00 My missionaries in a foreign field\\x00\\x0221\\x00\\x01\\x00\\x00\\x00\\x00\\x0222\\x00(\\x00\\x00\\x00 For some reason I can\\\'t explain\\x00\\x0223\\x00$\\x00\\x00\\x00 Once you go there was never\\x00\\x0224\\x00\\x1d\\x00\\x00\\x00 Never an honest word\\x00\\x0225\\x00,\\x00\\x00\\x00 And that was when I ruled the world\\x00\\x0226\\x00\\x01\\x00\\x00\\x00\\x00\\x0227\\x00(\\x00\\x00\\x00 It was the wicked and wild wind\\x00\\x0228\\x00)\\x00\\x00\\x00 Blew down the doors to let me in\\x00\\x0229\\x001\\x00\\x00\\x00 Shattered windows and the sound of drums\\x00\\x0230\\x000\\x00\\x00\\x00 People couldn\\\'t believe what I\\\'d become\\x00\\x0231\\x00\\x01\\x00\\x00\\x00\\x00\\x0232\\x00\\x1d\\x00\\x00\\x00 Revolutionaries wait\\x00\\x0233\\x00&\\x00\\x00\\x00 For my head on a silver plate\\x00\\x0234\\x00)\\x00\\x00\\x00 Just a puppet on a lonely string\\x00\\x0235\\x00+\\x00\\x00\\x00 Oh who would ever want to be king?\\x00\\x0236\\x00\\x01\\x00\\x00\\x00\\x00\\x0237\\x00)\\x00\\x00\\x00 I hear Jerusalem bells a ringing\\x00\\x0238\\x00)\\x00\\x00\\x00 Roman Cavalry choirs are singing\\x00\\x0239\\x00*\\x00\\x00\\x00 Be my mirror, my sword and shield\\x00\\x0240\\x00+\\x00\\x00\\x00 My missionaries in a foreign field\\x00\\x0241\\x00\\x01\\x00\\x00\\x00\\x00\\x0242\\x00(\\x00\\x00\\x00 For some reason I can\\\'t explain\\x00\\x0243\\x00.\\x00\\x00\\x00 I know Saint Peter won\\\'t call my name\\x00\\x0244\\x00\\x1d\\x00\\x00\\x00 Never an honest word\\x00\\x0245\\x00,\\x00\\x00\\x00 But that was when I ruled the world\\x00\\x0246\\x00\\x01\\x00\\x00\\x00\\x00\\x0247\\x00)\\x00\\x00\\x00 I hear Jerusalem bells a ringing\\x00\\x0248\\x00)\\x00\\x00\\x00 Roman Cavalry choirs are singing\\x00\\x0249\\x00*\\x00\\x00\\x00 Be my mirror, my sword and shield\\x00\\x0250\\x00+\\x00\\x00\\x00 My missionaries in a foreign field\\x00\\x0251\\x00\\x01\\x00\\x00\\x00\\x00\\x0252\\x00(\\x00\\x00\\x00 For some reason I can\\\'t explain\\x00\\x0253\\x00.\\x00\\x00\\x00 I know Saint Peter won\\\'t call my name\\x00\\x0254\\x00\\x1d\\x00\\x00\\x00 Never an honest word\\x00\\x0255\\x00,\\x00\\x00\\x00 But that was when I ruled the world\\x00\\x00\\x00\'')
130
131
132diff --git a/bson/tests/test_random_tree.py b/bson/tests/test_random_tree.py
133index ca9832e..e83e23b 100644
134--- a/bson/tests/test_random_tree.py
135+++ b/bson/tests/test_random_tree.py
136@@ -3,9 +3,6 @@
137 from random import randint
138 from unittest import TestCase
139
140-from six import text_type, PY3
141-from six.moves import xrange
142-
143 from bson import dumps, loads
144
145
146@@ -13,10 +10,10 @@ def populate(parent, howmany, max_children):
147 if howmany > max_children:
148 children = randint(2, max_children)
149 distribution = []
150- for _ in xrange(0, children - 1):
151+ for _ in range(0, children - 1):
152 distribution.append(int(howmany / children))
153 distribution.append(howmany - sum(distribution, 0))
154- for i in xrange(0, children):
155+ for i in range(0, children):
156 steal_target = randint(0, children - 1)
157 while steal_target == i:
158 steal_target = randint(0, children -1)
159@@ -25,7 +22,7 @@ def populate(parent, howmany, max_children):
160 distribution[i] += steal_count
161 distribution[steal_target] -= steal_count
162
163- for i in xrange(0, children):
164+ for i in range(0, children):
165 make_dict = randint(0, 1)
166 if make_dict:
167 baby = {}
168@@ -34,8 +31,7 @@ def populate(parent, howmany, max_children):
169 populate(baby, distribution[i], max_children)
170 if isinstance(parent, dict):
171 key = os.urandom(8)
172- key = "".join(chr(c) for c in hexlify(key)) \
173- if PY3 else key.encode("hex")
174+ key = "".join(chr(c) for c in hexlify(key))
175 parent[key] = baby
176 else:
177 parent.append(baby)
178@@ -44,17 +40,15 @@ def populate(parent, howmany, max_children):
179
180
181 def populate_with_leaves(parent, howmany):
182- for _ in xrange(0, howmany):
183+ for _ in range(0, howmany):
184 leaf = os.urandom(4)
185- leaf = "".join(chr(c) for c in hexlify(leaf)) \
186- if PY3 else leaf.encode("hex")
187+ leaf = "".join(chr(c) for c in hexlify(leaf))
188 make_unicode = randint(0, 1)
189 if make_unicode:
190- leaf = text_type(leaf)
191+ leaf = str(leaf)
192 if isinstance(parent, dict):
193 key = os.urandom(4)
194- key = "".join(chr(c) for c in hexlify(key)) \
195- if PY3 else key.encode("hex")
196+ key = "".join(chr(c) for c in hexlify(key))
197 parent[key] = leaf
198 else:
199 parent.append(leaf)
200@@ -62,7 +56,7 @@ def populate_with_leaves(parent, howmany):
201
202 class TestRandomTree(TestCase):
203 def test_random_tree(self):
204- for _ in xrange(0, 16):
205+ for _ in range(0, 16):
206 p = {}
207 populate(p, 256, 4)
208 sp = dumps(p)