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 ...@@ -55,7 +55,7 @@ def connect(ctx, wsuri): # -> Conn
# FIXME handle ctx cancel (but it won't stuck forever due to ._ws own timeout) # FIXME handle ctx cancel (but it won't stuck forever due to ._ws own timeout)
ws.connect(wsuri) ws.connect(wsuri)
except Exception as ex: except Exception as ex:
raise ConnError("connect") from ex raise_from(ConnError("connect"), ex)
return Conn(ws, wsuri) return Conn(ws, wsuri)
# Conn represents WebSocket connection to a service. # Conn represents WebSocket connection to a service.
...@@ -86,7 +86,7 @@ class Conn: ...@@ -86,7 +86,7 @@ class Conn:
raise ValueError("unexpected welcome message: %s" % msg0) raise ValueError("unexpected welcome message: %s" % msg0)
except Exception as ex: except Exception as ex:
ws.close() ws.close()
raise ConnError("handshake") from ex raise_from(ConnError("handshake"), ex)
conn.wsuri = wsuri conn.wsuri = wsuri
conn._ws = ws conn._ws = ws
...@@ -110,7 +110,7 @@ class Conn: ...@@ -110,7 +110,7 @@ class Conn:
conn._rx_wg.wait() conn._rx_wg.wait()
err = conn._down_err # no need to lock after shutdown/_rx_wg.wait() err = conn._down_err # no need to lock after shutdown/_rx_wg.wait()
if not isinstance(err, ConnClosedError): if not isinstance(err, ConnClosedError):
raise ConnError("close") from err raise_from(ConnError("close"), err)
# _shutdown brings the connection down due to err. # _shutdown brings the connection down due to err.
# only the first call has effect. # only the first call has effect.
...@@ -205,7 +205,7 @@ class Conn: ...@@ -205,7 +205,7 @@ class Conn:
_, ok = _rx _, ok = _rx
if not ok: if not ok:
# NOTE no need to lock - rxq is closed after ._down_err is set # 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 = _ rx, rx_raw = _
return (rx, rx_raw) return (rx, rx_raw)
...@@ -231,7 +231,7 @@ class Conn: ...@@ -231,7 +231,7 @@ class Conn:
# FIXME handle ctx cancel (but it won't stuck forever due to ._ws own timeout) # FIXME handle ctx cancel (but it won't stuck forever due to ._ws own timeout)
conn._ws.send(jmsg) conn._ws.send(jmsg)
except Exception as ex: except Exception as ex:
raise ConnError("send") from ex raise_from(ConnError("send"), ex)
return rxq return rxq
...@@ -250,3 +250,11 @@ class Conn: ...@@ -250,3 +250,11 @@ class Conn:
@property @property
def srv_version(conn): def srv_version(conn):
return conn.srv_ready_msg['version'] 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 . ...@@ -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 __future__ import print_function, division, absolute_import
from xlte import kpi from xlte import kpi
from xlte.amari import xlog from xlte.amari import xlog, raise_from
from golang import func from golang import func
...@@ -348,7 +348,7 @@ def _stats_check(stats): ...@@ -348,7 +348,7 @@ def _stats_check(stats):
stats.get1("counters", dict).get1("messages", dict) stats.get1("counters", dict).get1("messages", dict)
stats.get1("cells", dict).get1(cellname, dict).get1("counters", dict).get1("messages", dict) stats.get1("cells", dict).get1(cellname, dict).get1("counters", dict).get1("messages", dict)
except Exception as e: except Exception as e:
raise LogError(stats.timestamp, "stats: %s" % e) from None raise_from(LogError(stats.timestamp, "stats: %s" % e), None)
return return
# _stats_cc returns specified cumulative counter from stats result. # _stats_cc returns specified cumulative counter from stats result.
......
...@@ -66,7 +66,7 @@ LOS_window = 1000 ...@@ -66,7 +66,7 @@ LOS_window = 1000
from xlte import amari from xlte import amari
from xlte.amari import drb from xlte.amari import drb, raise_from
import json import json
import traceback import traceback
...@@ -644,7 +644,7 @@ def _jread1(xr): # -> xdict|None ...@@ -644,7 +644,7 @@ def _jread1(xr): # -> xdict|None
try: try:
l = xr._r.readline() l = xr._r.readline()
except Exception as e: except Exception as e:
raise xr._err("read") from e raise_from(xr._err("read"), e)
if len(l) == 0: if len(l) == 0:
return None # EOF return None # EOF
...@@ -652,7 +652,7 @@ def _jread1(xr): # -> xdict|None ...@@ -652,7 +652,7 @@ def _jread1(xr): # -> xdict|None
try: try:
d = json.loads(l) d = json.loads(l)
except Exception as e: 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): if not isinstance(d, dict):
raise xr._err("got %s instead of dict" % type(d)) 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