MCPcopy Index your code
hub / github.com/python/cpython / test_open

Function test_open

Lib/test/audit-tests.py:191–266  ·  view source on GitHub ↗
(testfn)

Source from the content-addressed store, hash-verified

189
190
191def test_open(testfn):
192 # SSLContext.load_dh_params uses Py_fopen() rather than normal open()
193 try:
194 import ssl
195
196 load_dh_params = ssl.create_default_context().load_dh_params
197 except ImportError:
198 load_dh_params = None
199
200 try:
201 import readline
202 except ImportError:
203 readline = None
204
205 def rl(name):
206 if readline:
207 return getattr(readline, name, None)
208 else:
209 return None
210
211 # Try a range of "open" functions.
212 # All of them should fail
213 with TestHook(raise_on_events={"open"}) as hook:
214 for fn, *args in [
215 (open, testfn, "r"),
216 (open, sys.executable, "rb"),
217 (open, 3, "wb"),
218 (open, testfn, "w", -1, None, None, None, False, lambda *a: 1),
219 (load_dh_params, testfn),
220 (rl("read_history_file"), testfn),
221 (rl("read_history_file"), None),
222 (rl("write_history_file"), testfn),
223 (rl("write_history_file"), None),
224 (rl("append_history_file"), 0, testfn),
225 (rl("append_history_file"), 0, None),
226 (rl("read_init_file"), testfn),
227 (rl("read_init_file"), None),
228 ]:
229 if not fn:
230 continue
231 with assertRaises(RuntimeError):
232 try:
233 fn(*args)
234 except NotImplementedError:
235 if fn == load_dh_params:
236 # Not callable in some builds
237 load_dh_params = None
238 raise RuntimeError
239 else:
240 raise
241
242 actual_mode = [(a[0], a[1]) for e, a in hook.seen if e == "open" and a[1]]
243 actual_flag = [(a[0], a[2]) for e, a in hook.seen if e == "open" and not a[1]]
244 assertSequenceEqual(
245 [
246 i
247 for i in [
248 (testfn, "r"),

Callers 1

Calls 5

assertRaisesFunction · 0.85
fnFunction · 0.85
assertSequenceEqualFunction · 0.85
TestHookClass · 0.70
rlFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…