Skip to content

Commit 40334a5

Browse files
committed
From DSS-Python: Error.UseExceptions, updated CompatFlags doc
Includes a test for Error.UseExceptions
1 parent 885a5b4 commit 40334a5

File tree

4 files changed

+53
-5
lines changed

4 files changed

+53
-5
lines changed

opendssdirect/Basic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ def CompatFlags(*args):
176176
so this flag can be used to toggle the old/bad values where feasible.
177177
- 0x4 (bit 2): Toggle some InvControl behavior introduced in OpenDSS 9.6.1.1. It could be a regression
178178
but needs further investigation, so we added this flag in the time being.
179+
- 0x8 (bit 3): When using "save circuit", the official OpenDSS always includes the "CalcVoltageBases" command
180+
in the saved script. We found that it is not always a good idea, so we removed the command (leaving it
181+
commented). Use this flag to enable the command in the saved script.
179182
180183
These flags may change for each version of DSS C-API, but the same value will not be reused. That is,
181184
when we remove a compatibility flag, it will have no effect but will also not affect anything else

opendssdirect/Error.py

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ._utils import lib, get_string
1+
from ._utils import lib, get_string, Base
22

33

44
def Description():
@@ -58,5 +58,33 @@ def ExtendedErrors(*args):
5858
lib.Error_Set_ExtendedErrors(Value)
5959

6060

61-
_columns = ["Description", "Number", "EarlyAbort"]
62-
__all__ = ["Description", "Number", "EarlyAbort", "ExtendedErrors"]
61+
def UseExceptions(*args):
62+
"""
63+
Controls whether the automatic error checking mechanism is enable, i.e., if
64+
the DSS engine errors (from the `Error` interface) are mapped exception when
65+
detected.
66+
67+
**When disabled, the user takes responsibility for checking for errors.**
68+
This can be done through the `Error` interface. When `Error.Number` is not
69+
zero, there should be an error message in `Error.Description`. This is compatible
70+
with the behavior on the official OpenDSS (Windows-only COM implementation) when
71+
`AllowForms` is disabled.
72+
73+
Users can also use the DSS command `Export ErrorLog` to inspect for errors.
74+
75+
**WARNING:** This is a global setting, affects all DSS instances from DSS-Python
76+
and OpenDSSDirect.py.
77+
78+
(API Extension)
79+
"""
80+
# Getter
81+
if len(args) == 0:
82+
return Base._use_exceptions
83+
84+
# Setter
85+
value, = args
86+
Base._enable_exceptions(bool(value))
87+
88+
89+
_columns = ["Description", "Number", "EarlyAbort", "ExtendedErrors", "UseExceptions"]
90+
__all__ = ["Description", "Number", "EarlyAbort", "ExtendedErrors", "UseExceptions"]

opendssdirect/_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import numpy as np
44
import warnings
55

6-
# Bind to the FFI module instance. This should be refined in a future version,
7-
# especially after DSS C-API 0.11 is fully released.
6+
# Bind to the FFI module instance. This should be refined in a future version
87
lib = dss_py.prime_api_util.lib
98
ffi = dss_py.prime_api_util.ffi
109
api_util = dss_py.prime_api_util
1110
codec = api_util.codec
1211
CheckForError = dss_py.DSS_GR.CheckForError
1312
DSSException = dss_py._cffi_api_util.DSSException
13+
Base = dss_py._cffi_api_util.Base
1414

1515
# Currently, we prefer the functions that return lists (suffix 2)
1616
# to keep higher compatibility with previous versions of OpenDSSDirect.py.

tests/test_opendssdirect.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5342,3 +5342,20 @@ def test_dss_extensions_debug():
53425342
assert "DEBUG" in opendssdirect.Basic.Version()
53435343
else:
53445344
assert "DEBUG" not in opendssdirect.Basic.Version()
5345+
5346+
5347+
def test_exception_control(dss):
5348+
# Default behavior
5349+
assert dss.Error.UseExceptions()
5350+
with pt.raises(dss.DSSException):
5351+
dss.Text.Command('this_is_an_invalid_command1')
5352+
5353+
# Behavior without exceptions
5354+
dss.Error.UseExceptions(False)
5355+
assert not dss.Error.UseExceptions()
5356+
dss.Text.Command('this_is_an_invalid_command2')
5357+
# There should be an error code waiting for us...
5358+
assert dss.Error.Number() != 0
5359+
# ...but it should be gone after we read it
5360+
assert dss.Error.Number() == 0
5361+

0 commit comments

Comments
 (0)