Best Python code snippet using slash
exception_handling.py
Source:exception_handling.py
...58 """59 if not PYPY and fake_traceback:60 # Only in CPython we're able to fake the original, full traceback61 try:62 fake_tbs = create_traceback_proxy(frame_correction=2)63 except (KeyError, IndexError) as e:64 _logger.warn("Could not extract full traceback for exceptions handling")65 _logger.trace("Extraction failure: {!r}", e, exc_info=True)66 fake_tbs = tuple()67 else:68 fake_tbs = tuple()69 swallow = kwargs.pop("swallow", False)70 swallow_types = kwargs.pop('swallow_types', ())71 if swallow:72 swallow_types = swallow_types + (Exception, )73 assert isinstance(swallow_types, (list, tuple)), 'swallow_types must be either a list or a tuple'74 passthrough_types = kwargs.pop('passthrough_types', ()) + tuple(_ignored_state.ignored_exception_types)75 return _HandlingException(fake_tbs, swallow_types, passthrough_types, kwargs)76class _HandledException(object):77 exception = None78class _HandlingException(object):79 def __init__(self, fake_tbs, swallow_types, passthrough_types, handling_kwargs):80 self._fake_traceback = fake_tbs81 self._kwargs = handling_kwargs82 self._passthrough_types = passthrough_types83 self._swallow_types = swallow_types84 self._handled = _HandledException()85 def __enter__(self):86 return self._handled87 def __exit__(self, *exc_info):88 if not exc_info or exc_info == NO_EXC_INFO:89 return90 exc_value = exc_info[1]91 if isinstance(exc_value, self._passthrough_types):92 return None93 if self._fake_traceback:94 (first_tb, last_tb) = self._fake_traceback95 (second_tb, _) = create_traceback_proxy(exc_info[2])96 last_tb.tb_next = second_tb97 exc_info = (exc_info[0], exc_info[1], first_tb._tb) # pylint: disable=protected-access98 handle_exception(exc_info, **self._kwargs)99 self._handled.exception = exc_info[1]100 skip_types = () if slash_context.session is None else slash_context.session.get_skip_exception_types()101 if isinstance(exc_value, skip_types) or isinstance(exc_value, exceptions.INTERRUPTION_EXCEPTIONS):102 return None103 if self._swallow_types and isinstance(exc_value, self._swallow_types):104 _logger.trace('Swallowing {!r}', exc_value)105 return True106 return None107def handle_exception(exc_info, context=None):108 """109 Call any handlers or debugging code before propagating an exception onwards....
traceback_proxy.py
Source:traceback_proxy.py
...13import inspect14from ..utils.python import PYPY15__all__ = ["create_traceback_proxy"]16if PYPY:17 def create_traceback_proxy(tb=None, frame_correction=0): # pylint: disable=unused-argument18 raise NotImplementedError("Tracebacks manipulation is not possible in PyPy")19else:20 class TracebackProxy(object):21 """22 Wraps the builtin traceback.traceback object.23 Exports exactly the same interface, making these types interchangable24 """25 _Py_ssize_t = ctypes.c_ssize_t26 class _PyObject(ctypes.Structure):27 pass28 # Python build with "--with-pydebug"29 if hasattr(sys, 'getobjects'):30 _PyObject._fields_ = [ # pylint: disable=protected-access31 ('_ob_next', ctypes.POINTER(_PyObject)),32 ('_ob_prev', ctypes.POINTER(_PyObject)),33 ('ob_refcnt', _Py_ssize_t),34 ('ob_type', ctypes.POINTER(_PyObject))35 ]36 else:37 _PyObject._fields_ = [ # pylint: disable=protected-access38 ('ob_refcnt', _Py_ssize_t),39 ('ob_type', ctypes.POINTER(_PyObject))40 ]41 class _Frame(_PyObject):42 """43 Represents a traceback.frame object44 """45 pass46 class _Traceback(_PyObject):47 """48 Represents a traceback.traceback object49 """50 pass51 _Traceback._fields_ = [ # pylint: disable=protected-access52 ('tb_next', ctypes.POINTER(_Traceback)),53 ('tb_frame', ctypes.POINTER(_Frame)),54 ('tb_lasti', ctypes.c_int),55 ('tb_lineno', ctypes.c_int)56 ]57 def __init__(self, tb=None, frame=None):58 assert tb is not None or frame is not None59 self._tb = TracebackProxy.create_traceback()60 self._obj = TracebackProxy._Traceback.from_address(id(self._tb)) # pylint: disable=no-member61 self.tb_next = None62 if tb:63 self.tb_frame = tb.tb_frame64 self.tb_lasti = tb.tb_lasti65 self.tb_lineno = tb.tb_lineno66 else:67 self.tb_frame = frame68 self.tb_lasti = frame.f_lasti69 self.tb_lineno = frame.f_lineno70 def print_tb(self):71 traceback.print_tb(self._tb)72 @property73 def tb_next(self):74 return self._tb.tb_next75 @tb_next.setter76 def tb_next(self, tb):77 if self._tb.tb_next:78 old = TracebackProxy._Traceback.from_address(id(self._tb.tb_next)) # pylint: disable=no-member79 old.ob_refcnt -= 180 assert tb is None or isinstance(tb, types.TracebackType) or isinstance(tb, TracebackProxy)81 if tb:82 obj = TracebackProxy._Traceback.from_address(id(tb)) # pylint: disable=no-member83 obj.ob_refcnt += 184 self._obj.tb_next = ctypes.pointer(obj)85 else:86 self._obj.tb_next = ctypes.POINTER(TracebackProxy._Traceback)()87 @property88 def tb_frame(self):89 return self._tb.tb_frame90 @tb_frame.setter91 def tb_frame(self, frame):92 if self._tb.tb_frame:93 old = TracebackProxy._Frame.from_address(id(self._tb.tb_frame)) # pylint: disable=no-member94 old.ob_refcnt -= 195 if frame:96 assert isinstance(frame, types.FrameType)97 frame = TracebackProxy._Frame.from_address(id(frame)) # pylint: disable=no-member98 frame.ob_refcnt += 199 self._obj.tb_frame = ctypes.pointer(frame)100 else:101 self._obj.tb_frame = ctypes.POINTER(TracebackProxy._Frame)()102 @property103 def tb_lasti(self):104 return self._tb.tb_lasti105 @tb_lasti.setter106 def tb_lasti(self, lasti):107 self._obj.tb_lasti = lasti108 @property109 def tb_lineno(self):110 return self._tb.tb_lineno111 @tb_lineno.setter112 def tb_lineno(self, lineno):113 self._obj.tb_lineno = lineno114 def __eq__(self, other):115 return self._tb == other._tb # pylint: disable=protected-access116 def __ne__(self, other):117 return self._tb != other._tb # pylint: disable=protected-access118 @staticmethod119 def create_traceback():120 try:121 1 / 0122 except:123 tb = sys.exc_info()[2]124 return tb125 @property126 def __class__(self):127 return types.TracebackType128 def create_traceback_proxy(tb=None, frame_correction=0):129 """130 Builds a TracebackProxy object, using a given traceback, or current context if None.131 Returns a tuple with the first and the last tracebacks.132 Works only on CPython (Both Python 2 and 3 are supported)133 :param tb: traceback.traceback object to extract frames from134 :param frame_correction: Specifies the amount of frames to skip135 """136 assert frame_correction >= 0137 if isinstance(tb, types.TracebackType):138 for i in range(frame_correction + 1): # pylint: disable=unused-variable139 first = current = TracebackProxy(tb=tb)140 tb = tb.tb_next141 while tb:142 current.tb_next = TracebackProxy(tb=tb)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!