Skip to content

Commit

Permalink
Remove Python 2.7 compatibility (#457)
Browse files Browse the repository at this point in the history
* Remove py27 support

* [py27] Remove useless compatibility files
  • Loading branch information
Djailla authored and jpadilla committed Nov 13, 2019
1 parent c404473 commit 8c93b2a
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 87 deletions.
5 changes: 0 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
language: python
matrix:
include:
- python: 2.7
env: TOXENV=py27-crypto,py27-nocrypto,py27-contrib_crypto
- python: 3.4
env: TOXENV=py34-crypto,py34-nocrypto
- python: 3.5
env: TOXENV=py35-crypto,py35-nocrypto,py35-contrib_crypto
- python: 3.6
env: TOXENV=py36-crypto,py36-nocrypto,py36-contrib_crypto
- python: 3.7
env: TOXENV=lint,typing,py37-crypto,py37-nocrypto,py37-contrib_crypto
dist: xenial
install:
- pip install -U pip
- pip install -U tox coveralls
Expand Down
9 changes: 6 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
environment:
matrix:
- PYTHON: "C:\\Python27-x64"
TOX_ENV: "py27-crypto"

- PYTHON: "C:\\Python35-x64"
TOX_ENV: "py35-crypto"

- PYTHON: "C:\\Python36-x64"
TOX_ENV: "py36-crypto"

- PYTHON: "C:\\Python37-x64"
TOX_ENV: "py37-crypto"

init:
- SET PATH=%PYTHON%;%PATH%
- python -c "import sys;sys.stdout.write(sys.version)"
Expand Down
4 changes: 1 addition & 3 deletions jwt/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python

from __future__ import absolute_import, print_function
#!/usr/bin/env python3

import argparse
import json
Expand Down
64 changes: 11 additions & 53 deletions jwt/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,69 +4,27 @@
"""
# flake8: noqa
import hmac
import struct
import sys

PY3 = sys.version_info[0] == 3


if PY3:
text_type = str
binary_type = bytes
else:
text_type = unicode
binary_type = str

string_types = (text_type, binary_type)
text_type = str
binary_type = bytes
string_types = (str, bytes)

try:
# Importing ABCs from collections will be removed in PY3.8
from collections.abc import Iterable, Mapping
except ImportError:
from collections import Iterable, Mapping

try:
constant_time_compare = hmac.compare_digest
except AttributeError:
# Fallback for Python < 2.7
def constant_time_compare(val1, val2):
"""
Returns True if the two strings are equal, False otherwise.
The time taken is independent of the number of characters that match.
"""
if len(val1) != len(val2):
return False

result = 0

for x, y in zip(val1, val2):
result |= ord(x) ^ ord(y)

return result == 0


# Use int.to_bytes if it exists (Python 3)
if getattr(int, "to_bytes", None):

def bytes_from_int(val):
remaining = val
byte_length = 0

while remaining != 0:
remaining = remaining >> 8
byte_length += 1

return val.to_bytes(byte_length, "big", signed=False)
constant_time_compare = hmac.compare_digest


else:
def bytes_from_int(val):
remaining = val
byte_length = 0

def bytes_from_int(val):
buf = []
while val:
val, remainder = divmod(val, 256)
buf.append(remainder)
while remaining != 0:
remaining = remaining >> 8
byte_length += 1

buf.reverse()
return struct.pack("%sB" % len(buf), *buf)
return val.to_bytes(byte_length, "big", signed=False)
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#!/usr/bin/env python3

import os
import re
import sys
Expand Down Expand Up @@ -60,14 +60,13 @@ def get_version(package):
"Natural Language :: English",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Topic :: Utilities",
],
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
python_requires=">=3, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*",
extras_require=EXTRAS_REQUIRE,
entry_points={"console_scripts": ["pyjwt = jwt.__main__:main"]},
options={"bdist_wheel": {"universal": "1"}},
Expand Down
12 changes: 0 additions & 12 deletions tests/compat.py

This file was deleted.

8 changes: 3 additions & 5 deletions tests/test_api_jws.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
)
from jwt.utils import base64url_decode, force_bytes, force_unicode

from .compat import string_types, text_type

try:
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.serialization import (
Expand Down Expand Up @@ -107,7 +105,7 @@ def test_decode_fails_when_alg_is_not_on_method_algorithms_param(

def test_decode_works_with_unicode_token(self, jws):
secret = "secret"
unicode_jws = text_type(
unicode_jws = (
"eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
".eyJoZWxsbyI6ICJ3b3JsZCJ9"
".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8"
Expand Down Expand Up @@ -732,13 +730,13 @@ def test_encode_headers_parameter_adds_headers(self, jws, payload):
headers = {"testheader": True}
token = jws.encode(payload, "secret", headers=headers)

if not isinstance(token, string_types):
if not isinstance(token, str):
token = token.decode()

header = token[0 : token.index(".")].encode()
header = base64url_decode(header)

if not isinstance(header, text_type):
if not isinstance(header, str):
header = header.decode()

header_obj = json.loads(header)
Expand Down
7 changes: 6 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
[tox]
envlist = lint, typing, py{27,34,35,36,37}-crypto, py{27,35,36,37}-contrib_crypto, py{27,35,36,37}-nocrypto
envlist =
lint
typing
py{35,36,37}-crypto
py{35,36,37}-contrib_crypto
py{35,36,37}-nocrypto


[testenv]
Expand Down

0 comments on commit 8c93b2a

Please sign in to comment.