Skip to content

Commit

Permalink
Drop python2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
danigm committed Nov 3, 2022
1 parent 7a9174c commit d53b91a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 34 deletions.
18 changes: 6 additions & 12 deletions bson/codec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions bson/network.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions bson/tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from unittest import TestCase

from bson import dumps, loads
from six import PY3


class TestArray(TestCase):
Expand Down Expand Up @@ -74,5 +73,5 @@ def test_long_array(self):

def test_encoded_order(self):
serialized = dumps(self.doc)
expected = repr(serialized)[1:] if PY3 else repr(serialized)
expected = repr(serialized)[1:]
self.assertEqual(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\'')
24 changes: 9 additions & 15 deletions bson/tests/test_random_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@
from random import randint
from unittest import TestCase

from six import text_type, PY3
from six.moves import xrange

from bson import dumps, loads


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)
Expand All @@ -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 = {}
Expand All @@ -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)
Expand All @@ -44,25 +40,23 @@ 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)


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)
Expand Down
4 changes: 1 addition & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def run(self):
name="bson",
version="0.5.10",
packages=["bson"],
install_requires=["python-dateutil>=2.4.0", "six>=1.9.0"],
install_requires=["python-dateutil>=2.4.0"],
author="Ayun Park",
author_email="iamparkayun@gmail.com",
description="BSON codec for Python",
Expand All @@ -37,8 +37,6 @@ def run(self):
keywords="BSON codec",
url="http://github.com/py-bson/bson",
classifiers=[
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
Expand Down

0 comments on commit d53b91a

Please sign in to comment.