Skip to content

Conversation

@marcel-kanter
Copy link
Contributor

@marcel-kanter marcel-kanter commented Apr 30, 2019

A starting point for the unification of the exception handling.
Includes some improvements for the ixxat interface (the improvements use the new exceptions).
Please pull this into new branch, to keep the develop working until this work is applied to all the interfaces/classes/etc...

Closes #560
Addresses #356

@marcel-kanter
Copy link
Contributor Author

Checks fail, since the ixxat interface cannot be loaded on the test environment.

@felixdivo
Copy link
Collaborator

felixdivo commented Apr 30, 2019

I will create a new branch for this and point the PR to it.

(Done)

@felixdivo felixdivo changed the base branch from develop to unified-exceptions April 30, 2019 19:08
@codecov
Copy link

codecov bot commented Apr 30, 2019

Codecov Report

Merging #562 into unified-exceptions will increase coverage by <.01%.
The diff coverage is 46.66%.

@@                  Coverage Diff                   @@
##           unified-exceptions     #562      +/-   ##
======================================================
+ Coverage               64.03%   64.04%   +<.01%     
======================================================
  Files                      63       64       +1     
  Lines                    5700     5715      +15     
======================================================
+ Hits                     3650     3660      +10     
- Misses                   2050     2055       +5

Copy link
Owner

@hardbyte hardbyte left a comment

Choose a reason for hiding this comment

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

Thanks so much for taking up the challenge.

class CanOperationError(CanError):
""" Indicates an error while operation.
"""
pass No newline at end of file
Copy link
Owner

Choose a reason for hiding this comment

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

nit: newline at end of file please

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oups. Will be fixed.

# Real return value is an unsigned long
result = ctypes.c_ulong(result).value

#print(hex(result), function)
Copy link
Owner

Choose a reason for hiding this comment

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

nit: remove


"""
Sends a message on the bus. The interface may buffer the message.
returns True on success or when timeout is None
Copy link
Owner

Choose a reason for hiding this comment

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

Please use the sphinx syntax we use elsewhere in the code base.

See for example https://github.com/hardbyte/python-can/blob/develop/can/interfaces/socketcan/socketcan.py#L573-L596

Sends a message on the bus. The interface may buffer the message.
returns True on success or when timeout is None
returns False on timeout (when timeout is not None)
raises CanOperationError
Copy link
Owner

Choose a reason for hiding this comment

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

Suggest we raise an exception on timeout rather return a boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm. Thats a good point. Maybe our way thinking about Exceptions differs a bit. This method is a good example for "fine-adjusting" the handling/rasising of exceptions.

We have two cases: With or without timeout
First without timeout: Thats easy. The user don't want a timeout, so any timeout is a case that should not happen -> exceptional behavior which needs special treatment.
Second with timeout: Thats the point. The user wanted a timeout, so he/she accepts any timeout (maybe don't cares about it - recovering from a fault situation might be a situation were this mades much sense. the fault may still be present and the other node not reachable). So is an timeout an exceptional behavior? Not really. (But i still need to clearify if an timeout may occur because of exceptional behavior of the driver. Then this can get complicated.)

Copy link
Collaborator

Choose a reason for hiding this comment

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

In any case the behavior should be consistent between interfaces. If we change to return a boolean instead of raising an exception then other interfaces must change too.

My original intention was that if a timeout is specified, the developer really wants to make sure the message is sent or queued successfully. If not then something is wrong and the program may be stopped or it can try to retransmit later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I'll stick to the more pythonic raise-an-exception way.
BUT. Should we generally use the built-in TimeoutError for timeouts or sub-class CanError?
I prefer the built-in.

Copy link
Owner

Choose a reason for hiding this comment

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

Would there be an issue inheriting from both? I agree that our specific exception e.g. CanBusWriteTimeout should inherit from TimeoutError and I imagine it should work also inheriting from our generic CanError exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think there would be no problem. But i would call this class CanTimeoutError and use it generally for timeout errors, not only for send.

Copy link
Collaborator

Choose a reason for hiding this comment

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

On the other hand I don’t think the OSErrors are supposed to be used for other than pure system level errors.

“Raised when a system function timed out at the system level. Corresponds to errno ETIMEDOUT.”

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oki. I had a look into other python packages and the common unterstanding is like christian said it. They dervie their own TimeoutError.
So we'll get an CanTimeoutError


class SoftwareTestCase(unittest.TestCase):
"""
Test cases that test the software only and do not rely on an existing/connected hardware.
Copy link
Owner

