-
-
Notifications
You must be signed in to change notification settings - Fork 95
python: Better error checking for events from python #270
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
EventandCardFormatconstants to support the new validation logic
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@rm5248 Please ignore the AI review, I'm just trying it out. |
| raise TypeError("event does not contain 'event' key") | ||
|
|
||
| if event['event'] == Event.CardRead: | ||
| expected_keys = ["reader_no", "format", "direction", "data"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are checked in osdp_sys and the missing keys are reported from there (see here). If this info is insufficient, the code there needs to be extended to avoid code duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is checked there, but the problem is that the calling code overwrites the python exception here in pd.c. This leads to the unclear error message.
If you comment out that line in pd.c the proper exception is set.
My logic behind doing it in python is that it's easier to check for multiple keys and give the user a more clear error message before dropping to the C code.
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_eventfunction to make sure that card read events have the correct data set.fixes: #267