Skip to content

Commit 542fc4a

Browse files
[STYLE] Refactor imports in test to resolve #151
Changes in file tests/MulticastUDPClient.py: - various simplifications of imports Changes in file tests/__init__.py: - various simplifications of imports and related work Changes in file tests/context.py: - various simplifications of imports and related work Changes in file tests/test_deps.py: - various simplifications of imports and related work Changes in file tests/test_fuzz.py: - related work
1 parent 55ddbeb commit 542fc4a

File tree

5 files changed

+25
-139
lines changed

5 files changed

+25
-139
lines changed

tests/MulticastUDPClient.py

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -111,68 +111,19 @@
111111
raise ModuleNotFoundError(
112112
"[CWE-440] OMG! sys.modules is not available or empty."
113113
) from None
114-
except ImportError as err:
115-
raise ImportError("[CWE-440] Unable to import sys module.") from err
114+
except ImportError as baton:
115+
raise ImportError("[CWE-440] Unable to import sys module.") from baton
116116

117117
try:
118-
if 'os' not in sys.modules:
119-
import os
120-
else: # pragma: no branch
121-
os = sys.modules["""os"""]
122-
except Exception as badErr: # pragma: no branch
123-
baton = ModuleNotFoundError(badErr, str("[CWE-758] Test module failed completely."))
124-
baton.module = __module__
125-
baton.path = __file__
126-
baton.__cause__ = badErr
127-
raise baton from badErr
118+
import socket
119+
import socketserver
120+
except ImportError as baton:
121+
raise ImportError("[CWE-758] Test module failed completely.") from baton
128122

129123
try:
130-
if 'functools' not in sys.modules:
131-
import functools
132-
else: # pragma: no branch
133-
functools = sys.modules["""functools"""]
134-
except Exception as badErr: # pragma: no branch
135-
baton = ModuleNotFoundError(badErr, str("[CWE-758] Test module failed completely."))
136-
baton.module = __module__
137-
baton.path = __file__
138-
baton.__cause__ = badErr
139-
raise baton from badErr
140-
141-
try:
142-
if 'socket' not in sys.modules:
143-
import socket
144-
else: # pragma: no branch
145-
socket = sys.modules["""socket"""]
146-
except Exception as badErr: # pragma: no branch
147-
baton = ModuleNotFoundError(badErr, str("[CWE-758] Test module failed completely."))
148-
baton.module = __module__
149-
baton.path = __file__
150-
baton.__cause__ = badErr
151-
raise baton from badErr
152-
153-
try:
154-
if 'socketserver' not in sys.modules:
155-
import socketserver
156-
else: # pragma: no branch
157-
socketserver = sys.modules["""socketserver"""]
158-
except Exception as badErr: # pragma: no branch
159-
baton = ModuleNotFoundError(badErr, str("[CWE-758] Test module failed completely."))
160-
baton.module = __module__
161-
baton.path = __file__
162-
baton.__cause__ = badErr
163-
raise baton from badErr
164-
165-
try:
166-
if 'random' not in sys.modules:
167-
import random
168-
else: # pragma: no branch
169-
random = sys.modules["""random"""]
170-
except Exception as badErr: # pragma: no branch
171-
baton = ModuleNotFoundError(badErr, str("[CWE-758] Test module failed completely."))
172-
baton.module = __module__
173-
baton.path = __file__
174-
baton.__cause__ = badErr
175-
raise baton from badErr
124+
import random
125+
except ImportError as baton: # pragma: no branch
126+
raise ModuleNotFoundError(baton, str("[CWE-758] Test module failed to randomize.")) from baton
176127

177128

178129
class MCastClient(object): # skipcq: PYL-R0205

tests/__init__.py

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,36 +55,10 @@
5555

5656
try:
5757
import sys
58-
if sys.__name__ is None: # pragma: no branch
59-
raise ModuleNotFoundError(
60-
"[CWE-440] OMG! we could not import sys. ABORT. ABORT."
61-
) from None
62-
except Exception as err: # pragma: no branch
63-
raise ImportError(err) from err
64-
65-
try:
66-
if 'os' not in sys.modules:
67-
import os
68-
else: # pragma: no branch
69-
os = sys.modules["""os"""]
70-
except Exception as err: # pragma: no branch
71-
raise ModuleNotFoundError("[CWE-440] OS Failed to import.") from err
72-
73-
try:
74-
if 'unittest' not in sys.modules:
75-
import unittest
76-
else: # pragma: no branch
77-
unittest = sys.modules["""unittest"""]
78-
except Exception as err: # pragma: no branch
79-
raise ModuleNotFoundError("[CWE-440] unittest Failed to import.") from err
80-
81-
try:
82-
if 'functools' not in sys.modules:
83-
import functools
84-
else: # pragma: no branch
85-
functools = sys.modules["""functools"""]
86-
except Exception as err: # pragma: no branch
87-
raise ModuleNotFoundError("[CWE-440] functools Failed to import.") from err
58+
import os
59+
import unittest
60+
except ImportError as baton: # pragma: no branch
61+
raise ModuleNotFoundError("[CWE-440] Module failed to import.") from baton
8862

