Re: Howto compile py-smbus with python 3.2

2013-04-24 Thread Tom Cox
I'm trying to do something that you were doing about a year ago:  find 
a version of the smbus module that is compatible with Python 3.

Do you know where I might find this?

--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


RE: Howto compile py-smbus with python 3.2

2012-05-21 Thread Renz, Bernhard
 /* tp_dict */
0,  /* tp_descr_get */
0,  /* tp_descr_set */
0,  /* tp_dictoffset */
-   (initproc)SMBus_init,   /* tp_init */
+   (initproc)SMBus_init,   /* tp_init */
0,  /* tp_alloc */
-   SMBus_new,  /* tp_new */
+   SMBus_new,  /* tp_new */
 };

-/*static PyMethodDef SMBus_module_methods[] = {
+static PyMethodDef SMBus_module_methods[] = {
{NULL}
-};*/
-
-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 */
-};
+};

 #ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
 #define PyMODINIT_FUNC void
 #endif
 PyMODINIT_FUNC
-PyInit_smbus(void)
+initsmbus(void)
 {
PyObject* m;

-if (PyType_Ready(&SMBus_type) < 0)
-   return NULL;
+   if (PyType_Ready(&SMBus_type) < 0)
+   return;

-/* old Python 2.7 declaration */
-   /*m = Py_InitModule3("smbus", SMBus_module_methods, SMBus_module_doc);*/
-   m = PyModule_Create(&SMBusModule);
-
-if (m == NULL)
-return NULL;
+   m = Py_InitModule3("smbus", SMBus_module_methods, SMBus_module_doc);

Py_INCREF(&SMBus_type);
PyModule_AddObject(m, "SMBus", (PyObject *)&SMBus_type);
-
-   return m;
 }

-Original Message-
From: Jean Delvare [mailto:kh...@linux-fr.org]
Sent: Monday, 21 May, 2012 17:14
To: Renz, Bernhard
Cc: linux-i2c@vger.kernel.org
Subject: Re: Howto compile py-smbus with python 3.2

Hi Bernhard,

On Mon, 21 May 2012 15:10:38 +, Renz, Bernhard wrote:
> Hello,
>
> I got it to work!
> With the following pages I did a conversion of py-smbus from Python2 to 
> Python3:
> http://docs.python.org/release/3.1.5/extending/building.html#building
> http://python3porting.com/cextensions.html
>
> Here is the smbusmodule.c, which is based on the i2c-tools-3.1.0 release, 
> which compile with Python3:
> Hope maybe Khali can provide it for other developers on his repository

I'll do if you provide your changes as a patch (use diff -ruNp). BTW I hope 
that your changes do not break support for python version 2, a lot of 
distributions are still shipping that so we can't break it.

Thanks,
--
Jean Delvare


The information contained in this message may be confidential and legally 
protected under applicable law. The message is intended solely for the 
addressee(s). If you are not the intended recipient, you are hereby notified 
that any use, forwarding, dissemination, or reproduction of this message is 
strictly prohibited and may be unlawful. If you are not the intended recipient, 
please contact the sender by return e-mail and destroy all copies of the 
original message.

--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: Howto compile py-smbus with python 3.2

2012-05-21 Thread Jean Delvare
Hi Bernhard,

On Mon, 21 May 2012 15:10:38 +, Renz, Bernhard wrote:
> Hello,
> 
> I got it to work!
> With the following pages I did a conversion of py-smbus from Python2 to 
> Python3:
> http://docs.python.org/release/3.1.5/extending/building.html#building
> http://python3porting.com/cextensions.html
> 
> Here is the smbusmodule.c, which is based on the i2c-tools-3.1.0 release, 
> which compile with Python3:
> Hope maybe Khali can provide it for other developers on his repository

I'll do if you provide your changes as a patch (use diff -ruNp). BTW I
hope that your changes do not break support for python version 2, a lot
of distributions are still shipping that so we can't break it.

