From 3d53fb3738a9cac29591c05da9d3796f848fa7f4 Mon Sep 17 00:00:00 2001 From: Lukas Magel Date: Sun, 18 Dec 2022 21:27:16 +0100 Subject: [PATCH 1/3] Add failing unit test to verify that issue #1485 is fixed https://github.com/hardbyte/python-can/issues/1458 --- test/test_pcan.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test_pcan.py b/test/test_pcan.py index 0a680fea0..8b51e088d 100644 --- a/test/test_pcan.py +++ b/test/test_pcan.py @@ -373,6 +373,16 @@ def test_auto_reset_init_fault(self): with self.assertRaises(CanInitializationError): self.bus = can.Bus(bustype="pcan", auto_reset=True) + def test_peak_fd_bus_constructor_regression(self): + # Tests that the following issue has been fixed: + # https://github.com/hardbyte/python-can/issues/1458 + params = {'interface': 'pcan', 'fd': True, 'f_clock': 80000000, 'nom_brp': 1, + 'nom_tseg1': 129, 'nom_tseg2': 30, 'nom_sjw': 1, 'data_brp': 1, + 'data_tseg1': 9, 'data_tseg2': 6, 'data_sjw': 1, 'channel': 'PCAN_USBBUS1'} + + can.Bus(**params) + + if __name__ == "__main__": unittest.main() From 9e78c3f5f6f09008aff49710e8c08bf55ca260ed Mon Sep 17 00:00:00 2001 From: Lukas Magel Date: Sun, 18 Dec 2022 21:30:02 +0100 Subject: [PATCH 2/3] Remove unused generic BitTiming creation in Bus.__new__ The BitTiming class is an attempt at unifying the various timing parameters of the individual interfaces. The idea is that instead of manually supplying multiple parameters that make up the timing definition of the interface, one can instead supply a single instance of the BitTiming class, which will also automatically calculate derivative values from its input. At the moment, this class is only used by two interfaces: CANtact and CANanalystii. Both either accept a single bitrate or a BitTiming instance. The latter will overrule the former. The code that is removed with this commit is part of the generic Bus.__new__ constructor. The removed code searches the set of kwargs parameters for timing-related values. If it finds at least one such value, it creates a BitTiming class instance and adds it to the list of parameters. However, it breaks compatibility with the PEAK interface, see issue #1458. Additionally, the code in question is generic and applies to all interfaces. Instantiating a class here is prone to issues since it must be generic enough to fit all use cases. A better approach would be to simply forward the parameters as was done previously and leave it up to the individual interfaces to handle things properly. --- can/util.py | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/can/util.py b/can/util.py index 41467542d..aa4a28d15 100644 --- a/can/util.py +++ b/can/util.py @@ -233,25 +233,6 @@ def _create_bus_config(config: Dict[str, Any]) -> typechecking.BusConfig: if "data_bitrate" in config: config["data_bitrate"] = int(config["data_bitrate"]) - # Create bit timing configuration if given - timing_conf = {} - for key in ( - "f_clock", - "brp", - "tseg1", - "tseg2", - "sjw", - "nof_samples", - "btr0", - "btr1", - ): - if key in config: - timing_conf[key] = int(str(config[key]), base=0) - del config[key] - if timing_conf: - timing_conf["bitrate"] = config["bitrate"] - config["timing"] = can.BitTiming(**timing_conf) - return cast(typechecking.BusConfig, config) From c6d0f874fd79307e1399caaf6090c71b689f443c Mon Sep 17 00:00:00 2001 From: lumagi Date: Sun, 18 Dec 2022 20:39:20 +0000 Subject: [PATCH 3/3] Format code with black --- test/test_pcan.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/test/test_pcan.py b/test/test_pcan.py index 8b51e088d..ab03bf0a1 100644 --- a/test/test_pcan.py +++ b/test/test_pcan.py @@ -376,13 +376,23 @@ def test_auto_reset_init_fault(self): def test_peak_fd_bus_constructor_regression(self): # Tests that the following issue has been fixed: # https://github.com/hardbyte/python-can/issues/1458 - params = {'interface': 'pcan', 'fd': True, 'f_clock': 80000000, 'nom_brp': 1, - 'nom_tseg1': 129, 'nom_tseg2': 30, 'nom_sjw': 1, 'data_brp': 1, - 'data_tseg1': 9, 'data_tseg2': 6, 'data_sjw': 1, 'channel': 'PCAN_USBBUS1'} + params = { + "interface": "pcan", + "fd": True, + "f_clock": 80000000, + "nom_brp": 1, + "nom_tseg1": 129, + "nom_tseg2": 30, + "nom_sjw": 1, + "data_brp": 1, + "data_tseg1": 9, + "data_tseg2": 6, + "data_sjw": 1, + "channel": "PCAN_USBBUS1", + } can.Bus(**params) - if __name__ == "__main__": unittest.main()