Skip to content

Fixed Slcan fileno raising an exception on windows#760

Closed
bmeisels wants to merge 2 commits into
hardbyte:developfrom
bmeisels:slcan_fileno_fix
Closed

Fixed Slcan fileno raising an exception on windows#760
bmeisels wants to merge 2 commits into
hardbyte:developfrom
bmeisels:slcan_fileno_fix

Conversation

@bmeisels

Copy link
Copy Markdown
Contributor

Slcan currently raises an exception when used with 'can.Notifier' on windows.

'can.Notifier.add_bus' calls 'bus.fileno' which calls 'serialPortOrig.fileno' which raises an exception on windows as it is not implemented.

The fix catches the exception raised by calling fileno instead of checking if it exists(as it can exist and still raise an error).

@codecov

codecov Bot commented Jan 28, 2020

Copy link
Copy Markdown

Codecov Report

Merging #760 into develop will decrease coverage by 0.60%.
The diff coverage is 25.00%.

@@             Coverage Diff             @@
##           develop     #760      +/-   ##
===========================================
- Coverage    70.39%   69.79%   -0.61%     
===========================================
  Files           70       70              
  Lines         6840     6488     -352     
===========================================
- Hits          4815     4528     -287     
+ Misses        2025     1960      -65     

@bessman

bessman commented Jan 28, 2020

Copy link
Copy Markdown
Contributor

Better specify the error type. Naked excepts can hide bugs.

Comment thread can/interfaces/slcan.py Outdated
@bmeisels

Copy link
Copy Markdown
Contributor Author

@felixdivo @bessman I made the changes. What do you think?

Comment thread can/interfaces/slcan.py Outdated
try:
return self.serialPortOrig.fileno()
# Return an invalid file descriptor on Windows
except (NotImplementedError, AttributeError, io.UnsupportedOperation):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can NotImplementedError or AttributeError actually be raised here? If not we shouldn't try to catch them. Based on the old code it looks like at least AttributeError could be raised. @christiansandberg, do you remember the reason for the hasattr check?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

That was probably a mistake from my end. I saw that pyserial did not implement the fileno() method but forgot to check if it was inherited instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In that case, if the only exception that has actually been observed in the wild here is UnsupportedOperation, I suggest only trying to catch that. If it turns out other exceptions (that we want to catch) can be raised here, add them later.

@bmeisels

bmeisels commented Feb 2, 2020

Copy link
Copy Markdown
Contributor Author

Changed to only catch io.UnsupportedOperation.

@christiansandberg

Copy link
Copy Markdown
Collaborator

I think an even better approach would be to implement the fileno() method in BusABC which would raise UnsupportedOperation. The slcan implementation would not need to handle the exception at all, but the Notifier would do that instead. Returning -1 instead of raising an exception was not very Pythonic I admit.

@bmeisels

Copy link
Copy Markdown
Contributor Author

@christiansandberg I like the general idea.
I think BusABC.fileno should raise NotImplementedError to be similar to other optional method in BusABC.

@bmeisels bmeisels force-pushed the slcan_fileno_fix branch from 9c3a70a to d87cad7 Compare May 24, 2020 12:36
@bmeisels

Copy link
Copy Markdown
Contributor Author

Sorry for the inactivity. I thought we were leaning towards the solution offered by @christiansandberg .

@hardbyte

Copy link
Copy Markdown
Owner

Ahh right you are. I've opened #877 if you'd like to check it out

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants