Skip to content

Commit 675d13b

Browse files
MaisenbacherDigaw
authored andcommitted
python: update error handling for bindings
The error handling has changed from returning -1 and errno set to just returing the error code. Negative values are internal errors, e.g. -ENOMEM. Positive values are status code from the transport. Signed-off-by: Dennis Maisenbacher <[email protected]>
1 parent fbf64ca commit 675d13b

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

libnvme/libnvme/nvme.i

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,9 @@ PyObject *hostid_from_file();
5555

5656
%exception nvme_ctrl::connect {
5757
connect_err = 0;
58-
errno = 0;
5958
$action /* $action sets connect_err to non-zero value on failure */
60-
if (connect_err == 1) {
61-
SWIG_exception(SWIG_AttributeError, "Existing controller connection");
62-
} else if (connect_err) {
63-
const char *errstr = nvme_errno_to_string(errno);
59+
if (connect_err) {
60+
const char *errstr = nvme_errno_to_string(-connect_err);
6461
if (errstr) {
6562
SWIG_exception(SWIG_RuntimeError, errstr);
6663
} else {
@@ -76,7 +73,7 @@ PyObject *hostid_from_file();
7673
if (connect_err == 1) {
7774
SWIG_exception(SWIG_AttributeError, "No controller connection");
7875
} else if (connect_err) {
79-
const char *errstr = nvme_errno_to_string(errno);
76+
const char *errstr = nvme_errno_to_string(-connect_err);
8077
if (errstr) {
8178
SWIG_exception(SWIG_RuntimeError, errstr);
8279
} else {
@@ -91,7 +88,12 @@ PyObject *hostid_from_file();
9188
if (discover_err == 1) {
9289
SWIG_exception(SWIG_AttributeError, "No controller connection");
9390
} else if (discover_err) {
94-
SWIG_exception(SWIG_RuntimeError, "Discover failed");
91+
const char *errstr = nvme_errno_to_string(-discover_err);
92+
if (errstr) {
93+
SWIG_exception(SWIG_RuntimeError, errstr);
94+
} else {
95+
SWIG_exception(SWIG_RuntimeError, "Discover failed");
96+
}
9597
}
9698
}
9799

@@ -720,16 +722,16 @@ struct nvme_ns {
720722

721723
dev = nvme_ctrl_get_name($self);
722724
if (dev && !cfg->duplicate_connect) {
723-
connect_err = 1;
725+
connect_err = -ENVME_CONNECT_ALREADY;
724726
return;
725727
}
726728

727729
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
728730
ret = nvmf_add_ctrl(h, $self, cfg);
729731
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
730732

731-
if (ret < 0) {
732-
connect_err = 2;
733+
if (ret) {
734+
connect_err = ret;
733735
return;
734736
}
735737
}
@@ -806,7 +808,7 @@ struct nvme_ns {
806808
return NULL;
807809
}
808810
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
809-
nvmf_get_discovery_wargs(&args, &logp);
811+
discover_err = nvmf_get_discovery_wargs(&args, &logp);
810812
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
811813

812814
if (logp == NULL) discover_err = 2;
@@ -825,7 +827,7 @@ struct nvme_ns {
825827
ret = nvme_get_log(nvme_ctrl_get_transport_handle($self), &cmd, rae, NVME_LOG_PAGE_PDU_SIZE, NULL);
826828
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
827829

828-
if (ret < 0) {
830+
if (ret) {
829831
Py_RETURN_NONE;
830832
}
831833

0 commit comments

Comments
 (0)