diff mbox series

py-smbus: fix i2c_smbus_* error propagation

Message ID 1542815148-12800-1-git-send-email-georgii.staroselskii@emlid.com
State Accepted
Headers show
Series py-smbus: fix i2c_smbus_* error propagation | expand

Commit Message

Georgii Staroselskii Nov. 21, 2018, 3:45 p.m. UTC
It seems like after a switch to uapi headers the Python bindings haven't
been updated. This led to erronenous behavior every time anything but -1
is returned by i2c_smbus_* functions.

Signed-off-by: Georgii Staroselskii <georgii.staroselskii@emlid.com>
---
 py-smbus/smbusmodule.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

Comments

Jean Delvare Nov. 22, 2018, 11:36 a.m. UTC | #1
Hi Georgii,

On Wed, 21 Nov 2018 18:45:48 +0300, Georgii Staroselskii wrote:
> It seems like after a switch to uapi headers the Python bindings haven't
> been updated. This led to erronenous behavior every time anything but -1
> is returned by i2c_smbus_* functions.

I can't see how this is related to switching to the uapi headers. The
kernel-provided headers are supposed to be the same as the copies we
were using before.

As far as I can see, the cause of the breakage is:

commit 330bba29f3d02432e2dca6f85082763b248887ff
Author: Jean Delvare
Date:   Tue Jul 10 13:16:54 2012 +0000

    libi2c: Properly propagate real error codes on read errors

So I'll take the blame :-( I find it odd that nobody noticed the
problem in more than 6 years...

Anyway, thanks for reporting and providing the fix, I'll commit it now.
diff mbox series

Patch

diff --git a/py-smbus/smbusmodule.c b/py-smbus/smbusmodule.c
index b189106..34af992 100644
--- a/py-smbus/smbusmodule.c
+++ b/py-smbus/smbusmodule.c
@@ -216,7 +216,7 @@  SMBus_read_byte(SMBus *self, PyObject *args)
 
 	SMBus_SET_ADDR(self, addr);
 
-	if ((result = i2c_smbus_read_byte(self->fd)) == -1) {
+	if ((result = i2c_smbus_read_byte(self->fd)) < 0) {
 		PyErr_SetFromErrno(PyExc_IOError);
 		return NULL;
 	}
@@ -239,7 +239,7 @@  SMBus_write_byte(SMBus *self, PyObject *args)
 
 	SMBus_SET_ADDR(self, addr);
 
-	if ((result = i2c_smbus_write_byte(self->fd, (__u8)val)) == -1) {
+	if ((result = i2c_smbus_write_byte(self->fd, (__u8)val)) < 0) {
 		PyErr_SetFromErrno(PyExc_IOError);
 		return NULL;
 	}
@@ -263,7 +263,7 @@  SMBus_read_byte_data(SMBus *self, PyObject *args)
 
 	SMBus_SET_ADDR(self, addr);
 
-	if ((result = i2c_smbus_read_byte_data(self->fd, (__u8)cmd)) == -1) {
+	if ((result = i2c_smbus_read_byte_data(self->fd, (__u8)cmd)) < 0) {
 		PyErr_SetFromErrno(PyExc_IOError);
 		return NULL;
 	}
@@ -287,7 +287,7 @@  SMBus_write_byte_data(SMBus *self, PyObject *args)
 	SMBus_SET_ADDR(self, addr);
 
 	if ((result = i2c_smbus_write_byte_data(self->fd,
-				(__u8)cmd, (__u8)val)) == -1) {
+				(__u8)cmd, (__u8)val)) < 0) {
 		PyErr_SetFromErrno(PyExc_IOError);
 		return NULL;
 	}
@@ -311,7 +311,7 @@  SMBus_read_word_data(SMBus *self, PyObject *args)
 
 	SMBus_SET_ADDR(self, addr);
 
-	if ((result = i2c_smbus_read_word_data(self->fd, (__u8)cmd)) == -1) {
+	if ((result = i2c_smbus_read_word_data(self->fd, (__u8)cmd)) < 0) {
 		PyErr_SetFromErrno(PyExc_IOError);
 		return NULL;
 	}
@@ -335,7 +335,7 @@  SMBus_write_word_data(SMBus *self, PyObject *args)
 	SMBus_SET_ADDR(self, addr);
 
 	if ((result = i2c_smbus_write_word_data(self->fd,
-				(__u8)cmd, (__u16)val)) == -1) {
+				(__u8)cmd, (__u16)val)) < 0) {
 		PyErr_SetFromErrno(PyExc_IOError);
 		return NULL;
 	}
@@ -360,7 +360,7 @@  SMBus_process_call(SMBus *self, PyObject *args)
 	SMBus_SET_ADDR(self, addr);
 
 	if ((result = i2c_smbus_process_call(self->fd,
-				(__u8)cmd, (__u16)val)) == -1) {
+				(__u8)cmd, (__u16)val)) < 0) {
 		PyErr_SetFromErrno(PyExc_IOError);
 		return NULL;
 	}