From patchwork Mon Jan 19 15:13:26 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Angelo Compagnucci X-Patchwork-Id: 430562 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3CFE3140213 for ; Tue, 20 Jan 2015 02:14:16 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752267AbbASPOJ (ORCPT ); Mon, 19 Jan 2015 10:14:09 -0500 Received: from mail-qc0-f180.google.com ([209.85.216.180]:56642 "EHLO mail-qc0-f180.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752266AbbASPOH (ORCPT ); Mon, 19 Jan 2015 10:14:07 -0500 Received: by mail-qc0-f180.google.com with SMTP id r5so20194586qcx.11 for ; Mon, 19 Jan 2015 07:14:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=k4GC+7gTQ2MEc0GFqdMS5+ECkiVNafgP2R8SDhvEvuk=; b=n2lKM/RGJJrnYFzZluwqllrrpnGZ+H86MgCg1jvYuxXjlBvdKmsl+MGK3QV9yqMs+5 Fd8tD6W242O+v8fh9GTUdRlRA/lE0nl7U9VdtnblbAnR5Dj+vDRdqAhamTAP6Imgl1iE dEjiR8uHQdT0xHBjZv/2Ygg+sBDZKMytAuhAla/7gOZkMWlOndP0r06uqqVdVwOg8gsm A8+qEoFQ7OefzO8j926XZIMoTyIGxJCrSIf5HIGeDWxrN6fuY8dexMHwzErLxdxpA228 9/xhyyY6y7ghpkYX0GT/v2NfkBJVGF9hsLz8LcRGIEhqnD2szEFthatyXu2ISkQ8R3QE xBIw== X-Received: by 10.229.240.130 with SMTP id la2mr48632713qcb.9.1421680446572; Mon, 19 Jan 2015 07:14:06 -0800 (PST) MIME-Version: 1.0 Received: by 10.140.20.248 with HTTP; Mon, 19 Jan 2015 07:13:26 -0800 (PST) From: Angelo Compagnucci Date: Mon, 19 Jan 2015 16:13:26 +0100 Message-ID: Subject: i2c-tools: add compatibility for python2/3 to py-smbus To: linux-i2c@vger.kernel.org, jdelvare@suse.de Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org Dear Jean Delvare, Attached you can find a patch that implements py-smbus for python3 with python2 backward compatibility. This patch is not heavily tested, but it wors well for me. Unfortunately, I don't know how to use svn for sharing source code, it is so ugly compared to git and it doesn't provide a way to send patches via email, so the best way I found was to attach the patch file. Hope this helps! Sincerely, Angelo Index: smbusmodule.c =================================================================== diff --git a/i2c-tools/trunk/py-smbus/smbusmodule.c b/i2c-tools/trunk/py-smbus/smbusmodule.c --- a/i2c-tools/trunk/py-smbus/smbusmodule.c (revision 6261) +++ b/i2c-tools/trunk/py-smbus/smbusmodule.c (working copy) @@ -18,14 +18,16 @@ #include #include "structmember.h" -#include #include #include #include -#include #include -#include +/* Required for backward compatibility with python 2*/ +#if PY_MAJOR_VERSION >= 3 +#define PY3 +#endif + /* ** These are required to build this module against Linux older than 2.6.23. */ @@ -35,6 +37,7 @@ #define I2C_SMBUS_I2C_BLOCK_DATA 8 #endif +#ifndef PY3 PyDoc_STRVAR(SMBus_module_doc, "This module defines an object type that allows SMBus transactions\n" "on hosts running the Linux kernel. The host kernel must have I2C\n" @@ -44,6 +47,7 @@ "\n" "Because the I2C device interface is opened R/W, users of this\n" "module usually must have root permissions.\n"); +#endif typedef struct { PyObject_HEAD @@ -94,7 +98,11 @@ PyObject *ref = SMBus_close(self); Py_XDECREF(ref); +#ifndef PY3 self->ob_type->tp_free((PyObject *)self); +#else + Py_TYPE(self)->tp_free((PyObject *)self); +#endif } #define MAXPATH 16 @@ -434,11 +442,19 @@ for (ii = 0; ii < len; ii++) { PyObject *val = PyList_GET_ITEM(list, ii); +#ifndef PY3 if (!PyInt_Check(val)) { PyErr_SetString(PyExc_TypeError, msg); return 0; /* fail */ } data->block[ii+1] = (__u8)PyInt_AS_LONG(val); +#else + if (!PyLong_Check(val)) { + PyErr_SetString(PyExc_TypeError, msg); + return 0; /* fail */ + } + data->block[ii+1] = (__u8)PyLong_AS_LONG(val); +#endif } return 1; /* success */ @@ -636,12 +652,18 @@ {NULL}, }; +#ifndef PY3 static PyTypeObject SMBus_type = { PyObject_HEAD_INIT(NULL) 0, /* ob_size */ "smbus.SMBus", /* tp_name */ - sizeof(SMBus), /* tp_basicsize */ - 0, /* tp_itemsize */ +#else +static PyTypeObject SMBus_type = { + PyVarObject_HEAD_INIT(NULL, 0) + "SMBus", /* tp_name */ +#endif + sizeof(SMBus), /* tp_basicsize */ + 0, /* tp_itemsize */ (destructor)SMBus_dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ @@ -678,24 +700,64 @@ SMBus_new, /* tp_new */ }; +#ifndef PY3 static PyMethodDef SMBus_module_methods[] = { {NULL} }; +#else +static struct PyModuleDef SMBusModule = { + PyModuleDef_HEAD_INIT, + "SMBus", /* m_name */ + "This module defines an object type that allows SMBus transactions\n" + "on hosts running the Linux kernel. The host kernel must have I2C\n" + "support, I2C device interface support, and a bus adapter driver.\n" + "All of these can be either built-in to the kernel, or loaded from\n" + "modules.\n" + "\n" + "Because the I2C device interface is opened R/W, users of this\n" + "module usually must have root permissions.\n", /* m_doc */ + -1, /* m_size */ + NULL, /* m_methods */ + NULL, /* m_reload */ + NULL, /* m_traverse */ + NULL, /* m_clear */ + NULL, /* m_free */ +}; +#endif #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */ #define PyMODINIT_FUNC void #endif PyMODINIT_FUNC -initsmbus(void) + +#ifndef PY3 +initsmbus(void) +#else +PyInit_smbus(void) +#endif { PyObject* m; if (PyType_Ready(&SMBus_type) < 0) +#ifndef PY3 return; +#else + return NULL; +#endif +#ifndef PY3 m = Py_InitModule3("smbus", SMBus_module_methods, SMBus_module_doc); +#else + m = PyModule_Create(&SMBusModule); + if (m == NULL) + return NULL; +#endif Py_INCREF(&SMBus_type); PyModule_AddObject(m, "SMBus", (PyObject *)&SMBus_type); + +#ifdef PY3 + return m; +#endif }