Skip to content

Commit ea023b4

Browse files
committed
Merge branch 'westphahl-python3'
2 parents d55fd61 + 23ba591 commit ea023b4

File tree

5 files changed

+99
-63
lines changed

5 files changed

+99
-63
lines changed

Adafruit_I2C.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
# ===========================================================================
66
# Adafruit_I2C Class
7-
# Adafruit_I2C.py is essentially a fork of the Adafruit Raspberry Pi I2C module.
8-
# Any pull requests for this module should be directed to the following, and I
7+
# Adafruit_I2C.py is essentially a fork of the Adafruit Raspberry Pi I2C module.
8+
# Any pull requests for this module should be directed to the following, and I
99
# can pull them. I'd rather not deviate from the original:
1010
# https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_I2C
1111
# ===========================================================================
@@ -28,59 +28,59 @@ def reverseByteOrder(self, data):
2828
return val
2929

3030
def errMsg(self):
31-
print "Error accessing 0x%02X: Check your I2C address" % self.address
31+
print("Error accessing 0x%02X: Check your I2C address" % self.address)
3232
return -1
3333

3434
def write8(self, reg, value):
3535
"Writes an 8-bit value to the specified register/address"
3636
try:
3737
self.bus.write_byte_data(self.address, reg, value)
3838
if self.debug:
39-
print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)
40-
except IOError, err:
39+
print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg))
40+
except IOError as err:
4141
return self.errMsg()
4242

4343
def write16(self, reg, value):
4444
"Writes a 16-bit value to the specified register/address pair"
4545
try:
4646
self.bus.write_word_data(self.address, reg, value)
4747
if self.debug:
48-
print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" %
48+
print("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" %
4949
(value, reg, reg+1))
50-
except IOError, err:
50+
except IOError as err:
5151
return self.errMsg()
5252

5353
def writeList(self, reg, list):
5454
"Writes an array of bytes using I2C format"
5555
try:
5656
if self.debug:
57-
print "I2C: Writing list to register 0x%02X:" % reg
58-
print list
57+
print("I2C: Writing list to register 0x%02X:" % reg)
58+
print(list)
5959
self.bus.write_i2c_block_data(self.address, reg, list)
60-
except IOError, err:
60+
except IOError as err:
6161
return self.errMsg()
6262

