w1thermsensor icon indicating copy to clipboard operation
w1thermsensor copied to clipboard

Cannot catch KernelModuleLoadError

Open ggstuart opened this issue 3 years ago • 1 comments

Hi, I was trying to catch the KernelModuleLoadError and handle it.

from w1thermsensor.errors import KernelModuleLoadError

However, its not possible to import the exception without raising the error.

This seems to be because your __init__.py module calls the load_kernel_modules() method immediately.

So its not possible to get a reference to the error without raising it first.

Any ideas about how I could work around this? Or ways to rearrange things?

ggstuart avatar Jul 03 '21 08:07 ggstuart

indeed 🤦

Any ideas about how I could work around this? Or ways to rearrange things?

I don't think it'll be possible unless I move the auto-loading of the kernel module to w1thermsensor.core and don't import that in __init__.py - and with that basically making the user to import w1thermsensor manually. AFAIK there is no way to import something like w1thermsensor.error as module without the w1thermsensor/__init__.py being imported, too ...

As a work around you could disable the auto-loading and just call it yourself:

# set W1THERMSENSOR_NO_KERNEL_MODULE=1 in environment (either before launching your app or in the app)
from w1thermsensor import W1ThermSensor, load_kernel_modules, KernelModuleLoadError

try:
   load_kernel_modules()
except KernelModuleLoadError as exc:
   ...

Or you are basically fine with just catching it as Exception ... but I agree to your point that with that the KernelModuleLoadError is basically useless.

I'm open for suggestions.

timofurrer avatar Oct 04 '21 08:10 timofurrer