Drop python2 support

This commit is contained in:
Bastien Gérard
2020-03-08 14:58:21 +01:00
parent a0b803959c
commit 421e3f324f
19 changed files with 51 additions and 134 deletions

View File

@@ -1,7 +1,6 @@
import weakref
from bson import DBRef
from future.utils import listitems
import six
from six import iteritems
@@ -181,19 +180,6 @@ class BaseList(list):
__iadd__ = mark_as_changed_wrapper(list.__iadd__)
__imul__ = mark_as_changed_wrapper(list.__imul__)
if six.PY2:
# Under py3 __setslice__, __delslice__ and __getslice__
# are replaced by __setitem__, __delitem__ and __getitem__ with a slice as parameter
# so we mimic this under python 2
def __setslice__(self, i, j, sequence):
return self.__setitem__(slice(i, j), sequence)
def __delslice__(self, i, j):
return self.__delitem__(slice(i, j))
def __getslice__(self, i, j):
return self.__getitem__(slice(i, j))
def _mark_as_changed(self, key=None):
if hasattr(self._instance, "_mark_as_changed"):
if key:
@@ -426,7 +412,7 @@ class StrictDict(object):
return len(list(iteritems(self)))
def __eq__(self, other):
return listitems(self) == listitems(other)
return list(self.items()) == list(other.items())
def __ne__(self, other):
return not (self == other)

View File

@@ -1,9 +1,9 @@
import copy
import numbers
from functools import partial
from bson import DBRef, ObjectId, SON, json_util
from future.utils import listitems
import pymongo
import six
from six import iteritems
@@ -26,7 +26,6 @@ from mongoengine.errors import (
OperationError,
ValidationError,
)
from mongoengine.python_support import Hashable
__all__ = ("BaseDocument", "NON_FIELD_ERRORS")
@@ -294,10 +293,7 @@ class BaseDocument(object):
def __str__(self):
# TODO this could be simpler?
if hasattr(self, "__unicode__"):
if six.PY3:
return self.__unicode__()
else:
return six.text_type(self).encode("utf-8")
return self.__unicode__()
return six.text_type("%s object" % self.__class__.__name__)
def __eq__(self, other):
@@ -671,7 +667,7 @@ class BaseDocument(object):
del set_data["_id"]
# Determine if any changed items were actually unset.
for path, value in listitems(set_data):
for path, value in list(set_data.items()):
if value or isinstance(
value, (numbers.Number, bool)
): # Account for 0 and True that are truthy

View File

@@ -5,12 +5,12 @@ import re
import socket
import time
import uuid
from io import BytesIO
from operator import itemgetter
from bson import Binary, DBRef, ObjectId, SON
from bson.int64 import Int64
import gridfs
from past.builtins import long
import pymongo
from pymongo import ReturnDocument
import six
@@ -39,7 +39,6 @@ from mongoengine.connection import DEFAULT_CONNECTION_NAME, get_db
from mongoengine.document import Document, EmbeddedDocument
from mongoengine.errors import DoesNotExist, InvalidQueryError, ValidationError
from mongoengine.mongodb_support import MONGODB_36, get_mongodb_version
from mongoengine.python_support import StringIO
from mongoengine.queryset import DO_NOTHING
from mongoengine.queryset.base import BaseQuerySet
from mongoengine.queryset.transform import STRING_OPERATORS
@@ -338,7 +337,7 @@ class IntField(BaseField):
class LongField(BaseField):
"""64-bit integer field."""
"""64-bit integer field. (Equivalent to IntField since the support to Python2 was dropped)"""
def __init__(self, min_value=None, max_value=None, **kwargs):
self.min_value, self.max_value = min_value, max_value
@@ -346,7 +345,7 @@ class LongField(BaseField):
def to_python(self, value):
try:
value = long(value)
value = int(value)
except (TypeError, ValueError):
pass
return value
@@ -356,7 +355,7 @@ class LongField(BaseField):
def validate(self, value):
try:
value = long(value)
value = int(value)
except (TypeError, ValueError):
self.error("%s could not be converted to long" % value)
@@ -370,7 +369,7 @@ class LongField(BaseField):
if value is None:
return value
return super(LongField, self).prepare_query_value(op, long(value))
return super(LongField, self).prepare_query_value(op, int(value))
class FloatField(BaseField):
@@ -1679,8 +1678,6 @@ class GridFSProxy(object):
def __bool__(self):
return bool(self.grid_id)
__nonzero__ = __bool__ # For Py2 support
def __getstate__(self):
self_dict = self.__dict__
self_dict["_fs"] = None
@@ -1952,7 +1949,7 @@ class ImageGridFsProxy(GridFSProxy):
w, h = img.size
io = StringIO()
io = BytesIO()
img.save(io, img_format, progressive=progressive)
io.seek(0)
@@ -1971,7 +1968,7 @@ class ImageGridFsProxy(GridFSProxy):
def _put_thumbnail(self, thumbnail, format, progressive, **kwargs):
w, h = thumbnail.size
io = StringIO()
io = BytesIO()
thumbnail.save(io, format, progressive=progressive)
io.seek(0)

View File

@@ -1,23 +0,0 @@
"""
Helper functions, constants, and types to aid with Python v2.7 - v3.x support
"""
import six
# six.BytesIO resolves to StringIO.StringIO in Py2 and io.BytesIO in Py3.
StringIO = six.BytesIO
# Additionally for Py2, try to use the faster cStringIO, if available
if not six.PY3:
try:
import cStringIO
except ImportError:
pass
else:
StringIO = cStringIO.StringIO
if six.PY3:
from collections.abc import Hashable
else:
# raises DeprecationWarnings in Python >=3.7
from collections import Hashable

View File

@@ -1,5 +1,3 @@
from __future__ import absolute_import
import copy
import itertools
import re
@@ -204,8 +202,6 @@ class BaseQuerySet(object):
"""Avoid to open all records in an if stmt in Py3."""
return self._has_data()
__nonzero__ = __bool__ # For Py2 support
# Core functions
def all(self):

View File

@@ -69,8 +69,6 @@ class QueryFieldList(object):
def __bool__(self):
return bool(self.fields)
__nonzero__ = __bool__ # For Py2 support
def as_dict(self):
field_list = {field: self.value for field in self.fields}
if self.slice:

View File

@@ -10,7 +10,7 @@ from mongoengine.base import UPDATE_OPERATORS
from mongoengine.common import _import_class
from mongoengine.errors import InvalidQueryError
__all__ = ("query", "update")
__all__ = ("query", "update", "STRING_OPERATORS")
COMPARISON_OPERATORS = (
"ne",

View File

@@ -143,8 +143,6 @@ class QCombination(QNode):
def __bool__(self):
return bool(self.children)
__nonzero__ = __bool__ # For Py2 support
def accept(self, visitor):
for i in range(len(self.children)):
if isinstance(self.children[i], QNode):
@@ -180,8 +178,6 @@ class Q(QNode):
def __bool__(self):
return bool(self.query)
__nonzero__ = __bool__ # For Py2 support
def __eq__(self, other):
return self.__class__ == other.__class__ and self.query == other.query