Skip to content

Commit a49f071

Browse files
authored
Removing Python 2 compatibility layer (#134)
* Removing Python 2 compatibility layer.
1 parent 7665b1e commit a49f071

File tree

11 files changed

+34
-108
lines changed

11 files changed

+34
-108
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
dist: xenial
1+
dist: bionic
22
services:
33
- redis-server
44
language: python
55
python:
6-
- "2.7"
76
- "3.5"
87
- "3.6"
98
- "3.7"
109
- "3.8"
1110
- "3.9"
12-
- "pypy"
1311
- "pypy3"
1412
install:
1513
- make develop

Pipfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ name = "pypi"
55

66
[packages]
77
python-magic = ">=0.4.5"
8-
six = ">=1.5.0"
98
decorator = ">=4.0.0"
109
urllib3 = ">=1.25.3"
1110
http-parser = ">=0.9.0"

README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Versioning
2727
==========
2828
Starting from *3.7.0*, Mocket major version will follow the same numbering pattern as Python's and therefore indicate the most recent Python version that is supported.
2929

30+
FYI: the last version compatible with Python 2.7 is *3.9.4*, bugfixing or backporting of features introduced after that release will only be available as commercial support.
31+
3032
Support it
3133
==========
3234
*Star* the project on GitHub, *Buy Me a Coffee* clicking the button below or, even better, contribute with patches or documentation.

mocket/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77

88
__all__ = ("mocketize", "Mocket", "MocketEntry", "Mocketizer")
99

10-
__version__ = "3.9.4"
10+
__version__ = "3.9.35"

mocket/async_mocket.py

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,19 @@
1-
from sys import version_info
2-
3-
import decorator
4-
5-
from mocket import Mocket, Mocketizer
1+
from mocket import Mocketizer
62

73

84
def get_async_mocketize():
9-
major, minor = version_info[:2]
10-
if major == 3 and minor >= 5:
11-
12-
class AsyncMocketizer(Mocketizer):
13-
async def __aenter__(self):
14-
Mocket.enable(
15-
namespace=self.namespace,
16-
truesocket_recording_dir=self.truesocket_recording_dir,
17-
)
18-
if self.instance:
19-
self.check_and_call("mocketize_setup")
20-
21-
async def __aexit__(self, type, value, tb):
22-
if self.instance:
23-
self.check_and_call("mocketize_teardown")
24-
Mocket.disable()
5+
class AsyncMocketizer(Mocketizer):
6+
async def __aenter__(*args, **kwargs):
7+
return Mocketizer.__enter__(*args, **kwargs)
258

26-
@staticmethod
27-
def async_wrap(test=None, truesocket_recording_dir=None):
28-
async def wrapper(t, *args, **kw):
29-
instance = args[0] if args else None
30-
namespace = ".".join(
31-
(
32-
instance.__class__.__module__,
33-
instance.__class__.__name__,
34-
t.__name__,
35-
)
36-
)
37-
async with AsyncMocketizer(
38-
instance,
39-
namespace=namespace,
40-
truesocket_recording_dir=truesocket_recording_dir,
41-
):
42-
await t(*args, **kw)
43-
return wrapper
9+
async def __aexit__(*args, **kwargs):
10+
return Mocketizer.__exit__(*args, **kwargs)
4411

45-
return decorator.decorator(wrapper, test)
12+
@staticmethod
13+
def async_wrap(*args, **kwargs):
14+
return Mocketizer.wrap(*args, **kwargs)
4615

47-
return AsyncMocketizer.async_wrap
16+
return AsyncMocketizer.async_wrap
4817

4918

5019
async_mocketize = get_async_mocketize()

mocket/compat.py

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,15 @@
11
import codecs
22
import os
33
import shlex
4-
import sys
5-
from socket import error as sock_error
6-
7-
import six
84

95
encoding = os.getenv("MOCKET_ENCODING", "utf-8")
106

11-
text_type = six.text_type
12-
byte_type = six.binary_type
13-
basestring = six.string_types
14-
15-
PY2 = sys.version_info[0] == 2
16-
if PY2:
17-
import collections as collections_abc
18-
from BaseHTTPServer import BaseHTTPRequestHandler
19-
from urlparse import urlsplit, parse_qs, unquote
20-
21-
def unquote_utf8(qs):
22-
if isinstance(qs, text_type):
23-
qs = qs.encode(encoding)
24-
s = unquote(qs)
25-
if isinstance(s, byte_type):
26-
return s.decode(encoding)
27-
else:
28-
return s
7+
text_type = str
8+
byte_type = bytes
9+
basestring = (str,)
2910

30-
FileNotFoundError = IOError
31-
BlockingIOError = sock_error
32-
else:
33-
import collections.abc as collections_abc
34-
from http.server import BaseHTTPRequestHandler
35-
from urllib.parse import urlsplit, parse_qs, unquote as unquote_utf8
36-
37-
FileNotFoundError = FileNotFoundError
38-
BlockingIOError = BlockingIOError
11+
FileNotFoundError = FileNotFoundError
12+
BlockingIOError = BlockingIOError
3913

4014
try:
4115
from json.decoder import JSONDecodeError
@@ -56,10 +30,7 @@ def decode_from_bytes(s, encoding=encoding):
5630

5731

5832
def shsplit(s):
59-
if PY2:
60-
s = encode_to_bytes(s)
61-
else:
62-
s = decode_from_bytes(s)
33+
s = decode_from_bytes(s)
6334
return shlex.split(s)
6435

6536

mocket/mocket.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import collections
2+
import collections.abc as collections_abc
23
import errno
34
import hashlib
45
import io
@@ -20,7 +21,6 @@
2021
JSONDecodeError,
2122
basestring,
2223
byte_type,
23-
collections_abc,
2424
decode_from_bytes,
2525
encode_to_bytes,
2626
text_type,
@@ -77,14 +77,11 @@ def __init__(self, sock=None, server_hostname=None, _context=None, *args, **kwar
7777
if isinstance(sock, MocketSocket):
7878
self.sock = sock
7979
self.sock._host = server_hostname
80-
if true_ssl_context:
81-
self.sock.true_socket = true_ssl_socket(
82-
sock=self.sock.true_socket,
83-
server_hostname=server_hostname,
84-
_context=true_ssl_context(protocol=SSL_PROTOCOL),
85-
)
86-
else: # Python 2.
87-
self.sock.true_socket = true_ssl_socket(sock=self.sock.true_socket)
80+
self.sock.true_socket = true_ssl_socket(
81+
sock=self.sock.true_socket,
82+
server_hostname=server_hostname,
83+
_context=true_ssl_context(protocol=SSL_PROTOCOL),
84+
)
8885
elif isinstance(sock, int) and true_ssl_context:
8986
self.context = true_ssl_context(sock)
9087

@@ -586,6 +583,7 @@ def __enter__(self):
586583
)
587584
if self.instance:
588585
self.check_and_call("mocketize_setup")
586+
return self
589587

590588
def __exit__(self, type, value, tb):
591589
if self.instance:

mocket/mockhttp.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
1-
from __future__ import unicode_literals
2-
31
import re
42
import time
3+
from http.server import BaseHTTPRequestHandler
4+
from urllib.parse import parse_qs, unquote, urlsplit
55

6-
from .compat import (
7-
BaseHTTPRequestHandler,
8-
decode_from_bytes,
9-
do_the_magic,
10-
encode_to_bytes,
11-
parse_qs,
12-
unquote_utf8,
13-
urlsplit,
14-
)
6+
from .compat import decode_from_bytes, do_the_magic, encode_to_bytes
157
from .mocket import Mocket, MocketEntry
168

179
try:
@@ -41,7 +33,7 @@ def __init__(self, data):
4133
self.path = self.parser.get_path()
4234
self.headers = self.parser.get_headers()
4335
self.querystring = parse_qs(
44-
unquote_utf8(self.parser.get_query_string()), keep_blank_values=True
36+
unquote(self.parser.get_query_string()), keep_blank_values=True
4537
)
4638
if self.querystring:
4739
self.path += "?{}".format(self.parser.get_query_string())

mocket/mockredis.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import unicode_literals
2-
31
from itertools import chain
42

53
from .compat import byte_type, decode_from_bytes, encode_to_bytes, shsplit, text_type

runtests.py renamed to run_tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44

55

6-
def runtests(args=None):
6+
def main(args=None):
77
import pytest
88

99
if not args:
@@ -37,4 +37,4 @@ def runtests(args=None):
3737

3838

3939
if __name__ == "__main__":
40-
runtests(sys.argv)
40+
main(sys.argv)

0 commit comments

Comments
 (0)