Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Don't re-use an exception object
Browse files Browse the repository at this point in the history
Closes: adafruit#28
  • Loading branch information
jepler committed Aug 7, 2021
1 parent 01ae093 commit 38f7361
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion adafruit_midi/channel_pressure.py
Expand Up @@ -36,7 +36,7 @@ def __init__(self, pressure, *, channel=None):
self.pressure = pressure
super().__init__(channel=channel)
if not 0 <= self.pressure <= 127:
raise self._EX_VALUEERROR_OOR
self._raise_valueerror_oor()

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK), self.pressure])
Expand Down
2 changes: 1 addition & 1 deletion adafruit_midi/control_change.py
Expand Up @@ -39,7 +39,7 @@ def __init__(self, control, value, *, channel=None):
self.value = value
super().__init__(channel=channel)
if not 0 <= self.control <= 127 or not 0 <= self.value <= 127:
raise self._EX_VALUEERROR_OOR
self._raise_valueerror_oor()

def __bytes__(self):
return bytes(
Expand Down
4 changes: 3 additions & 1 deletion adafruit_midi/midi_message.py
Expand Up @@ -89,7 +89,9 @@ class MIDIMessage:
ENDSTATUS = None

# Commonly used exceptions to save memory
_EX_VALUEERROR_OOR = ValueError("Out of range")
@staticmethod
def _raise_valueerror_oor():
raise ValueError("Out of range")

# Each element is ((status, mask), class)
# order is more specific masks first
Expand Down
2 changes: 1 addition & 1 deletion adafruit_midi/note_off.py
Expand Up @@ -40,7 +40,7 @@ def __init__(self, note, velocity=0, *, channel=None):
self._velocity = velocity
super().__init__(channel=channel)
if not 0 <= self._note <= 127 or not 0 <= self._velocity <= 127:
raise self._EX_VALUEERROR_OOR
self._raise_valueerror_oor()

def __bytes__(self):
return bytes(
Expand Down
2 changes: 1 addition & 1 deletion adafruit_midi/note_on.py
Expand Up @@ -40,7 +40,7 @@ def __init__(self, note, velocity=127, *, channel=None):
self.velocity = velocity
super().__init__(channel=channel)
if not 0 <= self.note <= 127 or not 0 <= self.velocity <= 127:
raise self._EX_VALUEERROR_OOR
self._raise_valueerror_oor()

def __bytes__(self):
return bytes(
Expand Down
2 changes: 1 addition & 1 deletion adafruit_midi/pitch_bend.py
Expand Up @@ -37,7 +37,7 @@ def __init__(self, pitch_bend, *, channel=None):
self.pitch_bend = pitch_bend
super().__init__(channel=channel)
if not 0 <= self.pitch_bend <= 16383:
raise self._EX_VALUEERROR_OOR
self._raise_valueerror_oor()

def __bytes__(self):
return bytes(
Expand Down
2 changes: 1 addition & 1 deletion adafruit_midi/polyphonic_key_pressure.py
Expand Up @@ -39,7 +39,7 @@ def __init__(self, note, pressure, *, channel=None):
self.pressure = pressure
super().__init__(channel=channel)
if not 0 <= self.note <= 127 or not 0 <= self.pressure <= 127:
raise self._EX_VALUEERROR_OOR
self._raise_valueerror_oor()

def __bytes__(self):
return bytes(
Expand Down
2 changes: 1 addition & 1 deletion adafruit_midi/program_change.py
Expand Up @@ -36,7 +36,7 @@ def __init__(self, patch, *, channel=None):
self.patch = patch
super().__init__(channel=channel)
if not 0 <= self.patch <= 127:
raise self._EX_VALUEERROR_OOR
self._raise_valueerror_oor()

def __bytes__(self):
return bytes([self._STATUS | (self.channel & self.CHANNELMASK), self.patch])
Expand Down

0 comments on commit 38f7361

Please sign in to comment.