Skip to content

Commit

Permalink
Merge pull request #801 from gpiozero/native-warn-515
Browse files Browse the repository at this point in the history
Warn users when falling back to NativeFactory, close #515
  • Loading branch information
bennuttall committed Apr 4, 2020
2 parents ab38ebf + 9bd94d5 commit dda329b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 11 additions & 1 deletion gpiozero/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,17 @@
GPIOPinMissing,
GPIOPinInUse,
GPIODeviceClosed,
NativePinFactoryFallback,
PinFactoryFallback,
)
from .compat import frozendict

native_fallback_message = (
'Falling back to the experimental pin factory NativeFactory because no other '
'pin factory could be loaded. For best results, install RPi.GPIO or pigpio. '
'See https://gpiozero.readthedocs.io/en/stable/api_pins.html for more information.'
)


class GPIOMeta(type):
# NOTE Yes, this is a metaclass. Don't be scared - it's a simple one.
Expand Down Expand Up @@ -272,7 +279,10 @@ def _default_pin_factory():
try:
mod_name, cls_name = entry_point.split(':', 1)
module = __import__(mod_name, fromlist=(cls_name,))
return getattr(module, cls_name)()
pin_factory = getattr(module, cls_name)()
if name == 'native':
warnings.warn(NativePinFactoryFallback(native_fallback_message))
return pin_factory
except Exception as e:
warnings.warn(
PinFactoryFallback(
Expand Down
3 changes: 3 additions & 0 deletions gpiozero/exc.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ class PinWarning(GPIOZeroWarning):
class PinFactoryFallback(PinWarning):
"Warning raised when a default pin factory fails to load and a fallback is tried"

class NativePinFactoryFallback(PinWarning):
"Warning raised when all other default pin factories fail to load and NativeFactory is used"

class PinNonPhysical(PinWarning):
"Warning raised when a non-physical pin is specified in a constructor"

Expand Down

0 comments on commit dda329b

Please sign in to comment.