Skip to content

Commit 14289a7

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 <dennis.maisenbacher@wdc.com>
1 parent 77b836e commit 14289a7

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

libnvme/nvme.i

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,9 @@ PyObject *hostid_from_file();
118118

119119
%exception nvme_ctrl::connect {
120120
connect_err = 0;
121-
errno = 0;
122121
$action /* $action sets connect_err to non-zero value on failure */
123-
if (connect_err == 1) {
124-
SWIG_exception(SWIG_AttributeError, "Existing controller connection");
125-
} else if (connect_err) {
126-
const char *errstr = nvme_errno_to_string(errno);
122+
if (connect_err) {
123+
const char *errstr = nvme_errno_to_string(-connect_err);
127124
if (errstr) {
128125
SWIG_exception(SWIG_RuntimeError, errstr);
129126
} else {
@@ -136,7 +133,12 @@ PyObject *hostid_from_file();
136133
discover_err = 0;
137134
$action /* $action sets discover_err to non-zero value on failure */
138135
if (discover_err) {
139-
SWIG_exception(SWIG_RuntimeError, "Discover failed");
136+
const char *errstr = nvme_errno_to_string(-discover_err);
137+
if (errstr) {
138+
SWIG_exception(SWIG_RuntimeError, errstr);
139+
} else {
140+
SWIG_exception(SWIG_RuntimeError, "Discover failed");
141+
}
140142
}
141143
}
142144

@@ -699,16 +701,16 @@ struct nvme_ns {
699701

700702
dev = nvme_ctrl_get_name($self);
701703
if (dev && !cfg->duplicate_connect) {
702-
connect_err = 1;
704+
connect_err = -ENVME_CONNECT_ALREADY;
703705
return;
704706
}
705707

706708
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
707709
ret = nvmf_add_ctrl(h, $self, cfg);
708710
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
709711

710-
if (ret < 0) {
711-
connect_err = 2;
712+
if (ret) {
713+
connect_err = ret;
712714
return;
713715
}
714716
}
@@ -769,10 +771,9 @@ struct nvme_ns {
769771
};
770772

771773
Py_BEGIN_ALLOW_THREADS /* Release Python GIL */
772-
nvmf_get_discovery_wargs(&args, &logp);
774+
discover_err = nvmf_get_discovery_wargs(&args, &logp);
773775
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
774776

775-
if (logp == NULL) discover_err = 1;
776777
return logp;
777778
}
778779

@@ -788,7 +789,7 @@ struct nvme_ns {
788789
ret = nvme_get_log(nvme_ctrl_get_transport_handle($self), &cmd, rae, NVME_LOG_PAGE_PDU_SIZE, NULL);
789790
Py_END_ALLOW_THREADS /* Reacquire Python GIL */
790791

791-
if (ret < 0) {
792+
if (ret) {
792793
Py_RETURN_NONE;
793794
}
794795

0 commit comments

Comments
 (0)