Skip to content

Commit 8c93b2a

Browse files
Djaillajpadilla
authored andcommitted
Remove Python 2.7 compatibility (jpadilla#457)
* Remove py27 support * [py27] Remove useless compatibility files
1 parent c404473 commit 8c93b2a

File tree

8 files changed

+31
-87
lines changed

8 files changed

+31
-87
lines changed

.travis.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
language: python
22
matrix:
33
include:
4-
- python: 2.7
5-
env: TOXENV=py27-crypto,py27-nocrypto,py27-contrib_crypto
6-
- python: 3.4
7-
env: TOXENV=py34-crypto,py34-nocrypto
84
- python: 3.5
95
env: TOXENV=py35-crypto,py35-nocrypto,py35-contrib_crypto
106
- python: 3.6
117
env: TOXENV=py36-crypto,py36-nocrypto,py36-contrib_crypto
128
- python: 3.7
139
env: TOXENV=lint,typing,py37-crypto,py37-nocrypto,py37-contrib_crypto
14-
dist: xenial
1510
install:
1611
- pip install -U pip
1712
- pip install -U tox coveralls

appveyor.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
environment:
22
matrix:
3-
- PYTHON: "C:\\Python27-x64"
4-
TOX_ENV: "py27-crypto"
5-
63
- PYTHON: "C:\\Python35-x64"
74
TOX_ENV: "py35-crypto"
85

6+
- PYTHON: "C:\\Python36-x64"
7+
TOX_ENV: "py36-crypto"
8+
9+
- PYTHON: "C:\\Python37-x64"
10+
TOX_ENV: "py37-crypto"
11+
912
init:
1013
- SET PATH=%PYTHON%;%PATH%
1114
- python -c "import sys;sys.stdout.write(sys.version)"

jwt/__main__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
#!/usr/bin/env python
2-
3-
from __future__ import absolute_import, print_function
1+
#!/usr/bin/env python3
42

53
import argparse
64
import json

jwt/compat.py

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,27 @@
44
"""
55
# flake8: noqa
66
import hmac
7-
import struct
8-
import sys
97

10-
PY3 = sys.version_info[0] == 3
11-
12-
13-
if PY3:
14-
text_type = str
15-
binary_type = bytes
16-
else:
17-
text_type = unicode
18-
binary_type = str
19-
20-
string_types = (text_type, binary_type)
8+
text_type = str
9+
binary_type = bytes
10+
string_types = (str, bytes)
2111

2212
try:
2313
# Importing ABCs from collections will be removed in PY3.8
2414
from collections.abc import Iterable, Mapping
2515
except ImportError:
2616
from collections import Iterable, Mapping
2717

28-
try:
29-
constant_time_compare = hmac.compare_digest
30-
except AttributeError:
31-
# Fallback for Python < 2.7
32-
def constant_time_compare(val1, val2):
33-
"""
34-
Returns True if the two strings are equal, False otherwise.
35-
36-
The time taken is independent of the number of characters that match.
37-
"""
38-
if len(val1) != len(val2):
39-
return False
40-
41-
result = 0
42-
43-
for x, y in zip(val1, val2):
44-
result |= ord(x) ^ ord(y)
45-
46-
return result == 0
47-
48-
49-
# Use int.to_bytes if it exists (Python 3)
50-
if getattr(int, "to_bytes", None):
51-
52-
def bytes_from_int(val):
53-
remaining = val
54-
byte_length = 0
55-
56-
while remaining != 0:
57-
remaining = remaining >> 8
58-
byte_length += 1
5918

60-
return val.to_bytes(byte_length, "big", signed=False)
19+
constant_time_compare = hmac.compare_digest
6120

6221

63-
else:
22+
def bytes_from_int(val):
23+
remaining = val
24+
byte_length = 0
6425

65-
def bytes_from_int(val):
66-
buf = []
67-
while val:
68-
val, remainder = divmod(val, 256)
69-
buf.append(remainder)
26+
while remaining != 0:
27+
remaining = remaining >> 8
28+
byte_length += 1
7029

71-
buf.reverse()
72-
return struct.pack("%sB" % len(buf), *buf)
30+
return val.to_bytes(byte_length, "big", signed=False)

setup.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
1+
#!/usr/bin/env python3
2+
33
import os
44
import re
55
import sys
@@ -60,14 +60,13 @@ def get_version(package):
6060
"Natural Language :: English",
6161
"License :: OSI Approved :: MIT License",
6262
"Programming Language :: Python",
63-
"Programming Language :: Python :: 2.7",
64-
"Programming Language :: Python :: 3.4",
63+
"Programming Language :: Python :: 3",
6564
"Programming Language :: Python :: 3.5",
6665
"Programming Language :: Python :: 3.6",
6766
"Programming Language :: Python :: 3.7",
6867
"Topic :: Utilities",
6968
],
70-
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
69+
python_requires=">=3, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*",
7170
extras_require=EXTRAS_REQUIRE,
7271
entry_points={"console_scripts": ["pyjwt = jwt.__main__:main"]},
7372
options={"bdist_wheel": {"universal": "1"}},

tests/compat.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/test_api_jws.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
)
1414
from jwt.utils import base64url_decode, force_bytes, force_unicode
1515

16-
from .compat import string_types, text_type
17-
1816
try:
1917
from cryptography.hazmat.backends import default_backend
2018
from cryptography.hazmat.primitives.serialization import (
@@ -107,7 +105,7 @@ def test_decode_fails_when_alg_is_not_on_method_algorithms_param(
107105

108106
def test_decode_works_with_unicode_token(self, jws):
109107
secret = "secret"
110-
unicode_jws = text_type(
108+
unicode_jws = (
111109
"eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9"
112110
".eyJoZWxsbyI6ICJ3b3JsZCJ9"
113111
".tvagLDLoaiJKxOKqpBXSEGy7SYSifZhjntgm9ctpyj8"
@@ -732,13 +730,13 @@ def test_encode_headers_parameter_adds_headers(self, jws, payload):
732730
headers = {"testheader": True}
733731
token = jws.encode(payload, "secret", headers=headers)
734732

735-
if not isinstance(token, string_types):
733+
if not isinstance(token, str):
736734
token = token.decode()
737735

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

741-
if not isinstance(header, text_type):
739+
if not isinstance(header, str):
742740
header = header.decode()
743741

744742
header_obj = json.loads(header)

tox.ini

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
[tox]
2-
envlist = lint, typing, py{27,34,35,36,37}-crypto, py{27,35,36,37}-contrib_crypto, py{27,35,36,37}-nocrypto
2+
envlist =
3+
lint
4+
typing
5+
py{35,36,37}-crypto
6+
py{35,36,37}-contrib_crypto
7+
py{35,36,37}-nocrypto
38

49

510
[testenv]

0 commit comments

Comments
 (0)