Skip to content

Commit 35915e4

Browse files
committed
refactor(max_speed_hz): validate property in Python class
1 parent 238f81f commit 35915e4

File tree

2 files changed

+32
-29
lines changed

2 files changed

+32
-29
lines changed

spidev/_cspi.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,27 +1211,14 @@ SpiDev_get_max_speed_hz(SpiDevObject *self, void *closure)
12111211
static int
12121212
SpiDev_set_max_speed_hz(SpiDevObject *self, PyObject *val, void *closure)
12131213
{
1214-
uint32_t max_speed_hz;
12151214

12161215
if (val == NULL) {
12171216
PyErr_SetString(PyExc_TypeError,
12181217
"Cannot delete attribute");
12191218
return -1;
12201219
}
1221-
#if PY_MAJOR_VERSION < 3
1222-
if (PyInt_Check(val)) {
1223-
max_speed_hz = PyInt_AS_LONG(val);
1224-
} else
1225-
#endif
1226-
{
1227-
if (PyLong_Check(val)) {
1228-
max_speed_hz = PyLong_AS_LONG(val);
1229-
} else {
1230-
PyErr_SetString(PyExc_TypeError,
1231-
"The max_speed_hz attribute must be an integer");
1232-
return -1;
1233-
}
1234-
}
1220+
1221+
uint32_t max_speed_hz = PyLong_AS_LONG(val);
12351222

12361223
if (self->max_speed_hz != max_speed_hz) {
12371224
if (ioctl(self->fd, SPI_IOC_WR_MAX_SPEED_HZ, &max_speed_hz) == -1) {

spidev/_spi.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,20 @@
55
if TYPE_CHECKING:
66
from os import PathLike
77
from types import TracebackType
8-
from typing import Self, Union, Sequence, overload
8+
from typing import Self, Union, Sequence, overload, Any
99
from collections.abc import Buffer
1010

1111
StrPath = Union[str, PathLike[str]]
1212

1313

14+
def as_int(val: Any, name: str = "Value") -> int:
15+
try:
16+
v = int(val)
17+
except (TypeError, ValueError):
18+
raise TypeError(f"{name} must be an integer, but is {type(val)}")
19+
return v
20+
21+
1422
class SpiDev(_cspi.SpiDev):
1523
def __init__(
1624
self,
@@ -20,22 +28,26 @@ def __init__(
2028
path: StrPath | None = None,
2129
mode: int | None = None,
2230
bits_per_word: int | None = None,
31+
max_speed_hz: int | None = None,
2332
):
2433
super().__init__(bus, client)
34+
2535
self.bus = bus
2636
self.client = client
2737
self.device = device
28-
if mode is not None:
29-
self.mode = mode
30-
if bits_per_word is not None:
31-
self.bits_per_word = bits_per_word
32-
3338
if path and (bus or device):
3439
raise ValueError(
3540
"both path and bus/device number of SPI device are provided"
3641
)
3742
self.path = path
3843

44+
if mode is not None:
45+
self.mode = mode
46+
if bits_per_word is not None:
47+
self.bits_per_word = bits_per_word
48+
if max_speed_hz is not None:
49+
self.max_speed_hz = max_speed_hz
50+
3951
@property
4052
def mode(self) -> int:
4153
"""SPI mode.
@@ -47,10 +59,7 @@ def mode(self) -> int:
4759

4860
@mode.setter
4961
def mode(self, value: int, /) -> None:
50-
try:
51-
v = int(value)
52-
except (TypeError, ValueError):
53-
raise TypeError(f"mode must be an integer, but is {type(value)}")
62+
v = as_int(value, "mode")
5463

5564
if not 0 <= v <= 3:
5665
raise ValueError(f"mode must be between 0 and 3, but is {v}")
@@ -64,16 +73,23 @@ def bits_per_word(self) -> int:
6473

6574
@bits_per_word.setter
6675
def bits_per_word(self, value: int, /) -> None:
67-
try:
68-
v = int(value)
69-
except (TypeError, ValueError):
70-
raise TypeError(f"bits_per_word must be an integer, but is {type(value)}")
76+
v = as_int(value, "bits_per_word")
7177

7278
if not (8 <= value <= 32):
7379
raise ValueError(f"bits_per_word must be between 8 and 32, but is {v}")
7480

7581
super().__setattr__("bits_per_word", v)
7682

83+
@property
84+
def max_speed_hz(self) -> int:
85+
"""Max speed (in Hertz)."""
86+
return super().max_speed_hz
87+
88+
@max_speed_hz.setter
89+
def max_speed_hz(self, value: int, /) -> None:
90+
v = as_int(value, "max_speed_hz")
91+
super().__setattr__("max_speed_hz", v)
92+
7793
def closed(self) -> bool:
7894
"""True if the connection is closed."""
7995
try:

0 commit comments

Comments
 (0)