Commit 80114cf1 authored by Kirill Smelkov's avatar Kirill Smelkov

py2: *: Emulate `raise from` by hand

`raise from` is invalid syntax on python2, but it still makes sense to
preserve error cause because pygolang can handle and display that even
on py2: nexedi/pygolang@bb9a94c3 .
parent 06fefd0b
......@@ -55,7 +55,7 @@ def connect(ctx, wsuri): # -> Conn
# FIXME handle ctx cancel (but it won't stuck forever due to ._ws own timeout)
ws.connect(wsuri)
except Exception as ex:
raise ConnError("connect") from ex
raise_from(ConnError("connect"), ex)
return Conn(ws, wsuri)
# Conn represents WebSocket connection to a service.
......@@ -86,7 +86,7 @@ class Conn:
raise ValueError("unexpected welcome message: %s" % msg0)
except Exception as ex:
ws.close()
raise ConnError("handshake") from ex
raise_from(ConnError("handshake"), ex)
conn.wsuri = wsuri
conn._ws = ws
......@@ -110,7 +110,7 @@ class Conn:
conn._rx_wg.wait()
err = conn._down_err # no need to lock after shutdown/_rx_wg.wait()
if not isinstance(err, ConnClosedError):
raise ConnError("close") from err
raise_from(ConnError("close"), err)
# _shutdown brings the connection down due to err.
# only the first call has effect.
......@@ -205,7 +205,7 @@ class Conn:
_, ok = _rx
if not ok:
# NOTE no need to lock - rxq is closed after ._down_err is set
raise ConnError("recv") from conn._down_err
raise_from(ConnError("recv"), conn._down_err)
rx, rx_raw = _
return (rx, rx_raw)
......@@ -231,7 +231,7 @@ class Conn:
# FIXME handle ctx cancel (but it won't stuck forever due to ._ws own timeout)
conn._ws.send(jmsg)
except Exception as ex:
raise ConnError("send") from ex
raise_from(ConnError("send"), ex)
return rxq
......@@ -250,3 +250,11 @@ class Conn:
@property
def srv_version(conn):
return conn.srv_ready_msg['version']
# py2 compat
def raise_from(exc, cause):
exc.__cause__ = cause
exc.__context__ = None
raise exc
......@@ -26,7 +26,7 @@ The KPIs themselves can be computed from Measurements via package xlte.kpi .
from __future__ import print_function, division, absolute_import
from xlte import kpi
from xlte.amari import xlog
from xlte.amari import xlog, raise_from
from golang import func
......@@ -348,7 +348,7 @@ def _stats_check(stats):
stats.get1("counters", dict).get1("messages", dict)
stats.get1("cells", dict).get1(cellname, dict).get1("counters", dict).get1("messages", dict)
except Exception as e:
raise LogError(stats.timestamp, "stats: %s" % e) from None
raise_from(LogError(stats.timestamp, "stats: %s" % e), None)
return
# _stats_cc returns specified cumulative counter from stats result.
......
......@@ -66,7 +66,7 @@ LOS_window = 1000
from xlte import amari
from xlte.amari import drb
from xlte.amari import drb, raise_from
import json
import traceback
......@@ -644,7 +644,7 @@ def _jread1(xr): # -> xdict|None
try:
l = xr._r.readline()
except Exception as e:
raise xr._err("read") from e
raise_from(xr._err("read"), e)
if len(l) == 0:
return None # EOF
......@@ -652,7 +652,7 @@ def _jread1(xr): # -> xdict|None
try:
d = json.loads(l)
except Exception as e:
raise xr._err("invalid json: %s" % e) from None
raise_from(xr._err("invalid json: %s" % e), None)
if not isinstance(d, dict):
raise xr._err("got %s instead of dict" % type(d))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment