From 524ce7aaab8135ea0a656f3d8372372086c9236c Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Fri, 16 Feb 2024 08:48:04 +0100 Subject: [PATCH] Let's see what happens. --- Makefile | 2 +- mocket/mocket.py | 48 ++++++++++++++++++++---------------------------- mocket/utils.py | 12 ------------ 3 files changed, 21 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 4b727aee..442b26f9 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/mocket/mocket.py b/mocket/mocket.py index c2c065cf..f06cab0c 100644 --- a/mocket/mocket.py +++ b/mocket/mocket.py @@ -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: @@ -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 @@ -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): @@ -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): @@ -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 @@ -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() @@ -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 @@ -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 @@ -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): @@ -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 = [] diff --git a/mocket/utils.py b/mocket/utils.py index 2f17838b..8a53e539 100644 --- a/mocket/utils.py +++ b/mocket/utils.py @@ -1,6 +1,4 @@ import binascii -import io -import os import ssl from typing import Tuple, Union @@ -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"))