Skip to content

Conversation

@rm5248
Copy link

@rm5248 rm5248 commented Dec 22, 2025

Use a context manager when locking in python to ensure that if an exception is thrown, the lock will be released.

Add some basic error checking to the submit_event function to make sure that card read events have the correct data set.

fixes: #267

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves error handling and thread safety in the Python OSDP peripheral device implementation. The changes use context managers for lock management to ensure locks are released even when exceptions occur, and add validation for card read events to catch malformed event data early.

Key changes:

  • Replaced manual lock.acquire()/lock.release() calls with context manager pattern (with lock:) for exception safety
  • Added input validation to submit_event() to verify event structure and required fields for card read events
  • Imported Event and CardFormat constants to support the new validation logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if not isinstance(event, dict):
raise TypeError("event must be of type dict")

if not 'event' in event:
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The membership test should use the more idiomatic Python syntax 'event' not in event instead of not 'event' in event. While both are functionally equivalent, the former is the preferred style according to PEP 8.

Suggested change
if not 'event' in event:
if 'event' not in event:

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +79
expected_keys = ["reader_no", "format", "direction", "data"]
if event['format'] != CardFormat.ASCII:
expected_keys.append("length")
if not all(key in event for key in expected_keys):
raise TypeError("cardread event missing expected key")
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code accesses event['format'] before validating that the 'format' key exists in the event dictionary. This will raise a KeyError if the 'format' key is missing. The validation should first check that all expected keys exist before attempting to access any of them, or should check for the 'format' key existence before accessing it on line 76.

Suggested change
expected_keys = ["reader_no", "format", "direction", "data"]
if event['format'] != CardFormat.ASCII:
expected_keys.append("length")
if not all(key in event for key in expected_keys):
raise TypeError("cardread event missing expected key")
# Base required keys for all CardRead events
expected_keys = ["reader_no", "format", "direction", "data"]
if not all(key in event for key in expected_keys):
raise TypeError("cardread event missing expected key")
# At this point, 'format' is guaranteed to exist
if event["format"] != CardFormat.ASCII and "length" not in event:
raise TypeError("cardread event missing expected key: 'length'")

Copilot uses AI. Check for mistakes.
if event['format'] != CardFormat.ASCII:
expected_keys.append("length")
if not all(key in event for key in expected_keys):
raise TypeError("cardread event missing expected key")
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message does not specify which key is missing from the cardread event, making it difficult for users to debug the issue. Consider providing a more specific error message that indicates which expected key is missing, such as including the list of expected keys and the keys that are actually present.

Suggested change
raise TypeError("cardread event missing expected key")
missing_keys = [key for key in expected_keys if key not in event]
present_keys = list(event.keys())
raise TypeError(
"cardread event missing expected key(s): "
f"{missing_keys}; expected keys: {expected_keys}; present keys: {present_keys}"
)

Copilot uses AI. Check for mistakes.
@sidcha
Copy link
Member

sidcha commented Dec 22, 2025

@rm5248 Please ignore the AI review, I'm just trying it out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python example only works with ASCII event

2 participants