Skip to content

Don't reuse exception objects #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion adafruit_midi/channel_pressure.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
9 changes: 3 additions & 6 deletions adafruit_midi/mtc_quarter_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,19 @@ def __init__(self, msgtype, value):
self.value = value
super().__init__()
if not 0 <= self.type <= 7 or not 0 <= self.value <= 0x0F:
raise self._EX_VALUEERROR_OOR
self._raise_valueerror_oor()

def __bytes__(self):
return bytes(
[
self._STATUS,
(self.type << 4) + self.value # Assemble low and high nibbles
(self.type << 4) + self.value, # Assemble low and high nibbles
]
)

@classmethod
def from_bytes(cls, msg_bytes):
return cls(
msg_bytes[1] >> 4, # High nibble
msg_bytes[1] & 15 # Low nibble
)
return cls(msg_bytes[1] >> 4, msg_bytes[1] & 15) # High nibble # Low nibble


MtcQuarterFrame.register_message_type()
2 changes: 1 addition & 1 deletion adafruit_midi/note_off.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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