Choose a reason for hiding this comment

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

Do they require any driver software to be installed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually they rely on the ixxat driver installed on the maschine. Currently I'll skip the tests, if they can't be loaded (ImportError).

Still looking for a good and simple way for testing as much as possible without any driver.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You could have a look at test_systec.py, which uses unittest.mock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They have linux drivers for x86 and AMD64 on their website.
I contacted HMS (Ixxat is a part of them) and got an beta driver for ARM64, I'm going to test them on the weekend on my ODROID.
Then we may get linux working support too.

@felixdivo felixdivo added this to the 4.0 Release milestone May 2, 2019
Win32 and cygwin will both load "vcimpl.dll".
Added comments.
@felixdivo
Copy link
Collaborator

We should also consider a way to document this. Maybe like this hierarchy, along with the autodoc classes added to python-can/doc/api.rst#errors?

@felixdivo felixdivo changed the title Exception handling General exception handling (+ changes to the ixxat interface) May 13, 2019
class CanBackEndError(CanError):
""" Indicates an error related to the backend (e.g. driver/OS/library)
Examples:
- A call to a library function results in an unexpected return value
Copy link
Collaborator

@felixdivo felixdivo May 13, 2019

Choose a reason for hiding this comment

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

Should we add the return code as a parameter, which is None if not explicitly set? Also applies to CanInitializationError. This way, we could unify it a little bit, and it would be opt-in for interfaces where it would be helpful to have the error codes as a user.

@marcel-kanter
Copy link
Contributor Author

We should also consider a way to document this. Maybe like this hierarchy, along with the autodoc classes added to python-can/doc/api.rst#errors?

Is there an easy and fast way to build the doc locally? When I try python setup.py build_sphinx I get an error that sphinx misses some a packet programoutput

@felixdivo
Copy link
Collaborator

I never heard of that error. It just works on my machine. Could you post the part of the output that indicates the problem?

@marcel-kanter
Copy link
Contributor Author

marcel-kanter commented May 18, 2019

I got this output, but then I found that the missing extension is an addon to sphinx, with this addon, building works:

Warning: 'classifiers' should be a list, got type 'tuple'
C:\Program Files\Python37\lib\site-packages\setuptools\dist.py:481: UserWarning: The version specified ('3.2.0-exception-handling') is an invalid version, this
may not work as expected with newer versions of setuptools, pip, and PyPI. Please see PEP 440 for more details.
"details." % self.metadata.version
running build_sphinx
Running Sphinx v2.0.1
loading translations [en]... done

Extension error:
Could not import extension sphinxcontrib.programoutput (exception: No module named 'sphinxcontrib.programoutput')

I'll change the version to 4.0.0-dev0

Copy link
Contributor Author

@marcel-kanter marcel-kanter left a comment

Choose a reason for hiding this comment

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

According to PEP440 and the implementation in setuptools, the normalized version should be 4.0.0.dev0 (with the 0 at the end).

Change version to 4.0.0.dev0
Fix classifiers in setup.py, it should be a list.
Copy link
Owner

@hardbyte hardbyte left a comment

Choose a reason for hiding this comment

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

I think we still have work to do with regard to common attributes our exceptions could expose but this is a great start

Copy link
Owner

@hardbyte hardbyte left a comment

Choose a reason for hiding this comment

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

Good start, we need to sort out the optional/required exception attributes next but we can merge this into develop

@hardbyte
Copy link
Owner

Erm sorry that was weird, ignore my double review. Glitch in the intewebs

@felixdivo felixdivo merged commit 739e405 into hardbyte:unified-exceptions May 19, 2019
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.

4 participants