8963
try:
9064
if 'multicast' not in sys.modules:

tests/context.py

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -83,44 +83,14 @@
8383
raise ImportError("[CWE-440] Unable to import sys module.") from err
8484

8585
try:
86-
if 'os' not in sys.modules:
87-
import os
88-
else: # pragma: no branch
89-
os = sys.modules["""os"""]
90-
except ImportError as err: # pragma: no branch
91-
raise ModuleNotFoundError("[CWE-440] OS Failed to import.") from err
92-
93-
try:
94-
if 'secrets' not in sys.modules:
95-
import secrets
96-
else: # pragma: no branch
97-
secrets = sys.modules["""secrets"""]
98-
except ImportError as err: # pragma: no branch
99-
raise ModuleNotFoundError("[CWE-440] Secrets Failed to import.") from err
100-
101-
try:
102-
if 'string' not in sys.modules:
103-
import string
104-
else: # pragma: no branch
105-
string = sys.modules["""string"""]
86+
import os
87+
if not hasattr(os, 'sep') or not os.sep: # pragma: no branch
88+
raise ModuleNotFoundError("[CWE-440] OS support is not available.") from None
89+
import secrets
90+
import string
91+
import unittest
10692
except ImportError as err: # pragma: no branch
107-
raise ModuleNotFoundError("[CWE-440] String Failed to import.") from err
108-
109-
try:
110-
if 'unittest' not in sys.modules:
111-
import unittest
112-
else: # pragma: no branch
113-
unittest = sys.modules["""unittest"""]
114-
except ImportError as err: # pragma: no branch
115-
raise ModuleNotFoundError("[CWE-440] unittest Failed to import.") from err
116-
117-
try:
118-
if 'contextlib' not in sys.modules:
119-
import contextlib
120-
else: # pragma: no branch
121-
contextlib = sys.modules["""contextlib"""]
122-
except ImportError as err: # pragma: no branch
123-
raise ModuleNotFoundError("[CWE-440] contextlib Failed to import.") from err
93+
raise ModuleNotFoundError("[CWE-440] Module Failed to import.") from err
12494

12595
try:
12696
from contextlib import contextmanager
@@ -136,10 +106,7 @@
136106
raise ModuleNotFoundError("[CWE-440] Process Failed to import.") from err
137107

138108
try:
139-
if 'subprocess' not in sys.modules:
140-
import subprocess
141-
else: # pragma: no branch
142-
subprocess = sys.modules["""subprocess"""]
109+
import subprocess
143110
except ImportError as err: # pragma: no branch
144111
raise ModuleNotFoundError("[CWE-440] subprocess Failed to import.") from err
145112

tests/test_deps.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,12 @@
3636
raise ImportError("[CWE-758] Failed to import test context") from _cause
3737

3838
try:
39-
if 're' not in sys.modules:
40-
import re
41-
else: # pragma: no branch
42-
re = sys.modules["""re"""]
39+
import re
4340
except Exception as _cause: # pragma: no branch
4441
raise ImportError("[CWE-440] re Failed to import.") from _cause
4542

4643
try:
47-
if 'venv' not in sys.modules:
48-
import venv
49-
else: # pragma: no branch
50-
venv = sys.modules["""venv"""]
44+
import venv
5145
except Exception as _cause: # pragma: no branch
5246
raise ImportError("[CWE-440] venv Failed to import.") from _cause
5347

tests/test_fuzz.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
from hypothesis import settings
7373
from hypothesis import strategies as st
7474
_has_hypothesis = True
75-
except ImportError as _: # pragma: no branch
76-
del _ # skipcq - cleanup any error vars early
75+
except ImportError as _cause: # pragma: no branch
76+
del _cause # skipcq - cleanup any error vars early
7777
_has_hypothesis = False
7878

7979

0 commit comments

Comments
 (0)