Thanks,
-- 
Jean Delvare
--
To unsubscribe from this list: send the line "unsubscribe linux-i2c" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


RE: Howto compile py-smbus with python 3.2

2012-05-21 Thread Renz, Bernhard
Hello,

I got it to work!
With the following pages I did a conversion of py-smbus from Python2 to Python3:
http://docs.python.org/release/3.1.5/extending/building.html#building
http://python3porting.com/cextensions.html

Here is the smbusmodule.c, which is based on the i2c-tools-3.1.0 release, which 
compile with Python3:
Hope maybe Khali can provide it for other developers on his repository


/*
 * smbusmodule.c - Python bindings for Linux SMBus access through i2c-dev
 * Copyright (C) 2005-2007 Mark M. Hoffman 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include 
#include "structmember.h"
#include 
#include 
#include 
#include 

/*
** These are required to build this module against Linux older than 2.6.23.
*/
#ifndef I2C_SMBUS_I2C_BLOCK_BROKEN
#undef I2C_SMBUS_I2C_BLOCK_DATA
#define I2C_SMBUS_I2C_BLOCK_BROKEN  6
#define I2C_SMBUS_I2C_BLOCK_DATA8
#endif

/*yDoc_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"
"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");
*/

typedef struct {
PyObject_HEAD

int fd; /* open file descriptor: /dev/i2c-?, or -1 */
int addr;   /* current client SMBus address */
int pec;/* !0 => Packet Error Codes enabled */
} SMBus;

static PyObject *
SMBus_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
SMBus *self;

if ((self = (SMBus *)type->tp_alloc(type, 0)) == NULL)
return NULL;

self->fd = -1;
self->addr = -1;
self->pec = 0;

return (PyObject *)self;
}

PyDoc_STRVAR(SMBus_close_doc,
"close()\n\n"
"Disconnects the object from the bus.\n");

static PyObject *
SMBus_close(SMBus *self)
{
if ((self->fd != -1) && (close(self->fd) == -1)) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}

self->fd = -1;
self->addr = -1;
self->pec = 0;

Py_INCREF(Py_None);
return Py_None;
}

static void
SMBus_dealloc(SMBus *self)
{
PyObject *ref = SMBus_close(self);
Py_XDECREF(ref);

/*old python 2.7 declaration */
/*self->ob_type->tp_free((PyObject *)self);*/
Py_TYPE(self)->tp_free((PyObject*)self);
}

#define MAXPATH 16

PyDoc_STRVAR(SMBus_open_doc,
"open(bus)\n\n"
"Connects the object to the specified SMBus.\n");

static PyObject *
SMBus_open(SMBus *self, PyObject *args, PyObject *kwds)
{
int bus;
char path[MAXPATH];

static char *kwlist[] = {"bus", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "i:open", kwlist, &bus))
return NULL;

if (snprintf(path, MAXPATH, "/dev/i2c-%d", bus) >= MAXPATH) {
PyErr_SetString(PyExc_OverflowError,
"Bus number is invalid.");
return NULL;
}

if ((self->fd = open(path, O_RDWR, 0)) == -1) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}

Py_INCREF(Py_None);
return Py_None;
}

static int
SMBus_init(SMBus *self, PyObject *args, PyObject *kwds)
{
int bus = -1;

static char *kwlist[] = {"bus", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:__init__",
kwlist, &bus))
return -1;

if (bus >= 0) {
SMBus_open(self, args, kwds);
if (PyErr_Occurred())
return -1;
}

return 0;
}

/*
 * private helper function, 0 => success, !0 => error
 */
static int
SMBus_set_addr(SMBus *self, int addr)
{
int ret = 0;

if (self->addr != addr) {
ret = ioctl(self->fd, I2C_SLAVE, addr);
self->addr = addr;
}

return ret;
}

#define SMBus_SET_ADDR(self, addr) do { \
if (SMBus_set_addr(self, addr)) { \
PyErr_SetFromErrno(PyExc_IOError); \
return NULL; \
}