Skip to content

Commit

Permalink
Drop python 2 and 3.5 (pallets-eco#62)
Browse files Browse the repository at this point in the history
* update noxfile

* remove py2 from setup.cfg

* remove specific py2 handling

* remove _compat.text_type
  • Loading branch information
northernSage authored Aug 16, 2021
1 parent 8c91262 commit 87beb8e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 59 deletions.
8 changes: 2 additions & 6 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import nox


@nox.session(python=["3.8", "3.7", "3.6", "3.5", "2.7", "pypy3"])
@nox.session(python=["3.9", "3.8", "3.7", "3.6", "pypy3"])
def tests(session):
if session.python == "2.7":
# unpinned on 2.7 so pip will find the last supported versions.
session.install(".", "-r", "requirements/tests.in")
else:
session.install(".", "-r", "requirements/tests.txt")
session.install(".", "-r", "requirements/tests.txt")
session.run("pytest", *session.posargs)


Expand Down
4 changes: 1 addition & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ classifiers =
License :: OSI Approved :: BSD License
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 3
Topic :: Internet :: WWW/HTTP
Topic :: Internet :: WWW/HTTP :: Session
Topic :: Internet :: WWW/HTTP :: WSGI
Expand All @@ -31,7 +29,7 @@ classifiers =
packages = find:
package_dir = =src
include_package_data = true
python_requires = >=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*,!=3.4.*
python_requires = >= 3.6
# install_requires is in setup.py for GitHub's dependency graph.

[options.packages.find]
Expand Down
49 changes: 13 additions & 36 deletions src/secure_cookie/_compat.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,24 @@
import sys

PY2 = sys.version_info[0] == 2
_default_encoding = sys.getdefaultencoding()

if not PY2:
text_type = str

def to_bytes(x, charset=_default_encoding, errors="strict"):
if x is None:
return None

if isinstance(x, (bytes, bytearray, memoryview)):
return bytes(x)

if isinstance(x, str):
return x.encode(charset, errors)

raise TypeError("Expected bytes")

def to_native(x, charset=_default_encoding, errors="strict"):
if x is None or isinstance(x, str):
return x

return x.decode(charset, errors)
_default_encoding = sys.getdefaultencoding()


else:
text_type = unicode # noqa: F821
def to_bytes(x, charset=_default_encoding, errors="strict"):
if x is None:
return None

def to_bytes(x, encoding=_default_encoding, errors="strict"):
if x is None:
return None
if isinstance(x, (bytes, bytearray, memoryview)):
return bytes(x)

if isinstance(x, (bytes, bytearray, buffer)): # noqa: F821
return bytes(x)
if isinstance(x, str):
return x.encode(charset, errors)

if isinstance(x, unicode): # noqa: F821
return x.encode(encoding, errors)
raise TypeError("Expected bytes")

raise TypeError("Expected bytes")

def to_native(x, encoding=_default_encoding, errors="strict"):
if x is None or isinstance(x, str):
return x
def to_native(x, charset=_default_encoding, errors="strict"):
if x is None or isinstance(x, str):
return x

return x.encode(encoding, errors)
return x.decode(charset, errors)
5 changes: 2 additions & 3 deletions src/secure_cookie/cookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def application(request):
from werkzeug.urls import url_unquote_plus
from werkzeug.utils import detect_utf_encoding

from ._compat import text_type
from ._compat import to_bytes
from ._compat import to_native
from .session import ModificationTrackingDict
Expand Down Expand Up @@ -303,10 +302,10 @@ def unserialize(cls, string, secret_key):
:param secret_key: The secret key used to serialize the cookie.
:return: A new :class:`SecureCookie`.
"""
if isinstance(string, text_type):
if isinstance(string, str):
string = string.encode("utf-8", "replace")

if isinstance(secret_key, text_type):
if isinstance(secret_key, str):
secret_key = secret_key.encode("utf-8", "replace")

try:
Expand Down
12 changes: 1 addition & 11 deletions src/secure_cookie/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,17 @@ def application(request):
from time import time

from werkzeug.datastructures import CallbackDict
from werkzeug.filesystem import get_filesystem_encoding
from werkzeug.http import dump_cookie
from werkzeug.http import parse_cookie
from werkzeug.wsgi import ClosingIterator

from ._compat import PY2
from ._compat import text_type

_sha1_re = re.compile(r"^[a-f0-9]{40}$")


def _urandom():
if hasattr(os, "urandom"):
return os.urandom(30)
return text_type(random()).encode("ascii")
return str(random()).encode("ascii")


def generate_key(salt=None):
Expand Down Expand Up @@ -242,9 +238,6 @@ def __init__(

self.path = path

if isinstance(filename_template, text_type) and PY2:
filename_template = filename_template.encode(get_filesystem_encoding())

assert not filename_template.endswith(_fs_transaction_suffix), (
"filename templates may not end with %s" % _fs_transaction_suffix
)
Expand All @@ -256,9 +249,6 @@ def get_session_filename(self, sid):
# Out of the box this should be a strict ASCII subset, but you
# might reconfigure the session object to have a more arbitrary
# string.
if isinstance(sid, text_type) and PY2:
sid = sid.encode(get_filesystem_encoding())

return path.join(self.path, self.filename_template % sid)

def save(self, session):
Expand Down

0 comments on commit 87beb8e

Please sign in to comment.