6363
def readList(self, reg, length):
6464
"Read a list of bytes from the I2C device"
6565
try:
6666
results = self.bus.read_i2c_block_data(self.address, reg, length)
6767
if self.debug:
68-
print ("I2C: Device 0x%02X returned the following from reg 0x%02X" %
68+
print("I2C: Device 0x%02X returned the following from reg 0x%02X" %
6969
(self.address, reg))
70-
print results
70+
print(results)
7171
return results
72-
except IOError, err:
72+
except IOError as err:
7373
return self.errMsg()
7474

7575
def readU8(self, reg):
7676
"Read an unsigned byte from the I2C device"
7777
try:
7878
result = self.bus.read_byte_data(self.address, reg)
7979
if self.debug:
80-
print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
80+
print("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
8181
(self.address, result & 0xFF, reg))
8282
return result
83-
except IOError, err:
83+
except IOError as err:
8484
return self.errMsg()
8585

8686
def readS8(self, reg):
@@ -89,35 +89,35 @@ def readS8(self, reg):
8989
result = self.bus.read_byte_data(self.address, reg)
9090
if result > 127: result -= 256
9191
if self.debug:
92-
print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
92+
print("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
9393
(self.address, result & 0xFF, reg))
9494
return result
95-
except IOError, err:
95+
except IOError as err:
9696
return self.errMsg()
9797

9898
def readU16(self, reg):
9999
"Reads an unsigned 16-bit value from the I2C device"
100100
try:
101101
result = self.bus.read_word_data(self.address,reg)
102102
if (self.debug):
103-
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
103+
print("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg))
104104
return result
105-
except IOError, err:
105+
except IOError as err:
106106
return self.errMsg()
107107

108108
def readS16(self, reg):
109109
"Reads a signed 16-bit value from the I2C device"
110110
try:
111111
result = self.bus.read_word_data(self.address,reg)
112112
if (self.debug):
113-
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
113+
print("I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg))
114114
return result
115-
except IOError, err:
115+
except IOError as err:
116116
return self.errMsg()
117117

118118
if __name__ == '__main__':
119119
try:
120120
bus = Adafruit_I2C(address=0)
121-
print "Default I2C bus is accessible"
121+
print("Default I2C bus is accessible")
122122
except:
123-
print "Error accessing default I2C bus"
123+
print("Error accessing default I2C bus")

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
0.0.30
2+
---
3+
* Merge Python 3 compatibility fixes from Github user westphahl.
4+
* Moved old Angstrom build fix for missing py_compile from setup.py to separate file.
5+
16
0.0.20
27
----
38
* Fix for SPI not loading spidevX.X correctly based on load order

fix_py_compile.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python2
2+
# Some Angstrom images are missing the py_compile module; get it if not
3+
# present:
4+
# Fix credit:https://github.com/alexanderhiam/PyBBIO/blob/master/setup.py
5+
import random, os
6+
python_lib_path = random.__file__.split('random')[0]
7+
if not os.path.exists(python_lib_path + 'py_compile.py'):
8+
print "py_compile module missing; installing to %spy_compile.py" %\
9+
python_lib_path
10+
import urllib2
11+
url = "http://hg.python.org/cpython/raw-file/4ebe1ede981e/Lib/py_compile.py"
12+
py_compile = urllib2.urlopen(url)
13+
with open(python_lib_path+'py_compile.py', 'w') as f:
14+
f.write(py_compile.read())
15+
print "testing py_compile..."
16+
try:
17+
import py_compile
18+
print "py_compile installed successfully"
19+
except Exception, e:
20+
print "*py_compile install failed, could not import"
21+
print "*Exception raised:"
22+
raise e

setup.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,3 @@
1-
# Some Angstrom images are missing the py_compile module; get it if not
2-
# present:
3-
# Fix credit:https://github.com/alexanderhiam/PyBBIO/blob/master/setup.py
4-
import random, os
5-
python_lib_path = random.__file__.split('random')[0]
6-
if not os.path.exists(python_lib_path + 'py_compile.py'):
7-
print "py_compile module missing; installing to %spy_compile.py" %\
8-
python_lib_path
9-
import urllib2
10-
url = "http://hg.python.org/cpython/raw-file/4ebe1ede981e/Lib/py_compile.py"
11-
py_compile = urllib2.urlopen(url)
12-
with open(python_lib_path+'py_compile.py', 'w') as f:
13-
f.write(py_compile.read())
14-
print "testing py_compile..."
15-
try:
16-
import py_compile
17-
print "py_compile installed successfully"
18-
except Exception, e:
19-
print "*py_compile install failed, could not import"
20-
print "*Exception raised:"
21-
raise e
22-
231
try:
242
from overlays import builder
253
builder.compile()
@@ -35,15 +13,14 @@
3513
'Operating System :: POSIX :: Linux',
3614
'License :: OSI Approved :: MIT License',
3715
'Intended Audience :: Developers',
38-
'Programming Language :: Python :: 2.6',
3916
'Programming Language :: Python :: 2.7',
4017
'Programming Language :: Python :: 3',
4118
'Topic :: Software Development',
4219
'Topic :: Home Automation',
4320
'Topic :: System :: Hardware']
4421

4522
setup(name = 'Adafruit_BBIO',
46-
version = '0.0.20',
23+
version = '0.0.30',
4724
author = 'Justin Cooper',
4825
author_email = '[email protected]',
4926
description = 'A module to control BeagleBone IO channels',
@@ -54,7 +31,7 @@
5431
classifiers = classifiers,
5532
packages = find_packages(),
5633
py_modules = ['Adafruit_I2C'],
57-
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
34+
ext_modules = [Extension('Adafruit_BBIO.GPIO', ['source/py_gpio.c', 'source/event_gpio.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
5835
Extension('Adafruit_BBIO.PWM', ['source/py_pwm.c', 'source/c_pwm.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
5936
Extension('Adafruit_BBIO.ADC', ['source/py_adc.c', 'source/c_adc.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),
6037
Extension('Adafruit_BBIO.SPI', ['source/spimodule.c', 'source/constants.c', 'source/common.c'], extra_compile_args=['-Wno-format-security']),

source/spimodule.c

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
#include <sys/ioctl.h>
3434
#include "common.h"
3535

36+
#if PY_MAJOR_VERSION < 3
37+
# define PyLong_AS_LONG(val) PyInt_AS_LONG(val)
38+
# define PyLong_AsLong(val) PyInt_AsLong(val)
39+
# define PyLong_Check(val) PyInt_Check(val)
40+
#endif
41+
3642
PyDoc_STRVAR(SPI_module_doc,
3743
"This module defines an object type that allows SPI transactions\n"
3844
"on hosts running the Linux kernel. The host kernel must have SPI\n"
@@ -94,7 +100,7 @@ SPI_dealloc(SPI *self)
94100
PyObject *ref = SPI_close(self);
95101
Py_XDECREF(ref);
96102

97-
self->ob_type->tp_free((PyObject *)self);
103+
Py_TYPE(self)->tp_free((PyObject *)self);
98104
}
99105

100106
#define MAXPATH 16
@@ -131,11 +137,11 @@ SPI_writebytes(SPI *self, PyObject *args)
131137

132138
for (ii = 0; ii < len; ii++) {
133139
PyObject *val = PyList_GET_ITEM(list, ii);
134-
if (!PyInt_Check(val)) {
140+
if (!PyLong_Check(val)) {
135141
PyErr_SetString(PyExc_TypeError, wrmsg);
136142
return NULL;
137143
}
138-
buf[ii] = (__u8)PyInt_AS_LONG(val);
144+
buf[ii] = (__u8)PyLong_AS_LONG(val);
139145
}
140146

141147
status = write(self->fd, &buf[0], len);
@@ -237,14 +243,14 @@ SPI_xfer(SPI *self, PyObject *args)
237243

238244
for (ii = 0; ii < len; ii++) {
239245
PyObject *val = PyList_GET_ITEM(list, ii);
240-
if (!PyInt_Check(val)) {
246+
if (!PyLong_Check(val)) {
241247
free(txbuf);
242248
free(rxbuf);
243249
free(xferptr);
244250
PyErr_SetString(PyExc_TypeError, wrmsg);
245251
return NULL;
246252
}
247-
txbuf[ii] = (__u8)PyInt_AS_LONG(val);
253+
txbuf[ii] = (__u8)PyLong_AS_LONG(val);
248254
xferptr[ii].tx_buf = (unsigned long)&txbuf[ii];
249255
xferptr[ii].rx_buf = (unsigned long)&rxbuf[ii];
250256
xferptr[ii].len = 1;
@@ -315,13 +321,13 @@ SPI_xfer2(SPI *self, PyObject *args)
315321

316322
for (ii = 0; ii < len; ii++) {
317323
PyObject *val = PyList_GET_ITEM(list, ii);
318-
if (!PyInt_Check(val)) {
324+
if (!PyLong_Check(val)) {
319325
free(txbuf);
320326
free(rxbuf);
321327
PyErr_SetString(PyExc_TypeError, msg);
322328
return NULL;
323329
}
324-
txbuf[ii] = (__u8)PyInt_AS_LONG(val);
330+
txbuf[ii] = (__u8)PyLong_AS_LONG(val);
325331
}
326332

327333
xfer.tx_buf = (unsigned long)txbuf;
@@ -447,13 +453,13 @@ SPI_set_mode(SPI *self, PyObject *val, void *closure)
447453
"Cannot delete attribute");
448454
return -1;
449455
}
450-
else if (!PyInt_Check(val)) {
456+
else if (!PyLong_Check(val)) {
451457
PyErr_SetString(PyExc_TypeError,
452458
"The mode attribute must be an integer");
453459
return -1;
454460
}
455461

456-
mode = PyInt_AsLong(val);
462+
mode = PyLong_AsLong(val);
457463

458464
if ( mode > 3 ) {
459465
PyErr_SetString(PyExc_TypeError,
@@ -601,13 +607,13 @@ SPI_set_bpw(SPI *self, PyObject *val, void *closure)
601607
"Cannot delete attribute");
602608
return -1;
603609
}
604-
else if (!PyInt_Check(val)) {
610+
else if (!PyLong_Check(val)) {
605611
PyErr_SetString(PyExc_TypeError,
606612
"The bpw attribute must be an integer");
607613
return -1;
608614
}
609615

610-
bits = PyInt_AsLong(val);
616+
bits = PyLong_AsLong(val);
611617

612618
if (bits < 8 || bits > 16) {
613619
PyErr_SetString(PyExc_TypeError,
@@ -642,13 +648,13 @@ SPI_set_msh(SPI *self, PyObject *val, void *closure)
642648
"Cannot delete attribute");
643649
return -1;
644650
}
645-
else if (!PyInt_Check(val)) {
651+
else if (!PyLong_Check(val)) {
646652
PyErr_SetString(PyExc_TypeError,
647653
"The msh attribute must be an integer");
648654
return -1;
649655
}
650656

651-
msh = PyInt_AsLong(val);
657+
msh = PyLong_AsLong(val);
652658
// DAW - 8/12/12 - removed limitation on SPI speed
653659
// if (8000000 < msh) {
654660
// PyErr_SetString(PyExc_TypeError,
@@ -793,8 +799,7 @@ static PyMethodDef SPI_methods[] = {
793799
};
794800

795801
static PyTypeObject SPI_type = {
796-
PyObject_HEAD_INIT(NULL)
797-
0, /* ob_size */
802+
PyVarObject_HEAD_INIT(NULL, 0)
798803
"SPI", /* tp_name */
799804
sizeof(SPI), /* tp_basicsize */
800805
0, /* tp_itemsize */
@@ -838,20 +843,47 @@ static PyMethodDef SPI_module_methods[] = {
838843
{NULL}
839844
};
840845

846+
#if PY_MAJOR_VERSION >= 3
847+
static struct PyModuleDef moduledef = {
848+
PyModuleDef_HEAD_INIT,
849+
"SPI", /* m_name */
850+
SPI_module_doc, /* m_doc */
851+
-1, /* m_size */
852+
SPI_module_methods, /* m_methods */
853+
NULL, /* m_reload */
854+
NULL, /* m_traverse */
855+
NULL, /* m_clear */
856+
NULL, /* m_free */
857+
};
858+
#endif
859+
841860
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
842861
#define PyMODINIT_FUNC void
843862
#endif
844863
PyMODINIT_FUNC
864+
#if PY_MAJOR_VERSION >= 3
865+
PyInit_SPI(void)
866+
#else
845867
initSPI(void)
868+
#endif
846869
{
847870
PyObject* m;
848871

849872
if (PyType_Ready(&SPI_type) < 0)
850873
return;
851874

875+
#if PY_MAJOR_VERSION >= 3
876+
m = PyModule_Create(&moduledef);
877+
#else
852878
m = Py_InitModule3("SPI", SPI_module_methods, SPI_module_doc);
879+
#endif
880+
853881
Py_INCREF(&SPI_type);
854882
PyModule_AddObject(m, "SPI", (PyObject *)&SPI_type);
883+
884+
#if PY_MAJOR_VERSION >= 3
885+
return m;
886+
#endif
855887
}
856888

857889

0 commit comments

Comments
 (0)