summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkhali <khali@7894878c-1315-0410-8ee3-d5d059ff63e0>2015-01-26 11:33:09 +0000
committerkhali <khali@7894878c-1315-0410-8ee3-d5d059ff63e0>2015-01-26 11:33:09 +0000
commitbdd18a72e3a70551d9528f8afbcbcb8d343934c5 (patch)
tree4a13279ad98ee92bc8e3765edf07a13822b42ad8
parent8dcb75036ca3d1871aec7d7c7666985880721a89 (diff)
downloadi2c-tools-bdd18a72e3a70551d9528f8afbcbcb8d343934c5.tar.gz
py-smbus: Add support for python 3
git-svn-id: http://lm-sensors.org/svn/i2c-tools/trunk@6267 7894878c-1315-0410-8ee3-d5d059ff63e0
-rw-r--r--CHANGES1
-rw-r--r--py-smbus/smbusmodule.c47
2 files changed, 46 insertions, 2 deletions
diff --git a/CHANGES b/CHANGES
index f0e2a6c..1fe5d71 100644
--- a/CHANGES
+++ b/CHANGES
@@ -55,6 +55,7 @@ SVN HEAD
Use I2C_SMBUS_BLOCK_MAX instead of hard-coding 32
lib/smbus.c: Add missing include which was causing a build error
py-smbus: Fix module level docs
+ Add support for python 3
3.1.0 (2011-12-04)
decode-dimms: Decode module configuration type of DDR SDRAM
diff --git a/py-smbus/smbusmodule.c b/py-smbus/smbusmodule.c
index d360a36..48a408b 100644
--- a/py-smbus/smbusmodule.c
+++ b/py-smbus/smbusmodule.c
@@ -94,7 +94,11 @@ SMBus_dealloc(SMBus *self)
PyObject *ref = SMBus_close(self);
Py_XDECREF(ref);
+#if PY_MAJOR_VERSION >= 3
+ Py_TYPE(self)->tp_free((PyObject *)self);
+#else
self->ob_type->tp_free((PyObject *)self);
+#endif
}
#define MAXPATH 16
@@ -434,11 +438,19 @@ SMBus_list_to_data(PyObject *list, union i2c_smbus_data *data)
for (ii = 0; ii < len; ii++) {
PyObject *val = PyList_GET_ITEM(list, ii);
+#if PY_MAJOR_VERSION >= 3
+ if (!PyLong_Check(val)) {
+ PyErr_SetString(PyExc_TypeError, msg);
+ return 0; /* fail */
+ }
+ data->block[ii+1] = (__u8)PyLong_AS_LONG(val);
+#else
if (!PyInt_Check(val)) {
PyErr_SetString(PyExc_TypeError, msg);
return 0; /* fail */
}
data->block[ii+1] = (__u8)PyInt_AS_LONG(val);
+#endif
}
return 1; /* success */
@@ -637,9 +649,14 @@ static PyGetSetDef SMBus_getset[] = {
};
static PyTypeObject SMBus_type = {
+#if PY_MAJOR_VERSION >= 3
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "SMBus", /* tp_name */
+#else
PyObject_HEAD_INIT(NULL)
0, /* ob_size */
"smbus.SMBus", /* tp_name */
+#endif
sizeof(SMBus), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)SMBus_dealloc, /* tp_dealloc */
@@ -678,24 +695,50 @@ static PyTypeObject SMBus_type = {
SMBus_new, /* tp_new */
};
+#if PY_MAJOR_VERSION >= 3
+static struct PyModuleDef SMBusModule = {
+ PyModuleDef_HEAD_INIT,
+ "SMBus", /* m_name */
+ SMBus_module_doc, /* m_doc */
+ -1, /* m_size */
+ NULL, /* m_methods */
+ NULL, /* m_reload */
+ NULL, /* m_traverse */
+ NULL, /* m_clear */
+ NULL, /* m_free */
+};
+#define INIT_RETURN(m) return m
+#define INIT_FNAME PyInit_smbus
+#else
static PyMethodDef SMBus_module_methods[] = {
{NULL}
};
+#define INIT_RETURN(m) return
+#define INIT_FNAME initsmbus
+#endif
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
-initsmbus(void)
+INIT_FNAME(void)
{
PyObject* m;
if (PyType_Ready(&SMBus_type) < 0)
- return;
+ INIT_RETURN(NULL);
+#if PY_MAJOR_VERSION >= 3
+ m = PyModule_Create(&SMBusModule);
+#else
m = Py_InitModule3("smbus", SMBus_module_methods, SMBus_module_doc);
+#endif
+ if (m == NULL)
+ INIT_RETURN(NULL);
Py_INCREF(&SMBus_type);
PyModule_AddObject(m, "SMBus", (PyObject *)&SMBus_type);
+
+ INIT_RETURN(m);
}