Skip to content

Commit

Permalink
Let's see what happens.
Browse files Browse the repository at this point in the history
  • Loading branch information
mindflayer committed Feb 16, 2024
1 parent c4240b7 commit 524ce7a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ services-down:

test-python:
@echo "Running Python tests"
wait-for-it --service httpbin.local:443 --service localhost:6379 --timeout 5 -- pytest tests/ || exit 1
wait-for-it --service httpbin.local:443 --service localhost:6379 --timeout 5 -- pytest --doctest-modules || exit 1
@echo ""

lint-python:
Expand Down
48 changes: 20 additions & 28 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@
urllib3_wrap_socket = None

from .compat import basestring, byte_type, decode_from_bytes, encode_to_bytes, text_type
from .utils import (
SSL_PROTOCOL,
MocketMode,
MocketSocketCore,
get_mocketize,
hexdump,
hexload,
)
from .utils import SSL_PROTOCOL, MocketMode, get_mocketize, hexdump, hexload

xxh32 = None
try:
Expand Down Expand Up @@ -176,6 +169,10 @@ class MocketSocket:
_mode = None
_bufsize = None
_secure_socket = False
_did_handshake = False
_sent_non_empty_bytes = False
r_fd = None
w_fd = None

def __init__(
self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, **kwargs
Expand All @@ -187,8 +184,6 @@ def __init__(
self.type = int(type)
self.proto = int(proto)
self._truesocket_recording_dir = None
self._did_handshake = False
self._sent_non_empty_bytes = False
self.kwargs = kwargs

def __str__(self):
Expand All @@ -205,7 +200,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
@property
def fd(self):
if self._fd is None:
self._fd = MocketSocketCore()
self._fd = io.BytesIO()
return self._fd

def gettimeout(self):
Expand Down Expand Up @@ -264,12 +259,11 @@ def unwrap(self):
def write(self, data):
return self.send(encode_to_bytes(data))

@staticmethod
def fileno():
if Mocket.r_fd is not None:
return Mocket.r_fd
Mocket.r_fd, Mocket.w_fd = os.pipe()
return Mocket.r_fd
def fileno(self):
if self.r_fd:
return self.r_fd
self.r_fd, self.w_fd = os.pipe()
return self.r_fd

def connect(self, address):
self._address = self._host, self._port = address
Expand Down Expand Up @@ -297,6 +291,8 @@ def sendall(self, data, entry=None, *args, **kwargs):
response = self.true_sendall(data, *args, **kwargs)

if response is not None:
if self.r_fd and self.w_fd:
os.write(self.w_fd, response)
self.fd.seek(0)
self.fd.write(response)
self.fd.truncate()
Expand All @@ -320,8 +316,8 @@ def recv_into(self, buffer, buffersize=None, flags=None):
return len(data)

def recv(self, buffersize, flags=None):
if Mocket.r_fd and Mocket.w_fd:
return os.read(Mocket.r_fd, buffersize)
if self.r_fd and self.w_fd:
return os.read(self.r_fd, buffersize)
data = self.read(buffersize)
if data:
return data
Expand Down Expand Up @@ -438,9 +434,13 @@ def close(self):
if self.true_socket and not self.true_socket._closed:
self.true_socket.close()
self._fd = None
if self.r_fd:
os.close(self.r_fd)
if self.w_fd:
os.close(self.w_fd)

def __getattr__(self, name):
"""Do nothing catchall function, for methods like close() and shutdown()"""
"""Do nothing catchall function, for methods like shutdown()"""

def do_nothing(*args, **kwargs):
pass
Expand All @@ -454,8 +454,6 @@ class Mocket:
_requests = []
_namespace = text_type(id(_entries))
_truesocket_recording_dir = None
r_fd = None
w_fd = None

@classmethod
def register(cls, *entries):
Expand All @@ -477,12 +475,6 @@ def collect(cls, data):

@classmethod
def reset(cls):
if cls.r_fd is not None:
os.close(cls.r_fd)
cls.r_fd = None
if cls.w_fd is not None:
os.close(cls.w_fd)
cls.w_fd = None
cls._entries = collections.defaultdict(list)
cls._requests = []

Expand Down
12 changes: 0 additions & 12 deletions mocket/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import binascii
import io
import os
import ssl
from typing import Tuple, Union

Expand All @@ -10,16 +8,6 @@
SSL_PROTOCOL = ssl.PROTOCOL_TLSv1_2


class MocketSocketCore(io.BytesIO):
def write(self, content):
super(MocketSocketCore, self).write(content)

from mocket import Mocket

if Mocket.r_fd and Mocket.w_fd:
os.write(Mocket.w_fd, content)


def hexdump(binary_string):
r"""
>>> hexdump(b"bar foobar foo") == decode_from_bytes(encode_to_bytes("62 61 72 20 66 6F 6F 62 61 72 20 66 6F 6F"))
Expand Down

0 comments on commit 524ce7a

Please sign in to comment.