• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mindflayer / python-mocket / 12479539086

24 Dec 2024 09:03AM UTC coverage: 98.68%. Remained the same
12479539086

push

github

mindflayer
Releasing `beta` version after all the refactors.

1 of 1 new or added line in 1 file covered. (100.0%)

3 existing lines in 2 files now uncovered.

972 of 985 relevant lines covered (98.68%)

6.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/mocket/inject.py
1
from __future__ import annotations
7✔
2

3
import contextlib
7✔
4
import socket
7✔
5
import ssl
7✔
6
from types import ModuleType
7✔
7
from typing import Any
7✔
8

9
import urllib3
7✔
10

11
_patches_restore: dict[tuple[ModuleType, str], Any] = {}
7✔
12

13

14
def _patch(module: ModuleType, name: str, patched_value: Any) -> None:
7✔
15
    with contextlib.suppress(KeyError):
7✔
16
        original_value, module.__dict__[name] = module.__dict__[name], patched_value
7✔
17
        _patches_restore[(module, name)] = original_value
7✔
18

19

20
def _restore(module: ModuleType, name: str) -> None:
7✔
21
    if original_value := _patches_restore.pop((module, name)):
7✔
22
        module.__dict__[name] = original_value
7✔
23

24

25
def enable() -> None:
7✔
26
    from mocket.socket import (
7✔
27
        MocketSocket,
28
        mock_create_connection,
29
        mock_getaddrinfo,
30
        mock_gethostbyname,
31
        mock_gethostname,
32
        mock_inet_pton,
33
        mock_socketpair,
34
    )
35
    from mocket.ssl.context import MocketSSLContext, mock_wrap_socket
7✔
36
    from mocket.urllib3 import (
7✔
37
        mock_match_hostname as mock_urllib3_match_hostname,
38
    )
39
    from mocket.urllib3 import (
7✔
40
        mock_ssl_wrap_socket as mock_urllib3_ssl_wrap_socket,
41
    )
42

43
    patches = {
7✔
44
        # stdlib: socket
45
        (socket, "socket"): MocketSocket,
46
        (socket, "create_connection"): mock_create_connection,
47
        (socket, "getaddrinfo"): mock_getaddrinfo,
48
        (socket, "gethostbyname"): mock_gethostbyname,
49
        (socket, "gethostname"): mock_gethostname,
50
        (socket, "inet_pton"): mock_inet_pton,
51
        (socket, "SocketType"): MocketSocket,
52
        (socket, "socketpair"): mock_socketpair,
53
        # stdlib: ssl
54
        (ssl, "SSLContext"): MocketSSLContext,
55
        (ssl, "wrap_socket"): mock_wrap_socket,  # python < 3.12.0
56
        # urllib3
57
        (urllib3.connection, "match_hostname"): mock_urllib3_match_hostname,
58
        (urllib3.connection, "ssl_wrap_socket"): mock_urllib3_ssl_wrap_socket,
59
        (urllib3.util, "ssl_wrap_socket"): mock_urllib3_ssl_wrap_socket,
60
        (urllib3.util.ssl_, "ssl_wrap_socket"): mock_urllib3_ssl_wrap_socket,
61
        (urllib3.util.ssl_, "wrap_socket"): mock_urllib3_ssl_wrap_socket,  # urllib3 < 2
62
    }
63

64
    for (module, name), new_value in patches.items():
7✔
65
        _patch(module, name, new_value)
7✔
66

67
    with contextlib.suppress(ImportError):
7✔
68
        from urllib3.contrib.pyopenssl import extract_from_urllib3
7✔
69

UNCOV
70
        extract_from_urllib3()
1✔
71

72

73
def disable() -> None:
7✔
74
    for module, name in list(_patches_restore.keys()):
7✔
75
        _restore(module, name)
7✔
76

77
    with contextlib.suppress(ImportError):
7✔
78
        from urllib3.contrib.pyopenssl import inject_into_urllib3
7✔
79

UNCOV
80
        inject_into_urllib3()
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc