-
Notifications
You must be signed in to change notification settings - Fork 0
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
close serial port #8
Conversation
WalkthroughThe pull request focuses on improving error handling in the Changes
Poem
Finishing Touches
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🔭 Outside diff range comments (1)
ovos_i2c_detection/__init__.py (1)
Line range hint
70-74
: Ensure serial port is closed in all code paths.The serial port is not closed in the exception handler, which could lead to resource leaks.
Consider using a context manager pattern for better resource handling:
if is_wm8960(): - try: - ser = serial.Serial("/dev/serial0", 9600, timeout=3) - ser.write(b'system.version') - while True: - is_mk1 = ser.readline().decode().rstrip() - if is_mk1 and "Command" in is_mk1: - ser.close() - # This is a Mark 1 - return True - break - ser.close() - return False - except: - return False + with serial.Serial("/dev/serial0", 9600, timeout=3) as ser: + try: + ser.write(b'system.version') + # Read single response + is_mk1 = ser.readline().decode().rstrip() + return bool(is_mk1 and "Command" in is_mk1) + except: + return FalseThis approach:
- Uses context manager to automatically close the port
- Simplifies the logic by removing redundant loop
- Handles port closure in all scenarios including exceptions
- Maintains the same functionality
🧰 Tools
🪛 Ruff (0.8.2)
72-72: Do not use bare
except
(E722)
ser.close() | ||
# This is a Mark 1 | ||
return True | ||
break |
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.
Fix unreachable code after return statement.
The break
statement after the return True
is unreachable. The while loop can be simplified since it only needs to read once.
Apply this diff to fix the logic:
- while True:
+ # Read single response
+ is_mk1 = ser.readline().decode().rstrip()
+ if is_mk1 and "Command" in is_mk1:
+ ser.close()
+ # This is a Mark 1
+ return True
- is_mk1 = ser.readline().decode().rstrip()
- if is_mk1 and "Command" in is_mk1:
- ser.close()
- # This is a Mark 1
- return True
- break
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
ser.close() | |
# This is a Mark 1 | |
return True | |
break | |
# Read single response | |
is_mk1 = ser.readline().decode().rstrip() | |
if is_mk1 and "Command" in is_mk1: | |
ser.close() | |
# This is a Mark 1 | |
return True |
Summary by CodeRabbit