MCPcopy
hub / github.com/mitmproxy/mitmproxy / _Placeholder

Class _Placeholder

test/mitmproxy/proxy/tutils.py:365–401  ·  view source on GitHub ↗

Placeholder value in playbooks, so that objects (flows in particular) can be referenced before they are known. Example: f = Placeholder(TCPFlow) assert ( playbook(tcp.TCPLayer(tctx)) << TcpStartHook(f) # the flow object returned here is generated by the layer.

Source from the content-addressed store, hash-verified

363
364
365class _Placeholder(Generic[T]):
366 """
367 Placeholder value in playbooks, so that objects (flows in particular) can be referenced before
368 they are known. Example:
369
370 f = Placeholder(TCPFlow)
371 assert (
372 playbook(tcp.TCPLayer(tctx))
373 << TcpStartHook(f) # the flow object returned here is generated by the layer.
374 )
375
376 # We can obtain the flow object now using f():
377 assert f().messages == 0
378 """
379
380 def __init__(self, cls: type[T]):
381 self._obj = None
382 self._cls = cls
383
384 def __call__(self) -> T:
385 """Get the actual object"""
386 return self._obj
387
388 def setdefault(self, value: T) -> T:
389 if self._obj is None:
390 if self._cls is not Any and not isinstance(value, self._cls):
391 raise TypeError(
392 f"expected {self._cls.__name__}, got {type(value).__name__}."
393 )
394 self._obj = value
395 return self._obj
396
397 def __repr__(self):
398 return f"Placeholder:{self._obj!r}"
399
400 def __str__(self):
401 return f"Placeholder:{self._obj}"
402
403
404# noinspection PyPep8Naming

Callers 1

PlaceholderFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…