Skip to content

Commit f830b91

Browse files
committed
Use better DSSContext to IDSS mapping.
1 parent 01fd7d3 commit f830b91

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

dss/IDSS.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class IDSS(Base):
5757
'DefaultEditor',
5858
]
5959

60+
_ctx_to_dss = {}
61+
6062
ActiveCircuit: ICircuit
6163
Circuits: ICircuit
6264
Error: IError
@@ -72,6 +74,13 @@ class IDSS(Base):
7274
Obj: IObj
7375

7476
def __init__(self, api_util):
77+
if api_util.ctx not in IDSS._ctx_to_dss:
78+
IDSS._ctx_to_dss[api_util.ctx] = self
79+
80+
if None not in IDSS._ctx_to_dss:
81+
# use this as the default instance
82+
IDSS._ctx_to_dss[None] = self
83+
7584
self._version = None
7685

7786
#: Provides access to the circuit attributes and objects in general.

dss/plot.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,22 +1870,12 @@ def dss_plot(DSS, params):
18701870

18711871

18721872

1873-
def ctx2dss(ctx, instances={}): #TODO: move this cache to the IDSS class itself
1874-
DSS = instances.get(ctx)
1875-
if DSS is not None:
1876-
return DSS
1877-
1878-
# Handle the most likely case
1879-
if DSSPrime._api_util.ctx == ctx:
1880-
instances[ctx] = DSSPrime
1881-
return DSSPrime
1882-
1883-
# For new ctx references, wrap the ctx in a
1884-
# new DSS instance and cache it for later
1885-
util = CffiApiUtil(api_util.ffi, api_util.lib_unpatched, ctx)
1886-
util.owns_ctx = False
1887-
DSS = IDSS(util)
1888-
instances[ctx] = DSS
1873+
def _ctx2dss(ctx):
1874+
DSS = IDSS._ctx_to_dss.get(ctx)
1875+
if DSS is None:
1876+
#TODO: maybe warn here?
1877+
DSS = IDSS._ctx_to_dss.get(None)
1878+
18891879
return DSS
18901880

18911881
# dss_progress_bar = None
@@ -1897,7 +1887,7 @@ def dss_python_cb_write(ctx, message_str, message_type):
18971887
global dss_progress_bar
18981888
global dss_progress_desc
18991889

1900-
# DSS = ctx2dss(ctx)
1890+
# DSS = _ctx2dss(ctx)
19011891

19021892
message_str = api_util.ffi.string(message_str).decode(api_util.codec)
19031893
if message_type == api_util.lib.DSSMessageType_Error:
@@ -1964,7 +1954,7 @@ def dss_python_cb_plot(ctx, paramsStr):
19641954
params = json.loads(api_util.ffi.string(paramsStr))
19651955
result = 0
19661956
try:
1967-
DSS = ctx2dss(ctx)
1957+
DSS = _ctx2dss(ctx)
19681958
result = dss_plot(DSS, params)
19691959
if _do_show:
19701960
plt.show()

0 commit comments

Comments
 (0)