mirror of
https://gitlab.alpinelinux.org/alpine/aports.git
synced 2026-04-30 18:13:53 +02:00
Replace removed `pkgutil` to fix `module 'pkgutil' has no attribute 'find_loader'` error. Remove dependency on the deprecated `six` library. Update `unittest.assertEquals` function calls. Switch source url to GitHub repo and run tests. Ref: https://gitlab.alpinelinux.org/alpine/aports/-/issues/18025
209 lines
11 KiB
Diff
209 lines
11 KiB
Diff
Patch-Source: https://github.com/py-bson/bson/pull/118
|
|
Backported for 0.5.10.
|
|
---
|
|
From d53b91a1148305a2e095d5e2537321cdc16ba546 Mon Sep 17 00:00:00 2001
|
|
From: Daniel Garcia Moreno <daniel.garcia@suse.com>
|
|
Date: Thu, 3 Nov 2022 13:38:57 +0100
|
|
Subject: [PATCH] Drop python2 support
|
|
|
|
See https://github.com/py-bson/bson/issues/115
|
|
---
|
|
bson/codec.py | 18 ++++++------------
|
|
bson/network.py | 4 ++--
|
|
bson/tests/test_array.py | 3 +--
|
|
bson/tests/test_random_tree.py | 24 +++++++++---------------
|
|
setup.py | 4 +---
|
|
5 files changed, 19 insertions(+), 34 deletions(-)
|
|
|
|
diff --git a/bson/codec.py b/bson/codec.py
|
|
index 6aa7e07..78449bc 100644
|
|
--- a/bson/codec.py
|
|
+++ b/bson/codec.py
|
|
@@ -24,9 +24,6 @@
|
|
from dateutil.tz import tzutc
|
|
from binascii import b2a_hex
|
|
|
|
-from six import integer_types, iterkeys, text_type, PY3
|
|
-from six.moves import xrange
|
|
-
|
|
|
|
utc = tzutc()
|
|
|
|
@@ -132,7 +129,7 @@ def encode_string(value):
|
|
|
|
def encode_cstring(value):
|
|
if not isinstance(value, bytes):
|
|
- value = text_type(value).encode("utf-8")
|
|
+ value = str(value).encode("utf-8")
|
|
if b"\x00" in value:
|
|
raise ValueError("Element names may not include NUL bytes.")
|
|
# A NUL byte is used to delimit our string, accepting one would cause
|
|
@@ -174,7 +171,7 @@ def encode_string_element(name, value):
|
|
|
|
|
|
def _is_string(value):
|
|
- if isinstance(value, text_type):
|
|
+ if isinstance(value, str):
|
|
return True
|
|
elif isinstance(value, str) or isinstance(value, bytes):
|
|
try:
|
|
@@ -189,7 +186,7 @@ def encode_value(name, value, buf, traversal_stack,
|
|
generator_func, on_unknown=None):
|
|
if isinstance(value, bool):
|
|
buf.write(encode_boolean_element(name, value))
|
|
- elif isinstance(value, integer_types):
|
|
+ elif isinstance(value, int):
|
|
if value < -0x80000000 or 0x7FFFFFFFFFFFFFFF >= value > 0x7fffffff:
|
|
buf.write(encode_int64_element(name, value))
|
|
elif value > 0x7FFFFFFFFFFFFFFF:
|
|
@@ -238,7 +235,7 @@ def encode_value(name, value, buf, traversal_stack,
|
|
def encode_document(obj, traversal_stack, traversal_parent=None,
|
|
generator_func=None, on_unknown=None):
|
|
buf = StringIO()
|
|
- key_iter = iterkeys(obj)
|
|
+ key_iter = iter(obj.keys())
|
|
if generator_func is not None:
|
|
key_iter = generator_func(obj, traversal_stack)
|
|
for name in key_iter:
|
|
@@ -256,7 +253,7 @@ def encode_document(obj, traversal_stack, traversal_parent=None,
|
|
def encode_array(array, traversal_stack, traversal_parent=None,
|
|
generator_func=None, on_unknown=None):
|
|
buf = StringIO()
|
|
- for i in xrange(0, len(array)):
|
|
+ for i in range(0, len(array)):
|
|
value = array[i]
|
|
traversal_stack.append(TraversalStep(traversal_parent or array, i))
|
|
encode_value(str(i), value, buf, traversal_stack,
|
|
@@ -295,10 +292,7 @@ def decode_document(data, base, as_array=False):
|
|
|
|
element_type = char_struct.unpack(data[base:base + 1])[0]
|
|
|
|
- if PY3:
|
|
- ll = data.index(0, base + 1) + 1
|
|
- else:
|
|
- ll = data.index("\x00", base + 1) + 1
|
|
+ ll = data.index(0, base + 1) + 1
|
|
if decode_name:
|
|
name = data[base + 1:ll - 1]
|
|
try:
|
|
diff --git a/bson/network.py b/bson/network.py
|
|
index d24e5bd..ccd2ae4 100644
|
|
--- a/bson/network.py
|
|
+++ b/bson/network.py
|
|
@@ -1,7 +1,7 @@
|
|
#!/usr/bin/env python
|
|
from struct import unpack
|
|
|
|
-from six import BytesIO, b
|
|
+from io import BytesIO
|
|
|
|
from . import dumps, loads
|
|
|
|
@@ -56,7 +56,7 @@ def recvbytes(self, bytes_needed, sock_buf = None):
|
|
part_count = len(chunk)
|
|
|
|
if type(chunk) == str:
|
|
- chunk = b(chunk)
|
|
+ chunk = chunk.encode('latin-1')
|
|
|
|
if part_count < 1:
|
|
return None
|
|
diff --git a/bson/tests/test_array.py b/bson/tests/test_array.py
|
|
index 029c5a1..e0eee3e 100644
|
|
--- a/bson/tests/test_array.py
|
|
+++ b/bson/tests/test_array.py
|
|
@@ -2,7 +2,6 @@
|
|
from unittest import TestCase
|
|
|
|
from bson import dumps, loads
|
|
-from six import PY3
|
|
|
|
|
|
class TestArray(TestCase):
|
|
@@ -74,5 +74,5 @@ class TestArray(TestCase):
|
|
|
|
def test_encoded_order(self):
|
|
serialized = dumps(self.doc)
|
|
- expected = repr(serialized)[1:] if PY3 else repr(serialized)
|
|
+ expected = repr(serialized)[1:]
|
|
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\'')
|
|
|
|
|
|
diff --git a/bson/tests/test_random_tree.py b/bson/tests/test_random_tree.py
|
|
index ca9832e..e83e23b 100644
|
|
--- a/bson/tests/test_random_tree.py
|
|
+++ b/bson/tests/test_random_tree.py
|
|
@@ -3,9 +3,6 @@
|
|
from random import randint
|
|
from unittest import TestCase
|
|
|
|
-from six import text_type, PY3
|
|
-from six.moves import xrange
|
|
-
|
|
from bson import dumps, loads
|
|
|
|
|
|
@@ -13,10 +10,10 @@ def populate(parent, howmany, max_children):
|
|
if howmany > max_children:
|
|
children = randint(2, max_children)
|
|
distribution = []
|
|
- for _ in xrange(0, children - 1):
|
|
+ for _ in range(0, children - 1):
|
|
distribution.append(int(howmany / children))
|
|
distribution.append(howmany - sum(distribution, 0))
|
|
- for i in xrange(0, children):
|
|
+ for i in range(0, children):
|
|
steal_target = randint(0, children - 1)
|
|
while steal_target == i:
|
|
steal_target = randint(0, children -1)
|
|
@@ -25,7 +22,7 @@ def populate(parent, howmany, max_children):
|
|
distribution[i] += steal_count
|
|
distribution[steal_target] -= steal_count
|
|
|
|
- for i in xrange(0, children):
|
|
+ for i in range(0, children):
|
|
make_dict = randint(0, 1)
|
|
if make_dict:
|
|
baby = {}
|
|
@@ -34,8 +31,7 @@ def populate(parent, howmany, max_children):
|
|
populate(baby, distribution[i], max_children)
|
|
if isinstance(parent, dict):
|
|
key = os.urandom(8)
|
|
- key = "".join(chr(c) for c in hexlify(key)) \
|
|
- if PY3 else key.encode("hex")
|
|
+ key = "".join(chr(c) for c in hexlify(key))
|
|
parent[key] = baby
|
|
else:
|
|
parent.append(baby)
|
|
@@ -44,17 +40,15 @@ def populate(parent, howmany, max_children):
|
|
|
|
|
|
def populate_with_leaves(parent, howmany):
|
|
- for _ in xrange(0, howmany):
|
|
+ for _ in range(0, howmany):
|
|
leaf = os.urandom(4)
|
|
- leaf = "".join(chr(c) for c in hexlify(leaf)) \
|
|
- if PY3 else leaf.encode("hex")
|
|
+ leaf = "".join(chr(c) for c in hexlify(leaf))
|
|
make_unicode = randint(0, 1)
|
|
if make_unicode:
|
|
- leaf = text_type(leaf)
|
|
+ leaf = str(leaf)
|
|
if isinstance(parent, dict):
|
|
key = os.urandom(4)
|
|
- key = "".join(chr(c) for c in hexlify(key)) \
|
|
- if PY3 else key.encode("hex")
|
|
+ key = "".join(chr(c) for c in hexlify(key))
|
|
parent[key] = leaf
|
|
else:
|
|
parent.append(leaf)
|
|
@@ -62,7 +56,7 @@ def populate_with_leaves(parent, howmany):
|
|
|
|
class TestRandomTree(TestCase):
|
|
def test_random_tree(self):
|
|
- for _ in xrange(0, 16):
|
|
+ for _ in range(0, 16):
|
|
p = {}
|
|
populate(p, 256, 4)
|
|
sp = dumps(p)
|